From b5c68b602821271bd32d17ad9753b1d916c5de55 Mon Sep 17 00:00:00 2001 From: Aiden Grossman Date: Fri, 2 Oct 2026 11:13:05 -0700 Subject: [PATCH] Use runCodeGen Abstraction for new LLVM This is the abstraction that clang/flang both use. It simplifies the code by quite a bit (although not until we delete the old version). Mainly this enables the use of the NewPM for LLVM targets that opt-in (currently only Lanai and MSP430, but more soon). --- .../rustc_llvm/llvm-wrapper/PassWrapper.cpp | 79 ++++++++++--------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 1b18b1f06674c..f891ef556db4c 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -22,6 +22,9 @@ #include "llvm/MC/TargetRegistry.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Passes/PassBuilder.h" +#if LLVM_VERSION_GE(24, 0) +#include "llvm/Passes/RunCodeGen.h" +#endif #include "llvm/Passes/StandardInstrumentations.h" #include "llvm/Plugins/PassPlugin.h" #include "llvm/Support/CBindingWrapping.h" @@ -470,30 +473,6 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMModuleRef M, const char *Path, const char *DwoPath, LLVMRustFileType RustFileType, bool VerifyIR, bool DisableSimplifyLibCalls) { - std::unique_ptr PM = - std::make_unique(); - - PM->add(createTargetTransformInfoWrapperPass( - unwrap(Target)->getTargetIRAnalysis())); - - auto TargetTriple = Triple(unwrap(M)->getTargetTriple()); - TargetOptions *Options = &unwrap(Target)->Options; - auto TLII = TargetLibraryInfoImpl(TargetTriple); - if (DisableSimplifyLibCalls) - TLII.disableAllFunctions(); - PM->add(new TargetLibraryInfoWrapperPass(TLII)); -#if LLVM_VERSION_GE(24, 0) - // LLVM 24 removed TargetOptions::EABIVersion and ExceptionModel; the EABI - // version and exception model are now derived from the target triple and - // module flags respectively instead. - PM->add(new RuntimeLibraryInfoWrapper(Options->MCOptions.ABIName, - Options->VecLib)); -#else - PM->add(new RuntimeLibraryInfoWrapper( - TargetTriple, Options->ExceptionModel, Options->FloatABIType, - Options->EABIVersion, Options->MCOptions.ABIName, Options->VecLib)); -#endif - auto FileType = fromRust(RustFileType); std::string ErrorInfo; @@ -506,29 +485,53 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMModuleRef M, return LLVMRustResult::Failure; } - // TargetMachine::addPassesToEmitFile stores pointers to the output streams - // in a couple of places inside of the object. Explicitly delete the PM after - // we call run() to avoid dangling references. + std::unique_ptr DwoOS = nullptr; + std::unique_ptr BDwoOS = nullptr; auto BOS = buffer_ostream(OS); if (DwoPath) { - auto DOS = raw_fd_ostream(DwoPath, EC, sys::fs::OF_None); - EC.clear(); - if (EC) + DwoOS = std::make_unique(DwoPath, EC, sys::fs::OF_None); + if (EC) { ErrorInfo = EC.message(); + DwoOS.reset(); + } if (ErrorInfo != "") { LLVMRustSetLastError(ErrorInfo.c_str()); return LLVMRustResult::Failure; } - auto DBOS = buffer_ostream(DOS); - unwrap(Target)->addPassesToEmitFile(*PM, BOS, &DBOS, FileType, !VerifyIR); - PM->run(*unwrap(M)); - PM.reset(); - } else { - unwrap(Target)->addPassesToEmitFile(*PM, BOS, nullptr, FileType, !VerifyIR); - PM->run(*unwrap(M)); - PM.reset(); + if (DwoOS) + BDwoOS = std::make_unique(*DwoOS); } +#if LLVM_VERSION_GE(24, 0) + Error CodeGenError = + runCodeGenPipeline(*unwrap(Target), *unwrap(M), BOS, BDwoOS.get(), + FileType, !VerifyIR, DisableSimplifyLibCalls); +#else + std::unique_ptr PM = + std::make_unique(); + + PM->add(createTargetTransformInfoWrapperPass( + unwrap(Target)->getTargetIRAnalysis())); + + auto TargetTriple = Triple(unwrap(M)->getTargetTriple()); + TargetOptions *Options = &unwrap(Target)->Options; + auto TLII = TargetLibraryInfoImpl(TargetTriple); + if (DisableSimplifyLibCalls) + TLII.disableAllFunctions(); + PM->add(new TargetLibraryInfoWrapperPass(TLII)); + PM->add(new RuntimeLibraryInfoWrapper( + TargetTriple, Options->ExceptionModel, Options->FloatABIType, + Options->EABIVersion, Options->MCOptions.ABIName, Options->VecLib)); + + // TargetMachine::addPassesToEmitFile stores pointers to the output streams + // in a couple of places inside of the object. Explicitly delete the PM after + // we call run() to avoid dangling references. + unwrap(Target)->addPassesToEmitFile(*PM, BOS, BDwoOS.get(), FileType, + !VerifyIR); + PM->run(*unwrap(M)); + PM.reset(); +#endif + return LLVMRustResult::Success; }