From 2d0a6945c4ab10bdfca5f55c126e7602ee225f4c Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Mon, 14 Sep 2020 10:45:00 -0700 Subject: [PATCH] [ThinLTO] add post-thinlto-merge option to -lto-embed-bitcode This will embed bitcode after (Thin)LTO merge, but before optimizations. In the case the thinlto backend is called from clang, the .llvmcmd section is also produced. Doing so in the case where the caller is the linker doesn't yet have a motivation, and would require plumbing through command line args. Differential Revision: https://reviews.llvm.org/D87636 --- include/llvm/LTO/LTOBackend.h | 3 ++- lib/LTO/LTOBackend.cpp | 34 +++++++++++++++++++++++++++---- test/LTO/X86/Inputs/start-lib1.ll | 1 + test/LTO/X86/embed-bitcode.ll | 9 +++++++- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/llvm/LTO/LTOBackend.h b/include/llvm/LTO/LTOBackend.h index 0226e4a3fbf..735969c4703 100644 --- a/include/llvm/LTO/LTOBackend.h +++ b/include/llvm/LTO/LTOBackend.h @@ -44,7 +44,8 @@ Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, - MapVector &ModuleMap); + MapVector &ModuleMap, + const std::vector *CmdArgs = nullptr); Error finalizeOptimizationRemarks( std::unique_ptr DiagOutputFile); diff --git a/lib/LTO/LTOBackend.cpp b/lib/LTO/LTOBackend.cpp index 00309b6d712..4c5778e8118 100644 --- a/lib/LTO/LTOBackend.cpp +++ b/lib/LTO/LTOBackend.cpp @@ -50,9 +50,12 @@ using namespace llvm; using namespace lto; +#define DEBUG_TYPE "lto-backend" + enum class LTOBitcodeEmbedding { DoNotEmbed = 0, EmbedOptimized = 1, + EmbedPostMergePreOptimized = 2 }; static cl::opt EmbedBitcode( @@ -60,7 +63,10 @@ static cl::opt EmbedBitcode( cl::values(clEnumValN(LTOBitcodeEmbedding::DoNotEmbed, "none", "Do not embed"), clEnumValN(LTOBitcodeEmbedding::EmbedOptimized, "optimized", - "Embed after all optimization passes")), + "Embed after all optimization passes"), + clEnumValN(LTOBitcodeEmbedding::EmbedPostMergePreOptimized, + "post-merge-pre-opt", + "Embed post merge, but before optimizations")), cl::desc("Embed LLVM bitcode in object files produced by LTO")); LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) { @@ -346,7 +352,25 @@ static void runOldPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM, bool opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod, bool IsThinLTO, ModuleSummaryIndex *ExportSummary, - const ModuleSummaryIndex *ImportSummary) { + const ModuleSummaryIndex *ImportSummary, + const std::vector *CmdArgs = nullptr) { + if (EmbedBitcode == LTOBitcodeEmbedding::EmbedPostMergePreOptimized) { + // FIXME: the motivation for capturing post-merge bitcode and command line + // is replicating the compilation environment from bitcode, without needing + // to understand the dependencies (the functions to be imported). This + // assumes a clang - based invocation, case in which we have the command + // line. + // It's not very clear how the above motivation would map in the + // linker-based case, so we currently don't plumb the command line args in + // that case. + if (CmdArgs == nullptr) + LLVM_DEBUG( + dbgs() << "Post-(Thin)LTO merge bitcode embedding was requested, but " + "command line arguments are not available"); + llvm::EmbedBitcodeInModule(Mod, llvm::MemoryBufferRef(), + /*EmbedBitcode*/ true, + /*EmbedMarker*/ false, CmdArgs); + } // FIXME: Plumb the combined index into the new pass manager. if (!Conf.OptPipeline.empty()) runNewPMCustomPasses(Conf, Mod, TM, Conf.OptPipeline, Conf.AAPipeline, @@ -531,7 +555,8 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream, Module &Mod, const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, - MapVector &ModuleMap) { + MapVector &ModuleMap, + const std::vector *CmdArgs) { Expected TOrErr = initAndLookupTarget(Conf, Mod); if (!TOrErr) return TOrErr.takeError(); @@ -599,7 +624,8 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream, return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true, - /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex)) + /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex, + CmdArgs)) return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex); diff --git a/test/LTO/X86/Inputs/start-lib1.ll b/test/LTO/X86/Inputs/start-lib1.ll index 9f42e6afff0..18b6ea25386 100644 --- a/test/LTO/X86/Inputs/start-lib1.ll +++ b/test/LTO/X86/Inputs/start-lib1.ll @@ -4,5 +4,6 @@ target triple = "x86_64-unknown-linux-gnu" declare void @bar() define void @foo() { + call void @bar() ret void } diff --git a/test/LTO/X86/embed-bitcode.ll b/test/LTO/X86/embed-bitcode.ll index c8b4d0faa74..bdddd079d22 100644 --- a/test/LTO/X86/embed-bitcode.ll +++ b/test/LTO/X86/embed-bitcode.ll @@ -11,13 +11,20 @@ ; RUN: llvm-lto2 run -r %t1.o,_start,px -r %t2.o,foo,px -r %t3.o,bar,px -r %t2.o,bar,lx -lto-embed-bitcode=optimized -o %t3 %t1.o %t2.o %t3.o ; RUN: llvm-readelf -S %t3.0 | FileCheck %s --check-prefix=CHECK-ELF ; RUN: llvm-objcopy --dump-section=.llvmbc=%t-embedded.bc %t3.0 /dev/null -; RUN: llvm-dis %t-embedded.bc -o - | FileCheck %s --check-prefix=CHECK-LL +; RUN: llvm-dis %t-embedded.bc -o - | FileCheck %s --check-prefixes=CHECK-LL,CHECK-OPT + +; RUN: llvm-lto2 run -r %t1.o,_start,px -r %t2.o,foo,px -r %t3.o,bar,px -r %t2.o,bar,lx -lto-embed-bitcode=post-merge-pre-opt -o %t3 %t1.o %t2.o %t3.o +; RUN: llvm-readelf -S %t3.0 | FileCheck %s --check-prefix=CHECK-ELF +; RUN: llvm-objcopy --dump-section=.llvmbc=%t-embedded.bc %t3.0 /dev/null +; RUN: llvm-dis %t-embedded.bc -o - | FileCheck %s --check-prefixes=CHECK-LL,CHECK-NOOPT ; CHECK-ELF: .text PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 AX 0 ; CHECK-ELF-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 0 ; CHECK-LL: @_start ; CHECK-LL: @foo +; CHECK-OPT-NEXT: ret void +; CHECK-NOOPT-NEXT: call void @bar ; CHECK-LL: @bar target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"