Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@

// Conditional includes prevent clang-format from fully sorting the list,
// so if any are needed, keep them separate down here.
#if LLVM_VERSION_GE(24, 0)
#include "llvm/Support/LibraryOptionsContext.h"
#endif

using namespace llvm;

static codegen::RegisterCodeGenFlags CGF;

#if LLVM_VERSION_GE(24, 0)
static cl::LibraryOptionsContext LibraryOptions;

@nikic nikic Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This global variable should not exist. The whole point of the change is making the options per-context.

View changes since the review

#endif

typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;

DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
Expand Down Expand Up @@ -369,6 +376,9 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
}

TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags(Trip);
#if LLVM_VERSION_GE(24, 0)
Options.MCOptions.LibraryOptions = &LibraryOptions;
#endif

#if LLVM_VERSION_LT(24, 0)
Options.FloatABIType = FloatABIType;
Expand Down Expand Up @@ -446,7 +456,11 @@ extern "C" void LLVMRustSetLLVMOptions(int Argc, char **Argv) {
if (Initialized)
return;
Initialized = true;
#if LLVM_VERSION_GE(24, 0)
cl::parseCommandLineOptions(LibraryOptions, ArrayRef(Argv, Argc));
#else
cl::ParseCommandLineOptions(Argc, Argv);
#endif
}

enum class LLVMRustFileType {
Expand All @@ -470,6 +484,9 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMModuleRef M,
const char *Path, const char *DwoPath,
LLVMRustFileType RustFileType, bool VerifyIR,
bool DisableSimplifyLibCalls) {
#if LLVM_VERSION_GE(24, 0)
unwrap(M)->getContext().getLibraryOptions().assign(LibraryOptions);
#endif
std::unique_ptr<llvm::legacy::PassManager> PM =
std::make_unique<llvm::legacy::PassManager>();

Expand Down Expand Up @@ -693,6 +710,9 @@ extern "C" LLVMRustResult LLVMRustOptimize(
const char *ExtraPasses, size_t ExtraPassesLen, const char *LLVMPlugins,
size_t LLVMPluginsLen) {
Module *TheModule = unwrap(ModuleRef);
#if LLVM_VERSION_GE(24, 0)
TheModule->getContext().getLibraryOptions().assign(LibraryOptions);
#endif
TargetMachine *TM = unwrap(TMRef);
OptimizationLevel OptLevel = fromRust(OptLevelRust);

Expand Down
Loading