From 488d1dc0edefb3b9852e1fd1ea579d585f7acec7 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Thu, 23 Mar 2017 19:47:49 +0000 Subject: [PATCH] [ThinLTO] Clang support for emitting minimized bitcode for thin link Summary: Clang companion patch to LLVM patch D31027, which adds support for emitting minimized bitcode file for use in the thin link step. Add a cc1 option -fthin-link-bitcode= to trigger this behavior. Depends on D31027. Reviewers: mehdi_amini, pcc Subscribers: cfe-commits, Prazek Differential Revision: https://reviews.llvm.org/D31050 llvm-svn: 298639 --- clang/include/clang/Driver/CC1Options.td | 2 ++ clang/include/clang/Frontend/CodeGenOptions.h | 5 +++++ clang/lib/CodeGen/BackendUtil.cpp | 19 +++++++++++++++++-- clang/lib/Frontend/CompilerInvocation.cpp | 1 + clang/test/CodeGen/thin_link_bitcode.c | 9 +++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 clang/test/CodeGen/thin_link_bitcode.c diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index acc76f6..a683efe 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -312,6 +312,8 @@ def flto_visibility_public_std: def flto_unit: Flag<["-"], "flto-unit">, HelpText<"Emit IR to support LTO unit features (CFI, whole program vtable opt)">; def fno_lto_unit: Flag<["-"], "fno-lto-unit">; +def fthin_link_bitcode_EQ : Joined<["-"], "fthin-link-bitcode=">, + HelpText<"Write minimized bitcode to for the ThinLTO thin link only">; //===----------------------------------------------------------------------===// // Dependency Output Options diff --git a/clang/include/clang/Frontend/CodeGenOptions.h b/clang/include/clang/Frontend/CodeGenOptions.h index f8f3266..b17f190 100644 --- a/clang/include/clang/Frontend/CodeGenOptions.h +++ b/clang/include/clang/Frontend/CodeGenOptions.h @@ -188,6 +188,11 @@ public: /// importing. std::string ThinLTOIndexFile; + /// Name of a file that can optionally be written with minimized bitcode + /// to be used as input for the ThinLTO thin link step, which only needs + /// the summary and module symbol table (and not, e.g. any debug metadata). + std::string ThinLinkBitcodeFile; + /// A list of file names passed with -fcuda-include-gpubinary options to /// forward to CUDA runtime back-end for incorporating them into host-side /// object file. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 35c31a0..35de1a45 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -686,13 +686,28 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action, CodeGenPasses.add( createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); + std::unique_ptr ThinLinkOS; + switch (Action) { case Backend_EmitNothing: break; case Backend_EmitBC: - if (CodeGenOpts.EmitSummaryIndex) - PerModulePasses.add(createWriteThinLTOBitcodePass(*OS)); + if (CodeGenOpts.EmitSummaryIndex) { + if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) { + std::error_code EC; + ThinLinkOS.reset(new llvm::raw_fd_ostream( + CodeGenOpts.ThinLinkBitcodeFile, EC, + llvm::sys::fs::F_None)); + if (EC) { + Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile + << EC.message(); + return; + } + } + PerModulePasses.add( + createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get())); + } else PerModulePasses.add( createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists)); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 01ac69e..c193620 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -641,6 +641,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, << A->getAsString(Args) << "-x ir"; Opts.ThinLTOIndexFile = Args.getLastArgValue(OPT_fthinlto_index_EQ); } + Opts.ThinLinkBitcodeFile = Args.getLastArgValue(OPT_fthin_link_bitcode_EQ); Opts.MSVolatile = Args.hasArg(OPT_fms_volatile); diff --git a/clang/test/CodeGen/thin_link_bitcode.c b/clang/test/CodeGen/thin_link_bitcode.c new file mode 100644 index 0000000..4cb5f79 --- /dev/null +++ b/clang/test/CodeGen/thin_link_bitcode.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -o %t -flto=thin -fthin-link-bitcode=%t.nodebug -triple x86_64-unknown-linux-gnu -emit-llvm-bc -debug-info-kind=limited %s +// RUN: llvm-bcanalyzer -dump %t | FileCheck %s +// RUN: llvm-bcanalyzer -dump %t.nodebug | FileCheck %s --check-prefix=NO_DEBUG +int main (void) { + return 0; +} + +// CHECK: COMPILE_UNIT +// NO_DEBUG-NOT: COMPILE_UNIT -- 2.7.4