From 0b1914e83a03be926569892c17ca743c5ea46d1f Mon Sep 17 00:00:00 2001 From: Hongtao Yu Date: Fri, 5 Feb 2021 13:17:51 -0800 Subject: [PATCH] [ThinLTO][gold] Fix filenaming scheme for tasks. The gold LTO plugin uses a set of hooks to implements emit-llvm and capture intermediate file generated during LTO. The hooks are called by each lto backend thread with a taskID as argument to differentiate between threads and tasks. Currently, all threads are overwriting the same file which results into only the intermediate output of the last backend thread to be preserved. This diff encodes the taskID into the filename. Reviewed By: tejohnson, wenlei Differential Revision: https://reviews.llvm.org/D96173 --- llvm/test/tools/gold/X86/Inputs/emit-llvm.bar.ll | 9 +++++++++ llvm/test/tools/gold/X86/Inputs/emit-llvm.foo.ll | 9 +++++++++ llvm/test/tools/gold/X86/thinlto-emit-llvm.ll | 17 +++++++++++++++++ llvm/tools/gold/gold-plugin.cpp | 5 ++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 llvm/test/tools/gold/X86/Inputs/emit-llvm.bar.ll create mode 100644 llvm/test/tools/gold/X86/Inputs/emit-llvm.foo.ll create mode 100644 llvm/test/tools/gold/X86/thinlto-emit-llvm.ll diff --git a/llvm/test/tools/gold/X86/Inputs/emit-llvm.bar.ll b/llvm/test/tools/gold/X86/Inputs/emit-llvm.bar.ll new file mode 100644 index 0000000..93a37fe --- /dev/null +++ b/llvm/test/tools/gold/X86/Inputs/emit-llvm.bar.ll @@ -0,0 +1,9 @@ +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define dso_local i32 @_Z3barv() #0 { + ret i32 0 +} + +^0 = module: (path: "bar.o", hash: (3853957065, 2310817429, 3119833948, 1366622700, 2470927843)) +^1 = gv: (name: "_Z3barv", summaries: (function: (module: ^0, flags: (linkage: external, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 1, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0)))) ; guid = 17377440600225628772 diff --git a/llvm/test/tools/gold/X86/Inputs/emit-llvm.foo.ll b/llvm/test/tools/gold/X86/Inputs/emit-llvm.foo.ll new file mode 100644 index 0000000..85c59b4 --- /dev/null +++ b/llvm/test/tools/gold/X86/Inputs/emit-llvm.foo.ll @@ -0,0 +1,9 @@ +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define dso_local i32 @_Z3foov() #0 { + ret i32 0 +} + +^0 = module: (path: "foo.o", hash: (3786635673, 3275786284, 1544384821, 2103351884, 2354656665)) +^1 = gv: (name: "_Z3foov", summaries: (function: (module: ^0, flags: (linkage: external, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 1, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0)))) ; guid = 9191153033785521275 diff --git a/llvm/test/tools/gold/X86/thinlto-emit-llvm.ll b/llvm/test/tools/gold/X86/thinlto-emit-llvm.ll new file mode 100644 index 0000000..09318dd --- /dev/null +++ b/llvm/test/tools/gold/X86/thinlto-emit-llvm.ll @@ -0,0 +1,17 @@ +; RUN: llvm-as %p/Inputs/emit-llvm.foo.ll -o %t.foo.bc +; RUN: llvm-as %p/Inputs/emit-llvm.bar.ll -o %t.bar.bc +; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext --shared -plugin-opt thinlto -plugin-opt emit-llvm -m elf_x86_64 %t.foo.bc %t.bar.bc -o %t.bc +; RUN: llvm-dis %t.bc1 -o - | FileCheck --check-prefix=CHECK-BC1 %s +; RUN: llvm-dis %t.bc2 -o - | FileCheck --check-prefix=CHECK-BC2 %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK-BC1: define dso_local i32 @_Z3foov() +define dso_local i32 @_Z3foov() { + ret i32 0 +} +; CHECK-BC2: define dso_local i32 @_Z3barv() +define dso_local i32 @_Z3barv() { + ret i32 0 +} diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp index c0e0d5b..38f9596 100644 --- a/llvm/tools/gold/gold-plugin.cpp +++ b/llvm/tools/gold/gold-plugin.cpp @@ -915,7 +915,10 @@ static std::unique_ptr createLTO(IndexWriteCallback OnIndexWrite, case options::OT_BC_ONLY: Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) { std::error_code EC; - raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::OF_None); + SmallString<128> TaskFilename; + getOutputFileName(output_name, /* TempOutFile */ false, TaskFilename, + Task); + raw_fd_ostream OS(TaskFilename, EC, sys::fs::OpenFlags::OF_None); if (EC) message(LDPL_FATAL, "Failed to write the output file."); WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false); -- 2.7.4