Skip to content
Open
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
81 changes: 43 additions & 38 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -470,30 +474,6 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMModuleRef M,
const char *Path, const char *DwoPath,
LLVMRustFileType RustFileType, bool VerifyIR,
bool DisableSimplifyLibCalls) {
std::unique_ptr<llvm::legacy::PassManager> PM =
std::make_unique<llvm::legacy::PassManager>();

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;
Expand All @@ -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<llvm::ToolOutputFile> 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<llvm::ToolOutputFile>(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<llvm::legacy::PassManager> PM =
std::make_unique<llvm::legacy::PassManager>();

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;
}

Expand Down
Loading