forked from greenplum-db/gpdb-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_variable_list_codegen.cc
277 lines (230 loc) · 9 KB
/
exec_variable_list_codegen.cc
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
//---------------------------------------------------------------------------
// Greenplum Database
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// codegen_utils.cpp
//
// @doc:
// Contains different code generators
//
//---------------------------------------------------------------------------
#include <assert.h>
#include <stddef.h>
#include <algorithm>
#include <string>
#include <cstdint>
#include "codegen/base_codegen.h"
#include "codegen/codegen_wrapper.h"
#include "codegen/exec_variable_list_codegen.h"
#include "codegen/slot_getattr_codegen.h"
#include "codegen/utils/gp_codegen_utils.h"
#include "codegen/utils/utility.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
extern "C" {
#include "postgres.h" // NOLINT(build/include)
#include "executor/tuptable.h"
#include "nodes/execnodes.h"
#include "utils/elog.h"
#include "access/tupdesc.h"
#include "nodes/pg_list.h"
}
namespace llvm {
class BasicBlock;
class Function;
class Value;
} // namespace llvm
using gpcodegen::ExecVariableListCodegen;
using gpcodegen::SlotGetAttrCodegen;
constexpr char ExecVariableListCodegen::kExecVariableListPrefix[];
ExecVariableListCodegen::ExecVariableListCodegen(
CodegenManager* manager,
ExecVariableListFn regular_func_ptr,
ExecVariableListFn* ptr_to_regular_func_ptr,
ProjectionInfo* proj_info,
TupleTableSlot* slot)
: BaseCodegen(manager,
kExecVariableListPrefix,
regular_func_ptr,
ptr_to_regular_func_ptr),
proj_info_(proj_info),
slot_(slot),
max_attr_(0),
slot_getattr_codegen_(nullptr) {
}
bool ExecVariableListCodegen::InitDependencies() {
assert(proj_info_ != nullptr);
// Find the largest attribute index in projInfo->pi_targetlist
max_attr_ = *std::max_element(
proj_info_->pi_varNumbers,
proj_info_->pi_varNumbers + list_length(proj_info_->pi_targetlist));
slot_getattr_codegen_ = SlotGetAttrCodegen::GetCodegenInstance(
manager(), slot_, max_attr_);
return true;
}
bool ExecVariableListCodegen::GenerateExecVariableList(
gpcodegen::GpCodegenUtils* codegen_utils) {
assert(nullptr != codegen_utils);
static_assert(sizeof(Datum) == sizeof(int64_t),
"sizeof(Datum) doesn't match sizeof(int64)");
if ( nullptr == proj_info_->pi_varSlotOffsets ) {
elog(DEBUG1,
"Cannot codegen ExecVariableList because varSlotOffsets are null");
return false;
}
// Only do codegen if all the elements in the target list are on the same
// tuple slot This is an assumption for scan nodes, but we fall back when
// joins are involved.
for (int i = list_length(proj_info_->pi_targetlist) - 1; i > 0; i--) {
if (proj_info_->pi_varSlotOffsets[i] !=
proj_info_->pi_varSlotOffsets[i-1]) {
elog(DEBUG1,
"Cannot codegen ExecVariableList because multiple slots to deform.");
return false;
}
}
// System attribute
if (max_attr_ <= 0) {
elog(DEBUG1, "Cannot generate code for ExecVariableList"
"because max_attr is negative (i.e., system attribute).");
return false;
} else if (max_attr_ > slot_->tts_tupleDescriptor->natts) {
elog(DEBUG1, "Cannot generate code for ExecVariableList"
"because max_attr is greater than natts.");
return false;
}
llvm::Function* exec_variable_list_func = CreateFunction<ExecVariableListFn>(
codegen_utils, GetUniqueFuncName());
auto irb = codegen_utils->ir_builder();
// BasicBlocks
llvm::BasicBlock* entry_block = codegen_utils->CreateBasicBlock(
"entry", exec_variable_list_func);
// BasicBlock for checking correct slot
llvm::BasicBlock* slot_check_block = codegen_utils->CreateBasicBlock(
"slot_check", exec_variable_list_func);
// BasicBlock for main.
llvm::BasicBlock* main_block = codegen_utils->CreateBasicBlock(
"main", exec_variable_list_func);
// BasicBlock for final computations.
llvm::BasicBlock* final_block = codegen_utils->CreateBasicBlock(
"final", exec_variable_list_func);
llvm::BasicBlock* fallback_block = codegen_utils->CreateBasicBlock(
"fallback", exec_variable_list_func);
// Generation-time constants
llvm::Value* llvm_max_attr = codegen_utils->GetConstant(max_attr_);
llvm::Value* llvm_slot = codegen_utils->GetConstant(slot_);
// Function arguments to ExecVariableList
llvm::Value* llvm_projInfo_arg = ArgumentByPosition(exec_variable_list_func,
0);
llvm::Value* llvm_values_arg = ArgumentByPosition(exec_variable_list_func, 1);
llvm::Value* llvm_isnull_arg = ArgumentByPosition(exec_variable_list_func, 2);
// Generate slot_getattr for attributes all the way to max_attr
assert(nullptr != slot_getattr_codegen_);
slot_getattr_codegen_->GenerateCode(codegen_utils);
llvm::Function* slot_getattr_func =
slot_getattr_codegen_->GetGeneratedFunction();
// In case the above generation failed, no point in continuing since that was
// the most crucial part of ExecVariableList code generation.
if (nullptr == slot_getattr_func) {
elog(DEBUG1, "Cannot generate code for ExecVariableList "
"because slot_getattr generation failed!");
return false;
}
// Entry block
// -----------
irb->SetInsertPoint(entry_block);
#ifdef CODEGEN_DEBUG
codegen_utils->CreateElog(
DEBUG1,
"Codegen'ed ExecVariableList called!");
#endif
irb->CreateBr(slot_check_block);
// Slot check block
// ----------------
irb->SetInsertPoint(slot_check_block);
llvm::Value* llvm_econtext =
irb->CreateLoad(codegen_utils->GetPointerToMember(
llvm_projInfo_arg, &ProjectionInfo::pi_exprContext));
// We want to fall back when ExecVariableList is called with a slot that's
// different from the one we generated the function (eg HashJoin). We also
// assume only 1 slot and that the slot is in a scan node ie from
// exprContext->ecxt_scantuple or varOffset = 0
llvm::Value* llvm_slot_arg =
irb->CreateLoad(codegen_utils->GetPointerToMember(
llvm_econtext, &ExprContext::ecxt_scantuple));
irb->CreateCondBr(
irb->CreateICmpEQ(llvm_slot, llvm_slot_arg),
main_block /* true */,
fallback_block /* false */);
// Main block
// ----------
irb->SetInsertPoint(main_block);
// Allocate a dummy int so that slot_getattr can write isnull out
llvm::Value* llvm_dummy_isnull =
irb->CreateAlloca(codegen_utils->GetType<bool>());
irb->CreateCall(slot_getattr_func, {
llvm_slot,
llvm_max_attr,
llvm_dummy_isnull
});
irb->CreateBr(final_block);
// Final Block
// -----------
irb->SetInsertPoint(final_block);
llvm::Value* llvm_slot_PRIVATE_tts_isnull /* bool* */ =
irb->CreateLoad(codegen_utils->GetPointerToMember(
llvm_slot, &TupleTableSlot::PRIVATE_tts_isnull));
llvm::Value* llvm_slot_PRIVATE_tts_values /* Datum* */ =
irb->CreateLoad(codegen_utils->GetPointerToMember(
llvm_slot, &TupleTableSlot::PRIVATE_tts_values));
// This code from ExecVariableList copies the contents of the isnull & values
// arrays in the slot to output variable from the function parameters to
// ExecVariableList
int *varNumbers = proj_info_->pi_varNumbers;
for (int i = list_length(proj_info_->pi_targetlist) - 1; i >= 0; i--) {
// *isnull = slot->PRIVATE_tts_isnull[attnum-1];
llvm::Value* llvm_isnull_from_slot_val =
irb->CreateLoad(irb->CreateInBoundsGEP(
llvm_slot_PRIVATE_tts_isnull,
{codegen_utils->GetConstant(varNumbers[i] - 1)}));
llvm::Value* llvm_isnull_ptr =
irb->CreateInBoundsGEP(llvm_isnull_arg,
{codegen_utils->GetConstant(i)});
irb->CreateStore(llvm_isnull_from_slot_val, llvm_isnull_ptr);
// values[i] = slot_getattr(varSlot, varNumber+1, &(isnull[i]));
llvm::Value* llvm_value_from_slot_val =
irb->CreateLoad(irb->CreateInBoundsGEP(
llvm_slot_PRIVATE_tts_values,
{codegen_utils->GetConstant(varNumbers[i] - 1)}));
llvm::Value* llvm_values_ptr =
irb->CreateInBoundsGEP(llvm_values_arg,
{codegen_utils->GetConstant(i)});
irb->CreateStore(llvm_value_from_slot_val, llvm_values_ptr);
}
irb->CreateRetVoid();
// Fall back Block
// ---------------
irb->SetInsertPoint(fallback_block);
codegen_utils->CreateElog(
DEBUG1,
"Falling back to regular ExecVariableList");
codegen_utils->CreateFallback<ExecVariableListFn>(
codegen_utils->GetOrRegisterExternalFunction(ExecVariableList,
"ExecVariableList"),
exec_variable_list_func);
return true;
}
bool ExecVariableListCodegen::GenerateCodeInternal(
GpCodegenUtils* codegen_utils) {
bool isGenerated = GenerateExecVariableList(codegen_utils);
if (isGenerated) {
elog(DEBUG1, "ExecVariableList was generated successfully!");
return true;
} else {
elog(DEBUG1, "ExecVariableList generation failed!");
return false;
}
}