Enable debug fission for thinLTO linked via gold-plugin

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
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index e9ac314..e4dffbf 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -136,6 +136,7 @@
   AddString(Conf.AAPipeline);
   AddString(Conf.OverrideTriple);
   AddString(Conf.DefaultTriple);
+  AddString(Conf.DwoDir);
 
   // Include the hash for the current module
   auto ModHash = Index.getModuleHash(ModuleID);
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 074a398..991e2f3 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -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"
@@ -279,11 +283,74 @@
   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))