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 bool llvm_in_fatal_on_oom(void);
97 extern void llvm_reset_after_error(void);
98 extern void llvm_assert_in_fatal_section(void);
99 
100 extern LLVMJitContext *llvm_create_context(int jitFlags);
101 extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
102 extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
103 extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
104 extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
105 extern LLVMValueRef llvm_get_decl(LLVMModuleRef mod, LLVMValueRef f);
106 extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
107 extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
108 						LLVMBuilderRef builder,
109 						LLVMModuleRef mod,
110 						FunctionCallInfo fcinfo);
111 
112 extern void llvm_inline(LLVMModuleRef mod);
113 
114 /*
115  ****************************************************************************
116  * Code generation functions.
117  ****************************************************************************
118  */
119 extern bool llvm_compile_expr(struct ExprState *state);
120 struct TupleTableSlotOps;
121 extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc,
122 										const struct TupleTableSlotOps *ops, int natts);
123 
124 /*
125  ****************************************************************************
126  * Extensions / Backward compatibility section of the LLVM C API
127  * Error handling related functions.
128  ****************************************************************************
129  */
130 #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
131 /** Get the host CPU as a string. The result needs to be disposed with
132   LLVMDisposeMessage. */
133 extern char *LLVMGetHostCPUName(void);
134 #endif
135 
136 #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
137 /** Get the host CPU features as a string. The result needs to be disposed
138   with LLVMDisposeMessage. */
139 extern char *LLVMGetHostCPUFeatures(void);
140 #endif
141 
142 extern unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx);
143 
144 #ifdef __cplusplus
145 } /* extern "C" */
146 #endif
147 
148 #endif							/* USE_LLVM */
149 #endif							/* LLVMJIT_H */
150