16#include <llvm-c/Analysis.h>
17#include <llvm-c/BitReader.h>
18#include <llvm-c/BitWriter.h>
19#include <llvm-c/Core.h>
20#include <llvm-c/ExecutionEngine.h>
21#if LLVM_VERSION_MAJOR > 16
22#include <llvm-c/Transforms/PassBuilder.h>
24#include <llvm-c/Orc.h>
25#include <llvm-c/OrcEE.h>
26#include <llvm-c/LLJIT.h>
27#include <llvm-c/Support.h>
28#include <llvm-c/Target.h>
29#if LLVM_VERSION_MAJOR < 17
30#include <llvm-c/Transforms/IPO.h>
31#include <llvm-c/Transforms/PassManagerBuilder.h>
32#include <llvm-c/Transforms/Scalar.h>
33#include <llvm-c/Transforms/Utils.h>
45#define LLVMJIT_LLVM_CONTEXT_REUSE_MAX 100
123 .
name =
"LLVM JIT context",
177 elog(
ERROR,
"Trying to recreate a non-existing context");
238 context->base.flags = jitFlags;
324 if (!context->module)
326 context->compiled =
false;
333 return context->module;
346 context->base.instr.created_functions++;
354 context->module_generation,
373 if (!context->compiled)
383 foreach(
lc, context->handles)
396 elog(
ERROR,
"failed to look up symbol \"%s\": %s",
431 elog(
ERROR,
"variable %s not in llvmjit_types.c", varname);
450 elog(
ERROR,
"function %s not in llvmjit_types.c", varname);
606#if LLVM_VERSION_MAJOR < 17
679 passes =
"default<O0>,mem2reg,inline";
682 passes =
"default<O0>,mem2reg";
686#ifdef LLVM_PASS_DEBUG
691#ifdef USE_ASSERT_CHECKING
739 context->module_generation);
758 context->module_generation);
789 context->module =
NULL;
805 context->module =
NULL;
806 context->compiled =
true;
810 context->handles =
lappend(context->handles, handle);
876 elog(
DEBUG2,
"LLVMJIT detected CPU \"%s\", with features \"%s\"",
933 elog(
PANIC,
"LLVMJitContext in use count not 0 at exit (is %zu)",
980 elog(
ERROR,
"failed to extract target information, llvmjit_types.c not loaded");
1007 elog(
ERROR,
"LLVMCreateMemoryBufferWithContentsOfFile(%s) failed: %s",
1014 elog(
ERROR,
"LLVMParseBitcodeInContext2 of %s failed", path);
1095#if defined(__darwin__)
1129#if LLVM_VERSION_MAJOR > 14
1179#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
1229 elog(
ERROR,
"failed to create lljit instance: %s",
1243 elog(
ERROR,
"failed to create generator: %s",
1251#if LLVM_VERSION_MAJOR > 14
1280 context->resowner =
NULL;
#define Assert(condition)
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
int errmsg_internal(const char *fmt,...)
int errhidestmt(bool hide_stmt)
int errhidecontext(bool hide_ctx)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
#define palloc_array(type, count)
#define palloc0_array(type, count)
void fmgr_symbol(Oid functionId, char **mod, char **fn)
#define PG_MODULE_MAGIC_EXT(...)
char pkglib_path[MAXPGPATH]
#define INSTR_TIME_SET_CURRENT(t)
#define INSTR_TIME_GET_DOUBLE(t)
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
void on_proc_exit(pg_on_exit_callback function, Datum arg)
bool proc_exit_inprogress
void jit_release_context(JitContext *context)
bool jit_profiling_support
bool jit_debugging_support
List * lappend(List *list, void *datum)
void list_free(List *list)
LLVMTypeRef StructFunctionCallInfoData
LLVMTypeRef StructMinimalTupleData
LLVMTypeRef StructMinimalTupleTableSlot
LLVMTypeRef StructPlanState
static LLVMContextRef llvm_context
LLVMJitContext * llvm_create_context(int jitFlags)
LLVMTypeRef StructExprState
static LLVMOrcLLJITRef llvm_opt3_orc
static const char * llvm_triple
LLVMTypeRef StructExprEvalStep
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
static void llvm_recreate_llvm_context(void)
LLVMTypeRef TypeParamBool
LLVMTypeRef StructMemoryContextData
static void llvm_set_target(void)
static LLVMTargetRef llvm_targetref
static bool llvm_session_initialized
LLVMTypeRef StructAggStatePerGroupData
static void llvm_create_types(void)
LLVMTypeRef llvm_pg_var_type(const char *varname)
static void ResourceOwnerRememberJIT(ResourceOwner owner, LLVMJitContext *handle)
static void llvm_session_initialize(void)
static size_t llvm_jit_context_in_use_count
static const char * llvm_layout
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
char * llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
static void ResOwnerReleaseJitContext(Datum res)
void llvm_split_symbol_name(const char *name, char **modname, char **funcname)
LLVMTypeRef llvm_pg_var_func_type(const char *varname)
LLVMTypeRef StructTupleTableSlot
LLVMTypeRef TypeStorageBool
static LLVMTypeRef load_return_type(LLVMModuleRef mod, const char *name)
static size_t llvm_generation
static LLVMOrcLLJITRef llvm_opt0_orc
static LLVMOrcObjectLayerRef llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
static void ResourceOwnerForgetJIT(ResourceOwner owner, LLVMJitContext *handle)
static void llvm_compile_module(LLVMJitContext *context)
LLVMTypeRef StructHeapTupleTableSlot
static char * llvm_error_message(LLVMErrorRef error)
static LLVMOrcLLJITRef llvm_create_jit_instance(LLVMTargetMachineRef tm)
static LLVMModuleRef llvm_types_module
static void llvm_shutdown(int code, Datum arg)
static LLVMOrcThreadSafeContextRef llvm_ts_context
static const ResourceOwnerDesc jit_resowner_desc
#define LLVMJIT_LLVM_CONTEXT_REUSE_MAX
static LLVMErrorRef llvm_resolve_symbols(LLVMOrcDefinitionGeneratorRef GeneratorObj, void *Ctx, LLVMOrcLookupStateRef *LookupState, LLVMOrcLookupKind Kind, LLVMOrcJITDylibRef JD, LLVMOrcJITDylibLookupFlags JDLookupFlags, LLVMOrcCLookupSet LookupSet, size_t LookupSetSize)
static void llvm_copy_attributes_at_index(LLVMValueRef v_from, LLVMValueRef v_to, uint32 index)
LLVMModuleRef llvm_mutable_module(LLVMJitContext *context)
LLVMTypeRef StructAggState
LLVMTypeRef TypePGFunction
LLVMTypeRef StructTupleDescData
LLVMValueRef AttributeTemplate
static size_t llvm_llvm_context_reuse_count
LLVMTypeRef StructExprContext
static void llvm_optimize_module(LLVMJitContext *context, LLVMModuleRef module)
static uint64_t llvm_resolve_symbol(const char *name, void *ctx)
LLVMTypeRef StructHeapTupleHeaderData
static void llvm_log_jit_error(void *ctx, LLVMErrorRef error)
LLVMTypeRef StructHeapTupleData
static void llvm_release_context(JitContext *context)
void * llvm_get_function(LLVMJitContext *context, const char *funcname)
LLVMTypeRef StructNullableDatum
LLVMValueRef ExecEvalSubroutineTemplate
LLVMValueRef ExecEvalBoolSubroutineTemplate
void _PG_jit_provider_init(JitProviderCallbacks *cb)
LLVMTypeRef StructAggStatePerTransData
void llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
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)
void llvm_inline(LLVMModuleRef M)
void llvm_inline_reset_caches(void)
LLVMTypeRef LLVMGetFunctionReturnType(LLVMValueRef r)
LLVMTypeRef LLVMGetFunctionType(LLVMValueRef r)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext TopMemoryContext
char * pnstrdup(const char *in, Size len)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static char buf[DEFAULT_XLOG_SEG_SIZE]
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
char * psprintf(const char *fmt,...)
ResourceOwner CurrentResourceOwner
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
void ResourceOwnerEnlarge(ResourceOwner owner)
#define RELEASE_PRIO_JIT_CONTEXTS
@ RESOURCE_RELEASE_BEFORE_LOCKS
JitProviderCompileExprCB compile_expr
JitProviderResetAfterErrorCB reset_after_error
JitProviderReleaseContextCB release_context
LLVMOrcResourceTrackerRef resource_tracker