LTO: Replace split dwarf implementation that uses objcopy with one that uses direct emission.

Part of PR37466.

Differential Revision: https://reviews.llvm.org/D47091

llvm-svn: 332884
This commit is contained in:
Peter Collingbourne 2018-05-21 20:26:49 +00:00
parent 94b1f846b2
commit c5a9765cea
4 changed files with 23 additions and 78 deletions

View File

@ -76,9 +76,6 @@ struct Config {
/// The directory to store .dwo files.
std::string DwoDir;
/// The objcopy binary used to extract dwo files.
std::string Objcopy;
/// Optimization remarks file path.
std::string RemarksFilename = "";

View File

@ -285,80 +285,36 @@ bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
}
void codegenWithSplitDwarf(Config &Conf, TargetMachine *TM,
AddStreamFn AddStream, unsigned Task, Module &Mod) {
SmallString<128> TempFile;
int FD = -1;
if (auto EC =
sys::fs::createTemporaryFile("lto-llvm-fission", "o", FD, TempFile))
report_fatal_error("Could not create temporary file " +
TempFile.str() + ": " + EC.message());
llvm::raw_fd_ostream OS(FD, true);
SmallString<1024> DwarfFile(Conf.DwoDir);
std::string DwoName = sys::path::filename(Mod.getModuleIdentifier()).str() +
"-" + std::to_string(Task) + "-";
size_t index = TempFile.str().rfind("lto-llvm-fission");
StringRef TempID = TempFile.str().substr(index + 17, 6);
DwoName += TempID.str() + ".dwo";
sys::path::append(DwarfFile, DwoName);
TM->Options.MCOptions.SplitDwarfFile = DwarfFile.str().str();
legacy::PassManager CodeGenPasses;
if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, Conf.CGFileType))
report_fatal_error("Failed to setup codegen");
CodeGenPasses.run(Mod);
if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
report_fatal_error("Failed to create directory " +
Conf.DwoDir + ": " + EC.message());
SmallVector<const char*, 5> ExtractArgs, StripArgs;
ExtractArgs.push_back(Conf.Objcopy.c_str());
ExtractArgs.push_back("--extract-dwo");
ExtractArgs.push_back(TempFile.c_str());
ExtractArgs.push_back(TM->Options.MCOptions.SplitDwarfFile.c_str());
ExtractArgs.push_back(nullptr);
StripArgs.push_back(Conf.Objcopy.c_str());
StripArgs.push_back("--strip-dwo");
StripArgs.push_back(TempFile.c_str());
StripArgs.push_back(nullptr);
if (auto Ret = sys::ExecuteAndWait(Conf.Objcopy, ExtractArgs.data())) {
report_fatal_error("Failed to extract dwo from " + TempFile.str() +
". Exit code " + std::to_string(Ret));
}
if (auto Ret = sys::ExecuteAndWait(Conf.Objcopy, StripArgs.data())) {
report_fatal_error("Failed to strip dwo from " + TempFile.str() +
". Exit code " + std::to_string(Ret));
}
auto Stream = AddStream(Task);
auto Buffer = MemoryBuffer::getFile(TempFile);
if (auto EC = Buffer.getError())
report_fatal_error("Failed to load file " +
TempFile.str() + ": " + EC.message());
*Stream->OS << Buffer.get()->getBuffer();
if (auto EC = sys::fs::remove(TempFile))
report_fatal_error("Failed to delete file " +
TempFile.str() + ": " + EC.message());
}
void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
unsigned Task, Module &Mod) {
if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
return;
std::unique_ptr<ToolOutputFile> DwoOut;
if (!Conf.DwoDir.empty()) {
codegenWithSplitDwarf(Conf, TM, AddStream, Task, Mod);
return;
std::error_code EC;
if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
report_fatal_error("Failed to create directory " + Conf.DwoDir + ": " +
EC.message());
SmallString<1024> DwoFile(Conf.DwoDir);
sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
TM->Options.MCOptions.SplitDwarfFile = DwoFile.str().str();
DwoOut = make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::F_None);
if (EC)
report_fatal_error("Failed to open " + DwoFile + ": " + EC.message());
}
auto Stream = AddStream(Task);
legacy::PassManager CodeGenPasses;
if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, nullptr,
if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
DwoOut ? &DwoOut->os() : nullptr,
Conf.CGFileType))
report_fatal_error("Failed to setup codegen");
CodeGenPasses.run(Mod);
if (DwoOut)
DwoOut->keep();
}
void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,

View File

@ -3,15 +3,14 @@
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
; RUN: -m elf_x86_64 \
; RUN: --plugin-opt=thinlto \
; RUN: --plugin-opt=objcopy=%llvm-objcopy \
; RUN: --plugin-opt=dwo_dir=%t/dwo_dir \
; RUN: %t/split-dwarf.o --shared -o %t/split-dwarf
; RUN: llvm-dwarfdump -debug-info %t/split-dwarf | FileCheck %s
; CHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/split-dwarf.{{.*}}
; CHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/1.dwo
; CHECK-NOT: DW_TAG_subprogram
; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/split-dwarf.* | FileCheck --check-prefix DWOCHECK %s
; DWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/split-dwarf.o{{.*}}
; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/1.dwo | FileCheck --check-prefix DWOCHECK %s
; DWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/1.dwo
; DWOCHECK: DW_AT_name{{.*}}split-dwarf.c
; DWOCHECK: DW_TAG_subprogram
@ -20,15 +19,14 @@
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
; RUN: -m elf_x86_64 \
; RUN: --plugin-opt=thinlto \
; RUN: --plugin-opt=objcopy=%llvm-objcopy \
; RUN: --plugin-opt=dwo_dir=%t/dwo_dir \
; RUN: %t/split-dwarf.o --shared -o %t/split-dwarf
; RUN: llvm-dwarfdump -debug-info %t/split-dwarf | FileCheck --check-prefix LTOCHECK %s
; LTOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/ld-temp.{{.*}}
; LTOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/0.dwo
; LTOCHECK-NOT: DW_TAG_subprogram
; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/ld-temp.* | FileCheck --check-prefix LTODWOCHECK %s
; LTODWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/ld-temp.o{{.*}}
; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/0.dwo | FileCheck --check-prefix LTODWOCHECK %s
; LTODWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/0.dwo
; LTODWOCHECK: DW_AT_name{{.*}}split-dwarf.c
; LTODWOCHECK: DW_TAG_subprogram

View File

@ -199,8 +199,6 @@ namespace options {
static bool new_pass_manager = false;
// Debug new pass manager
static bool debug_pass_manager = false;
// Objcopy for debug fission.
static std::string objcopy;
// Directory to store the .dwo files.
static std::string dwo_dir;
/// Statistics output filename.
@ -272,8 +270,6 @@ namespace options {
new_pass_manager = true;
} else if (opt == "debug-pass-manager") {
debug_pass_manager = true;
} else if (opt.startswith("objcopy=")) {
objcopy = opt.substr(strlen("objcopy="));
} else if (opt.startswith("dwo_dir=")) {
dwo_dir = opt.substr(strlen("dwo_dir="));
} else if (opt.startswith("opt-remarks-filename=")) {
@ -892,8 +888,6 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
Conf.DwoDir = options::dwo_dir;
Conf.Objcopy = options::objcopy;
// Set up optimization remarks handling.
Conf.RemarksFilename = options::OptRemarksFilename;
Conf.RemarksWithHotness = options::OptRemarksWithHotness;