forked from greenplum-db/gpdb-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_eval_expr_codegen.cc
188 lines (160 loc) · 5.64 KB
/
exec_eval_expr_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
//---------------------------------------------------------------------------
// Greenplum Database
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// exec_eval_expr_codegen.cc
//
// @doc:
// Generates code for ExecEvalExpr function.
//
//---------------------------------------------------------------------------
#include <assert.h>
#include <stddef.h>
#include <cstdint>
#include <memory>
#include <string>
#include "codegen/base_codegen.h"
#include "codegen/codegen_wrapper.h"
#include "codegen/exec_eval_expr_codegen.h"
#include "codegen/expr_tree_generator.h"
#include "codegen/op_expr_tree_generator.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"
extern "C" {
#include "postgres.h" // NOLINT(build/include)
#include "nodes/execnodes.h"
#include "utils/elog.h"
#include "executor/tuptable.h"
#include "nodes/nodes.h"
}
namespace llvm {
class BasicBlock;
class Function;
class Value;
} // namespace llvm
using gpcodegen::ExecEvalExprCodegen;
using gpcodegen::SlotGetAttrCodegen;
constexpr char ExecEvalExprCodegen::kExecEvalExprPrefix[];
ExecEvalExprCodegen::ExecEvalExprCodegen(
CodegenManager* manager,
ExecEvalExprFn regular_func_ptr,
ExecEvalExprFn* ptr_to_regular_func_ptr,
ExprState *exprstate,
ExprContext *econtext,
PlanState* plan_state)
: BaseCodegen(manager,
kExecEvalExprPrefix,
regular_func_ptr, ptr_to_regular_func_ptr),
exprstate_(exprstate),
plan_state_(plan_state),
gen_info_(econtext, nullptr, nullptr, nullptr, 0),
slot_getattr_codegen_(nullptr),
expr_tree_generator_(nullptr) {
}
bool ExecEvalExprCodegen::InitDependencies() {
OpExprTreeGenerator::InitializeSupportedFunction();
ExprTreeGenerator::VerifyAndCreateExprTree(
exprstate_, &gen_info_, &expr_tree_generator_);
// Prepare dependent slot_getattr() generation
PrepareSlotGetAttr();
return true;
}
void ExecEvalExprCodegen::PrepareSlotGetAttr() {
TupleTableSlot* slot = nullptr;
assert(nullptr != plan_state_);
switch (nodeTag(plan_state_)) {
case T_SeqScanState:
case T_TableScanState:
// Generate dependent slot_getattr() implementation for the given slot
if (gen_info_.max_attr > 0) {
slot = reinterpret_cast<ScanState*>(plan_state_)
->ss_ScanTupleSlot;
assert(nullptr != slot);
}
break;
case T_AggState:
// For now, we assume that tuples for the Aggs are already going to be
// deformed in which case, we can avoid generating and calling the
// generated slot_getattr(). This may not be true always, but calling the
// regular slot_getattr() will still preserve correctness.
break;
default:
elog(DEBUG1,
"Attempting to generate ExecEvalExpr for an unsupported operator!");
}
if (nullptr != slot) {
slot_getattr_codegen_ = SlotGetAttrCodegen::GetCodegenInstance(
manager(), slot, gen_info_.max_attr);
}
}
bool ExecEvalExprCodegen::GenerateExecEvalExpr(
gpcodegen::GpCodegenUtils* codegen_utils) {
assert(NULL != codegen_utils);
if (nullptr == exprstate_ ||
nullptr == exprstate_->expr ||
nullptr == gen_info_.econtext) {
return false;
}
if (nullptr == expr_tree_generator_.get()) {
return false;
}
// In case the generation above either failed or was not needed,
// we revert to use the external slot_getattr()
if (nullptr == slot_getattr_codegen_) {
gen_info_.llvm_slot_getattr_func =
codegen_utils->GetOrRegisterExternalFunction(slot_getattr,
"slot_getattr");
} else {
slot_getattr_codegen_->GenerateCode(codegen_utils);
gen_info_.llvm_slot_getattr_func =
slot_getattr_codegen_->GetGeneratedFunction();
}
llvm::Function* exec_eval_expr_func = CreateFunction<ExecEvalExprFn>(
codegen_utils, GetUniqueFuncName());
// Function arguments to ExecVariableList
llvm::Value* llvm_isnull_arg = ArgumentByPosition(exec_eval_expr_func, 2);
// BasicBlock of function entry.
llvm::BasicBlock* llvm_entry_block = codegen_utils->CreateBasicBlock(
"entry", exec_eval_expr_func);
llvm::BasicBlock* llvm_error_block = codegen_utils->CreateBasicBlock(
"error_block", exec_eval_expr_func);
gen_info_.llvm_main_func = exec_eval_expr_func;
gen_info_.llvm_error_block = llvm_error_block;
auto irb = codegen_utils->ir_builder();
irb->SetInsertPoint(llvm_entry_block);
#ifdef CODEGEN_DEBUG
codegen_utils->CreateElog(
DEBUG1,
"Codegen'ed expression evaluation called!");
#endif
// Generate code from expression tree generator
llvm::Value* value = nullptr;
bool is_generated = expr_tree_generator_->GenerateCode(codegen_utils,
gen_info_,
llvm_isnull_arg,
&value);
if (!is_generated ||
nullptr == value) {
return false;
}
llvm::Value* llvm_ret_value = codegen_utils->CreateCppTypeToDatumCast(value);
irb->CreateRet(llvm_ret_value);
irb->SetInsertPoint(llvm_error_block);
irb->CreateRet(codegen_utils->GetConstant<int64_t>(0));
return true;
}
bool ExecEvalExprCodegen::GenerateCodeInternal(GpCodegenUtils* codegen_utils) {
bool isGenerated = GenerateExecEvalExpr(codegen_utils);
if (isGenerated) {
elog(DEBUG1, "ExecEvalExpr was generated successfully!");
return true;
} else {
elog(DEBUG1, "ExecEvalExpr generation failed!");
return false;
}
}