Skip to content

Commit

Permalink
Enable debug fission for thinLTO linked via gold-plugin
Browse files Browse the repository at this point in the history
Summary: This enables debug fission on implicit ThinLTO when linked with gold. It will put the .dwo files in a directory specified by user. 

Reviewers: tejohnson, pcc, dblaikie

Reviewed By: pcc

Subscribers: JDevlieghere, mehdi_amini, inglorion

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

llvm-svn: 329988
  • Loading branch information
Yunlian Jiang committed Apr 13, 2018
1 parent d83c883 commit bd200b9
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
6 changes: 6 additions & 0 deletions llvm/include/llvm/LTO/Config.h
Expand Up @@ -73,6 +73,12 @@ struct Config {
/// Sample PGO profile path.
std::string SampleProfile;

/// 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 = "";

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/LTO/LTO.cpp
Expand Up @@ -136,6 +136,7 @@ static void computeCacheKey(
AddString(Conf.AAPipeline);
AddString(Conf.OverrideTriple);
AddString(Conf.DefaultTriple);
AddString(Conf.DwoDir);

// Include the hash for the current module
auto ModHash = Index.getModuleHash(ModuleID);
Expand Down
67 changes: 67 additions & 0 deletions llvm/lib/LTO/LTOBackend.cpp
Expand Up @@ -30,6 +30,10 @@
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Target/TargetMachine.h"
Expand Down Expand Up @@ -279,11 +283,74 @@ 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, 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;

if (!Conf.DwoDir.empty()) {
codegenWithSplitDwarf(Conf, TM, AddStream, Task, Mod);
return;
}

auto Stream = AddStream(Task);
legacy::PassManager CodeGenPasses;
if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, Conf.CGFileType))
Expand Down
1 change: 1 addition & 0 deletions llvm/test/lit.cfg.py
Expand Up @@ -134,6 +134,7 @@ def get_asan_rtlib():
ToolSubst('%ocamlc', ocamlc_command, unresolved='ignore'),
ToolSubst('%ocamlopt', ocamlopt_command, unresolved='ignore'),
ToolSubst('%opt-viewer', opt_viewer_cmd),
ToolSubst('%llvm-objcopy', FindTool('llvm-objcopy')),
]

# FIXME: Why do we have both `lli` and `%lli` that do slightly different things?
Expand Down
63 changes: 63 additions & 0 deletions llvm/test/tools/gold/X86/split-dwarf.ll
@@ -0,0 +1,63 @@
; RUN: rm -rf %t && mkdir -p %t
; RUN: opt -module-summary %s -o %t/split-dwarf.o
; 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-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{{.*}}
; DWOCHECK: DW_AT_name{{.*}}split-dwarf.c
; DWOCHECK: DW_TAG_subprogram

; RUN:rm -rf %t/dwo_dir
; RUN: opt %s -o %t/split-dwarf.o
; 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-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{{.*}}
; LTODWOCHECK: DW_AT_name{{.*}}split-dwarf.c
; LTODWOCHECK: DW_TAG_subprogram

; ModuleID = 'split-dwarf.c'
source_filename = "split-dwarf.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @split_dwarf() #0 !dbg !7 {
entry:
ret i32 0, !dbg !11
}

attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4, !5}
!llvm.ident = !{!6}

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 7.0.0 (https://github.com/llvm-mirror/clang.git b641d31365414ba3ea0305fdaa80369a9efb6bd9) (https://github.com/llvm-mirror/llvm.git 6165a776d1a8bb181be93f2dc97088f7a1abc405)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "split-dwarf.c", directory: "/usr/local/google/home/yunlian/dwp/build/bin")
!2 = !{}
!3 = !{i32 2, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 4}
!6 = !{!"clang version 7.0.0 (https://github.com/llvm-mirror/clang.git b641d31365414ba3ea0305fdaa80369a9efb6bd9) (https://github.com/llvm-mirror/llvm.git 6165a776d1a8bb181be93f2dc97088f7a1abc405)"}
!7 = distinct !DISubprogram(name: "split_dwarf", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, variables: !2)
!8 = !DISubroutineType(types: !9)
!9 = !{!10}
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!11 = !DILocation(line: 2, column: 2, scope: !7)
12 changes: 12 additions & 0 deletions llvm/tools/gold/gold-plugin.cpp
Expand Up @@ -199,6 +199,10 @@ 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;

static void process_plugin_option(const char *opt_)
{
Expand Down Expand Up @@ -262,6 +266,10 @@ 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 {
// Save this option to pass to the code generator.
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
Expand Down Expand Up @@ -870,6 +878,10 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
if (!options::sample_profile.empty())
Conf.SampleProfile = options::sample_profile;

Conf.DwoDir = options::dwo_dir;

Conf.Objcopy = options::objcopy;

// Use new pass manager if set in driver
Conf.UseNewPM = options::new_pass_manager;
// Debug new pass manager if requested
Expand Down

0 comments on commit bd200b9

Please sign in to comment.