From 3122f9e759e33e7c30e916649020dad0aee09a64 Mon Sep 17 00:00:00 2001 From: William Furr Date: Wed, 6 May 2026 13:28:26 +0200 Subject: [PATCH 1/2] Add compatibility with LLVM 21.x. LLVM 21 makes a breaking change in LLVMContext construction: LLVMOrcCreateNewThreadSafeContext() no longer creates an implicit LLVMContext. Instead, a context must be constructed first and then wrapped in a ThreadSafeContext using the new LLVMOrcCreateNewThreadSafeContextFromLLVMContext() API. WAMR still needs to support older LLVM versions (e.g. for xtensa/esp32 support which is only available in at most LLVM 19), so the new code path is guarded with LLVM_VERSION_MAJOR >= 21. Co-authored-by: Peter Tatrai Ref: https://github.com/bytecodealliance/wasm-micro-runtime/pull/4654 --- core/iwasm/compilation/aot_llvm.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 1a9da63fac..e9f86817f0 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -2650,6 +2650,25 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) comp_ctx->comp_data = comp_data; /* Create LLVM context, module and builder */ +#if LLVM_VERSION_MAJOR >= 21 + /* Construct an LLVMContext directly, note: + different from non LAZY JIT mode, no need to dispose this context, if + will be disposed when the thread safe context is disposed */ + comp_ctx->context = LLVMContextCreate(); + if (!comp_ctx->context) { + aot_set_last_error("create LLVM Context failed."); + goto fail; + } + + /* Wrap the LLVM context in a thread safe context. */ + comp_ctx->orc_thread_safe_context = + LLVMOrcCreateNewThreadSafeContextFromLLVMContext(comp_ctx->context); + if (!comp_ctx->orc_thread_safe_context) { + aot_set_last_error( + "Create LLVM ThreadSafeContext from LLVMContext failed."); + goto fail; + } +#else /* LLVM_VERSION_MAJOR < 21 */ comp_ctx->orc_thread_safe_context = LLVMOrcCreateNewThreadSafeContext(); if (!comp_ctx->orc_thread_safe_context) { aot_set_last_error("create LLVM ThreadSafeContext failed."); @@ -2664,6 +2683,7 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) aot_set_last_error("get context from LLVM ThreadSafeContext failed."); goto fail; } +#endif /* LLVM_VERSION_MAJOR >= 21 */ if (!(comp_ctx->builder = LLVMCreateBuilderInContext(comp_ctx->context))) { aot_set_last_error("create LLVM builder failed."); From cc383930440ca582a17fc6fdce4543b997e492d7 Mon Sep 17 00:00:00 2001 From: Peter Tatrai Date: Wed, 6 May 2026 13:28:51 +0200 Subject: [PATCH 2/2] Add compatibility with LLVM 22.x. LLVM 22 removes the IntrusiveRefCntPtr parameter from the PGOOptions constructor. The VFS was moved to PassBuilder directly (see llvm/llvm-project#160188), allowing all passes that need filesystem access to get it from PassBuilder rather than routing it through PGOOptions. Add LLVM_VERSION_MAJOR version guards to retain the FS argument for LLVM 17-21 while dropping it for LLVM 22+. --- core/iwasm/compilation/aot_llvm_extra.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/iwasm/compilation/aot_llvm_extra.cpp b/core/iwasm/compilation/aot_llvm_extra.cpp index 35b25c8300..3a3dc29a32 100644 --- a/core/iwasm/compilation/aot_llvm_extra.cpp +++ b/core/iwasm/compilation/aot_llvm_extra.cpp @@ -212,18 +212,23 @@ aot_apply_llvm_new_pass_manager(AOTCompContext *comp_ctx, LLVMModuleRef module) cl::ParseCommandLineOptions(2, argv); #if LLVM_VERSION_MAJOR < 17 PGO = PGOOptions("", "", "", PGOOptions::IRInstr); -#else +#elif LLVM_VERSION_MAJOR < 22 auto FS = vfs::getRealFileSystem(); PGO = PGOOptions("", "", "", "", FS, PGOOptions::IRInstr); +#else + PGO = PGOOptions("", "", "", "", PGOOptions::IRInstr); #endif } else if (comp_ctx->use_prof_file) { #if LLVM_VERSION_MAJOR < 17 PGO = PGOOptions(comp_ctx->use_prof_file, "", "", PGOOptions::IRUse); -#else +#elif LLVM_VERSION_MAJOR < 22 auto FS = vfs::getRealFileSystem(); PGO = PGOOptions(comp_ctx->use_prof_file, "", "", "", FS, PGOOptions::IRUse); +#else + PGO = + PGOOptions(comp_ctx->use_prof_file, "", "", "", PGOOptions::IRUse); #endif }