PostgreSQL Source Code  git master
llvmjit.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * llvmjit.h
3  * LLVM JIT provider.
4  *
5  * Copyright (c) 2016-2021, 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 #include "access/tupdesc.h"
34 #include "fmgr.h"
35 #include "jit/jit.h"
36 #include "nodes/pg_list.h"
37 
38 typedef struct LLVMJitContext
39 {
40  JitContext base;
41 
42  /* number of modules created */
43  size_t module_generation;
44 
45  /* current, "open for write", module */
46  LLVMModuleRef module;
47 
48  /* is there any pending code that needs to be emitted */
49  bool compiled;
50 
51  /* # of objects emitted, used to generate non-conflicting names */
52  int counter;
53 
54  /* list of handles for code emitted via Orc */
55  List *handles;
56 } LLVMJitContext;
57 
58 /* llvm module containing information about types */
59 extern LLVMModuleRef llvm_types_module;
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 
84 
85 extern void llvm_enter_fatal_on_oom(void);
86 extern void llvm_leave_fatal_on_oom(void);
87 extern bool llvm_in_fatal_on_oom(void);
88 extern void llvm_reset_after_error(void);
89 extern void llvm_assert_in_fatal_section(void);
90 
91 extern LLVMJitContext *llvm_create_context(int jitFlags);
92 extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
93 extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
94 extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
95 extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
96 extern LLVMTypeRef llvm_pg_var_type(const char *varname);
97 extern LLVMTypeRef llvm_pg_var_func_type(const char *varname);
98 extern LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname);
99 extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
100 extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
101  LLVMBuilderRef builder,
102  LLVMModuleRef mod,
103  FunctionCallInfo fcinfo);
104 
105 extern void llvm_inline(LLVMModuleRef mod);
106 
107 /*
108  ****************************************************************************
109  * Code generation functions.
110  ****************************************************************************
111  */
112 extern bool llvm_compile_expr(struct ExprState *state);
113 struct TupleTableSlotOps;
114 extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc,
115  const struct TupleTableSlotOps *ops, int natts);
116 
117 /*
118  ****************************************************************************
119  * Extensions / Backward compatibility section of the LLVM C API
120  * Error handling related functions.
121  ****************************************************************************
122  */
123 #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
124 
126 extern char *LLVMGetHostCPUName(void);
127 #endif
128 
129 #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
130 
132 extern char *LLVMGetHostCPUFeatures(void);
133 #endif
134 
135 extern unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx);
136 
137 #ifdef __cplusplus
138 } /* extern "C" */
139 #endif
140 
141 #endif /* USE_LLVM */
142 #endif /* LLVMJIT_H */
LLVMValueRef slot_compile_deform(LLVMJitContext *context, TupleDesc desc, const TupleTableSlotOps *ops, int natts)
LLVMTypeRef StructFunctionCallInfoData
Definition: llvmjit.c:81
#define F(x)
Definition: blf.c:249
LLVMTypeRef StructExprEvalStep
Definition: llvmjit.c:83
LLVMTypeRef TypeSizeT
Definition: llvmjit.c:59
LLVMTypeRef StructExprState
Definition: llvmjit.c:84
LLVMTypeRef StructNullableDatum
Definition: llvmjit.c:63
Definition: jit.h:54
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
Definition: llvmjit.c:496
LLVMJitContext * llvm_create_context(int jitFlags)
Definition: llvmjit.c:146
LLVMTypeRef StructHeapTupleTableSlot
Definition: llvmjit.c:76
LLVMTypeRef StructAggState
Definition: llvmjit.c:85
LLVMTypeRef llvm_pg_var_type(const char *varname)
Definition: llvmjit.c:375
LLVMTypeRef StructHeapTupleData
Definition: llvmjit.c:68
bool llvm_in_fatal_on_oom(void)
void llvm_split_symbol_name(const char *name, char **modname, char **funcname)
Definition: llvmjit.c:1035
unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx)
char * llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
Definition: llvmjit.c:255
void llvm_assert_in_fatal_section(void)
LLVMTypeRef TypeStorageBool
Definition: llvmjit.c:61
unsigned int uint32
Definition: c.h:441
LLVMTypeRef StructMemoryContextData
Definition: llvmjit.c:78
LLVMTypeRef StructExprContext
Definition: llvmjit.c:82
LLVMTypeRef TypePGFunction
Definition: llvmjit.c:62
LLVMTypeRef StructAggStatePerTransData
Definition: llvmjit.c:87
void llvm_reset_after_error(void)
LLVMTypeRef StructMinimalTupleTableSlot
Definition: llvmjit.c:77
LLVMTypeRef StructTupleTableSlot
Definition: llvmjit.c:75
LLVMModuleRef llvm_mutable_module(LLVMJitContext *context)
Definition: llvmjit.c:230
Definition: regguts.h:317
bool llvm_compile_expr(ExprState *state)
Definition: llvmjit_expr.c:78
LLVMTypeRef StructTupleDescData
Definition: llvmjit.c:74
LLVMTypeRef TypeParamBool
Definition: llvmjit.c:60
void * llvm_get_function(LLVMJitContext *context, const char *funcname)
Definition: llvmjit.c:276
const char * name
Definition: encode.c:561
void llvm_inline(LLVMModuleRef M)
LLVMModuleRef llvm_types_module
Definition: llvmjit.c:91
void llvm_enter_fatal_on_oom(void)
LLVMValueRef AttributeTemplate
Definition: llvmjit.c:89
void llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
Definition: llvmjit.c:475
Definition: pg_list.h:50
LLVMTypeRef llvm_pg_var_func_type(const char *varname)
Definition: llvmjit.c:399
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
Definition: llvmjit.c:419
LLVMTypeRef StructAggStatePerGroupData
Definition: llvmjit.c:86
void llvm_leave_fatal_on_oom(void)