Support AOT compiler with LLVM 17 (#2567)

Adapt API usage to new interfaces where applicable, including LLVM function
usage, obsoleted llvm::Optional type and removal of unavailable headers.

Know issues:
- AOT static PGO isn't enabled
- LLVM JIT may run failed due to llvm_orc_registerEHFrameSectionWrapper
  isn't linked into iwasm
This commit is contained in:
Alfred E. Neumayer 2023-09-25 13:00:46 +02:00 committed by GitHub
parent f474f3d668
commit 99b47fd334
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 3 deletions

View File

@ -526,12 +526,18 @@ aot_add_precheck_function(AOTCompContext *comp_ctx, LLVMModuleRef module,
} }
wasm_runtime_free(params); wasm_runtime_free(params);
params = NULL; params = NULL;
#if LLVM_VERSION_MAJOR < 17
if (aot_target_precheck_can_use_musttail(comp_ctx)) { if (aot_target_precheck_can_use_musttail(comp_ctx)) {
LLVMSetTailCallKind(retval, LLVMTailCallKindMustTail); LLVMSetTailCallKind(retval, LLVMTailCallKindMustTail);
} }
else { else {
LLVMSetTailCallKind(retval, LLVMTailCallKindTail); LLVMSetTailCallKind(retval, LLVMTailCallKindTail);
} }
#else
LLVMSetTailCall(retval, true);
#endif
if (ret_type == VOID_TYPE) { if (ret_type == VOID_TYPE) {
if (!LLVMBuildRetVoid(b)) { if (!LLVMBuildRetVoid(b)) {
goto fail; goto fail;
@ -2172,8 +2178,10 @@ bool
aot_compiler_init(void) aot_compiler_init(void)
{ {
/* Initialize LLVM environment */ /* Initialize LLVM environment */
#if LLVM_VERSION_MAJOR < 17
LLVMInitializeCore(LLVMGetGlobalPassRegistry()); LLVMInitializeCore(LLVMGetGlobalPassRegistry());
#endif
#if WASM_ENABLE_WAMR_COMPILER != 0 #if WASM_ENABLE_WAMR_COMPILER != 0
/* Init environment of all targets for AOT compiler */ /* Init environment of all targets for AOT compiler */
LLVMInitializeAllTargetInfos(); LLVMInitializeAllTargetInfos();

View File

@ -15,15 +15,18 @@
#include "llvm-c/ExecutionEngine.h" #include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Analysis.h" #include "llvm-c/Analysis.h"
#include "llvm-c/BitWriter.h" #include "llvm-c/BitWriter.h"
#if LLVM_VERSION_MAJOR < 17
#include "llvm-c/Transforms/Utils.h" #include "llvm-c/Transforms/Utils.h"
#include "llvm-c/Transforms/Scalar.h" #include "llvm-c/Transforms/Scalar.h"
#include "llvm-c/Transforms/Vectorize.h" #include "llvm-c/Transforms/Vectorize.h"
#include "llvm-c/Transforms/PassManagerBuilder.h" #include "llvm-c/Transforms/PassManagerBuilder.h"
#include "llvm-c/Initialization.h"
#endif
#include "llvm-c/Orc.h" #include "llvm-c/Orc.h"
#include "llvm-c/Error.h" #include "llvm-c/Error.h"
#include "llvm-c/Support.h" #include "llvm-c/Support.h"
#include "llvm-c/Initialization.h"
#include "llvm-c/TargetMachine.h" #include "llvm-c/TargetMachine.h"
#include "llvm-c/LLJIT.h" #include "llvm-c/LLJIT.h"
#if WASM_ENABLE_DEBUG_AOT != 0 #if WASM_ENABLE_DEBUG_AOT != 0

View File

@ -5,11 +5,13 @@
#include <llvm/Passes/StandardInstrumentations.h> #include <llvm/Passes/StandardInstrumentations.h>
#include <llvm/Support/Error.h> #include <llvm/Support/Error.h>
#if LLVM_VERSION_MAJOR < 17
#include <llvm/ADT/None.h> #include <llvm/ADT/None.h>
#include <llvm/ADT/Optional.h> #include <llvm/ADT/Optional.h>
#include <llvm/ADT/Triple.h>
#endif
#include <llvm/ADT/SmallVector.h> #include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/Twine.h> #include <llvm/ADT/Twine.h>
#include <llvm/ADT/Triple.h>
#include <llvm/Analysis/TargetTransformInfo.h> #include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/CodeGen/TargetPassConfig.h> #include <llvm/CodeGen/TargetPassConfig.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h> #include <llvm/ExecutionEngine/ExecutionEngine.h>
@ -18,7 +20,9 @@
#include <llvm/Target/TargetMachine.h> #include <llvm/Target/TargetMachine.h>
#include <llvm-c/Core.h> #include <llvm-c/Core.h>
#include <llvm-c/ExecutionEngine.h> #include <llvm-c/ExecutionEngine.h>
#if LLVM_VERSION_MAJOR < 17
#include <llvm-c/Initialization.h> #include <llvm-c/Initialization.h>
#endif
#include <llvm/ExecutionEngine/GenericValue.h> #include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JITEventListener.h> #include <llvm/ExecutionEngine/JITEventListener.h>
#include <llvm/ExecutionEngine/RTDyldMemoryManager.h> #include <llvm/ExecutionEngine/RTDyldMemoryManager.h>
@ -30,6 +34,9 @@
#include <llvm/IR/PassManager.h> #include <llvm/IR/PassManager.h>
#include <llvm/Support/CommandLine.h> #include <llvm/Support/CommandLine.h>
#include <llvm/Support/ErrorHandling.h> #include <llvm/Support/ErrorHandling.h>
#if LLVM_VERSION_MAJOR >= 17
#include <llvm/Support/PGOOptions.h>
#endif
#include <llvm/Target/CodeGenCWrappers.h> #include <llvm/Target/CodeGenCWrappers.h>
#include <llvm/Target/TargetMachine.h> #include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h> #include <llvm/Target/TargetOptions.h>
@ -55,6 +62,13 @@
using namespace llvm; using namespace llvm;
using namespace llvm::orc; using namespace llvm::orc;
#if LLVM_VERSION_MAJOR >= 17
namespace llvm {
template<typename T>
using Optional = std::optional<T>;
}
#endif
LLVM_C_EXTERN_C_BEGIN LLVM_C_EXTERN_C_BEGIN
bool bool
@ -110,7 +124,14 @@ ExpandMemoryOpPass::run(Function &F, FunctionAnalysisManager &AM)
Memcpy->eraseFromParent(); Memcpy->eraseFromParent();
} }
else if (MemMoveInst *Memmove = dyn_cast<MemMoveInst>(MemCall)) { else if (MemMoveInst *Memmove = dyn_cast<MemMoveInst>(MemCall)) {
#if LLVM_VERSION_MAJOR >= 17
Function *ParentFunc = Memmove->getParent()->getParent();
const TargetTransformInfo &TTI =
AM.getResult<TargetIRAnalysis>(*ParentFunc);
expandMemMoveAsLoop(Memmove, TTI);
#else
expandMemMoveAsLoop(Memmove); expandMemMoveAsLoop(Memmove);
#endif
Memmove->eraseFromParent(); Memmove->eraseFromParent();
} }
else if (MemSetInst *Memset = dyn_cast<MemSetInst>(MemCall)) { else if (MemSetInst *Memset = dyn_cast<MemSetInst>(MemCall)) {
@ -181,6 +202,9 @@ aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module)
#else #else
Optional<PGOOptions> PGO = llvm::None; Optional<PGOOptions> PGO = llvm::None;
#endif #endif
// TODO
#if LLVM_VERSION_MAJOR < 17
if (comp_ctx->enable_llvm_pgo) { if (comp_ctx->enable_llvm_pgo) {
/* Disable static counter allocation for value profiler, /* Disable static counter allocation for value profiler,
it will be allocated by runtime */ it will be allocated by runtime */
@ -191,6 +215,7 @@ aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module)
else if (comp_ctx->use_prof_file) { else if (comp_ctx->use_prof_file) {
PGO = PGOOptions(comp_ctx->use_prof_file, "", "", PGOOptions::IRUse); PGO = PGOOptions(comp_ctx->use_prof_file, "", "", PGOOptions::IRUse);
} }
#endif
#ifdef DEBUG_PASS #ifdef DEBUG_PASS
PassInstrumentationCallbacks PIC; PassInstrumentationCallbacks PIC;

View File

@ -4,8 +4,10 @@
*/ */
#include <llvm-c/TargetMachine.h> #include <llvm-c/TargetMachine.h>
#if LLVM_VERSION_MAJOR < 17
#include <llvm/ADT/None.h> #include <llvm/ADT/None.h>
#include <llvm/ADT/Optional.h> #include <llvm/ADT/Optional.h>
#endif
#include <llvm/IR/Instructions.h> #include <llvm/IR/Instructions.h>
#if LLVM_VERSION_MAJOR >= 14 #if LLVM_VERSION_MAJOR >= 14
#include <llvm/MC/TargetRegistry.h> #include <llvm/MC/TargetRegistry.h>
@ -18,6 +20,13 @@
#include "aot_llvm_extra2.h" #include "aot_llvm_extra2.h"
#if LLVM_VERSION_MAJOR >= 17
namespace llvm {
template<typename T>
using Optional = std::optional<T>;
}
#endif
static llvm::Optional<llvm::Reloc::Model> static llvm::Optional<llvm::Reloc::Model>
convert(LLVMRelocMode reloc_mode) convert(LLVMRelocMode reloc_mode)
{ {

View File

@ -8,8 +8,10 @@
#include "llvm-c/OrcEE.h" #include "llvm-c/OrcEE.h"
#include "llvm-c/TargetMachine.h" #include "llvm-c/TargetMachine.h"
#if LLVM_VERSION_MAJOR < 17
#include "llvm/ADT/None.h" #include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h" #include "llvm/ADT/Optional.h"
#endif
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h" #include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h" #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
@ -21,6 +23,13 @@
#include "aot_orc_extra.h" #include "aot_orc_extra.h"
#include "aot.h" #include "aot.h"
#if LLVM_VERSION_MAJOR >= 17
namespace llvm {
template<typename T>
using Optional = std::optional<T>;
}
#endif
using namespace llvm; using namespace llvm;
using namespace llvm::orc; using namespace llvm::orc;
using GlobalValueSet = std::set<const GlobalValue *>; using GlobalValueSet = std::set<const GlobalValue *>;