From a7df949f3a3220cd7348610e44a914a13afb9e59 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 23 May 2025 12:08:25 +0200 Subject: [PATCH] Don't emit module metadata for non-toplevel modules. Instead, rely on the linker automatically inheriting the metadata from the parent module. This avoids warnings and aborts with external users such as GPUCompiler.jl. --- src/aotcompile.cpp | 7 ++++--- src/codegen.cpp | 23 ++++++++++++++--------- src/jitlayers.h | 15 +++++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp index 33c0ad011e6f0..7c00f1bb26d37 100644 --- a/src/aotcompile.cpp +++ b/src/aotcompile.cpp @@ -813,9 +813,10 @@ void *jl_emit_native_impl(jl_array_t *codeinfos, LLVMOrcThreadSafeModuleRef llvm continue; // skip any duplicates that accidentally made there way in here (or make this an error?) if (jl_ir_inlining_cost((jl_value_t*)src) < UINT16_MAX) params.safepoint_on_entry = false; // ensure we don't block ExpandAtomicModifyPass from inlining this code if applicable - orc::ThreadSafeModule result_m = jl_create_ts_module(name_from_method_instance(jl_get_ci_mi(codeinst)), - params.tsctx, clone.getModuleUnlocked()->getDataLayout(), - Triple(clone.getModuleUnlocked()->getTargetTriple())); + orc::ThreadSafeModule result_m = jl_create_ts_module( + name_from_method_instance(jl_get_ci_mi(codeinst)), params.tsctx, + clone.getModuleUnlocked()->getDataLayout(), + Triple(clone.getModuleUnlocked()->getTargetTriple()), false); jl_llvm_functions_t decls; if (!(params.params->force_emit_all) && jl_atomic_load_relaxed(&codeinst->invoke) == jl_fptr_const_return_addr) decls.functionObject = "jl_fptr_const_return"; diff --git a/src/codegen.cpp b/src/codegen.cpp index c0aeb5bac1915..bd1e08e058af3 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2682,17 +2682,12 @@ static jl_cgval_t convert_julia_type(jl_codectx_t &ctx, const jl_cgval_t &v, jl_ return jl_cgval_t(v, typ, new_tindex); } -std::unique_ptr jl_create_llvm_module(StringRef name, LLVMContext &context, const DataLayout &DL, const Triple &triple) JL_NOTSAFEPOINT +std::unique_ptr jl_create_llvm_module(StringRef name, LLVMContext &context, + const DataLayout &DL, const Triple &triple, + bool toplevel) JL_NOTSAFEPOINT { ++ModulesCreated; auto m = std::make_unique(name, context); - // According to clang darwin above 10.10 supports dwarfv4 - if (!m->getModuleFlag("Dwarf Version")) { - m->addModuleFlag(llvm::Module::Warning, "Dwarf Version", 4); - } - if (!m->getModuleFlag("Debug Info Version")) - m->addModuleFlag(llvm::Module::Warning, "Debug Info Version", - llvm::DEBUG_METADATA_VERSION); m->setDataLayout(DL); m->setTargetTriple(triple.str()); @@ -2703,9 +2698,19 @@ std::unique_ptr jl_create_llvm_module(StringRef name, LLVMContext &conte m->setOverrideStackAlignment(16); } + // when this is a toplevel module, add additional flags. + // otherwise, these are inherited from the parent module when linking. + if (toplevel) { + // According to clang darwin above 10.10 supports dwarfv4 + m->addModuleFlag(llvm::Module::Warning, "Dwarf Version", 4); + m->addModuleFlag(llvm::Module::Warning, "Debug Info Version", + llvm::DEBUG_METADATA_VERSION); + #if defined(JL_DEBUG_BUILD) - m->setStackProtectorGuard("global"); + m->setStackProtectorGuard("global"); #endif + } + return m; } diff --git a/src/jitlayers.h b/src/jitlayers.h index 43fdc9b83b89b..dabba9f611815 100644 --- a/src/jitlayers.h +++ b/src/jitlayers.h @@ -671,15 +671,22 @@ class JuliaOJIT { OptSelLayerT OptSelLayer; }; extern JuliaOJIT *jl_ExecutionEngine; -std::unique_ptr jl_create_llvm_module(StringRef name, LLVMContext &ctx, const DataLayout &DL, const Triple &triple) JL_NOTSAFEPOINT; -inline orc::ThreadSafeModule jl_create_ts_module(StringRef name, orc::ThreadSafeContext ctx, const DataLayout &DL, const Triple &triple) JL_NOTSAFEPOINT { +std::unique_ptr jl_create_llvm_module(StringRef name, LLVMContext &ctx, + const DataLayout &DL, const Triple &triple, + bool toplevel) JL_NOTSAFEPOINT; +inline orc::ThreadSafeModule jl_create_ts_module(StringRef name, orc::ThreadSafeContext ctx, + const DataLayout &DL, const Triple &triple, + bool toplevel = true) JL_NOTSAFEPOINT +{ auto lock = ctx.getLock(); - return orc::ThreadSafeModule(jl_create_llvm_module(name, *ctx.getContext(), DL, triple), ctx); + return orc::ThreadSafeModule( + jl_create_llvm_module(name, *ctx.getContext(), DL, triple, toplevel), ctx); } Module &jl_codegen_params_t::shared_module() JL_NOTSAFEPOINT { if (!_shared_module) { - _shared_module = jl_create_llvm_module("globals", getContext(), DL, TargetTriple); + _shared_module = + jl_create_llvm_module("globals", getContext(), DL, TargetTriple, true); } return *_shared_module; }