@@ -367,6 +367,47 @@ llvm_get_function(LLVMJitContext *context, const char *funcname)
367367 return NULL ;
368368}
369369
370+ /*
371+ * Return type of a variable in llvmjit_types.c. This is useful to keep types
372+ * in sync between plain C and JIT related code.
373+ */
374+ LLVMTypeRef
375+ llvm_pg_var_type (const char * varname )
376+ {
377+ LLVMValueRef v_srcvar ;
378+ LLVMTypeRef typ ;
379+
380+ /* this'll return a *pointer* to the global */
381+ v_srcvar = LLVMGetNamedGlobal (llvm_types_module , varname );
382+ if (!v_srcvar )
383+ elog (ERROR , "variable %s not in llvmjit_types.c" , varname );
384+
385+ /* look at the contained type */
386+ typ = LLVMTypeOf (v_srcvar );
387+ Assert (typ != NULL && LLVMGetTypeKind (typ ) == LLVMPointerTypeKind );
388+ typ = LLVMGetElementType (typ );
389+ Assert (typ != NULL );
390+
391+ return typ ;
392+ }
393+
394+ /*
395+ * Return function type of a variable in llvmjit_types.c. This is useful to
396+ * keep function types in sync between C and JITed code.
397+ */
398+ LLVMTypeRef
399+ llvm_pg_var_func_type (const char * varname )
400+ {
401+ LLVMTypeRef typ = llvm_pg_var_type (varname );
402+
403+ /* look at the contained type */
404+ Assert (LLVMGetTypeKind (typ ) == LLVMPointerTypeKind );
405+ typ = LLVMGetElementType (typ );
406+ Assert (typ != NULL && LLVMGetTypeKind (typ ) == LLVMFunctionTypeKind );
407+
408+ return typ ;
409+ }
410+
370411/*
371412 * Return declaration for a function referenced in llvmjit_types.c, adding it
372413 * to the module if necessary.
@@ -889,26 +930,6 @@ llvm_shutdown(int code, Datum arg)
889930#endif /* LLVM_VERSION_MAJOR > 11 */
890931}
891932
892- /* helper for llvm_create_types, returning a global var's type */
893- static LLVMTypeRef
894- load_type (LLVMModuleRef mod , const char * name )
895- {
896- LLVMValueRef value ;
897- LLVMTypeRef typ ;
898-
899- /* this'll return a *pointer* to the global */
900- value = LLVMGetNamedGlobal (mod , name );
901- if (!value )
902- elog (ERROR , "type %s is unknown" , name );
903-
904- /* therefore look at the contained type and return that */
905- typ = LLVMTypeOf (value );
906- Assert (typ != NULL );
907- typ = LLVMGetElementType (typ );
908- Assert (typ != NULL );
909- return typ ;
910- }
911-
912933/* helper for llvm_create_types, returning a function's return type */
913934static LLVMTypeRef
914935load_return_type (LLVMModuleRef mod , const char * name )
@@ -970,24 +991,24 @@ llvm_create_types(void)
970991 llvm_triple = pstrdup (LLVMGetTarget (llvm_types_module ));
971992 llvm_layout = pstrdup (LLVMGetDataLayoutStr (llvm_types_module ));
972993
973- TypeSizeT = load_type ( llvm_types_module , "TypeSizeT" );
994+ TypeSizeT = llvm_pg_var_type ( "TypeSizeT" );
974995 TypeParamBool = load_return_type (llvm_types_module , "FunctionReturningBool" );
975- TypeStorageBool = load_type ( llvm_types_module , "TypeStorageBool" );
976- TypePGFunction = load_type ( llvm_types_module , "TypePGFunction" );
977- StructNullableDatum = load_type ( llvm_types_module , "StructNullableDatum" );
978- StructExprContext = load_type ( llvm_types_module , "StructExprContext" );
979- StructExprEvalStep = load_type ( llvm_types_module , "StructExprEvalStep" );
980- StructExprState = load_type ( llvm_types_module , "StructExprState" );
981- StructFunctionCallInfoData = load_type ( llvm_types_module , "StructFunctionCallInfoData" );
982- StructMemoryContextData = load_type ( llvm_types_module , "StructMemoryContextData" );
983- StructTupleTableSlot = load_type ( llvm_types_module , "StructTupleTableSlot" );
984- StructHeapTupleTableSlot = load_type ( llvm_types_module , "StructHeapTupleTableSlot" );
985- StructMinimalTupleTableSlot = load_type ( llvm_types_module , "StructMinimalTupleTableSlot" );
986- StructHeapTupleData = load_type ( llvm_types_module , "StructHeapTupleData" );
987- StructTupleDescData = load_type ( llvm_types_module , "StructTupleDescData" );
988- StructAggState = load_type ( llvm_types_module , "StructAggState" );
989- StructAggStatePerGroupData = load_type ( llvm_types_module , "StructAggStatePerGroupData" );
990- StructAggStatePerTransData = load_type ( llvm_types_module , "StructAggStatePerTransData" );
996+ TypeStorageBool = llvm_pg_var_type ( "TypeStorageBool" );
997+ TypePGFunction = llvm_pg_var_type ( "TypePGFunction" );
998+ StructNullableDatum = llvm_pg_var_type ( "StructNullableDatum" );
999+ StructExprContext = llvm_pg_var_type ( "StructExprContext" );
1000+ StructExprEvalStep = llvm_pg_var_type ( "StructExprEvalStep" );
1001+ StructExprState = llvm_pg_var_type ( "StructExprState" );
1002+ StructFunctionCallInfoData = llvm_pg_var_type ( "StructFunctionCallInfoData" );
1003+ StructMemoryContextData = llvm_pg_var_type ( "StructMemoryContextData" );
1004+ StructTupleTableSlot = llvm_pg_var_type ( "StructTupleTableSlot" );
1005+ StructHeapTupleTableSlot = llvm_pg_var_type ( "StructHeapTupleTableSlot" );
1006+ StructMinimalTupleTableSlot = llvm_pg_var_type ( "StructMinimalTupleTableSlot" );
1007+ StructHeapTupleData = llvm_pg_var_type ( "StructHeapTupleData" );
1008+ StructTupleDescData = llvm_pg_var_type ( "StructTupleDescData" );
1009+ StructAggState = llvm_pg_var_type ( "StructAggState" );
1010+ StructAggStatePerGroupData = llvm_pg_var_type ( "StructAggStatePerGroupData" );
1011+ StructAggStatePerTransData = llvm_pg_var_type ( "StructAggStatePerTransData" );
9911012
9921013 AttributeTemplate = LLVMGetNamedFunction (llvm_types_module , "AttributeTemplate" );
9931014}
0 commit comments