1 /*-------------------------------------------------------------------------
2  * llvmjit.h
3  *	  LLVM JIT provider.
4  *
5  * Copyright (c) 2016-2018, 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 StructtupleDesc;
68 extern LLVMTypeRef StructHeapTupleData;
69 extern LLVMTypeRef StructTupleTableSlot;
70 extern LLVMTypeRef StructMemoryContextData;
71 extern LLVMTypeRef StructFunctionCallInfoData;
72 extern LLVMTypeRef StructExprContext;
73 extern LLVMTypeRef StructExprEvalStep;
74 extern LLVMTypeRef StructExprState;
75 extern LLVMTypeRef StructAggState;
76 extern LLVMTypeRef StructAggStatePerTransData;
77 extern LLVMTypeRef StructAggStatePerGroupData;
78 
79 extern LLVMValueRef AttributeTemplate;
80 extern LLVMValueRef FuncStrlen;
81 extern LLVMValueRef FuncVarsizeAny;
82 extern LLVMValueRef FuncSlotGetsomeattrs;
83 extern LLVMValueRef FuncSlotGetmissingattrs;
84 extern LLVMValueRef FuncHeapGetsysattr;
85 extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
86 extern LLVMValueRef FuncExecEvalArrayRefSubscript;
87 extern LLVMValueRef FuncExecAggTransReparent;
88 extern LLVMValueRef FuncExecAggInitGroup;
89 
90 
91 extern void llvm_enter_fatal_on_oom(void);
92 extern void llvm_leave_fatal_on_oom(void);
93 extern bool llvm_in_fatal_on_oom(void);
94 extern void llvm_reset_after_error(void);
95 extern void llvm_assert_in_fatal_section(void);
96 
97 extern LLVMJitContext *llvm_create_context(int jitFlags);
98 extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
99 extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
100 extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
101 extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
102 extern LLVMValueRef llvm_get_decl(LLVMModuleRef mod, LLVMValueRef f);
103 extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
104 extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
105 						LLVMBuilderRef builder,
106 						LLVMModuleRef mod,
107 						FunctionCallInfo fcinfo);
108 
109 extern void llvm_inline(LLVMModuleRef mod);
110 
111 /*
112  ****************************************************************************
113  * Code generation functions.
114  ****************************************************************************
115  */
116 extern bool llvm_compile_expr(struct ExprState *state);
117 extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc, int natts);
118 
119 /*
120  ****************************************************************************
121  * Extensions / Backward compatibility section of the LLVM C API
122  * Error handling related functions.
123  ****************************************************************************
124  */
125 #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
126 /** Get the host CPU as a string. The result needs to be disposed with
127   LLVMDisposeMessage. */
128 extern char *LLVMGetHostCPUName(void);
129 #endif
130 
131 #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
132 /** Get the host CPU features as a string. The result needs to be disposed
133   with LLVMDisposeMessage. */
134 extern char *LLVMGetHostCPUFeatures(void);
135 #endif
136 
137 extern unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx);
138 
139 #ifdef __cplusplus
140 } /* extern "C" */
141 #endif
142 
143 #endif							/* USE_LLVM */
144 #endif							/* LLVMJIT_H */
145