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
|
#include "mkql_element.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_node_builder.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
enum class EOptionalityHandlerStrategy {
// Just return child as is.
ReturnChildAsIs,
// Return child and add optionality to it.
AddOptionalToChild,
// Return child but set optionality to (tuple & child) intersection.
IntersectOptionals,
};
inline bool IsOptionalOrNull(const TType* type) {
return type->IsOptional() || type->IsNull() || type->IsPg();
}
// The strategy is based on tuple and its child optionality.
// Tuple<X> -> return child as is (ReturnChildAsIs).
// Tuple<X?> -> return child as is (ReturnChildAsIs).
// Tuple<X>? -> return child and add extra optional level (AddOptionalToChild).
// Tuple<X?>? -> return child as is BUT set mask to (tuple & child) intersection (IntersectOptionals).
EOptionalityHandlerStrategy GetStrategyBasedOnTupleType(TType* tupleType, TType* elementType) {
if (!tupleType->IsOptional()) {
return EOptionalityHandlerStrategy::ReturnChildAsIs;
} else if (IsOptionalOrNull(elementType)) {
return EOptionalityHandlerStrategy::IntersectOptionals;
} else {
return EOptionalityHandlerStrategy::AddOptionalToChild;
}
Y_UNREACHABLE();
}
constexpr bool IsTupleOptional(EOptionalityHandlerStrategy strategy) {
switch (strategy) {
case EOptionalityHandlerStrategy::ReturnChildAsIs:
return false;
case EOptionalityHandlerStrategy::AddOptionalToChild:
case EOptionalityHandlerStrategy::IntersectOptionals:
return true;
}
Y_UNREACHABLE();
}
template <bool IsOptional>
class TElementsWrapper : public TMutableCodegeneratorNode<TElementsWrapper<IsOptional>> {
typedef TMutableCodegeneratorNode<TElementsWrapper<IsOptional>> TBaseComputation;
public:
TElementsWrapper(TComputationMutables& mutables, IComputationNode* array)
: TBaseComputation(mutables, EValueRepresentation::Embedded), Array(array)
{}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& compCtx) const {
const auto& array = Array->GetValue(compCtx);
if constexpr (IsOptional) {
return array ? NUdf::TUnboxedValuePod(reinterpret_cast<ui64>(array.GetElements())) : NUdf::TUnboxedValuePod();
} else {
return NUdf::TUnboxedValuePod(reinterpret_cast<ui64>(array.GetElements()));
}
}
#ifndef MKQL_DISABLE_CODEGEN
Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto array = GetNodeValue(Array, ctx, block);
const auto elementsType = PointerType::getUnqual(array->getType());
if constexpr (IsOptional) {
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto result = PHINode::Create(array->getType(), 2U, "result", done);
result->addIncoming(ConstantInt::get(array->getType(), 0ULL), block);
BranchInst::Create(done, good, IsEmpty(array, block, context), block);
block = good;
const auto elements = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElements>(elementsType, array, ctx.Codegen, block);
const auto cast = CastInst::Create(Instruction::PtrToInt, elements, Type::getInt64Ty(context), "cast", block);
const auto wide = SetterFor<ui64>(cast, context, block);
result->addIncoming(wide, block);
BranchInst::Create(done, block);
block = done;
return result;
} else {
const auto elements = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElements>(elementsType, array, ctx.Codegen, block);
const auto cast = CastInst::Create(Instruction::PtrToInt, elements, Type::getInt64Ty(context), "cast", block);
return SetterFor<ui64>(cast, context, block);
}
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Array);
}
IComputationNode* const Array;
};
template <EOptionalityHandlerStrategy Strategy>
class TElementWrapper : public TMutableCodegeneratorPtrNode<TElementWrapper<Strategy>> {
typedef TMutableCodegeneratorPtrNode<TElementWrapper<Strategy>> TBaseComputation;
public:
TElementWrapper(TComputationMutables& mutables, EValueRepresentation kind, IComputationNode* cache, IComputationNode* array, ui32 index)
: TBaseComputation(mutables, kind), Cache(cache), Array(array), Index(index)
{}
NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
if (Cache->GetDependencesCount() > 1U) {
const auto cache = Cache->GetValue(ctx);
if (IsTupleOptional(Strategy) && !cache) {
return NUdf::TUnboxedValue();
}
const auto elements = cache.Get<ui64>();
if (elements) {
auto element = reinterpret_cast<const NUdf::TUnboxedValuePod*>(elements)[Index];
if constexpr (Strategy == EOptionalityHandlerStrategy::IntersectOptionals) {
return element;
} else if constexpr (Strategy == EOptionalityHandlerStrategy::AddOptionalToChild) {
return element.MakeOptional();
} else if constexpr (Strategy == EOptionalityHandlerStrategy::ReturnChildAsIs) {
return element;
} else {
static_assert(false, "Unsupported type.");
}
}
}
const auto& array = Array->GetValue(ctx);
if constexpr (Strategy == EOptionalityHandlerStrategy::IntersectOptionals) {
return array ? array.GetElement(Index) : NUdf::TUnboxedValue();
} else if constexpr (Strategy == EOptionalityHandlerStrategy::AddOptionalToChild) {
return array ? NUdf::TUnboxedValue(array.GetElement(Index).MakeOptional()) : NUdf::TUnboxedValue();
} else if constexpr (Strategy == EOptionalityHandlerStrategy::ReturnChildAsIs) {
return array.GetElement(Index);
} else {
static_assert(false, "Unsupported type.");
}
}
#ifndef MKQL_DISABLE_CODEGEN
void DoGenerateGetElement(const TCodegenContext& ctx, Value* pointer, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto array = GetNodeValue(Array, ctx, block);
const auto index = ConstantInt::get(Type::getInt32Ty(context), Index);
if constexpr (IsTupleOptional(Strategy)) {
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto zero = BasicBlock::Create(context, "zero", ctx.Func);
const auto exit = BasicBlock::Create(context, "exit", ctx.Func);
BranchInst::Create(zero, good, IsEmpty(array, block, context), block);
block = zero;
new StoreInst(ConstantInt::get(array->getType(), 0ULL), pointer, block);
BranchInst::Create(exit, block);
block = good;
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(pointer, array, ctx.Codegen, block, index);
if constexpr (Strategy == EOptionalityHandlerStrategy::AddOptionalToChild) {
const auto load = new LoadInst(valueType, pointer, "load", block);
new StoreInst(MakeOptional(context, load, block), pointer, block);
}
if (Array->IsTemporaryValue())
CleanupBoxed(array, ctx, block);
BranchInst::Create(exit, block);
block = exit;
} else if constexpr (Strategy == EOptionalityHandlerStrategy::ReturnChildAsIs){
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(pointer, array, ctx.Codegen, block, index);
if (Array->IsTemporaryValue())
CleanupBoxed(array, ctx, block);
} else {
static_assert(false, "Unhandled case.");
}
}
void DoGenerateGetValue(const TCodegenContext& ctx, Value* pointer, BasicBlock*& block) const {
if (Cache->GetDependencesCount() <= 1U) {
return DoGenerateGetElement(ctx, pointer, block);
}
auto& context = ctx.Codegen.GetContext();
const auto cache = GetNodeValue(Cache, ctx, block);
const auto fast = BasicBlock::Create(context, "fast", ctx.Func);
const auto slow = BasicBlock::Create(context, "slow", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
if constexpr (IsTupleOptional(Strategy)) {
const auto zero = ConstantInt::get(cache->getType(), 0ULL);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, cache, zero, "check", block);
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto none = BasicBlock::Create(context, "none", ctx.Func);
BranchInst::Create(none, good, check, block);
block = none;
new StoreInst(zero, pointer, block);
BranchInst::Create(done, block);
block = good;
}
const auto trunc = CastInst::Create(Instruction::Trunc, cache, Type::getInt64Ty(context), "trunc", block);
const auto type = PointerType::getUnqual(cache->getType());
const auto elements = CastInst::Create(Instruction::IntToPtr, trunc, type, "elements", block);
const auto fill = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, elements, ConstantPointerNull::get(type), "fill", block);
BranchInst::Create(fast, slow, fill, block);
block = fast;
const auto index = ConstantInt::get(Type::getInt32Ty(context), this->Index);
const auto ptr = GetElementPtrInst::CreateInBounds(cache->getType(), elements, {index}, "ptr", block);
const auto item = new LoadInst(cache->getType(), ptr, "item", block);
ValueAddRef(this->GetRepresentation(), item, ctx, block);
if constexpr (Strategy == EOptionalityHandlerStrategy::AddOptionalToChild) {
new StoreInst(MakeOptional(context, item, block), pointer, block);
} else {
new StoreInst(item, pointer, block);
}
BranchInst::Create(done, block);
block = slow;
DoGenerateGetElement(ctx, pointer, block);
BranchInst::Create(done, block);
block = done;
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Array);
this->DependsOn(Cache);
}
IComputationNode *const Cache;
IComputationNode *const Array;
const ui32 Index;
};
IComputationNode* WrapElements(IComputationNode* array, const TComputationNodeFactoryContext& ctx, bool isOptional) {
if (isOptional) {
return new TElementsWrapper<true>(ctx.Mutables, array);
} else {
return new TElementsWrapper<false>(ctx.Mutables, array);
}
}
}
IComputationNode* WrapNth(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 2U, "Expected two args.");
const auto input = callable.GetInput(0U);
bool isOptional;
const auto tupleType = AS_TYPE(TTupleType, UnpackOptional(input.GetStaticType(), isOptional));
const auto indexData = AS_VALUE(TDataLiteral, callable.GetInput(1U));
const auto index = indexData->AsValue().Get<ui32>();
MKQL_ENSURE(index < tupleType->GetElementsCount(), "Bad tuple index");
auto nthStrategy = GetStrategyBasedOnTupleType(input.GetStaticType(), tupleType->GetElementType(index));
const auto tuple = LocateNode(ctx.NodeLocator, callable, 0);
const auto ins = ctx.ElementsCache.emplace(tuple, nullptr);
if (ins.second) {
ctx.NodePushBack(ins.first->second = WrapElements(tuple, ctx, isOptional));
}
switch (nthStrategy) {
case EOptionalityHandlerStrategy::ReturnChildAsIs:
return new TElementWrapper<EOptionalityHandlerStrategy::ReturnChildAsIs>(ctx.Mutables, GetValueRepresentation(tupleType->GetElementType(index)), ins.first->second, tuple, index);
case EOptionalityHandlerStrategy::IntersectOptionals:
return new TElementWrapper<EOptionalityHandlerStrategy::IntersectOptionals>(ctx.Mutables, GetValueRepresentation(tupleType->GetElementType(index)), ins.first->second, tuple, index);
case EOptionalityHandlerStrategy::AddOptionalToChild:
return new TElementWrapper<EOptionalityHandlerStrategy::AddOptionalToChild>(ctx.Mutables, GetValueRepresentation(tupleType->GetElementType(index)), ins.first->second, tuple, index);
}
}
IComputationNode* WrapMember(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 2U, "Expected two args.");
const auto input = callable.GetInput(0U);
bool isOptional;
const auto structType = AS_TYPE(TStructType, UnpackOptional(input.GetStaticType(), isOptional));
const auto indexData = AS_VALUE(TDataLiteral, callable.GetInput(1U));
const auto index = indexData->AsValue().Get<ui32>();
MKQL_ENSURE(index < structType->GetMembersCount(), "Bad member index");
const auto structObj = LocateNode(ctx.NodeLocator, callable, 0U);
const auto ins = ctx.ElementsCache.emplace(structObj, nullptr);
if (ins.second) {
ctx.NodePushBack(ins.first->second = WrapElements(structObj, ctx, isOptional));
}
auto nthStrategy = GetStrategyBasedOnTupleType(input.GetStaticType(), structType->GetMemberType(index));
switch (nthStrategy) {
case EOptionalityHandlerStrategy::ReturnChildAsIs:
return new TElementWrapper<EOptionalityHandlerStrategy::ReturnChildAsIs>(
ctx.Mutables,
GetValueRepresentation(structType->GetMemberType(index)),
ins.first->second, structObj, index);
case EOptionalityHandlerStrategy::AddOptionalToChild:
return new TElementWrapper<EOptionalityHandlerStrategy::AddOptionalToChild>(
ctx.Mutables,
GetValueRepresentation(structType->GetMemberType(index)),
ins.first->second, structObj, index);
case EOptionalityHandlerStrategy::IntersectOptionals:
return new TElementWrapper<EOptionalityHandlerStrategy::IntersectOptionals>(
ctx.Mutables,
GetValueRepresentation(structType->GetMemberType(index)),
ins.first->second, structObj, index);
}
}
}
}
|