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-2025, 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
21
22#include <llvm-c/Types.h>
23#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
24#include <llvm-c/OrcEE.h>
25#endif
26
27
28/*
29 * File needs to be includable by both C and C++ code, and include other
30 * headers doing the same. Therefore wrap C portion in our own extern "C" if
31 * in C++ mode.
32 */
33#ifdef __cplusplus
34extern "C"
35{
36#endif
37
38#include "access/tupdesc.h"
39#include "fmgr.h"
40#include "jit/jit.h"
41#include "nodes/pg_list.h"
42
43typedef struct LLVMJitContext
44{
45 JitContext base;
46
47 /* used to ensure cleanup of context */
48 ResourceOwner resowner;
49
50 /* number of modules created */
51 size_t module_generation;
52
53 /*
54 * The LLVM Context used by this JIT context. An LLVM context is reused
55 * across many compilations, but occasionally reset to prevent it using
56 * too much memory due to more and more types accumulating.
57 */
58 LLVMContextRef llvm_context;
59
60 /* current, "open for write", module */
61 LLVMModuleRef module;
62
63 /* is there any pending code that needs to be emitted */
64 bool compiled;
65
66 /* # of objects emitted, used to generate non-conflicting names */
67 int counter;
68
69 /* list of handles for code emitted via Orc */
70 List *handles;
71} LLVMJitContext;
72
73/* type and struct definitions */
74extern PGDLLIMPORT LLVMTypeRef TypeParamBool;
75extern PGDLLIMPORT LLVMTypeRef TypePGFunction;
76extern PGDLLIMPORT LLVMTypeRef TypeSizeT;
77extern PGDLLIMPORT LLVMTypeRef TypeStorageBool;
78
79extern PGDLLIMPORT LLVMTypeRef StructNullableDatum;
80extern PGDLLIMPORT LLVMTypeRef StructTupleDescData;
81extern PGDLLIMPORT LLVMTypeRef StructHeapTupleData;
82extern PGDLLIMPORT LLVMTypeRef StructHeapTupleHeaderData;
83extern PGDLLIMPORT LLVMTypeRef StructMinimalTupleData;
84extern PGDLLIMPORT LLVMTypeRef StructTupleTableSlot;
85extern PGDLLIMPORT LLVMTypeRef StructHeapTupleTableSlot;
87extern PGDLLIMPORT LLVMTypeRef StructMemoryContextData;
89extern PGDLLIMPORT LLVMTypeRef StructExprContext;
90extern PGDLLIMPORT LLVMTypeRef StructExprEvalStep;
91extern PGDLLIMPORT LLVMTypeRef StructExprState;
92extern PGDLLIMPORT LLVMTypeRef StructAggState;
95extern PGDLLIMPORT LLVMTypeRef StructPlanState;
96
97extern PGDLLIMPORT LLVMValueRef AttributeTemplate;
99extern PGDLLIMPORT LLVMValueRef ExecEvalSubroutineTemplate;
100
101
102extern void llvm_enter_fatal_on_oom(void);
103extern void llvm_leave_fatal_on_oom(void);
104extern bool llvm_in_fatal_on_oom(void);
105extern void llvm_reset_after_error(void);
106extern void llvm_assert_in_fatal_section(void);
107
108extern LLVMJitContext *llvm_create_context(int jitFlags);
109extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
110extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
111extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
112extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
113extern LLVMTypeRef llvm_pg_var_type(const char *varname);
114extern LLVMTypeRef llvm_pg_var_func_type(const char *varname);
115extern LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname);
116extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
117extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
118 LLVMBuilderRef builder,
119 LLVMModuleRef mod,
120 FunctionCallInfo fcinfo);
121
122extern void llvm_inline_reset_caches(void);
123extern void llvm_inline(LLVMModuleRef mod);
124
125/*
126 ****************************************************************************
127 * Code generation functions.
128 ****************************************************************************
129 */
130extern bool llvm_compile_expr(struct ExprState *state);
131struct TupleTableSlotOps;
132extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc,
133 const struct TupleTableSlotOps *ops, int natts);
134
135/*
136 ****************************************************************************
137 * Extensions / Backward compatibility section of the LLVM C API
138 * Error handling related functions.
139 ****************************************************************************
140 */
141extern LLVMTypeRef LLVMGetFunctionReturnType(LLVMValueRef r);
142extern LLVMTypeRef LLVMGetFunctionType(LLVMValueRef r);
143#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
144extern LLVMOrcObjectLayerRef LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES);
145#endif
146
147#ifdef __cplusplus
148} /* extern "C" */
149#endif
150
151#endif /* USE_LLVM */
152#endif /* LLVMJIT_H */
#define PGDLLIMPORT
Definition: c.h:1291
#define funcname
Definition: indent_codes.h:69
LLVMTypeRef StructFunctionCallInfoData
Definition: llvmjit.c:69
LLVMTypeRef StructMinimalTupleData
Definition: llvmjit.c:62
LLVMTypeRef StructMinimalTupleTableSlot
Definition: llvmjit.c:67
LLVMTypeRef StructPlanState
Definition: llvmjit.c:76
static LLVMContextRef llvm_context
Definition: llvmjit.c:94
LLVMJitContext * llvm_create_context(int jitFlags)
Definition: llvmjit.c:220
LLVMTypeRef StructExprState
Definition: llvmjit.c:72
LLVMTypeRef StructExprEvalStep
Definition: llvmjit.c:71
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
Definition: llvmjit.c:461
LLVMTypeRef TypeParamBool
Definition: llvmjit.c:57
LLVMTypeRef StructMemoryContextData
Definition: llvmjit.c:68
LLVMTypeRef StructAggStatePerGroupData
Definition: llvmjit.c:74
LLVMTypeRef llvm_pg_var_type(const char *varname)
Definition: llvmjit.c:419
LLVMTypeRef TypeSizeT
Definition: llvmjit.c:56
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
Definition: llvmjit.c:537
char * llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
Definition: llvmjit.c:338
void llvm_split_symbol_name(const char *name, char **modname, char **funcname)
Definition: llvmjit.c:1042
LLVMTypeRef llvm_pg_var_func_type(const char *varname)
Definition: llvmjit.c:439
LLVMTypeRef StructTupleTableSlot
Definition: llvmjit.c:64
LLVMTypeRef TypeStorageBool
Definition: llvmjit.c:58
LLVMTypeRef StructHeapTupleTableSlot
Definition: llvmjit.c:66
LLVMModuleRef llvm_mutable_module(LLVMJitContext *context)
Definition: llvmjit.c:313
LLVMTypeRef StructAggState
Definition: llvmjit.c:73
LLVMTypeRef TypePGFunction
Definition: llvmjit.c:59
LLVMTypeRef StructTupleDescData
Definition: llvmjit.c:63
LLVMValueRef AttributeTemplate
Definition: llvmjit.c:78
LLVMTypeRef StructExprContext
Definition: llvmjit.c:70
LLVMTypeRef StructHeapTupleHeaderData
Definition: llvmjit.c:65
LLVMTypeRef StructHeapTupleData
Definition: llvmjit.c:61
void * llvm_get_function(LLVMJitContext *context, const char *funcname)
Definition: llvmjit.c:359
LLVMTypeRef StructNullableDatum
Definition: llvmjit.c:60
LLVMValueRef ExecEvalSubroutineTemplate
Definition: llvmjit.c:79
LLVMValueRef ExecEvalBoolSubroutineTemplate
Definition: llvmjit.c:80
LLVMTypeRef StructAggStatePerTransData
Definition: llvmjit.c:75
void llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
Definition: llvmjit.c:513
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)
Definition: jit.h:58
Definition: pg_list.h:54
Definition: regguts.h:323
const char * name