forked from greenplum-db/gpdb-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_wrapper.cc
More file actions
117 lines (100 loc) · 3.51 KB
/
codegen_wrapper.cc
File metadata and controls
117 lines (100 loc) · 3.51 KB
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
//---------------------------------------------------------------------------
// Greenplum Database
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// init_codegen.cpp
//
// @doc:
// C wrappers for initialization of code generator.
//
//---------------------------------------------------------------------------
#include "codegen/codegen_wrapper.h"
#include "codegen/codegen_manager.h"
#include "codegen/slot_deform_tuple_codegen.h"
#include "codegen/utils/codegen_utils.h"
using gpcodegen::CodegenManager;
using gpcodegen::BaseCodegen;
using gpcodegen::SlotDeformTupleCodegen;
// Current code generator manager that oversees all code generators
static void* ActiveCodeGeneratorManager = nullptr;
static bool is_codegen_initalized = false;
extern bool codegen; // defined from guc
// Perform global set-up tasks for code generation. Returns 0 on
// success, nonzero on error.
unsigned int InitCodegen() {
return gpcodegen::CodegenUtils::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 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(
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* SlotDeformTupleCodegenEnroll(
SlotDeformTupleFn regular_func_ptr,
SlotDeformTupleFn* ptr_to_chosen_func_ptr,
TupleTableSlot* slot) {
SlotDeformTupleCodegen* generator = CodegenEnroll<SlotDeformTupleCodegen>(
regular_func_ptr, ptr_to_chosen_func_ptr, slot);
return generator;
}