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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
#include "mkql_flow.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <yql/essentials/minikql/mkql_node_cast.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
template <bool IsStream>
class TToFlowWrapper : public TFlowSourceCodegeneratorNode<TToFlowWrapper<IsStream>> {
typedef TFlowSourceCodegeneratorNode<TToFlowWrapper<IsStream>> TBaseComputation;
public:
TToFlowWrapper(TComputationMutables& mutables, EValueRepresentation kind,IComputationNode* stream)
: TBaseComputation(mutables, kind, EValueRepresentation::Any), Stream(stream)
{}
NUdf::TUnboxedValuePod DoCalculate(NUdf::TUnboxedValue& stream, TComputationContext& ctx) const {
if (stream.IsInvalid()) {
stream = IsStream ? Stream->GetValue(ctx) : Stream->GetValue(ctx).GetListIterator();
}
NUdf::TUnboxedValue next;
if constexpr (IsStream) {
switch (const auto state = stream.Fetch(next)) {
case NUdf::EFetchStatus::Ok: return next.Release();
case NUdf::EFetchStatus::Finish: return NUdf::TUnboxedValuePod::MakeFinish();
case NUdf::EFetchStatus::Yield: return NUdf::TUnboxedValuePod::MakeYield();
}
} else {
return stream.Next(next) ? next.Release() : NUdf::TUnboxedValuePod::MakeFinish();
}
}
#ifndef MKQL_DISABLE_CODEGEN
Value* DoGenerateGetValue(const TCodegenContext& ctx, Value* statePtr, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto init = BasicBlock::Create(context, "init", ctx.Func);
const auto main = BasicBlock::Create(context, "main", ctx.Func);
const auto load = new LoadInst(valueType, statePtr, "load", block);
const auto state = PHINode::Create(load->getType(), 2U, "state", main);
state->addIncoming(load, block);
BranchInst::Create(init, main, IsInvalid(load, block), block);
block = init;
if constexpr (IsStream) {
GetNodeValue(statePtr, Stream, ctx, block);
} else {
const auto list = GetNodeValue(Stream, ctx, block);
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetListIterator>(statePtr, list, ctx.Codegen, block);
if (Stream->IsTemporaryValue())
CleanupBoxed(list, ctx, block);
}
const auto save = new LoadInst(valueType, statePtr, "save", block);
state->addIncoming(save, block);
BranchInst::Create(main, block);
block = main;
const auto valuePtr = new AllocaInst(valueType, 0U, "value_ptr", &ctx.Func->getEntryBlock().back());
new StoreInst(ConstantInt::get(valueType, 0), valuePtr, block);
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto result = PHINode::Create(valueType, 2U, "result", done);
if constexpr (IsStream) {
const auto status = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::Fetch>(Type::getInt32Ty(context), state, ctx.Codegen, block, valuePtr);
const auto ok = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, status, ConstantInt::get(status->getType(), static_cast<ui32>(NUdf::EFetchStatus::Ok)), "ok", block);
const auto none = BasicBlock::Create(context, "none", ctx.Func);
BranchInst::Create(good, none, ok, block);
block = none;
const auto yield = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, status, ConstantInt::get(status->getType(), static_cast<ui32>(NUdf::EFetchStatus::Yield)), "yield", block);
const auto special = SelectInst::Create(yield, GetYield(context), GetFinish(context), "special", block);
result->addIncoming(special, block);
BranchInst::Create(done, block);
} else {
const auto status = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::Next>(Type::getInt1Ty(context), state, ctx.Codegen, block, valuePtr);
result->addIncoming(GetFinish(context), block);
BranchInst::Create(good, done, status, block);
}
block = good;
const auto value = new LoadInst(valueType, valuePtr, "value", block);
ValueRelease(static_cast<const IComputationNode*>(this)->GetRepresentation(), value, ctx, block);
result->addIncoming(value, block);
BranchInst::Create(done, block);
block = done;
return result;
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Stream);
}
IComputationNode* const Stream;
};
template <bool IsItemOptional = true>
class TOptToFlowWrapper : public TFlowSourceCodegeneratorNode<TOptToFlowWrapper<IsItemOptional>> {
typedef TFlowSourceCodegeneratorNode<TOptToFlowWrapper<IsItemOptional>> TBaseComputation;
public:
TOptToFlowWrapper(TComputationMutables& mutables, EValueRepresentation kind, IComputationNode* optional)
: TBaseComputation(mutables, kind, EValueRepresentation::Embedded), Optional(optional)
{}
NUdf::TUnboxedValuePod DoCalculate(NUdf::TUnboxedValue& state, TComputationContext& ctx) const {
if (state.IsFinish()) {
return state;
}
state = NUdf::TUnboxedValue::MakeFinish();
if (auto value = Optional->GetValue(ctx)) {
return value.Release().GetOptionalValueIf<IsItemOptional>();
}
return state;
}
#ifndef MKQL_DISABLE_CODEGEN
Value* DoGenerateGetValue(const TCodegenContext& ctx, Value* statePtr, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto main = BasicBlock::Create(context, "main", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto load = new LoadInst(valueType, statePtr, "load", block);
const auto result = PHINode::Create(valueType, 2U, "state", done);
result->addIncoming(load, block);
BranchInst::Create(done, main, IsFinish(load, block), block);
block = main;
const auto finish = GetFinish(context);
new StoreInst(finish, statePtr, block);
const auto optional = GetNodeValue(Optional, ctx, block);
const auto value = IsItemOptional ? GetOptionalValue(context, optional, block) : optional;
const auto output = SelectInst::Create(IsEmpty(optional, block), finish, value, "output", block);
result->addIncoming(output, block);
BranchInst::Create(done, block);
block = done;
return result;
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Optional);
}
IComputationNode* const Optional;
};
class TFromFlowWrapper : public TCustomValueCodegeneratorNode<TFromFlowWrapper> {
typedef TCustomValueCodegeneratorNode<TFromFlowWrapper> TBaseComputation;
public:
class TStreamValue : public TComputationValue<TStreamValue> {
public:
using TBase = TComputationValue<TStreamValue>;
TStreamValue(TMemoryUsageInfo* memInfo, TComputationContext& compCtx, IComputationNode* flow)
: TBase(memInfo), CompCtx(compCtx), Flow(flow)
{}
private:
NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& result) override {
result = Flow->GetValue(CompCtx);
if (result.IsFinish())
return NUdf::EFetchStatus::Finish;
if (result.IsYield())
return NUdf::EFetchStatus::Yield;
return NUdf::EFetchStatus::Ok;
}
TComputationContext& CompCtx;
IComputationNode* const Flow;
};
class TStreamCodegenValue : public TComputationValue<TStreamCodegenValue> {
public:
using TBase = TComputationValue<TStreamCodegenValue>;
using TFetchPtr = NUdf::EFetchStatus (*)(TComputationContext*, NUdf::TUnboxedValuePod&);
TStreamCodegenValue(TMemoryUsageInfo* memInfo, TFetchPtr fetch, TComputationContext* ctx)
: TBase(memInfo), FetchFunc(fetch), Ctx(ctx)
{}
protected:
NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& result) override {
return FetchFunc(Ctx, result);
}
const TFetchPtr FetchFunc;
TComputationContext* const Ctx;
};
TFromFlowWrapper(TComputationMutables& mutables, IComputationNode* flow)
: TBaseComputation(mutables), Flow(flow)
{}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
#ifndef MKQL_DISABLE_CODEGEN
if (ctx.ExecuteLLVM && Fetch)
return ctx.HolderFactory.Create<TStreamCodegenValue>(Fetch, &ctx);
#endif
return ctx.HolderFactory.Create<TStreamValue>(ctx, Flow);
}
private:
void RegisterDependencies() const final {
this->DependsOn(Flow);
}
#ifndef MKQL_DISABLE_CODEGEN
void GenerateFunctions(NYql::NCodegen::ICodegen& codegen) final {
FetchFunc = GenerateFetcher(codegen);
codegen.ExportSymbol(FetchFunc);
}
void FinalizeFunctions(NYql::NCodegen::ICodegen& codegen) final {
if (FetchFunc)
Fetch = reinterpret_cast<TStreamCodegenValue::TFetchPtr>(codegen.GetPointerToFunction(FetchFunc));
}
Function* GenerateFetcher(NYql::NCodegen::ICodegen& codegen) const {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
const auto& name = TBaseComputation::MakeName("Fetch");
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto valueType = Type::getInt128Ty(context);
const auto contextType = GetCompContextType(context);
const auto statusType = Type::getInt32Ty(context);
const auto funcType = FunctionType::get(statusType, {PointerType::getUnqual(contextType), 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 valuePtr = &*++args;
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
SafeUnRefUnboxed(valuePtr, ctx, block);
GetNodeValue(valuePtr, Flow, ctx, block);
const auto value = new LoadInst(valueType, valuePtr, "value", block);
const auto second = SelectInst::Create(IsYield(value, block), ConstantInt::get(statusType, static_cast<ui32>(NUdf::EFetchStatus::Yield)), ConstantInt::get(statusType, static_cast<ui32>(NUdf::EFetchStatus::Ok)), "second", block);
const auto first = SelectInst::Create(IsFinish(value, block), ConstantInt::get(statusType, static_cast<ui32>(NUdf::EFetchStatus::Finish)), second, "second", block);
ReturnInst::Create(context, first, block);
return ctx.Func;
}
Function* FetchFunc = nullptr;
TStreamCodegenValue::TFetchPtr Fetch = nullptr;
#endif
IComputationNode* const Flow;
};
class TToWideFlowWrapper : public TWideFlowSourceCodegeneratorNode<TToWideFlowWrapper> {
using TBaseComputation = TWideFlowSourceCodegeneratorNode<TToWideFlowWrapper>;
public:
TToWideFlowWrapper(TComputationMutables& mutables, IComputationNode* stream, ui32 width)
: TBaseComputation(mutables, EValueRepresentation::Any)
, Stream(stream)
, Width(width)
, TempStateIndex(std::exchange(mutables.CurValueIndex, mutables.CurValueIndex + Width))
{}
EFetchResult DoCalculate(NUdf::TUnboxedValue& state, TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
if (state.IsInvalid()) {
state = Stream->GetValue(ctx);
}
switch (const auto status = state.WideFetch(ctx.MutableValues.get() + TempStateIndex, Width)) {
case NUdf::EFetchStatus::Finish:
return EFetchResult::Finish;
case NUdf::EFetchStatus::Yield:
return EFetchResult::Yield;
case NUdf::EFetchStatus::Ok:
break;
}
for (auto i = 0U; i < Width; ++i) {
if (const auto out = output[i]) {
*out = std::move(ctx.MutableValues[TempStateIndex + i]);
}
}
return EFetchResult::One;
}
#ifndef MKQL_DISABLE_CODEGEN
TGenerateResult DoGenGetValues(const TCodegenContext& ctx, Value* statePtr, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto indexType = Type::getInt32Ty(context);
const auto values = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(indexType, TempStateIndex)}, "values", &ctx.Func->getEntryBlock().back());
const auto init = BasicBlock::Create(context, "init", ctx.Func);
const auto main = BasicBlock::Create(context, "main", ctx.Func);
const auto load = new LoadInst(valueType, statePtr, "load", block);
const auto state = PHINode::Create(load->getType(), 2U, "state", main);
state->addIncoming(load, block);
BranchInst::Create(init, main, IsInvalid(load, block), block);
block = init;
GetNodeValue(statePtr, Stream, ctx, block);
const auto save = new LoadInst(valueType, statePtr, "save", block);
state->addIncoming(save, block);
BranchInst::Create(main, block);
block = main;
const auto status = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::WideFetch>(indexType, state, ctx.Codegen, block, values, ConstantInt::get(indexType, Width));
const auto ok = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, status, ConstantInt::get(indexType, static_cast<ui32>(NUdf::EFetchStatus::Ok)), "ok", block);
const auto yield = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, status, ConstantInt::get(indexType, static_cast<ui32>(NUdf::EFetchStatus::Yield)), "yield", block);
const auto special = SelectInst::Create(yield, ConstantInt::get(indexType, static_cast<i32>(EFetchResult::Yield)), ConstantInt::get(indexType, static_cast<i32>(EFetchResult::Finish)), "special", block);
const auto complete = SelectInst::Create(ok, ConstantInt::get(indexType, static_cast<i32>(EFetchResult::One)), special, "complete", block);
TGettersList getters(Width);
for (auto i = 0U; i < getters.size(); ++i) {
getters[i] = [idx = TempStateIndex + i, valueType, indexType](const TCodegenContext& ctx, BasicBlock*& block) {
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(indexType, idx)}, (TString("ptr_") += ToString(idx)).c_str(), block);
return new LoadInst(valueType, valuePtr, (TString("val_") += ToString(idx)).c_str(), block);
};
};
return {complete, std::move(getters)};
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Stream);
}
IComputationNode* const Stream;
const ui32 Width;
const ui32 TempStateIndex;
};
class TFromWideFlowWrapper : public TCustomValueCodegeneratorNode<TFromWideFlowWrapper> {
using TBaseComputation = TCustomValueCodegeneratorNode<TFromWideFlowWrapper>;
public:
class TStreamValue : public TComputationValue<TStreamValue> {
public:
using TBase = TComputationValue<TStreamValue>;
TStreamValue(TMemoryUsageInfo* memInfo, TComputationContext& compCtx, IComputationWideFlowNode* wideFlow, ui32 width, ui32 stubsIndex)
: TBase(memInfo)
, CompCtx(compCtx)
, WideFlow(wideFlow)
, Width(width)
, StubsIndex(stubsIndex)
, ClientBuffer(nullptr)
{}
private:
NUdf::EFetchStatus WideFetch(NUdf::TUnboxedValue* result, ui32 width) final {
if (width != Width)
Throw(width, Width);
const auto valuePtrs = CompCtx.WideFields.data() + StubsIndex;
if (result != ClientBuffer) {
for (ui32 i = 0; i < width; ++i) {
valuePtrs[i] = result + i;
}
ClientBuffer = result;
}
switch (const auto status = WideFlow->FetchValues(CompCtx, valuePtrs)) {
case EFetchResult::Finish:
return NUdf::EFetchStatus::Finish;
case EFetchResult::Yield:
return NUdf::EFetchStatus::Yield;
case EFetchResult::One:
return NUdf::EFetchStatus::Ok;
}
}
TComputationContext& CompCtx;
IComputationWideFlowNode* const WideFlow;
const ui32 Width;
const ui32 StubsIndex;
const NUdf::TUnboxedValue* ClientBuffer;
};
class TStreamCodegenValue : public TComputationValue<TStreamCodegenValue> {
public:
using TBase = TComputationValue<TStreamCodegenValue>;
using TWideFetchPtr = NUdf::EFetchStatus (*)(TComputationContext*, NUdf::TUnboxedValuePod*, ui32);
TStreamCodegenValue(TMemoryUsageInfo* memInfo, TWideFetchPtr fetch, TComputationContext* ctx)
: TBase(memInfo), WideFetchFunc(fetch), Ctx(ctx)
{}
private:
NUdf::EFetchStatus WideFetch(NUdf::TUnboxedValue* result, ui32 width) final {
return WideFetchFunc(Ctx, result, width);
}
const TWideFetchPtr WideFetchFunc;
TComputationContext* const Ctx;
};
TFromWideFlowWrapper(TComputationMutables& mutables, IComputationWideFlowNode* wideFlow, std::vector<EValueRepresentation>&& representations)
: TBaseComputation(mutables)
, WideFlow(wideFlow)
, Representations(std::move(representations))
, StubsIndex(mutables.IncrementWideFieldsIndex(Representations.size()))
{}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
#ifndef MKQL_DISABLE_CODEGEN
if (ctx.ExecuteLLVM && WideFetch)
return ctx.HolderFactory.Create<TStreamCodegenValue>(WideFetch, &ctx);
#endif
return ctx.HolderFactory.Create<TStreamValue>(ctx, WideFlow, Representations.size(), StubsIndex);
}
private:
void RegisterDependencies() const final {
this->DependsOn(WideFlow);
}
[[noreturn]] static void Throw(ui32 requested, ui32 expected) {
TStringBuilder res;
res << "Requested " << requested << " fields, but expected " << expected;
UdfTerminate(res.data());
}
#ifndef MKQL_DISABLE_CODEGEN
void GenerateFunctions(NYql::NCodegen::ICodegen& codegen) final {
WideFetchFunc = GenerateFetcher(codegen);
codegen.ExportSymbol(WideFetchFunc);
}
void FinalizeFunctions(NYql::NCodegen::ICodegen& codegen) final {
if (WideFetchFunc)
WideFetch = reinterpret_cast<TStreamCodegenValue::TWideFetchPtr>(codegen.GetPointerToFunction(WideFetchFunc));
}
Function* GenerateFetcher(NYql::NCodegen::ICodegen& codegen) const {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
const auto& name = TBaseComputation::MakeName("WideFetch");
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto valueType = Type::getInt128Ty(context);
const auto contextType = GetCompContextType(context);
const auto statusType = Type::getInt32Ty(context);
const auto indexType = Type::getInt32Ty(context);
const auto funcType = FunctionType::get(statusType, {PointerType::getUnqual(contextType), PointerType::getUnqual(valueType), indexType}, 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 valuesPtr = &*++args;
const auto width = &*++args;
const auto main = BasicBlock::Create(context, "main", ctx.Func);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
const auto fail = BasicBlock::Create(context, "fail", ctx.Func);
auto block = main;
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, width, ConstantInt::get(width->getType(), Representations.size()), "check", block);
BranchInst::Create(work, fail, check, block);
block = work;
std::vector<Value*> pointers(Representations.size());
for (auto i = 0U; i < pointers.size(); ++i) {
pointers[i] = GetElementPtrInst::CreateInBounds(valueType, valuesPtr, {ConstantInt::get(indexType, i)}, (TString("ptr_") += ToString(i)).c_str(), block);
SafeUnRefUnboxed(pointers[i], ctx, block);
}
const auto getres = GetNodeValues(WideFlow, ctx, block);
const auto yield = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, getres.first, ConstantInt::get(indexType, static_cast<i32>(EFetchResult::Yield)), "yield", block);
const auto special = SelectInst::Create(yield, ConstantInt::get(indexType, static_cast<ui32>(NUdf::EFetchStatus::Yield)), ConstantInt::get(indexType, static_cast<ui32>(NUdf::EFetchStatus::Finish)), "special", block);
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto result = PHINode::Create(statusType, 2U, "result", done);
result->addIncoming(special, block);
const auto row = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, getres.first, ConstantInt::get(indexType, static_cast<i32>(EFetchResult::One)), "row", block);
BranchInst::Create(good, done, row, block);
block = good;
for (auto i = 0U; i < pointers.size(); ++i) {
auto value = getres.second[i](ctx, block);
ValueAddRef(Representations[i], value, ctx, block);
new StoreInst(value, pointers[i], block);
}
result->addIncoming(ConstantInt::get(indexType, static_cast<ui32>(NUdf::EFetchStatus::Ok)), block);
BranchInst::Create(done, block);
block = done;
ReturnInst::Create(context, result, block);
block = fail;
const auto throwFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TFromWideFlowWrapper::Throw));
const auto throwFuncType = FunctionType::get(Type::getVoidTy(context), { indexType, indexType }, false);
const auto throwFuncPtr = CastInst::Create(Instruction::IntToPtr, throwFunc, PointerType::getUnqual(throwFuncType), "thrower", block);
CallInst::Create(throwFuncType, throwFuncPtr, { width, ConstantInt::get(width->getType(), Representations.size()) }, "", block)->setTailCall();
new UnreachableInst(context, block);
return ctx.Func;
}
Function* WideFetchFunc = nullptr;
TStreamCodegenValue::TWideFetchPtr WideFetch = nullptr;
#endif
IComputationWideFlowNode* const WideFlow;
const std::vector<EValueRepresentation> Representations;
const ui32 StubsIndex;
};
} // namespace
IComputationNode* WrapToFlow(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 args, got " << callable.GetInputsCount());
const auto type = callable.GetInput(0).GetStaticType();
const auto outType = AS_TYPE(TFlowType, callable.GetType()->GetReturnType())->GetItemType();
const auto kind = GetValueRepresentation(outType);
if (type->IsStream()) {
if (const auto streamType = AS_TYPE(TStreamType, type); streamType->GetItemType()->IsMulti()) {
const auto multiType = AS_TYPE(TMultiType, streamType->GetItemType());
return new TToWideFlowWrapper(ctx.Mutables, LocateNode(ctx.NodeLocator, callable, 0), multiType->GetElementsCount());
}
return new TToFlowWrapper<true>(ctx.Mutables, kind, LocateNode(ctx.NodeLocator, callable, 0));
} else if (type->IsList()) {
return new TToFlowWrapper<false>(ctx.Mutables, kind, LocateNode(ctx.NodeLocator, callable, 0));
} else if (type->IsOptional()) {
if (outType->IsOptional()) {
return new TOptToFlowWrapper<true>(ctx.Mutables, kind, LocateNode(ctx.NodeLocator, callable, 0));
} else {
return new TOptToFlowWrapper<false>(ctx.Mutables, kind, LocateNode(ctx.NodeLocator, callable, 0));
}
}
THROW yexception() << "Expected optional, list or stream.";
}
IComputationNode* WrapFromFlow(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 args, got " << callable.GetInputsCount());
const auto flow = LocateNode(ctx.NodeLocator, callable, 0);
if (const auto wide = dynamic_cast<IComputationWideFlowNode*>(flow)) {
const auto multiType = AS_TYPE(TMultiType, AS_TYPE(TFlowType, callable.GetInput(0).GetStaticType())->GetItemType());
std::vector<EValueRepresentation> outputRepresentations(multiType->GetElementsCount());
for (auto i = 0U; i < outputRepresentations.size(); ++i)
outputRepresentations[i] = GetValueRepresentation(multiType->GetElementType(i));
return new TFromWideFlowWrapper(ctx.Mutables, wide, std::move(outputRepresentations));
}
return new TFromFlowWrapper(ctx.Mutables, flow);
}
}
}
|