llvm.org GIT mirror llvm / aadeae8
LTO: Replace split dwarf implementation that uses objcopy with one that uses direct emission. Part of PR37466. Differential Revision: https://reviews.llvm.org/D47091 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332884 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
4 changed file(s) with 23 addition(s) and 78 deletion(s). Raw diff Collapse all Expand all
7474
7575 /// The directory to store .dwo files.
7676 std::string DwoDir;
77
78 /// The objcopy binary used to extract dwo files.
79 std::string Objcopy;
8077
8178 /// Optimization remarks file path.
8279 std::string RemarksFilename = "";
284284 return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
285285 }
286286
287 void codegenWithSplitDwarf(Config &Conf, TargetMachine *TM,
288 AddStreamFn AddStream, unsigned Task, Module &Mod) {
289 SmallString<128> TempFile;
290 int FD = -1;
291 if (auto EC =
292 sys::fs::createTemporaryFile("lto-llvm-fission", "o", FD, TempFile))
293 report_fatal_error("Could not create temporary file " +
294 TempFile.str() + ": " + EC.message());
295 llvm::raw_fd_ostream OS(FD, true);
296 SmallString<1024> DwarfFile(Conf.DwoDir);
297 std::string DwoName = sys::path::filename(Mod.getModuleIdentifier()).str() +
298 "-" + std::to_string(Task) + "-";
299 size_t index = TempFile.str().rfind("lto-llvm-fission");
300 StringRef TempID = TempFile.str().substr(index + 17, 6);
301 DwoName += TempID.str() + ".dwo";
302 sys::path::append(DwarfFile, DwoName);
303 TM->Options.MCOptions.SplitDwarfFile = DwarfFile.str().str();
304
305 legacy::PassManager CodeGenPasses;
306 if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, Conf.CGFileType))
307 report_fatal_error("Failed to setup codegen");
308 CodeGenPasses.run(Mod);
309
310 if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
311 report_fatal_error("Failed to create directory " +
312 Conf.DwoDir + ": " + EC.message());
313
314 SmallVector ExtractArgs, StripArgs;
315 ExtractArgs.push_back(Conf.Objcopy.c_str());
316 ExtractArgs.push_back("--extract-dwo");
317 ExtractArgs.push_back(TempFile.c_str());
318 ExtractArgs.push_back(TM->Options.MCOptions.SplitDwarfFile.c_str());
319 ExtractArgs.push_back(nullptr);
320 StripArgs.push_back(Conf.Objcopy.c_str());
321 StripArgs.push_back("--strip-dwo");
322 StripArgs.push_back(TempFile.c_str());
323 StripArgs.push_back(nullptr);
324
325 if (auto Ret = sys::ExecuteAndWait(Conf.Objcopy, ExtractArgs.data())) {
326 report_fatal_error("Failed to extract dwo from " + TempFile.str() +
327 ". Exit code " + std::to_string(Ret));
328 }
329 if (auto Ret = sys::ExecuteAndWait(Conf.Objcopy, StripArgs.data())) {
330 report_fatal_error("Failed to strip dwo from " + TempFile.str() +
331 ". Exit code " + std::to_string(Ret));
332 }
333
334 auto Stream = AddStream(Task);
335 auto Buffer = MemoryBuffer::getFile(TempFile);
336 if (auto EC = Buffer.getError())
337 report_fatal_error("Failed to load file " +
338 TempFile.str() + ": " + EC.message());
339 *Stream->OS << Buffer.get()->getBuffer();
340 if (auto EC = sys::fs::remove(TempFile))
341 report_fatal_error("Failed to delete file " +
342 TempFile.str() + ": " + EC.message());
343 }
344
345287 void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
346288 unsigned Task, Module &Mod) {
347289 if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
348290 return;
349291
292 std::unique_ptr DwoOut;
350293 if (!Conf.DwoDir.empty()) {
351 codegenWithSplitDwarf(Conf, TM, AddStream, Task, Mod);
352 return;
294 std::error_code EC;
295 if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
296 report_fatal_error("Failed to create directory " + Conf.DwoDir + ": " +
297 EC.message());
298
299 SmallString<1024> DwoFile(Conf.DwoDir);
300 sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
301 TM->Options.MCOptions.SplitDwarfFile = DwoFile.str().str();
302 DwoOut = make_unique(DwoFile, EC, sys::fs::F_None);
303 if (EC)
304 report_fatal_error("Failed to open " + DwoFile + ": " + EC.message());
353305 }
354306
355307 auto Stream = AddStream(Task);
356308 legacy::PassManager CodeGenPasses;
357 if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, nullptr,
309 if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
310 DwoOut ? &DwoOut->os() : nullptr,
358311 Conf.CGFileType))
359312 report_fatal_error("Failed to setup codegen");
360313 CodeGenPasses.run(Mod);
314
315 if (DwoOut)
316 DwoOut->keep();
361317 }
362318
363319 void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
22 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
33 ; RUN: -m elf_x86_64 \
44 ; RUN: --plugin-opt=thinlto \
5 ; RUN: --plugin-opt=objcopy=%llvm-objcopy \
65 ; RUN: --plugin-opt=dwo_dir=%t/dwo_dir \
76 ; RUN: %t/split-dwarf.o --shared -o %t/split-dwarf
87
98 ; RUN: llvm-dwarfdump -debug-info %t/split-dwarf | FileCheck %s
10 ; CHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/split-dwarf.{{.*}}
9 ; CHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/1.dwo
1110 ; CHECK-NOT: DW_TAG_subprogram
12 ; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/split-dwarf.* | FileCheck --check-prefix DWOCHECK %s
13 ; DWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/split-dwarf.o{{.*}}
11 ; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/1.dwo | FileCheck --check-prefix DWOCHECK %s
12 ; DWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/1.dwo
1413 ; DWOCHECK: DW_AT_name{{.*}}split-dwarf.c
1514 ; DWOCHECK: DW_TAG_subprogram
1615
1918 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
2019 ; RUN: -m elf_x86_64 \
2120 ; RUN: --plugin-opt=thinlto \
22 ; RUN: --plugin-opt=objcopy=%llvm-objcopy \
2321 ; RUN: --plugin-opt=dwo_dir=%t/dwo_dir \
2422 ; RUN: %t/split-dwarf.o --shared -o %t/split-dwarf
2523
2624 ; RUN: llvm-dwarfdump -debug-info %t/split-dwarf | FileCheck --check-prefix LTOCHECK %s
27 ; LTOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/ld-temp.{{.*}}
25 ; LTOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/0.dwo
2826 ; LTOCHECK-NOT: DW_TAG_subprogram
29 ; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/ld-temp.* | FileCheck --check-prefix LTODWOCHECK %s
30 ; LTODWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/ld-temp.o{{.*}}
27 ; RUN: llvm-dwarfdump -debug-info %t/dwo_dir/0.dwo | FileCheck --check-prefix LTODWOCHECK %s
28 ; LTODWOCHECK: DW_AT_GNU_dwo_name{{.*}}dwo_dir/0.dwo
3129 ; LTODWOCHECK: DW_AT_name{{.*}}split-dwarf.c
3230 ; LTODWOCHECK: DW_TAG_subprogram
3331
198198 static bool new_pass_manager = false;
199199 // Debug new pass manager
200200 static bool debug_pass_manager = false;
201 // Objcopy for debug fission.
202 static std::string objcopy;
203201 // Directory to store the .dwo files.
204202 static std::string dwo_dir;
205203 /// Statistics output filename.
271269 new_pass_manager = true;
272270 } else if (opt == "debug-pass-manager") {
273271 debug_pass_manager = true;
274 } else if (opt.startswith("objcopy=")) {
275 objcopy = opt.substr(strlen("objcopy="));
276272 } else if (opt.startswith("dwo_dir=")) {
277273 dwo_dir = opt.substr(strlen("dwo_dir="));
278274 } else if (opt.startswith("opt-remarks-filename=")) {
890886 Conf.SampleProfile = options::sample_profile;
891887
892888 Conf.DwoDir = options::dwo_dir;
893
894 Conf.Objcopy = options::objcopy;
895889
896890 // Set up optimization remarks handling.
897891 Conf.RemarksFilename = options::OptRemarksFilename;