mkql_wide_map.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "mkql_wide_map.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  3. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  4. #include <yql/essentials/minikql/mkql_node_cast.h>
  5. #include <yql/essentials/utils/cast.h>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. using NYql::EnsureDynamicCast;
  9. namespace {
  10. class TExpandMapWrapper : public TStatelessWideFlowCodegeneratorNode<TExpandMapWrapper> {
  11. using TBaseComputation = TStatelessWideFlowCodegeneratorNode<TExpandMapWrapper>;
  12. public:
  13. TExpandMapWrapper(IComputationNode* flow, IComputationExternalNode* item, TComputationNodePtrVector&& newItems)
  14. : TBaseComputation(flow), Flow(flow), Item(item), NewItems(std::move(newItems))
  15. {}
  16. EFetchResult DoCalculate(TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
  17. if (auto item = Flow->GetValue(ctx); item.IsSpecial()) {
  18. return item.IsYield() ? EFetchResult::Yield : EFetchResult::Finish;
  19. } else {
  20. Item->SetValue(ctx, std::move(item));
  21. }
  22. for (const auto item : NewItems)
  23. if (const auto out = *output++)
  24. *out = item->GetValue(ctx);
  25. return EFetchResult::One;
  26. }
  27. #ifndef MKQL_DISABLE_CODEGEN
  28. TGenerateResult DoGenGetValues(const TCodegenContext& ctx, BasicBlock*& block) const {
  29. auto& context = ctx.Codegen.GetContext();
  30. const auto codegenItem = dynamic_cast<ICodegeneratorExternalNode*>(Item);
  31. MKQL_ENSURE(codegenItem, "Item must be codegenerator node.");
  32. const auto item = GetNodeValue(Flow, ctx, block);
  33. const auto resultType = Type::getInt32Ty(context);
  34. const auto outres = SelectInst::Create(IsYield(item, block, context), ConstantInt::get(resultType, 0), ConstantInt::get(resultType, -1), "outres", block);
  35. const auto work = BasicBlock::Create(context, "work", ctx.Func);
  36. const auto pass = BasicBlock::Create(context, "pass", ctx.Func);
  37. const auto result = PHINode::Create(outres->getType(), 2, "result", pass);
  38. result->addIncoming(outres, block);
  39. BranchInst::Create(pass, work, IsSpecial(item, block, context), block);
  40. block = work;
  41. codegenItem->CreateSetValue(ctx, block, item);
  42. result->addIncoming(ConstantInt::get(resultType, 1), block);
  43. BranchInst::Create(pass, block);
  44. block = pass;
  45. TGettersList getters;
  46. getters.reserve(NewItems.size());
  47. std::transform(NewItems.cbegin(), NewItems.cend(), std::back_inserter(getters), [&](IComputationNode* node) {
  48. return [node](const TCodegenContext& ctx, BasicBlock*& block){ return GetNodeValue(node, ctx, block); };
  49. });
  50. return {result, std::move(getters)};
  51. }
  52. #endif
  53. private:
  54. void RegisterDependencies() const final {
  55. if (const auto flow = FlowDependsOn(Flow)) {
  56. Own(flow, Item);
  57. std::for_each(NewItems.cbegin(), NewItems.cend(), std::bind(&TExpandMapWrapper::DependsOn, flow, std::placeholders::_1));
  58. }
  59. }
  60. IComputationNode* const Flow;
  61. IComputationExternalNode *const Item;
  62. const TComputationNodePtrVector NewItems;
  63. };
  64. class TWideMapWrapper : public TStatelessWideFlowCodegeneratorNode<TWideMapWrapper> {
  65. using TBaseComputation = TStatelessWideFlowCodegeneratorNode<TWideMapWrapper>;
  66. public:
  67. TWideMapWrapper(TComputationMutables& mutables, IComputationWideFlowNode* flow, TComputationExternalNodePtrVector&& items, TComputationNodePtrVector&& newItems)
  68. : TBaseComputation(flow)
  69. , Flow(flow)
  70. , Items(std::move(items))
  71. , NewItems(std::move(newItems))
  72. , PasstroughtMap(GetPasstroughtMapOneToOne(Items, NewItems))
  73. , ReversePasstroughtMap(GetPasstroughtMapOneToOne(NewItems, Items))
  74. , WideFieldsIndex(mutables.IncrementWideFieldsIndex(Items.size()))
  75. {}
  76. EFetchResult DoCalculate(TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
  77. auto** fields = ctx.WideFields.data() + WideFieldsIndex;
  78. for (auto i = 0U; i < Items.size(); ++i)
  79. if (const auto& map = PasstroughtMap[i]; map && !Items[i]->GetDependencesCount()) {
  80. if (const auto out = output[*map])
  81. fields[i] = out;
  82. } else
  83. fields[i] = &Items[i]->RefValue(ctx);
  84. if (const auto result = Flow->FetchValues(ctx, fields); EFetchResult::One != result)
  85. return result;
  86. for (auto i = 0U; i < NewItems.size(); ++i) {
  87. if (const auto out = output[i]) {
  88. if (const auto& map = ReversePasstroughtMap[i]) {
  89. if (const auto from = *map; !Items[from]->GetDependencesCount()) {
  90. if (const auto first = *PasstroughtMap[from]; first != i)
  91. *out = *output[first];
  92. continue;
  93. }
  94. }
  95. *out = NewItems[i]->GetValue(ctx);
  96. }
  97. }
  98. return EFetchResult::One;
  99. }
  100. #ifndef MKQL_DISABLE_CODEGEN
  101. TGenerateResult DoGenGetValues(const TCodegenContext& ctx, BasicBlock*& block) const {
  102. auto& context = ctx.Codegen.GetContext();
  103. const auto result = GetNodeValues(Flow, ctx, block);
  104. const auto good = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, result.first, ConstantInt::get(result.first->getType(), 0), "good", block);
  105. const auto work = BasicBlock::Create(context, "work", ctx.Func);
  106. const auto pass = BasicBlock::Create(context, "pass", ctx.Func);
  107. BranchInst::Create(work, pass, good, block);
  108. block = work;
  109. for (auto i = 0U; i < Items.size(); ++i)
  110. if (Items[i]->GetDependencesCount() > 0U || !PasstroughtMap[i])
  111. EnsureDynamicCast<ICodegeneratorExternalNode*>(Items[i])->CreateSetValue(ctx, block, result.second[i](ctx, block));
  112. BranchInst::Create(pass, block);
  113. block = pass;
  114. TGettersList getters;
  115. getters.reserve(NewItems.size());
  116. for (auto i = 0U; i < NewItems.size(); ++i) {
  117. if (const auto map = ReversePasstroughtMap[i])
  118. getters.emplace_back(result.second[*map]);
  119. else
  120. getters.emplace_back([node=NewItems[i]](const TCodegenContext& ctx, BasicBlock*& block){ return GetNodeValue(node, ctx, block); });
  121. };
  122. return {result.first, std::move(getters)};
  123. }
  124. #endif
  125. private:
  126. void RegisterDependencies() const final {
  127. if (const auto flow = FlowDependsOn(Flow)) {
  128. std::for_each(Items.cbegin(), Items.cend(), std::bind(&TWideMapWrapper::Own, flow, std::placeholders::_1));
  129. std::for_each(NewItems.cbegin(), NewItems.cend(), std::bind(&TWideMapWrapper::DependsOn, flow, std::placeholders::_1));
  130. }
  131. }
  132. IComputationWideFlowNode* const Flow;
  133. const TComputationExternalNodePtrVector Items;
  134. const TComputationNodePtrVector NewItems;
  135. const TPasstroughtMap PasstroughtMap, ReversePasstroughtMap;
  136. const ui32 WideFieldsIndex;
  137. };
  138. class TNarrowMapWrapper : public TStatelessFlowCodegeneratorNode<TNarrowMapWrapper> {
  139. using TBaseComputation = TStatelessFlowCodegeneratorNode<TNarrowMapWrapper>;
  140. public:
  141. TNarrowMapWrapper(TComputationMutables& mutables, EValueRepresentation kind, IComputationWideFlowNode* flow, TComputationExternalNodePtrVector&& items, IComputationNode* newItem)
  142. : TBaseComputation(flow, kind)
  143. , Flow(flow)
  144. , Items(std::move(items))
  145. , NewItem(newItem)
  146. , PasstroughItem(GetPasstroughtMap(TComputationNodePtrVector{NewItem}, Items).front())
  147. , WideFieldsIndex(mutables.IncrementWideFieldsIndex(Items.size()))
  148. {}
  149. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  150. auto** fields = ctx.WideFields.data() + WideFieldsIndex;
  151. for (auto i = 0U; i < Items.size(); ++i) {
  152. if (NewItem == Items[i] || Items[i]->GetDependencesCount() > 0U)
  153. fields[i] = &Items[i]->RefValue(ctx);
  154. }
  155. switch (const auto result = Flow->FetchValues(ctx, fields)) {
  156. case EFetchResult::Finish:
  157. return NUdf::TUnboxedValuePod::MakeFinish();
  158. case EFetchResult::Yield:
  159. return NUdf::TUnboxedValuePod::MakeYield();
  160. case EFetchResult::One:
  161. return NewItem->GetValue(ctx).Release();
  162. }
  163. }
  164. #ifndef MKQL_DISABLE_CODEGEN
  165. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  166. auto& context = ctx.Codegen.GetContext();
  167. const auto getres = GetNodeValues(Flow, ctx, block);
  168. const auto yield = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, getres.first, ConstantInt::get(getres.first->getType(), 0), "yield", block);
  169. const auto good = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, getres.first, ConstantInt::get(getres.first->getType(), 0), "good", block);
  170. const auto outres = SelectInst::Create(yield, GetYield(context), GetFinish(context), "outres", block);
  171. const auto work = BasicBlock::Create(context, "work", ctx.Func);
  172. const auto pass = BasicBlock::Create(context, "pass", ctx.Func);
  173. const auto result = PHINode::Create(outres->getType(), 2, "result", pass);
  174. result->addIncoming(outres, block);
  175. BranchInst::Create(work, pass, good, block);
  176. block = work;
  177. if (const auto passtrough = PasstroughItem) {
  178. result->addIncoming(getres.second[*passtrough](ctx, block), block);
  179. } else {
  180. for (auto i = 0U; i < Items.size(); ++i)
  181. if (Items[i]->GetDependencesCount() > 0U)
  182. EnsureDynamicCast<ICodegeneratorExternalNode*>(Items[i])->CreateSetValue(ctx, block, getres.second[i](ctx, block));
  183. result->addIncoming(GetNodeValue(NewItem, ctx, block), block);
  184. }
  185. BranchInst::Create(pass, block);
  186. block = pass;
  187. return result;
  188. }
  189. #endif
  190. private:
  191. void RegisterDependencies() const final {
  192. if (const auto flow = FlowDependsOn(Flow)) {
  193. std::for_each(Items.cbegin(), Items.cend(), std::bind(&TNarrowMapWrapper::Own, flow, std::placeholders::_1));
  194. DependsOn(flow, NewItem);
  195. }
  196. }
  197. IComputationWideFlowNode* const Flow;
  198. const TComputationExternalNodePtrVector Items;
  199. IComputationNode* const NewItem;
  200. const std::optional<size_t> PasstroughItem;
  201. const ui32 WideFieldsIndex;
  202. };
  203. }
  204. IComputationNode* WrapExpandMap(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  205. const auto width = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetType()->GetReturnType()));
  206. MKQL_ENSURE(callable.GetInputsCount() == width + 2U, "Expected two or more args.");
  207. const auto flow = LocateNode(ctx.NodeLocator, callable, 0U);
  208. TComputationNodePtrVector newItems(width, nullptr);
  209. ui32 index = 1U;
  210. std::generate(newItems.begin(), newItems.end(), [&](){ return LocateNode(ctx.NodeLocator, callable, ++index); });
  211. const auto itemArg = LocateExternalNode(ctx.NodeLocator, callable, 1U);
  212. return new TExpandMapWrapper(flow, itemArg, std::move(newItems));
  213. }
  214. IComputationNode* WrapWideMap(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  215. MKQL_ENSURE(callable.GetInputsCount() > 0U, "Expected argument.");
  216. const auto inputWidth = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetInput(0U).GetStaticType()));
  217. const auto outputWidth = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetType()->GetReturnType()));
  218. MKQL_ENSURE(callable.GetInputsCount() == inputWidth + outputWidth + 1U, "Wrong signature.");
  219. const auto flow = LocateNode(ctx.NodeLocator, callable, 0U);
  220. if (const auto wide = dynamic_cast<IComputationWideFlowNode*>(flow)) {
  221. TComputationNodePtrVector newItems(outputWidth, nullptr);
  222. ui32 index = inputWidth;
  223. std::generate(newItems.begin(), newItems.end(), [&](){ return LocateNode(ctx.NodeLocator, callable, ++index); });
  224. TComputationExternalNodePtrVector args(inputWidth, nullptr);
  225. index = 0U;
  226. std::generate(args.begin(), args.end(), [&](){ return LocateExternalNode(ctx.NodeLocator, callable, ++index); });
  227. return new TWideMapWrapper(ctx.Mutables, wide, std::move(args), std::move(newItems));
  228. }
  229. THROW yexception() << "Expected wide flow.";
  230. }
  231. IComputationNode* WrapNarrowMap(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  232. MKQL_ENSURE(callable.GetInputsCount() > 1U, "Expected two or more args.");
  233. const auto width = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetInput(0U).GetStaticType()));
  234. MKQL_ENSURE(callable.GetInputsCount() == width + 2U, "Wrong signature.");
  235. const auto flow = LocateNode(ctx.NodeLocator, callable, 0U);
  236. if (const auto wide = dynamic_cast<IComputationWideFlowNode*>(flow)) {
  237. const auto newItem = LocateNode(ctx.NodeLocator, callable, callable.GetInputsCount() - 1U);
  238. TComputationExternalNodePtrVector args(width, nullptr);
  239. ui32 index = 0U;
  240. std::generate(args.begin(), args.end(), [&](){ return LocateExternalNode(ctx.NodeLocator, callable, ++index); });
  241. return new TNarrowMapWrapper(ctx.Mutables, GetValueRepresentation(callable.GetType()->GetReturnType()), wide, std::move(args), newItem);
  242. }
  243. THROW yexception() << "Expected wide flow.";
  244. }
  245. }
  246. }