forked from greenplum-db/gpdb-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_eval_expr_codegen.cc
136 lines (114 loc) · 4.14 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
//---------------------------------------------------------------------------
// Greenplum Database
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// exec_eval_expr_codegen.cc
//
// @doc:
// Generates code for ExecEvalExpr function.
//
//---------------------------------------------------------------------------
#include <algorithm>
#include <cstdint>
#include <string>
#include "codegen/exec_eval_expr_codegen.h"
#include "codegen/expr_tree_generator.h"
#include "codegen/op_expr_tree_generator.h"
#include "codegen/utils/clang_compiler.h"
#include "codegen/utils/utility.h"
#include "codegen/utils/instance_method_wrappers.h"
#include "codegen/utils/codegen_utils.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/Casting.h"
extern "C" {
#include "postgres.h" // NOLINT(build/include)
#include "utils/elog.h"
#include "nodes/execnodes.h"
}
using gpcodegen::ExecEvalExprCodegen;
constexpr char ExecEvalExprCodegen::kExecEvalExprPrefix[];
ExecEvalExprCodegen::ExecEvalExprCodegen
(
ExecEvalExprFn regular_func_ptr,
ExecEvalExprFn* ptr_to_regular_func_ptr,
ExprState *exprstate,
ExprContext *econtext) :
BaseCodegen(kExecEvalExprPrefix,
regular_func_ptr, ptr_to_regular_func_ptr),
exprstate_(exprstate),
econtext_(econtext) {
}
bool ExecEvalExprCodegen::GenerateExecEvalExpr(
gpcodegen::GpCodegenUtils* codegen_utils) {
assert(NULL != codegen_utils);
if (nullptr == exprstate_ ||
nullptr == exprstate_->expr ||
nullptr == econtext_) {
return false;
}
// TODO(krajaraman): move to better place
OpExprTreeGenerator::InitializeSupportedFunction();
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);
auto irb = codegen_utils->ir_builder();
irb->SetInsertPoint(llvm_entry_block);
codegen_utils->CreateElog(
DEBUG1,
"Calling codegen'ed expression evaluation");
// Check if we can codegen. If so create ExprTreeGenerator
std::unique_ptr<ExprTreeGenerator> expr_tree_generator(nullptr);
bool can_generate = ExprTreeGenerator::VerifyAndCreateExprTree(
exprstate_, econtext_, &expr_tree_generator);
if (!can_generate ||
expr_tree_generator.get() == nullptr) {
return false;
}
// Generate code from expression tree generator
llvm::Value* value = nullptr;
bool is_generated = expr_tree_generator->GenerateCode(codegen_utils,
econtext_,
exec_eval_expr_func,
llvm_error_block,
llvm_isnull_arg,
&value);
if (!is_generated ||
nullptr == value) {
return false;
}
llvm::Value* llvm_ret_value = codegen_utils->CreateCast<int64_t>(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;
}
}