Commit b059d2f4 authored by Andres Freund's avatar Andres Freund

jit: Reference expression step functions via llvmjit_types.

The main benefit of doing so is that this allows llvm to ensure that
types match - previously that'd only be detected by a crash within the
called function. There were a number of cases where we passed a
superfluous parameter...

To avoid needing to add all the functions to llvmjit.{c,h}, instead
get them from the llvm module for llvmjit_types.c. Also use that for
the functions from llvmjit_types already in llvmjit.h.

Author: Soumyadeep Chakraborty and Andres Freund
Discussion: https://postgr.es/m/CADwEdooww3wZv-sXSfatzFRwMuwa186LyTwkBfwEW6NjtooBPA@mail.gmail.com
parent c4f3b63c
...@@ -76,16 +76,8 @@ LLVMTypeRef StructAggStatePerGroupData; ...@@ -76,16 +76,8 @@ LLVMTypeRef StructAggStatePerGroupData;
LLVMTypeRef StructAggStatePerTransData; LLVMTypeRef StructAggStatePerTransData;
LLVMValueRef AttributeTemplate; LLVMValueRef AttributeTemplate;
LLVMValueRef FuncStrlen;
LLVMValueRef FuncVarsizeAny;
LLVMValueRef FuncSlotGetsomeattrsInt;
LLVMValueRef FuncSlotGetmissingattrs;
LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
LLVMValueRef FuncExecEvalSubscriptingRef;
LLVMValueRef FuncExecEvalSysVar;
LLVMValueRef FuncExecAggTransReparent;
LLVMValueRef FuncExecAggInitGroup;
LLVMModuleRef llvm_types_module = NULL;
static bool llvm_session_initialized = false; static bool llvm_session_initialized = false;
static size_t llvm_generation = 0; static size_t llvm_generation = 0;
...@@ -301,26 +293,32 @@ llvm_get_function(LLVMJitContext *context, const char *funcname) ...@@ -301,26 +293,32 @@ llvm_get_function(LLVMJitContext *context, const char *funcname)
} }
/* /*
* Return declaration for passed function, adding it to the module if * Return declaration for a function referenced in llvmjit_types.c, adding it
* necessary. * to the module if necessary.
* *
* This is used to make functions imported by llvm_create_types() known to the * This is used to make functions discovered via llvm_create_types() known to
* module that's currently being worked on. * the module that's currently being worked on.
*/ */
LLVMValueRef LLVMValueRef
llvm_get_decl(LLVMModuleRef mod, LLVMValueRef v_src) llvm_pg_func(LLVMModuleRef mod, const char *funcname)
{ {
LLVMValueRef v_srcfn;
LLVMValueRef v_fn; LLVMValueRef v_fn;
/* don't repeatedly add function */ /* don't repeatedly add function */
v_fn = LLVMGetNamedFunction(mod, LLVMGetValueName(v_src)); v_fn = LLVMGetNamedFunction(mod, funcname);
if (v_fn) if (v_fn)
return v_fn; return v_fn;
v_srcfn = LLVMGetNamedFunction(llvm_types_module, funcname);
if (!v_srcfn)
elog(ERROR, "function %s not in llvmjit_types.c", funcname);
v_fn = LLVMAddFunction(mod, v_fn = LLVMAddFunction(mod,
LLVMGetValueName(v_src), funcname,
LLVMGetElementType(LLVMTypeOf(v_src))); LLVMGetElementType(LLVMTypeOf(v_srcfn)));
llvm_copy_attributes(v_src, v_fn); llvm_copy_attributes(v_srcfn, v_fn);
return v_fn; return v_fn;
} }
...@@ -775,7 +773,6 @@ llvm_create_types(void) ...@@ -775,7 +773,6 @@ llvm_create_types(void)
char path[MAXPGPATH]; char path[MAXPGPATH];
LLVMMemoryBufferRef buf; LLVMMemoryBufferRef buf;
char *msg; char *msg;
LLVMModuleRef mod = NULL;
snprintf(path, MAXPGPATH, "%s/%s", pkglib_path, "llvmjit_types.bc"); snprintf(path, MAXPGPATH, "%s/%s", pkglib_path, "llvmjit_types.bc");
...@@ -787,7 +784,7 @@ llvm_create_types(void) ...@@ -787,7 +784,7 @@ llvm_create_types(void)
} }
/* eagerly load contents, going to need it all */ /* eagerly load contents, going to need it all */
if (LLVMParseBitcode2(buf, &mod)) if (LLVMParseBitcode2(buf, &llvm_types_module))
{ {
elog(ERROR, "LLVMParseBitcode2 of %s failed", path); elog(ERROR, "LLVMParseBitcode2 of %s failed", path);
} }
...@@ -797,43 +794,29 @@ llvm_create_types(void) ...@@ -797,43 +794,29 @@ llvm_create_types(void)
* Load triple & layout from clang emitted file so we're guaranteed to be * Load triple & layout from clang emitted file so we're guaranteed to be
* compatible. * compatible.
*/ */
llvm_triple = pstrdup(LLVMGetTarget(mod)); llvm_triple = pstrdup(LLVMGetTarget(llvm_types_module));
llvm_layout = pstrdup(LLVMGetDataLayoutStr(mod)); llvm_layout = pstrdup(LLVMGetDataLayoutStr(llvm_types_module));
TypeSizeT = load_type(mod, "TypeSizeT"); TypeSizeT = load_type(llvm_types_module, "TypeSizeT");
TypeParamBool = load_return_type(mod, "FunctionReturningBool"); TypeParamBool = load_return_type(llvm_types_module, "FunctionReturningBool");
TypeStorageBool = load_type(mod, "TypeStorageBool"); TypeStorageBool = load_type(llvm_types_module, "TypeStorageBool");
TypePGFunction = load_type(mod, "TypePGFunction"); TypePGFunction = load_type(llvm_types_module, "TypePGFunction");
StructNullableDatum = load_type(mod, "StructNullableDatum"); StructNullableDatum = load_type(llvm_types_module, "StructNullableDatum");
StructExprContext = load_type(mod, "StructExprContext"); StructExprContext = load_type(llvm_types_module, "StructExprContext");
StructExprEvalStep = load_type(mod, "StructExprEvalStep"); StructExprEvalStep = load_type(llvm_types_module, "StructExprEvalStep");
StructExprState = load_type(mod, "StructExprState"); StructExprState = load_type(llvm_types_module, "StructExprState");
StructFunctionCallInfoData = load_type(mod, "StructFunctionCallInfoData"); StructFunctionCallInfoData = load_type(llvm_types_module, "StructFunctionCallInfoData");
StructMemoryContextData = load_type(mod, "StructMemoryContextData"); StructMemoryContextData = load_type(llvm_types_module, "StructMemoryContextData");
StructTupleTableSlot = load_type(mod, "StructTupleTableSlot"); StructTupleTableSlot = load_type(llvm_types_module, "StructTupleTableSlot");
StructHeapTupleTableSlot = load_type(mod, "StructHeapTupleTableSlot"); StructHeapTupleTableSlot = load_type(llvm_types_module, "StructHeapTupleTableSlot");
StructMinimalTupleTableSlot = load_type(mod, "StructMinimalTupleTableSlot"); StructMinimalTupleTableSlot = load_type(llvm_types_module, "StructMinimalTupleTableSlot");
StructHeapTupleData = load_type(mod, "StructHeapTupleData"); StructHeapTupleData = load_type(llvm_types_module, "StructHeapTupleData");
StructTupleDescData = load_type(mod, "StructTupleDescData"); StructTupleDescData = load_type(llvm_types_module, "StructTupleDescData");
StructAggState = load_type(mod, "StructAggState"); StructAggState = load_type(llvm_types_module, "StructAggState");
StructAggStatePerGroupData = load_type(mod, "StructAggStatePerGroupData"); StructAggStatePerGroupData = load_type(llvm_types_module, "StructAggStatePerGroupData");
StructAggStatePerTransData = load_type(mod, "StructAggStatePerTransData"); StructAggStatePerTransData = load_type(llvm_types_module, "StructAggStatePerTransData");
AttributeTemplate = LLVMGetNamedFunction(mod, "AttributeTemplate"); AttributeTemplate = LLVMGetNamedFunction(llvm_types_module, "AttributeTemplate");
FuncStrlen = LLVMGetNamedFunction(mod, "strlen");
FuncVarsizeAny = LLVMGetNamedFunction(mod, "varsize_any");
FuncSlotGetsomeattrsInt = LLVMGetNamedFunction(mod, "slot_getsomeattrs_int");
FuncSlotGetmissingattrs = LLVMGetNamedFunction(mod, "slot_getmissingattrs");
FuncMakeExpandedObjectReadOnlyInternal = LLVMGetNamedFunction(mod, "MakeExpandedObjectReadOnlyInternal");
FuncExecEvalSubscriptingRef = LLVMGetNamedFunction(mod, "ExecEvalSubscriptingRef");
FuncExecEvalSysVar = LLVMGetNamedFunction(mod, "ExecEvalSysVar");
FuncExecAggTransReparent = LLVMGetNamedFunction(mod, "ExecAggTransReparent");
FuncExecAggInitGroup = LLVMGetNamedFunction(mod, "ExecAggInitGroup");
/*
* Leave the module alive, otherwise references to function would be
* dangling.
*/
} }
/* /*
......
...@@ -331,7 +331,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc, ...@@ -331,7 +331,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
v_params[0] = v_slot; v_params[0] = v_slot;
v_params[1] = LLVMBuildZExt(b, v_maxatt, LLVMInt32Type(), ""); v_params[1] = LLVMBuildZExt(b, v_maxatt, LLVMInt32Type(), "");
v_params[2] = l_int32_const(natts); v_params[2] = l_int32_const(natts);
LLVMBuildCall(b, llvm_get_decl(mod, FuncSlotGetmissingattrs), LLVMBuildCall(b, llvm_pg_func(mod, "slot_getmissingattrs"),
v_params, lengthof(v_params), ""); v_params, lengthof(v_params), "");
LLVMBuildBr(b, b_find_start); LLVMBuildBr(b, b_find_start);
} }
...@@ -682,7 +682,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc, ...@@ -682,7 +682,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
else if (att->attlen == -1) else if (att->attlen == -1)
{ {
v_incby = LLVMBuildCall(b, v_incby = LLVMBuildCall(b,
llvm_get_decl(mod, FuncVarsizeAny), llvm_pg_func(mod, "varsize_any"),
&v_attdatap, 1, &v_attdatap, 1,
"varsize_any"); "varsize_any");
l_callsite_ro(v_incby); l_callsite_ro(v_incby);
...@@ -691,7 +691,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc, ...@@ -691,7 +691,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
else if (att->attlen == -2) else if (att->attlen == -2)
{ {
v_incby = LLVMBuildCall(b, v_incby = LLVMBuildCall(b,
llvm_get_decl(mod, FuncStrlen), llvm_pg_func(mod, "strlen"),
&v_attdatap, 1, "strlen"); &v_attdatap, 1, "strlen");
l_callsite_ro(v_incby); l_callsite_ro(v_incby);
......
This diff is collapsed.
...@@ -98,13 +98,43 @@ FunctionReturningBool(void) ...@@ -98,13 +98,43 @@ FunctionReturningBool(void)
*/ */
void *referenced_functions[] = void *referenced_functions[] =
{ {
strlen, ExecAggInitGroup,
varsize_any, ExecAggTransReparent,
slot_getsomeattrs_int, ExecEvalAggOrderedTransDatum,
slot_getmissingattrs, ExecEvalAggOrderedTransTuple,
MakeExpandedObjectReadOnlyInternal, ExecEvalAlternativeSubPlan,
ExecEvalArrayCoerce,
ExecEvalArrayExpr,
ExecEvalConstraintCheck,
ExecEvalConstraintNotNull,
ExecEvalConvertRowtype,
ExecEvalCurrentOfExpr,
ExecEvalFieldSelect,
ExecEvalFieldStoreDeForm,
ExecEvalFieldStoreForm,
ExecEvalFuncExprFusage,
ExecEvalFuncExprStrictFusage,
ExecEvalGroupingFunc,
ExecEvalMinMax,
ExecEvalNextValueExpr,
ExecEvalParamExec,
ExecEvalParamExtern,
ExecEvalRow,
ExecEvalRowNotNull,
ExecEvalRowNull,
ExecEvalSQLValueFunction,
ExecEvalScalarArrayOp,
ExecEvalSubPlan,
ExecEvalSubscriptingRef, ExecEvalSubscriptingRef,
ExecEvalSubscriptingRefAssign,
ExecEvalSubscriptingRefFetch,
ExecEvalSubscriptingRefOld,
ExecEvalSysVar, ExecEvalSysVar,
ExecAggTransReparent, ExecEvalWholeRowVar,
ExecAggInitGroup ExecEvalXmlExpr,
MakeExpandedObjectReadOnlyInternal,
slot_getmissingattrs,
slot_getsomeattrs_int,
strlen,
varsize_any,
}; };
...@@ -55,6 +55,8 @@ typedef struct LLVMJitContext ...@@ -55,6 +55,8 @@ typedef struct LLVMJitContext
List *handles; List *handles;
} LLVMJitContext; } LLVMJitContext;
/* llvm module containing information about types */
extern LLVMModuleRef llvm_types_module;
/* type and struct definitions */ /* type and struct definitions */
extern LLVMTypeRef TypeParamBool; extern LLVMTypeRef TypeParamBool;
...@@ -78,15 +80,6 @@ extern LLVMTypeRef StructAggStatePerTransData; ...@@ -78,15 +80,6 @@ extern LLVMTypeRef StructAggStatePerTransData;
extern LLVMTypeRef StructAggStatePerGroupData; extern LLVMTypeRef StructAggStatePerGroupData;
extern LLVMValueRef AttributeTemplate; extern LLVMValueRef AttributeTemplate;
extern LLVMValueRef FuncStrlen;
extern LLVMValueRef FuncVarsizeAny;
extern LLVMValueRef FuncSlotGetmissingattrs;
extern LLVMValueRef FuncSlotGetsomeattrsInt;
extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
extern LLVMValueRef FuncExecEvalSubscriptingRef;
extern LLVMValueRef FuncExecEvalSysVar;
extern LLVMValueRef FuncExecAggTransReparent;
extern LLVMValueRef FuncExecAggInitGroup;
extern void llvm_enter_fatal_on_oom(void); extern void llvm_enter_fatal_on_oom(void);
...@@ -99,7 +92,7 @@ extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context); ...@@ -99,7 +92,7 @@ extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename); extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
extern void *llvm_get_function(LLVMJitContext *context, const char *funcname); extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname); extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
extern LLVMValueRef llvm_get_decl(LLVMModuleRef mod, LLVMValueRef f); extern LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname);
extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to); extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
extern LLVMValueRef llvm_function_reference(LLVMJitContext *context, extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
LLVMBuilderRef builder, LLVMBuilderRef builder,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment