From 144b0e73666d69c615788131aa32551748e54c54 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Mon, 19 Oct 2026 18:26:20 +0200 Subject: [PATCH 1/4] Parse LLVM options into an explicit global LibraryOptionsContext --- .../src/back/llvm_backend.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 2 +- .../rustc_llvm/llvm-wrapper/PassWrapper.cpp | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs index 69d63c52e4b88..51357af7dd93e 100644 --- a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs +++ b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs @@ -465,7 +465,7 @@ unsafe impl Send for ModuleLlvm {} /// [`llvm::LLVMContextSetDiscardValueNames`]. fn create_context_and_set_fewer_names(fewer_names: bool) -> ptr::NonNull { unsafe { - let llcx_raw = llvm::LLVMContextCreate(); + let llcx_raw = llvm::LLVMRustContextCreate(); llvm::LLVMContextSetDiscardValueNames( llvm::wrap_llcx(llcx_raw), fewer_names.to_llvm_bool(), diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 9c2ab218a02ec..b9dcc624542a9 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -889,7 +889,7 @@ pub(crate) type GetSymbolsErrorCallback = unsafe extern "C" fn(*const c_char) -> unsafe extern "C" { // Create and destroy contexts. - pub(crate) fn LLVMContextCreate() -> ptr::NonNull; + pub(crate) fn LLVMRustContextCreate() -> ptr::NonNull; pub(crate) fn LLVMContextDispose(C: ptr::NonNull); pub(crate) fn LLVMContextSetDiscardValueNames<'ll>(C: Context<'ll>, Discard: Bool); pub(crate) fn LLVMGetMDKindIDInContext<'ll>( diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index f891ef556db4c..a6c2807a83532 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -29,6 +29,9 @@ #include "llvm/Plugins/PassPlugin.h" #include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/FileSystem.h" +#if LLVM_VERSION_GE(24, 0) +#include "llvm/Support/LibraryOptionsContext.h" +#endif #include "llvm/Support/Program.h" #include "llvm/Support/TimeProfiler.h" #include "llvm/Support/VirtualFileSystem.h" @@ -440,6 +443,18 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( return wrap(TM); } +#if LLVM_VERSION_GE(24, 0) +static cl::LibraryOptionsContext OptionsContext; +#endif + +extern "C" LLVMContextRef LLVMRustContextCreate() { + LLVMContextRef Ctx = LLVMContextCreate(); +#if LLVM_VERSION_GE(24, 0) + unwrap(Ctx)->getLibraryOptions().assign(OptionsContext); +#endif + return Ctx; +} + extern "C" void LLVMRustSetLLVMOptions(int Argc, char **Argv) { // Initializing the command-line options more than once is not allowed. So, // check if they've already been initialized. (This could happen if we're @@ -449,7 +464,11 @@ extern "C" void LLVMRustSetLLVMOptions(int Argc, char **Argv) { if (Initialized) return; Initialized = true; +#if LLVM_VERSION_LT(24, 0) cl::ParseCommandLineOptions(Argc, Argv); +#else + cl::parseCommandLineOptions(OptionsContext, ArrayRef(Argv, Argc)); +#endif } enum class LLVMRustFileType { From 70b5d3afc740b210a4f8dd5ecff411025bd5bb53 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Mon, 19 Oct 2026 19:45:40 +0200 Subject: [PATCH 2/4] fix llvm::LLVMContextCreate reference in comment --- compiler/rustc_codegen_llvm/src/back/llvm_backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs index 51357af7dd93e..1de4971b08c6c 100644 --- a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs +++ b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs @@ -461,7 +461,7 @@ pub struct ModuleLlvm { // `ModuleLlvm` owns the context and only allows borrowing LLVM objects tied to the context. unsafe impl Send for ModuleLlvm {} -/// Shared helper to call [`llvm::LLVMContextCreate`] and then configure +/// Shared helper to call [`llvm::LLVMRustContextCreate`] and then configure /// [`llvm::LLVMContextSetDiscardValueNames`]. fn create_context_and_set_fewer_names(fewer_names: bool) -> ptr::NonNull { unsafe { From 5e2c070ab48593583fca45fef3389ab1c3a5424d Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Mon, 19 Oct 2026 21:10:06 +0200 Subject: [PATCH 3/4] hook into LLVMRustCreateTargetMachine too --- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index a6c2807a83532..7e97a6702efbb 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -350,6 +350,10 @@ extern "C" const char *LLVMRustGetHostCPUName(size_t *OutLen) { return Name.data(); } +#if LLVM_VERSION_GE(24, 0) +static cl::LibraryOptionsContext OptionsContext; +#endif + extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( const char *TripleStr, const char *CPU, const char *Feature, const char *ABIStr, LLVMRustCodeModel RustCM, LLVMRustRelocModel RustReloc, @@ -375,6 +379,9 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( } TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags(Trip); + #if LLVM_VERSION_GE(24, 0) + Options.MCOptions.LibraryOptions = &OptionsContext; + #endif #if LLVM_VERSION_LT(24, 0) Options.FloatABIType = FloatABIType; @@ -443,10 +450,6 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( return wrap(TM); } -#if LLVM_VERSION_GE(24, 0) -static cl::LibraryOptionsContext OptionsContext; -#endif - extern "C" LLVMContextRef LLVMRustContextCreate() { LLVMContextRef Ctx = LLVMContextCreate(); #if LLVM_VERSION_GE(24, 0) From 626d88892a228b961a1289e64beb57c83c438050 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Tue, 20 Oct 2026 11:39:57 +0200 Subject: [PATCH 4/4] format --- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 7e97a6702efbb..113709afd223a 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -379,9 +379,9 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( } TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags(Trip); - #if LLVM_VERSION_GE(24, 0) +#if LLVM_VERSION_GE(24, 0) Options.MCOptions.LibraryOptions = &OptionsContext; - #endif +#endif #if LLVM_VERSION_LT(24, 0) Options.FloatABIType = FloatABIType;