1 | /*------------------------------------------------------------------------- |
2 | * llvmjit.h |
3 | * LLVM JIT provider. |
4 | * |
5 | * Copyright (c) 2016-2019, PostgreSQL Global Development Group |
6 | * |
7 | * src/include/jit/llvmjit.h |
8 | * |
9 | *------------------------------------------------------------------------- |
10 | */ |
11 | #ifndef LLVMJIT_H |
12 | #define LLVMJIT_H |
13 | |
14 | /* |
15 | * To avoid breaking cpluspluscheck, allow including the file even when LLVM |
16 | * is not available. |
17 | */ |
18 | #ifdef USE_LLVM |
19 | |
20 | #include <llvm-c/Types.h> |
21 | |
22 | |
23 | /* |
24 | * File needs to be includable by both C and C++ code, and include other |
25 | * headers doing the same. Therefore wrap C portion in our own extern "C" if |
26 | * in C++ mode. |
27 | */ |
28 | #ifdef __cplusplus |
29 | extern "C" |
30 | { |
31 | #endif |
32 | |
33 | |
34 | #include "fmgr.h" |
35 | #include "jit/jit.h" |
36 | #include "nodes/pg_list.h" |
37 | #include "access/tupdesc.h" |
38 | |
39 | |
40 | typedef struct LLVMJitContext |
41 | { |
42 | JitContext base; |
43 | |
44 | /* number of modules created */ |
45 | size_t module_generation; |
46 | |
47 | /* current, "open for write", module */ |
48 | LLVMModuleRef module; |
49 | |
50 | /* is there any pending code that needs to be emitted */ |
51 | bool compiled; |
52 | |
53 | /* # of objects emitted, used to generate non-conflicting names */ |
54 | int counter; |
55 | |
56 | /* list of handles for code emitted via Orc */ |
57 | List *handles; |
58 | } LLVMJitContext; |
59 | |
60 | |
61 | /* type and struct definitions */ |
62 | extern LLVMTypeRef TypeParamBool; |
63 | extern LLVMTypeRef TypePGFunction; |
64 | extern LLVMTypeRef TypeSizeT; |
65 | extern LLVMTypeRef TypeStorageBool; |
66 | |
67 | extern LLVMTypeRef StructNullableDatum; |
68 | extern LLVMTypeRef StructTupleDescData; |
69 | extern LLVMTypeRef StructHeapTupleData; |
70 | extern LLVMTypeRef StructTupleTableSlot; |
71 | extern LLVMTypeRef StructHeapTupleTableSlot; |
72 | extern LLVMTypeRef StructMinimalTupleTableSlot; |
73 | extern LLVMTypeRef StructMemoryContextData; |
74 | extern LLVMTypeRef StructFunctionCallInfoData; |
75 | extern LLVMTypeRef StructExprContext; |
76 | extern LLVMTypeRef StructExprEvalStep; |
77 | extern LLVMTypeRef StructExprState; |
78 | extern LLVMTypeRef StructAggState; |
79 | extern LLVMTypeRef StructAggStatePerTransData; |
80 | extern LLVMTypeRef StructAggStatePerGroupData; |
81 | |
82 | extern LLVMValueRef AttributeTemplate; |
83 | extern LLVMValueRef FuncStrlen; |
84 | extern LLVMValueRef FuncVarsizeAny; |
85 | extern LLVMValueRef FuncSlotGetmissingattrs; |
86 | extern LLVMValueRef FuncSlotGetsomeattrsInt; |
87 | extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal; |
88 | extern LLVMValueRef FuncExecEvalSubscriptingRef; |
89 | extern LLVMValueRef FuncExecEvalSysVar; |
90 | extern LLVMValueRef FuncExecAggTransReparent; |
91 | extern LLVMValueRef FuncExecAggInitGroup; |
92 | |
93 | |
94 | extern void llvm_enter_fatal_on_oom(void); |
95 | extern void llvm_leave_fatal_on_oom(void); |
96 | extern void llvm_reset_after_error(void); |
97 | extern void llvm_assert_in_fatal_section(void); |
98 | |
99 | extern LLVMJitContext *llvm_create_context(int jitFlags); |
100 | extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context); |
101 | extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename); |
102 | extern void *llvm_get_function(LLVMJitContext *context, const char *funcname); |
103 | extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname); |
104 | extern LLVMValueRef llvm_get_decl(LLVMModuleRef mod, LLVMValueRef f); |
105 | extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to); |
106 | extern LLVMValueRef llvm_function_reference(LLVMJitContext *context, |
107 | LLVMBuilderRef builder, |
108 | LLVMModuleRef mod, |
109 | FunctionCallInfo fcinfo); |
110 | |
111 | extern void llvm_inline(LLVMModuleRef mod); |
112 | |
113 | /* |
114 | **************************************************************************** |
115 | * Code generation functions. |
116 | **************************************************************************** |
117 | */ |
118 | extern bool llvm_compile_expr(struct ExprState *state); |
119 | struct TupleTableSlotOps; |
120 | extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc, |
121 | const struct TupleTableSlotOps *ops, int natts); |
122 | |
123 | /* |
124 | **************************************************************************** |
125 | * Extensions / Backward compatibility section of the LLVM C API |
126 | * Error handling related functions. |
127 | **************************************************************************** |
128 | */ |
129 | #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME |
130 | /** Get the host CPU as a string. The result needs to be disposed with |
131 | LLVMDisposeMessage. */ |
132 | extern char *LLVMGetHostCPUName(void); |
133 | #endif |
134 | |
135 | #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES |
136 | /** Get the host CPU features as a string. The result needs to be disposed |
137 | with LLVMDisposeMessage. */ |
138 | extern char *LLVMGetHostCPUFeatures(void); |
139 | #endif |
140 | |
141 | #ifdef __cplusplus |
142 | } /* extern "C" */ |
143 | #endif |
144 | |
145 | #endif /* USE_LLVM */ |
146 | #endif /* LLVMJIT_H */ |
147 | |