From c1f72de6e5ff742e7512909862c7592be7027907 Mon Sep 17 00:00:00 2001 From: Troels Bjerre Lund Date: Mon, 6 Nov 2023 14:41:28 +0100 Subject: [PATCH] [K/N] LLVM: Add missing opaque pointer API LLVMConstGEP2 and LLVMConstInBoundsGEP2 were forward declared in LLVM-11, but not implemented until LLVM-14. This patch adds these along with the missing LLVMAddAlias2. All three implementations are copied from llvm branch release/14.x --- kotlin-native/backend.native/llvm.def | 6 +-- .../src/main/cpp/OpaquePointerAPI.cpp | 37 +++++++++++++++++++ .../src/main/include/OpaquePointerAPI.h | 35 ++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 kotlin-native/libllvmext/src/main/cpp/OpaquePointerAPI.cpp create mode 100644 kotlin-native/libllvmext/src/main/include/OpaquePointerAPI.h diff --git a/kotlin-native/backend.native/llvm.def b/kotlin-native/backend.native/llvm.def index fb94f9db1d7..41eb9677712 100644 --- a/kotlin-native/backend.native/llvm.def +++ b/kotlin-native/backend.native/llvm.def @@ -1,9 +1,9 @@ headers = llvm-c/Core.h llvm-c/Target.h llvm-c/Analysis.h llvm-c/BitWriter.h \ llvm-c/BitReader.h llvm-c/Transforms/PassManagerBuilder.h llvm-c/Transforms/IPO.h \ llvm-c/TargetMachine.h llvm-c/Target.h llvm-c/Linker.h llvm-c/Initialization.h \ - llvm-c/DebugInfo.h DebugInfoC.h CoverageMappingC.h CAPIExtensions.h RemoveRedundantSafepoints.h + llvm-c/DebugInfo.h DebugInfoC.h CoverageMappingC.h CAPIExtensions.h RemoveRedundantSafepoints.h OpaquePointerAPI.h -headerFilter = llvm-c/* llvm-c/**/* DebugInfoC.h CoverageMappingC.h CAPIExtensions.h RemoveRedundantSafepoints.h +headerFilter = llvm-c/* llvm-c/**/* DebugInfoC.h CoverageMappingC.h CAPIExtensions.h RemoveRedundantSafepoints.h OpaquePointerAPI.h compilerOpts = -std=c99 \ -Wall -W -Wno-unused-parameter -Wwrite-strings -Wmissing-field-initializers \ @@ -81,7 +81,7 @@ excludedFunctions.mingw = LLVMDumpType excludedFunctions = LLVMInitializeAllAsmParsers LLVMInitializeAllAsmPrinters LLVMInitializeAllDisassemblers \ LLVMInitializeAllTargetInfos LLVMInitializeAllTargetMCs LLVMInitializeAllTargets LLVMInitializeNativeTarget \ LLVMInitializeNativeAsmParser LLVMInitializeNativeAsmPrinter LLVMInitializeNativeDisassembler \ - LLVMConstInBoundsGEP2 LLVMConstGEP2 LLVMIntPtrType LLVMIntPtrTypeForAS LLVMGetMDKindID LLVMInt1Type LLVMInt8Type \ + LLVMIntPtrType LLVMIntPtrTypeForAS LLVMGetMDKindID LLVMInt1Type LLVMInt8Type \ LLVMInt16Type LLVMInt32Type LLVMInt64Type LLVMInt128Type LLVMIntType LLVMHalfType LLVMFloatType LLVMDoubleType \ LLVMX86FP80Type LLVMFP128Type LLVMPPCFP128Type LLVMX86MMXType LLVMStructType LLVMVoidType LLVMLabelType \ LLVMMDString LLVMMDNode LLVMConstString LLVMConstStruct LLVMAppendBasicBlock LLVMInsertBasicBlock LLVMCreateBuilder \ diff --git a/kotlin-native/libllvmext/src/main/cpp/OpaquePointerAPI.cpp b/kotlin-native/libllvmext/src/main/cpp/OpaquePointerAPI.cpp new file mode 100644 index 00000000000..c58538de828 --- /dev/null +++ b/kotlin-native/libllvmext/src/main/cpp/OpaquePointerAPI.cpp @@ -0,0 +1,37 @@ +#include "OpaquePointerAPI.h" + +#include +#include + +using namespace llvm; + +/* These are the opaque pointer functions that are missing in llvm-11, which +must be removed upon upgrading LLVM version. + +Copied from llvm release/14.x. +*/ + +LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy, + unsigned AddrSpace, LLVMValueRef Aliasee, + const char *Name) { + return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace, + GlobalValue::ExternalLinkage, Name, + unwrap(Aliasee), unwrap(M))); +} + +LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, unsigned NumIndices) { + ArrayRef IdxList(unwrap(ConstantIndices, NumIndices), + NumIndices); + Constant *Val = unwrap(ConstantVal); + return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList)); +} + +LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, + unsigned NumIndices) { + ArrayRef IdxList(unwrap(ConstantIndices, NumIndices), + NumIndices); + Constant *Val = unwrap(ConstantVal); + return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList)); +} \ No newline at end of file diff --git a/kotlin-native/libllvmext/src/main/include/OpaquePointerAPI.h b/kotlin-native/libllvmext/src/main/include/OpaquePointerAPI.h new file mode 100644 index 00000000000..d62dcccf070 --- /dev/null +++ b/kotlin-native/libllvmext/src/main/include/OpaquePointerAPI.h @@ -0,0 +1,35 @@ + +#ifndef LIBLLVMEXT_OPAQUE_POINTER_API_H +#define LIBLLVMEXT_OPAQUE_POINTER_API_H + +#include +#include + +# ifdef __cplusplus +extern "C" { +# endif + +/* These are the opaque pointer functions that are missing in llvm-11, which +must be removed upon upgrading LLVM version. + +Copied from llvm release/14.x. +*/ + +// LLVMConstGEP2 and LLVMConstInBoundsGEP2 are already forward declared in Core.h +LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, unsigned NumIndices); + +LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, + unsigned NumIndices); + +LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy, + unsigned AddrSpace, LLVMValueRef Aliasee, + const char *Name); + +# ifdef __cplusplus +} +# endif + +#endif // LIBLLVMEXT_OPAQUE_POINTER_API_H +