mkql_logical.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include "mkql_logical.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  3. #include <yql/essentials/minikql/mkql_node_cast.h>
  4. #include <yql/essentials/minikql/mkql_node_builder.h>
  5. #include "mkql_check_args.h"
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. namespace {
  9. template <bool IsLeftOptional, bool IsRightOptional>
  10. class TAndWrapper : public TBinaryCodegeneratorNode<TAndWrapper<IsLeftOptional, IsRightOptional>> {
  11. typedef TBinaryCodegeneratorNode<TAndWrapper<IsLeftOptional, IsRightOptional>> TBaseComputation;
  12. public:
  13. TAndWrapper(TComputationMutables& mutables, IComputationNode* left, IComputationNode* right)
  14. : TBaseComputation(left, right, EValueRepresentation::Embedded)
  15. {
  16. Y_UNUSED(mutables);
  17. }
  18. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  19. const auto& left = this->Left->GetValue(ctx);
  20. if (!IsLeftOptional || left) {
  21. if (!left.template Get<bool>()) {
  22. return NUdf::TUnboxedValuePod(false);
  23. }
  24. }
  25. const auto& right = this->Right->GetValue(ctx);
  26. if (!IsRightOptional || right) {
  27. if (!right.template Get<bool>()) {
  28. return NUdf::TUnboxedValuePod(false);
  29. }
  30. }
  31. // both either true (just true) or nothing
  32. if (IsLeftOptional && !left || IsRightOptional && !right) {
  33. return NUdf::TUnboxedValuePod();
  34. }
  35. return NUdf::TUnboxedValuePod(true);
  36. }
  37. #ifndef MKQL_DISABLE_CODEGEN
  38. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  39. auto& context = ctx.Codegen.GetContext();
  40. const auto valueType = Type::getInt128Ty(context);
  41. const auto left = GetNodeValue(this->Left, ctx, block);
  42. const auto uvFalse = GetFalse(context);
  43. const auto skip = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, left, uvFalse, "skip", block);
  44. const auto both = BasicBlock::Create(context, "both", ctx.Func);
  45. const auto done = BasicBlock::Create(context, "done", ctx.Func);
  46. const auto result = PHINode::Create(valueType, 2, "result", done);
  47. result->addIncoming(uvFalse, block);
  48. BranchInst::Create(done, both, skip, block);
  49. block = both;
  50. const auto right = GetNodeValue(this->Right, ctx, block);
  51. if (IsLeftOptional) {
  52. const auto andr = BinaryOperator::CreateAnd(left, right, "and", block);
  53. const auto over = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, right, uvFalse, "over", block);
  54. const auto full = SelectInst::Create(over, uvFalse, andr, "full", block);
  55. result->addIncoming(full, block);
  56. } else {
  57. result->addIncoming(right, block);
  58. }
  59. BranchInst::Create(done, block);
  60. block = done;
  61. return result;
  62. }
  63. #endif
  64. };
  65. template <bool IsLeftOptional, bool IsRightOptional>
  66. class TOrWrapper : public TBinaryCodegeneratorNode<TOrWrapper<IsLeftOptional, IsRightOptional>> {
  67. typedef TBinaryCodegeneratorNode<TOrWrapper<IsLeftOptional, IsRightOptional>> TBaseComputation;
  68. public:
  69. TOrWrapper(TComputationMutables& mutables, IComputationNode* left, IComputationNode* right)
  70. : TBaseComputation(left, right, EValueRepresentation::Embedded)
  71. {
  72. Y_UNUSED(mutables);
  73. }
  74. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  75. const auto& left = this->Left->GetValue(ctx);
  76. if (!IsLeftOptional || left) {
  77. if (left.template Get<bool>()) {
  78. return NUdf::TUnboxedValuePod(true);
  79. }
  80. }
  81. const auto& right = this->Right->GetValue(ctx);
  82. if (!IsRightOptional || right) {
  83. if (right.template Get<bool>()) {
  84. return NUdf::TUnboxedValuePod(true);
  85. }
  86. }
  87. // both either false (just false) or nothing
  88. if (IsLeftOptional && !left || IsRightOptional && !right) {
  89. return NUdf::TUnboxedValuePod();
  90. }
  91. return NUdf::TUnboxedValuePod(false);
  92. }
  93. #ifndef MKQL_DISABLE_CODEGEN
  94. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  95. auto& context = ctx.Codegen.GetContext();
  96. const auto valueType = Type::getInt128Ty(context);
  97. const auto left = GetNodeValue(this->Left, ctx, block);
  98. const auto uvTrue = GetTrue(context);
  99. const auto skip = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, left, uvTrue, "skip", block);
  100. const auto both = BasicBlock::Create(context, "both", ctx.Func);
  101. const auto done = BasicBlock::Create(context, "done", ctx.Func);
  102. const auto result = PHINode::Create(valueType, 2, "result", done);
  103. result->addIncoming(uvTrue, block);
  104. BranchInst::Create(done, both, skip, block);
  105. block = both;
  106. const auto right = GetNodeValue(this->Right, ctx, block);
  107. if (IsLeftOptional) {
  108. const auto andr = BinaryOperator::CreateAnd(left, right, "and", block);
  109. const auto over = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, right, uvTrue, "over", block);
  110. const auto full = SelectInst::Create(over, uvTrue, andr, "full", block);
  111. result->addIncoming(full, block);
  112. } else {
  113. result->addIncoming(right, block);
  114. }
  115. BranchInst::Create(done, block);
  116. block = done;
  117. return result;
  118. }
  119. #endif
  120. };
  121. template <bool IsLeftOptional, bool IsRightOptional>
  122. class TXorWrapper : public TBinaryCodegeneratorNode<TXorWrapper<IsLeftOptional, IsRightOptional>> {
  123. typedef TBinaryCodegeneratorNode<TXorWrapper<IsLeftOptional, IsRightOptional>> TBaseComputation;
  124. public:
  125. TXorWrapper(TComputationMutables& mutables, IComputationNode* left, IComputationNode* right)
  126. : TBaseComputation(left, right, EValueRepresentation::Embedded)
  127. {
  128. Y_UNUSED(mutables);
  129. }
  130. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  131. const auto& left = this->Left->GetValue(ctx);
  132. if (IsLeftOptional && !left) {
  133. return NUdf::TUnboxedValuePod();
  134. }
  135. const auto& right = this->Right->GetValue(ctx);
  136. if (IsRightOptional && !right) {
  137. return NUdf::TUnboxedValuePod();
  138. }
  139. const bool res = left.template Get<bool>() != right.template Get<bool>();
  140. return NUdf::TUnboxedValuePod(res);
  141. }
  142. #ifndef MKQL_DISABLE_CODEGEN
  143. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  144. auto& context = ctx.Codegen.GetContext();
  145. const auto valueType = Type::getInt128Ty(context);
  146. if (IsLeftOptional || IsRightOptional) {
  147. const auto zero = ConstantInt::get(valueType, 0);
  148. const auto both = BasicBlock::Create(context, "both", ctx.Func);
  149. const auto done = BasicBlock::Create(context, "done", ctx.Func);
  150. const auto result = PHINode::Create(valueType, 2, "result", done);
  151. if (IsLeftOptional) {
  152. const auto left = GetNodeValue(this->Left, ctx, block);
  153. const auto skip = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, left, zero, "skip", block);
  154. result->addIncoming(zero, block);
  155. BranchInst::Create(done, both, skip, block);
  156. block = both;
  157. const auto right = GetNodeValue(this->Right, ctx, block);
  158. if (IsRightOptional) {
  159. const auto xorr = BinaryOperator::CreateXor(left, right, "xor", block);
  160. const auto full = BinaryOperator::CreateOr(xorr, GetFalse(context), "full", block);
  161. const auto null = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, right, zero, "null", block);
  162. const auto last = SelectInst::Create(null, zero, full, "last", block);
  163. result->addIncoming(last, block);
  164. } else {
  165. const auto xorr = BinaryOperator::CreateXor(left, right, "xor", block);
  166. const auto full = BinaryOperator::CreateOr(xorr, GetFalse(context), "full", block);
  167. result->addIncoming(full, block);
  168. }
  169. } else if (IsRightOptional) {
  170. const auto right = GetNodeValue(this->Right, ctx, block);
  171. const auto skip = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, right, zero, "skip", block);
  172. result->addIncoming(zero, block);
  173. BranchInst::Create(done, both, skip, block);
  174. block = both;
  175. const auto left = GetNodeValue(this->Left, ctx, block);
  176. const auto xorr = BinaryOperator::CreateXor(left, right, "xor", block);
  177. const auto full = BinaryOperator::CreateOr(xorr, GetFalse(context), "full", block);
  178. result->addIncoming(full, block);
  179. }
  180. BranchInst::Create(done, block);
  181. block = done;
  182. return result;
  183. } else {
  184. const auto left = GetNodeValue(this->Left, ctx, block);
  185. const auto right = GetNodeValue(this->Right, ctx, block);
  186. const auto xorr = BinaryOperator::CreateXor(left, right, "xor", block);
  187. const auto full = BinaryOperator::CreateOr(xorr, GetFalse(context), "full", block);
  188. return full;
  189. }
  190. }
  191. #endif
  192. };
  193. template <bool IsOptional>
  194. class TNotWrapper : public TDecoratorCodegeneratorNode<TNotWrapper<IsOptional>> {
  195. typedef TDecoratorCodegeneratorNode<TNotWrapper<IsOptional>> TBaseComputation;
  196. public:
  197. TNotWrapper(IComputationNode* arg)
  198. : TBaseComputation(arg)
  199. {}
  200. NUdf::TUnboxedValuePod DoCalculate(TComputationContext&, const NUdf::TUnboxedValuePod& arg) const {
  201. if (IsOptional && !arg) {
  202. return NUdf::TUnboxedValuePod();
  203. }
  204. const bool res = !arg.template Get<bool>();
  205. return NUdf::TUnboxedValuePod(res);
  206. }
  207. #ifndef MKQL_DISABLE_CODEGEN
  208. Value* DoGenerateGetValue(const TCodegenContext& ctx, Value* arg, BasicBlock*& block) const {
  209. auto& context = ctx.Codegen.GetContext();
  210. const auto xorr = BinaryOperator::CreateXor(arg, ConstantInt::get(arg->getType(), 1), "xor", block);
  211. const auto result = IsOptional ? SelectInst::Create(IsExists(arg, block, context), xorr, arg, "sel", block) : static_cast<Value*>(xorr);
  212. return result;
  213. }
  214. #endif
  215. };
  216. template <template <bool, bool> class TWrapper>
  217. IComputationNode* WrapLogicalFunction(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  218. const auto nodeLocator = ctx.NodeLocator;
  219. MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
  220. const auto leftType = callable.GetInput(0).GetStaticType();
  221. const auto rightType = callable.GetInput(1).GetStaticType();
  222. CheckBinaryFunctionArgs(leftType, rightType, true, true);
  223. const bool isLeftOptional = leftType->IsOptional();
  224. const bool isRightOptional = rightType->IsOptional();
  225. const auto left = LocateNode(nodeLocator, callable, 0);
  226. const auto right = LocateNode(nodeLocator, callable, 1);
  227. if (isLeftOptional) {
  228. if (isRightOptional) {
  229. return new TWrapper<true, true>(ctx.Mutables, left, right);
  230. } else {
  231. return new TWrapper<true, false>(ctx.Mutables, left, right);
  232. }
  233. }
  234. else {
  235. if (isRightOptional) {
  236. return new TWrapper<false, true>(ctx.Mutables, left, right);
  237. } else {
  238. return new TWrapper<false, false>(ctx.Mutables, left, right);
  239. }
  240. }
  241. }
  242. }
  243. IComputationNode* WrapAnd(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  244. return WrapLogicalFunction<TAndWrapper>(callable, ctx);
  245. }
  246. IComputationNode* WrapOr(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  247. return WrapLogicalFunction<TOrWrapper>(callable, ctx);
  248. }
  249. IComputationNode* WrapXor(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  250. return WrapLogicalFunction<TXorWrapper>(callable, ctx);
  251. }
  252. IComputationNode* WrapNot(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  253. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  254. bool isOptional;
  255. const auto& dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
  256. const auto schemeType = dataType->GetSchemeType();
  257. MKQL_ENSURE(schemeType == NUdf::TDataType<bool>::Id, "Expected bool");
  258. const auto node = LocateNode(ctx.NodeLocator, callable, 0);
  259. if (isOptional) {
  260. return new TNotWrapper<true>(node);
  261. }
  262. else {
  263. return new TNotWrapper<false>(node);
  264. }
  265. }
  266. }
  267. }