diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 3873701ae09ac..ef67e6c1e6ad7 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -22,12 +22,16 @@ #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" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Program.h" #include "llvm/Support/TimeProfiler.h" +#include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/VirtualFileSystem.h" #include "llvm/Target/TargetMachine.h" #include "llvm/TargetParser/Host.h" @@ -470,30 +474,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 +486,54 @@ 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 DOS; auto BOS = buffer_ostream(OS); if (DwoPath) { - auto DOS = raw_fd_ostream(DwoPath, EC, sys::fs::OF_None); - EC.clear(); - if (EC) + DOS = std::make_unique(DwoPath, EC, + llvm::sys::fs::OF_None); + if (EC) { ErrorInfo = EC.message(); + DOS.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(); + DOS->keep(); } +#if LLVM_VERSION_GE(24, 0) + Error CodeGenError = + runCodeGenPipeline(*unwrap(Target), *unwrap(M), BOS, DOS, FileType, false, + !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)); +#if LLVM_VERSION_GE(22, 0) + PM->add(new RuntimeLibraryInfoWrapper( + TargetTriple, Options->ExceptionModel, Options->FloatABIType, + Options->EABIVersion, Options->MCOptions.ABIName, Options->VecLib)); +#endif + + // 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, DOS ? &DOS->os() : nullptr, + FileType, !VerifyIR); + PM->run(*unwrap(M)); + PM.reset(); +#endif + return LLVMRustResult::Success; }