1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#include "mkql_dictitems.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_holders_codegen.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_program_builder.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
class TDictItemsWrapper : public TCustomValueCodegeneratorNode<TDictItemsWrapper> {
typedef TCustomValueCodegeneratorNode<TDictItemsWrapper> TBaseComputation;
public:
using TSelf = TDictItemsWrapper;
#ifndef MKQL_DISABLE_CODEGEN
class TCodegenValue : public TComputationValue<TCodegenValue> {
public:
using TNextPtr = TCodegenIterator::TNextPtr;
TCodegenValue(TMemoryUsageInfo* memInfo, TNextPtr next, TComputationContext* ctx, NUdf::TUnboxedValue&& dict)
: TComputationValue<TCodegenValue>(memInfo)
, NextFunc(next)
, Ctx(ctx)
, Dict(std::move(dict))
{}
private:
NUdf::TUnboxedValue GetListIterator() const final {
return Ctx->HolderFactory.Create<TCodegenIterator>(NextFunc, Ctx, Dict.GetDictIterator());
}
ui64 GetListLength() const final {
return Dict.GetDictLength();
}
bool HasListItems() const final {
return Dict.HasDictItems();
}
bool HasFastListLength() const final {
return true;
}
const TNextPtr NextFunc;
TComputationContext* const Ctx;
const NUdf::TUnboxedValue Dict;
};
#endif
class TValue : public TComputationValue<TValue> {
public:
class TIterator : public TComputationValue<TIterator> {
public:
TIterator(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& inner,
TComputationContext& compCtx, const TSelf* self)
: TComputationValue<TIterator>(memInfo)
, Inner(std::move(inner))
, CompCtx(compCtx)
, Self(self)
{
}
private:
bool Next(NUdf::TUnboxedValue& value) override {
NUdf::TUnboxedValue key, payload;
if (!Inner.NextPair(key, payload))
return false;
NUdf::TUnboxedValue* items = nullptr;
value = Self->ResPair.NewArray(CompCtx, 2, items);
items[0] = std::move(key);
items[1] = std::move(payload);
return true;
}
bool Skip() override {
return Inner.Skip();
}
const NUdf::TUnboxedValue Inner;
TComputationContext& CompCtx;
const TSelf* const Self;
};
TValue(
TMemoryUsageInfo* memInfo,
const NUdf::TUnboxedValue&& dict,
TComputationContext& compCtx, const TSelf* self)
: TComputationValue<TValue>(memInfo)
, Dict(std::move(dict))
, CompCtx(compCtx)
, Self(self)
{
}
private:
ui64 GetListLength() const final {
return Dict.GetDictLength();
}
bool HasListItems() const final {
return Dict.HasDictItems();
}
bool HasFastListLength() const final {
return true;
}
NUdf::TUnboxedValue GetListIterator() const final {
return CompCtx.HolderFactory.Create<TIterator>(Dict.GetDictIterator(), CompCtx, Self);
}
const NUdf::TUnboxedValue Dict;
TComputationContext& CompCtx;
const TSelf* const Self;
};
TDictItemsWrapper(TComputationMutables& mutables, IComputationNode* dict)
: TBaseComputation(mutables)
, Dict(dict)
, ResPair(mutables)
{}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
#ifndef MKQL_DISABLE_CODEGEN
if (ctx.ExecuteLLVM && Next)
return ctx.HolderFactory.Create<TCodegenValue>(Next, &ctx, Dict->GetValue(ctx));
#endif
return ctx.HolderFactory.Create<TValue>(Dict->GetValue(ctx), ctx, this);
}
private:
void RegisterDependencies() const final {
DependsOn(Dict);
}
#ifndef MKQL_DISABLE_CODEGEN
void GenerateFunctions(NYql::NCodegen::ICodegen& codegen) final {
NextFunc = GenerateNext(codegen);
codegen.ExportSymbol(NextFunc);
}
void FinalizeFunctions(NYql::NCodegen::ICodegen& codegen) final {
if (NextFunc)
Next = reinterpret_cast<TNextPtr>(codegen.GetPointerToFunction(NextFunc));
}
Function* GenerateNext(NYql::NCodegen::ICodegen& codegen) const {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
const auto& name = TBaseComputation::MakeName("Next");
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto valueType = Type::getInt128Ty(context);
const auto indexType = Type::getInt32Ty(context);
const auto pairType = ArrayType::get(valueType, 2U);
const auto containerType = codegen.GetEffectiveTarget() == NYql::NCodegen::ETarget::Windows ? static_cast<Type*>(PointerType::getUnqual(valueType)) : static_cast<Type*>(valueType);
const auto contextType = GetCompContextType(context);
const auto statusType = Type::getInt1Ty(context);
const auto funcType = FunctionType::get(statusType, {PointerType::getUnqual(contextType), containerType, PointerType::getUnqual(valueType)}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
ctx.Ctx = &*args;
const auto containerArg = &*++args;
const auto valuePtr = &*++args;
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
const auto container = codegen.GetEffectiveTarget() == NYql::NCodegen::ETarget::Windows ?
new LoadInst(valueType, containerArg, "load_container", false, block) : static_cast<Value*>(containerArg);
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto pairPtr = new AllocaInst(pairType, 0U, "pair_ptr", block);
new StoreInst(ConstantAggregateZero::get(pairType), pairPtr, block);
const auto keyPtr = GetElementPtrInst::CreateInBounds(pairType, pairPtr, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 0)}, "key_ptr", block);
const auto payPtr = GetElementPtrInst::CreateInBounds(pairType, pairPtr, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 1)}, "pay_ptr", block);
const auto status = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::NextPair>(statusType, container, codegen, block, keyPtr, payPtr);
BranchInst::Create(good, done, status, block);
block = good;
SafeUnRefUnboxed(valuePtr, ctx, block);
const auto itemsType = PointerType::getUnqual(pairType);
const auto itemsPtr = new AllocaInst(itemsType, 0U, "items_ptr", block);
const auto output = ResPair.GenNewArray(2U, itemsPtr, ctx, block);
AddRefBoxed(output, ctx, block);
const auto items = new LoadInst(itemsType, itemsPtr, "items", block);
const auto pair = new LoadInst(pairType, pairPtr, "pair", block);
new StoreInst(pair, items, block);
new StoreInst(output, valuePtr, block);
BranchInst::Create(done, block);
block = done;
ReturnInst::Create(context, status, block);
return ctx.Func;
}
using TNextPtr = typename TCodegenIterator::TNextPtr;
Function* NextFunc = nullptr;
TNextPtr Next = nullptr;
#endif
IComputationNode* const Dict;
const TContainerCacheOnContext ResPair;
};
template <bool KeysOrPayloads>
class TDictHalfsWrapper : public TMutableComputationNode<TDictHalfsWrapper<KeysOrPayloads>> {
typedef TMutableComputationNode<TDictHalfsWrapper<KeysOrPayloads>> TBaseComputation;
public:
using TSelf = TDictHalfsWrapper<KeysOrPayloads>;
class TValue : public TComputationValue<TValue> {
public:
TValue(
TMemoryUsageInfo* memInfo,
const NUdf::TUnboxedValue&& dict,
TComputationContext&, const TSelf*)
: TComputationValue<TValue>(memInfo)
, Dict(std::move(dict))
{}
private:
ui64 GetListLength() const final {
return Dict.GetDictLength();
}
bool HasListItems() const final {
return Dict.HasDictItems();
}
bool HasFastListLength() const final {
return true;
}
NUdf::TUnboxedValue GetListIterator() const final {
return KeysOrPayloads ? Dict.GetKeysIterator() : Dict.GetPayloadsIterator();
}
const NUdf::TUnboxedValue Dict;
};
TDictHalfsWrapper(TComputationMutables& mutables, IComputationNode* dict)
: TBaseComputation(mutables), Dict(dict)
{}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
return ctx.HolderFactory.Create<TValue>(Dict->GetValue(ctx), ctx, this);
}
private:
void RegisterDependencies() const final {
this->DependsOn(Dict);
}
IComputationNode* const Dict;
};
}
IComputationNode* WrapDictItems(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 1 || callable.GetInputsCount() == 2, "Expected one or two args");
const auto node = LocateNode(ctx.NodeLocator, callable, 0);
if (1U == callable.GetInputsCount()) {
return new TDictItemsWrapper(ctx.Mutables, node);
}
const auto mode = AS_VALUE(TDataLiteral, callable.GetInput(1))->AsValue().Get<ui32>();
switch (static_cast<EDictItems>(mode)) {
case EDictItems::Both:
return new TDictItemsWrapper(ctx.Mutables, node);
case EDictItems::Keys:
return new TDictHalfsWrapper<true>(ctx.Mutables, node);
case EDictItems::Payloads:
return new TDictHalfsWrapper<false>(ctx.Mutables, node);
default:
Y_ABORT("Unknown mode: %" PRIu32, mode);
}
}
IComputationNode* WrapDictKeys(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected one arg");
const auto node = LocateNode(ctx.NodeLocator, callable, 0);
return new TDictHalfsWrapper<true>(ctx.Mutables, node);
}
IComputationNode* WrapDictPayloads(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected one arg");
const auto node = LocateNode(ctx.NodeLocator, callable, 0);
return new TDictHalfsWrapper<false>(ctx.Mutables, node);
}
}
}
|