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-2022, 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 PGDLLIMPORT LLVMModuleRef llvm_types_module;
60 
61 /* type and struct definitions */
62 extern PGDLLIMPORT LLVMTypeRef TypeParamBool;
63 extern PGDLLIMPORT LLVMTypeRef TypePGFunction;
64 extern PGDLLIMPORT LLVMTypeRef TypeSizeT;
65 extern PGDLLIMPORT LLVMTypeRef TypeStorageBool;
66 
67 extern PGDLLIMPORT LLVMTypeRef StructNullableDatum;
68 extern PGDLLIMPORT LLVMTypeRef StructTupleDescData;
69 extern PGDLLIMPORT LLVMTypeRef StructHeapTupleData;
70 extern PGDLLIMPORT LLVMTypeRef StructTupleTableSlot;
71 extern PGDLLIMPORT LLVMTypeRef StructHeapTupleTableSlot;
72 extern PGDLLIMPORT LLVMTypeRef StructMinimalTupleTableSlot;
73 extern PGDLLIMPORT LLVMTypeRef StructMemoryContextData;
74 extern PGDLLIMPORT LLVMTypeRef StructFunctionCallInfoData;
75 extern PGDLLIMPORT LLVMTypeRef StructExprContext;
76 extern PGDLLIMPORT LLVMTypeRef StructExprEvalStep;
77 extern PGDLLIMPORT LLVMTypeRef StructExprState;
78 extern PGDLLIMPORT LLVMTypeRef StructAggState;
79 extern PGDLLIMPORT LLVMTypeRef StructAggStatePerTransData;
80 extern PGDLLIMPORT LLVMTypeRef StructAggStatePerGroupData;
81 
82 extern PGDLLIMPORT 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
126 extern char *LLVMGetHostCPUName(void);
127 #endif
128 
129 #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
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 */
unsigned int uint32
Definition: c.h:452
#define PGDLLIMPORT
Definition: c.h:1342
const char * name
Definition: encode.c:561
LLVMTypeRef StructFunctionCallInfoData
Definition: llvmjit.c:81
LLVMTypeRef StructMinimalTupleTableSlot
Definition: llvmjit.c:77
LLVMTypeRef StructExprState
Definition: llvmjit.c:84
LLVMTypeRef StructExprEvalStep
Definition: llvmjit.c:83
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
Definition: llvmjit.c:419
LLVMTypeRef TypeParamBool
Definition: llvmjit.c:60
LLVMTypeRef StructMemoryContextData
Definition: llvmjit.c:78
LLVMTypeRef StructAggStatePerGroupData
Definition: llvmjit.c:86
LLVMTypeRef llvm_pg_var_type(const char *varname)
Definition: llvmjit.c:375
LLVMTypeRef TypeSizeT
Definition: llvmjit.c:59
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
Definition: llvmjit.c:496
char * llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
Definition: llvmjit.c:255
void llvm_split_symbol_name(const char *name, char **modname, char **funcname)
Definition: llvmjit.c:1035
LLVMTypeRef llvm_pg_var_func_type(const char *varname)
Definition: llvmjit.c:399
LLVMTypeRef StructTupleTableSlot
Definition: llvmjit.c:75
LLVMTypeRef TypeStorageBool
Definition: llvmjit.c:61
LLVMTypeRef StructHeapTupleTableSlot
Definition: llvmjit.c:76
LLVMModuleRef llvm_types_module
Definition: llvmjit.c:91
LLVMJitContext * llvm_create_context(int jitFlags)
Definition: llvmjit.c:146
LLVMModuleRef llvm_mutable_module(LLVMJitContext *context)
Definition: llvmjit.c:230
LLVMTypeRef StructAggState
Definition: llvmjit.c:85
LLVMTypeRef TypePGFunction
Definition: llvmjit.c:62
LLVMTypeRef StructTupleDescData
Definition: llvmjit.c:74
LLVMValueRef AttributeTemplate
Definition: llvmjit.c:89
LLVMTypeRef StructExprContext
Definition: llvmjit.c:82
LLVMTypeRef StructHeapTupleData
Definition: llvmjit.c:68
LLVMTypeRef StructNullableDatum
Definition: llvmjit.c:63
void * llvm_get_function(LLVMJitContext *context, const char *funcname)
Definition: llvmjit.c:276
LLVMTypeRef StructAggStatePerTransData
Definition: llvmjit.c:87
void llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
Definition: llvmjit.c:475
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)
unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx)
#define F(X, Y, Z)
Definition: md5.c:60
Definition: jit.h:55
Definition: pg_list.h:52
Definition: regguts.h:318