diff --git a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs index 69d63c52e4b88..1de4971b08c6c 100644 --- a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs +++ b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs @@ -461,11 +461,11 @@ 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 { - 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..113709afd223a 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" @@ -347,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, @@ -372,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; @@ -440,6 +450,14 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( return wrap(TM); } +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 +467,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 {