summaryrefslogtreecommitdiff
path: root/src/include/jit/llvmjit_emit.h
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2023-10-18 22:09:05 +1300
committerThomas Munro <tmunro@postgresql.org>2023-10-18 22:59:46 +1300
commiteed1feb3fee1a558b67b04cbd709f31142f071d5 (patch)
tree0a6ce2908f1ba4687fd1502e1a93dfb63520345e /src/include/jit/llvmjit_emit.h
parentc4e561c1e029461430c86d81a1956ddddb4fbe13 (diff)
jit: Support opaque pointers in LLVM 16.
Remove use of LLVMGetElementType() and provide the type of all pointers to LLVMBuildXXX() functions when emitting IR, as required by modern LLVM versions[1]. * For LLVM <= 14, we'll still use the old LLVMBuildXXX() functions. * For LLVM == 15, we'll continue to do the same, explicitly opting out of opaque pointer mode. * For LLVM >= 16, we'll use the new LLVMBuildXXX2() functions that take the extra type argument. The difference is hidden behind some new IR emitting wrapper functions l_load(), l_gep(), l_call() etc. The change is mostly mechanical, except that at each site the correct type had to be provided. In some places we needed to do some extra work to get functions types, including some new wrappers for C++ APIs that are not yet exposed by in LLVM's C API, and some new "example" functions in llvmjit_types.c because it's no longer possible to start from the function pointer type and ask for the function type. Back-patch to 12, because it's a little tricker in 11 and we agreed not to put the latest LLVM support into the upcoming final release of 11. [1] https://llvm.org/docs/OpaquePointers.html Reviewed-by: Dmitry Dolgov <9erthalion6@gmail.com> Reviewed-by: Ronan Dunklau <ronan.dunklau@aiven.io> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CA%2BhUKGKNX_%3Df%2B1C4r06WETKTq0G4Z_7q4L4Fxn5WWpMycDj9Fw%40mail.gmail.com
Diffstat (limited to 'src/include/jit/llvmjit_emit.h')
-rw-r--r--src/include/jit/llvmjit_emit.h106
1 files changed, 81 insertions, 25 deletions
diff --git a/src/include/jit/llvmjit_emit.h b/src/include/jit/llvmjit_emit.h
index 379be09200e..27a080bc841 100644
--- a/src/include/jit/llvmjit_emit.h
+++ b/src/include/jit/llvmjit_emit.h
@@ -16,6 +16,7 @@
#ifdef USE_LLVM
#include <llvm-c/Core.h>
+#include <llvm-c/Target.h>
#include "jit/llvmjit.h"
@@ -103,26 +104,65 @@ l_pbool_const(bool i)
return LLVMConstInt(TypeParamBool, (int) i, false);
}
+static inline LLVMValueRef
+l_struct_gep(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef v, int32 idx, const char *name)
+{
+#if LLVM_VERSION_MAJOR < 16
+ return LLVMBuildStructGEP(b, v, idx, "");
+#else
+ return LLVMBuildStructGEP2(b, t, v, idx, "");
+#endif
+}
+
+static inline LLVMValueRef
+l_gep(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef v, LLVMValueRef *indices, int32 nindices, const char *name)
+{
+#if LLVM_VERSION_MAJOR < 16
+ return LLVMBuildGEP(b, v, indices, nindices, name);
+#else
+ return LLVMBuildGEP2(b, t, v, indices, nindices, name);
+#endif
+}
+
+static inline LLVMValueRef
+l_load(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef v, const char *name)
+{
+#if LLVM_VERSION_MAJOR < 16
+ return LLVMBuildLoad(b, v, name);
+#else
+ return LLVMBuildLoad2(b, t, v, name);
+#endif
+}
+
+static inline LLVMValueRef
+l_call(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef fn, LLVMValueRef *args, int32 nargs, const char *name)
+{
+#if LLVM_VERSION_MAJOR < 16
+ return LLVMBuildCall(b, fn, args, nargs, name);
+#else
+ return LLVMBuildCall2(b, t, fn, args, nargs, name);
+#endif
+}
+
/*
* Load a pointer member idx from a struct.
*/
static inline LLVMValueRef
-l_load_struct_gep(LLVMBuilderRef b, LLVMValueRef v, int32 idx, const char *name)
+l_load_struct_gep(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef v, int32 idx, const char *name)
{
- LLVMValueRef v_ptr = LLVMBuildStructGEP(b, v, idx, "");
-
- return LLVMBuildLoad(b, v_ptr, name);
+ return l_load(b,
+ LLVMStructGetTypeAtIndex(t, idx),
+ l_struct_gep(b, t, v, idx, ""),
+ name);
}
/*
* Load value of a pointer, after applying one index operation.
*/
static inline LLVMValueRef
-l_load_gep1(LLVMBuilderRef b, LLVMValueRef v, LLVMValueRef idx, const char *name)
+l_load_gep1(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef v, LLVMValueRef idx, const char *name)
{
- LLVMValueRef v_ptr = LLVMBuildGEP(b, v, &idx, 1, "");
-
- return LLVMBuildLoad(b, v_ptr, name);
+ return l_load(b, t, l_gep(b, t, v, &idx, 1, ""), name);
}
/* separate, because pg_attribute_printf(2, 3) can't appear in definition */
@@ -210,7 +250,7 @@ l_mcxt_switch(LLVMModuleRef mod, LLVMBuilderRef b, LLVMValueRef nc)
if (!(cur = LLVMGetNamedGlobal(mod, cmc)))
cur = LLVMAddGlobal(mod, l_ptr(StructMemoryContextData), cmc);
- ret = LLVMBuildLoad(b, cur, cmc);
+ ret = l_load(b, l_ptr(StructMemoryContextData), cur, cmc);
LLVMBuildStore(b, nc, cur);
return ret;
@@ -225,13 +265,21 @@ l_funcnullp(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
LLVMValueRef v_args;
LLVMValueRef v_argn;
- v_args = LLVMBuildStructGEP(b,
- v_fcinfo,
- FIELDNO_FUNCTIONCALLINFODATA_ARGS,
- "");
- v_argn = LLVMBuildStructGEP(b, v_args, argno, "");
-
- return LLVMBuildStructGEP(b, v_argn, FIELDNO_NULLABLE_DATUM_ISNULL, "");
+ v_args = l_struct_gep(b,
+ StructFunctionCallInfoData,
+ v_fcinfo,
+ FIELDNO_FUNCTIONCALLINFODATA_ARGS,
+ "");
+ v_argn = l_struct_gep(b,
+ LLVMArrayType(StructNullableDatum, 0),
+ v_args,
+ argno,
+ "");
+ return l_struct_gep(b,
+ StructNullableDatum,
+ v_argn,
+ FIELDNO_NULLABLE_DATUM_ISNULL,
+ "");
}
/*
@@ -243,13 +291,21 @@ l_funcvaluep(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
LLVMValueRef v_args;
LLVMValueRef v_argn;
- v_args = LLVMBuildStructGEP(b,
- v_fcinfo,
- FIELDNO_FUNCTIONCALLINFODATA_ARGS,
- "");
- v_argn = LLVMBuildStructGEP(b, v_args, argno, "");
-
- return LLVMBuildStructGEP(b, v_argn, FIELDNO_NULLABLE_DATUM_DATUM, "");
+ v_args = l_struct_gep(b,
+ StructFunctionCallInfoData,
+ v_fcinfo,
+ FIELDNO_FUNCTIONCALLINFODATA_ARGS,
+ "");
+ v_argn = l_struct_gep(b,
+ LLVMArrayType(StructNullableDatum, 0),
+ v_args,
+ argno,
+ "");
+ return l_struct_gep(b,
+ StructNullableDatum,
+ v_argn,
+ FIELDNO_NULLABLE_DATUM_DATUM,
+ "");
}
/*
@@ -258,7 +314,7 @@ l_funcvaluep(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
static inline LLVMValueRef
l_funcnull(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
{
- return LLVMBuildLoad(b, l_funcnullp(b, v_fcinfo, argno), "");
+ return l_load(b, TypeStorageBool, l_funcnullp(b, v_fcinfo, argno), "");
}
/*
@@ -267,7 +323,7 @@ l_funcnull(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
static inline LLVMValueRef
l_funcvalue(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
{
- return LLVMBuildLoad(b, l_funcvaluep(b, v_fcinfo, argno), "");
+ return l_load(b, TypeSizeT, l_funcvaluep(b, v_fcinfo, argno), "");
}
#endif /* USE_LLVM */