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-2024, 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  /* used to ensure cleanup of context */
43  ResourceOwner resowner;
44 
45  /* number of modules created */
46  size_t module_generation;
47 
48  /*
49  * The LLVM Context used by this JIT context. An LLVM context is reused
50  * across many compilations, but occasionally reset to prevent it using
51  * too much memory due to more and more types accumulating.
52  */
53  LLVMContextRef llvm_context;
54 
55  /* current, "open for write", module */
56  LLVMModuleRef module;
57 
58  /* is there any pending code that needs to be emitted */
59  bool compiled;
60 
61  /* # of objects emitted, used to generate non-conflicting names */
62  int counter;
63 
64  /* list of handles for code emitted via Orc */
65  List *handles;
66 } LLVMJitContext;
67 
68 /* type and struct definitions */
69 extern PGDLLIMPORT LLVMTypeRef TypeParamBool;
70 extern PGDLLIMPORT LLVMTypeRef TypePGFunction;
71 extern PGDLLIMPORT LLVMTypeRef TypeSizeT;
72 extern PGDLLIMPORT LLVMTypeRef TypeStorageBool;
73 
74 extern PGDLLIMPORT LLVMTypeRef StructNullableDatum;
75 extern PGDLLIMPORT LLVMTypeRef StructTupleDescData;
76 extern PGDLLIMPORT LLVMTypeRef StructHeapTupleData;
77 extern PGDLLIMPORT LLVMTypeRef StructHeapTupleHeaderData;
78 extern PGDLLIMPORT LLVMTypeRef StructMinimalTupleData;
79 extern PGDLLIMPORT LLVMTypeRef StructTupleTableSlot;
80 extern PGDLLIMPORT LLVMTypeRef StructHeapTupleTableSlot;
81 extern PGDLLIMPORT LLVMTypeRef StructMinimalTupleTableSlot;
82 extern PGDLLIMPORT LLVMTypeRef StructMemoryContextData;
83 extern PGDLLIMPORT LLVMTypeRef StructFunctionCallInfoData;
84 extern PGDLLIMPORT LLVMTypeRef StructExprContext;
85 extern PGDLLIMPORT LLVMTypeRef StructExprEvalStep;
86 extern PGDLLIMPORT LLVMTypeRef StructExprState;
87 extern PGDLLIMPORT LLVMTypeRef StructAggState;
88 extern PGDLLIMPORT LLVMTypeRef StructAggStatePerTransData;
89 extern PGDLLIMPORT LLVMTypeRef StructAggStatePerGroupData;
90 extern PGDLLIMPORT LLVMTypeRef StructPlanState;
91 
92 extern PGDLLIMPORT LLVMValueRef AttributeTemplate;
94 extern PGDLLIMPORT LLVMValueRef ExecEvalSubroutineTemplate;
95 
96 
97 extern void llvm_enter_fatal_on_oom(void);
98 extern void llvm_leave_fatal_on_oom(void);
99 extern bool llvm_in_fatal_on_oom(void);
100 extern void llvm_reset_after_error(void);
101 extern void llvm_assert_in_fatal_section(void);
102 
103 extern LLVMJitContext *llvm_create_context(int jitFlags);
104 extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
105 extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
106 extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
107 extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
108 extern LLVMTypeRef llvm_pg_var_type(const char *varname);
109 extern LLVMTypeRef llvm_pg_var_func_type(const char *varname);
110 extern LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname);
111 extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
112 extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
113  LLVMBuilderRef builder,
114  LLVMModuleRef mod,
115  FunctionCallInfo fcinfo);
116 
117 extern void llvm_inline_reset_caches(void);
118 extern void llvm_inline(LLVMModuleRef mod);
119 
120 /*
121  ****************************************************************************
122  * Code generation functions.
123  ****************************************************************************
124  */
125 extern bool llvm_compile_expr(struct ExprState *state);
126 struct TupleTableSlotOps;
127 extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc,
128  const struct TupleTableSlotOps *ops, int natts);
129 
130 /*
131  ****************************************************************************
132  * Extensions / Backward compatibility section of the LLVM C API
133  * Error handling related functions.
134  ****************************************************************************
135  */
136 extern LLVMTypeRef LLVMGetFunctionReturnType(LLVMValueRef r);
137 extern LLVMTypeRef LLVMGetFunctionType(LLVMValueRef r);
138 
139 #ifdef __cplusplus
140 } /* extern "C" */
141 #endif
142 
143 #endif /* USE_LLVM */
144 #endif /* LLVMJIT_H */
#define PGDLLIMPORT
Definition: c.h:1319
#define funcname
Definition: indent_codes.h:69
LLVMTypeRef StructFunctionCallInfoData
Definition: llvmjit.c:68
LLVMTypeRef StructMinimalTupleData
Definition: llvmjit.c:61
LLVMTypeRef StructMinimalTupleTableSlot
Definition: llvmjit.c:66
LLVMTypeRef StructPlanState
Definition: llvmjit.c:75
static LLVMContextRef llvm_context
Definition: llvmjit.c:93
LLVMTypeRef StructExprState
Definition: llvmjit.c:71
LLVMTypeRef StructExprEvalStep
Definition: llvmjit.c:70
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
Definition: llvmjit.c:460
LLVMTypeRef TypeParamBool
Definition: llvmjit.c:56
LLVMTypeRef StructMemoryContextData
Definition: llvmjit.c:67
LLVMTypeRef StructAggStatePerGroupData
Definition: llvmjit.c:73
LLVMTypeRef llvm_pg_var_type(const char *varname)
Definition: llvmjit.c:418
LLVMTypeRef TypeSizeT
Definition: llvmjit.c:55
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
Definition: llvmjit.c:536
char * llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
Definition: llvmjit.c:337
void llvm_split_symbol_name(const char *name, char **modname, char **funcname)
Definition: llvmjit.c:1041
LLVMTypeRef llvm_pg_var_func_type(const char *varname)
Definition: llvmjit.c:438
LLVMTypeRef StructTupleTableSlot
Definition: llvmjit.c:63
LLVMTypeRef TypeStorageBool
Definition: llvmjit.c:57
LLVMTypeRef StructHeapTupleTableSlot
Definition: llvmjit.c:65
LLVMJitContext * llvm_create_context(int jitFlags)
Definition: llvmjit.c:219
LLVMModuleRef llvm_mutable_module(LLVMJitContext *context)
Definition: llvmjit.c:312
LLVMTypeRef StructAggState
Definition: llvmjit.c:72
LLVMTypeRef TypePGFunction
Definition: llvmjit.c:58
LLVMTypeRef StructTupleDescData
Definition: llvmjit.c:62
LLVMValueRef AttributeTemplate
Definition: llvmjit.c:77
LLVMTypeRef StructExprContext
Definition: llvmjit.c:69
LLVMTypeRef StructHeapTupleHeaderData
Definition: llvmjit.c:64
LLVMTypeRef StructHeapTupleData
Definition: llvmjit.c:60
LLVMTypeRef StructNullableDatum
Definition: llvmjit.c:59
LLVMValueRef ExecEvalSubroutineTemplate
Definition: llvmjit.c:78
LLVMValueRef ExecEvalBoolSubroutineTemplate
Definition: llvmjit.c:79
void * llvm_get_function(LLVMJitContext *context, const char *funcname)
Definition: llvmjit.c:358
LLVMTypeRef StructAggStatePerTransData
Definition: llvmjit.c:74
void llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
Definition: llvmjit.c:512
LLVMValueRef slot_compile_deform(LLVMJitContext *context, TupleDesc desc, const TupleTableSlotOps *ops, int natts)
void llvm_enter_fatal_on_oom(void)
bool llvm_in_fatal_on_oom(void)
void llvm_assert_in_fatal_section(void)
void llvm_reset_after_error(void)
void llvm_leave_fatal_on_oom(void)
bool llvm_compile_expr(ExprState *state)
Definition: llvmjit_expr.c:78
void llvm_inline(LLVMModuleRef M)
void llvm_inline_reset_caches(void)
LLVMTypeRef LLVMGetFunctionReturnType(LLVMValueRef r)
LLVMTypeRef LLVMGetFunctionType(LLVMValueRef r)
tree context
Definition: radixtree.h:1835
Definition: jit.h:58
Definition: pg_list.h:54
Definition: regguts.h:323
const char * name