forked from greenplum-db/gpdb-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen_wrapper.cc
169 lines (145 loc) · 4.73 KB
/
codegen_wrapper.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
//---------------------------------------------------------------------------
// Greenplum Database
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// codegen_wrapper.cc
//
// @doc:
// C wrappers for initialization of code generator.
//
//---------------------------------------------------------------------------
#include "codegen/codegen_wrapper.h"
#include <assert.h>
#include <string>
#include <type_traits>
#include "codegen/base_codegen.h"
#include "codegen/codegen_manager.h"
#include "codegen/exec_eval_expr_codegen.h"
#include "codegen/exec_variable_list_codegen.h"
#include "codegen/expr_tree_generator.h"
#include "codegen/utils/gp_codegen_utils.h"
extern "C" {
#include "lib/stringinfo.h"
}
using gpcodegen::CodegenManager;
using gpcodegen::BaseCodegen;
using gpcodegen::ExecVariableListCodegen;
using gpcodegen::ExecEvalExprCodegen;
// Current code generator manager that oversees all code generators
static void* ActiveCodeGeneratorManager = nullptr;
extern bool codegen; // defined from guc
extern bool init_codegen; // defined from guc
// Perform global set-up tasks for code generation. Returns 0 on
// success, nonzero on error.
unsigned int InitCodegen() {
return gpcodegen::GpCodegenUtils::InitializeGlobal();
}
void* CodeGeneratorManagerCreate(const char* module_name) {
if (!codegen) {
return nullptr;
}
return new CodegenManager(module_name);
}
unsigned int CodeGeneratorManagerGenerateCode(void* manager) {
if (!codegen) {
return 0;
}
return static_cast<CodegenManager*>(manager)->GenerateCode();
}
unsigned int CodeGeneratorManagerPrepareGeneratedFunctions(void* manager) {
if (!codegen) {
return 0;
}
return static_cast<CodegenManager*>(manager)->PrepareGeneratedFunctions();
}
unsigned int CodeGeneratorManagerNotifyParameterChange(void* manager) {
// parameter change notification is not supported yet
assert(false);
return 0;
}
void CodeGeneratorManagerAccumulateExplainString(void* manager) {
if (!codegen) {
return;
}
assert(nullptr != manager);
static_cast<CodegenManager*>(manager)->AccumulateExplainString();
}
char* CodeGeneratorManagerGetExplainString(void* manager) {
if (!codegen) {
return nullptr;
}
StringInfo return_string = makeStringInfo();
appendStringInfoString(
return_string,
static_cast<CodegenManager*>(manager)->GetExplainString().c_str());
return return_string->data;
}
void CodeGeneratorManagerDestroy(void* manager) {
delete (static_cast<CodegenManager*>(manager));
}
void* GetActiveCodeGeneratorManager() {
return ActiveCodeGeneratorManager;
}
void SetActiveCodeGeneratorManager(void* manager) {
ActiveCodeGeneratorManager = manager;
}
/**
* @brief Template function to facilitate enroll for any type of
* codegen
*
* @tparam ClassType Type of Code Generator class
* @tparam FuncType Type of the regular function
* @tparam Args Variable argument that ClassType will take in its constructor
*
* @param regular_func_ptr Regular version of the target function.
* @param ptr_to_chosen_func_ptr Reference to the function pointer that the caller will call.
* @param args Variable length argument for ClassType
*
* @return Pointer to ClassType
**/
template <typename ClassType, typename FuncType, typename ...Args>
ClassType* CodegenEnroll(FuncType regular_func_ptr,
FuncType* ptr_to_chosen_func_ptr,
Args&&... args) { // NOLINT(build/c++11)
CodegenManager* manager = static_cast<CodegenManager*>(
GetActiveCodeGeneratorManager());
if (nullptr == manager ||
!codegen) { // if codegen guc is false
BaseCodegen<FuncType>::SetToRegular(
regular_func_ptr, ptr_to_chosen_func_ptr);
return nullptr;
}
ClassType* generator = new ClassType(
manager,
regular_func_ptr,
ptr_to_chosen_func_ptr,
std::forward<Args>(args)...);
bool is_enrolled = manager->EnrollCodeGenerator(
CodegenFuncLifespan_Parameter_Invariant, generator);
assert(is_enrolled);
return generator;
}
void* ExecVariableListCodegenEnroll(
ExecVariableListFn regular_func_ptr,
ExecVariableListFn* ptr_to_chosen_func_ptr,
ProjectionInfo* proj_info,
TupleTableSlot* slot) {
ExecVariableListCodegen* generator = CodegenEnroll<ExecVariableListCodegen>(
regular_func_ptr, ptr_to_chosen_func_ptr, proj_info, slot);
return generator;
}
void* ExecEvalExprCodegenEnroll(
ExecEvalExprFn regular_func_ptr,
ExecEvalExprFn* ptr_to_chosen_func_ptr,
ExprState *exprstate,
ExprContext *econtext,
PlanState* plan_state) {
ExecEvalExprCodegen* generator = CodegenEnroll<ExecEvalExprCodegen>(
regular_func_ptr,
ptr_to_chosen_func_ptr,
exprstate,
econtext,
plan_state);
return generator;
}