Skip to content

Commit

Permalink
[LTO] Enable module summary emission by default for regular LTO
Browse files Browse the repository at this point in the history
Summary:
With D33921, we gained the ability to have module summaries in regular
LTO modules without triggering ThinLTO compilation. Module summaries in
regular LTO allow garbage collection (dead stripping) before LTO
compilation and thus open up additional optimization opportunities.

This patch enables summary emission in regular LTO for all targets
except ld64-based ones (which use the legacy LTO API).

Reviewers: pcc, tejohnson, mehdi_amini

Subscribers: inglorion, eraman, cfe-commits

Differential Revision: https://reviews.llvm.org/D34156

llvm-svn: 335284
  • Loading branch information
Tobias Edler von Koch committed Jun 21, 2018
1 parent a76b700 commit 9a8be60
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Frontend/CodeGenOptions.def
Expand Up @@ -110,7 +110,7 @@ CODEGENOPT(LessPreciseFPMAD , 1, 0) ///< Enable less precise MAD instructions t
///< be generated.
CODEGENOPT(PrepareForLTO , 1, 0) ///< Set when -flto is enabled on the
///< compile step.
CODEGENOPT(EmitSummaryIndex, 1, 0) ///< Set when -flto=thin is enabled on the
CODEGENOPT(PrepareForThinLTO , 1, 0) ///< Set when -flto=thin is enabled on the
///< compile step.
CODEGENOPT(LTOUnit, 1, 0) ///< Emit IR to support LTO unit features (CFI, whole
///< program vtable opt).
Expand Down
38 changes: 28 additions & 10 deletions clang/lib/CodeGen/BackendUtil.cpp
Expand Up @@ -524,7 +524,7 @@ void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
PMBuilder.Inliner = createFunctionInliningPass(
CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
(!CodeGenOpts.SampleProfileFile.empty() &&
CodeGenOpts.EmitSummaryIndex));
CodeGenOpts.PrepareForThinLTO));
}

PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
Expand All @@ -534,7 +534,7 @@ void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,

PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;

Expand Down Expand Up @@ -776,18 +776,28 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
break;

case Backend_EmitBC:
if (CodeGenOpts.EmitSummaryIndex) {
if (CodeGenOpts.PrepareForThinLTO) {
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
if (!ThinLinkOS)
return;
}
PerModulePasses.add(createWriteThinLTOBitcodePass(
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
}
else
} else {
// Emit a module summary by default for Regular LTO except for ld64
// targets
bool EmitLTOSummary =
(CodeGenOpts.PrepareForLTO &&
llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
llvm::Triple::Apple);
if (EmitLTOSummary && !TheModule->getModuleFlag("ThinLTO"))
TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));

PerModulePasses.add(
createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
EmitLTOSummary));
}
break;

case Backend_EmitLL:
Expand Down Expand Up @@ -935,7 +945,7 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
ModulePassManager MPM(CodeGenOpts.DebugPassManager);

if (!CodeGenOpts.DisableLLVMPasses) {
bool IsThinLTO = CodeGenOpts.EmitSummaryIndex;
bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
bool IsLTO = CodeGenOpts.PrepareForLTO;

if (CodeGenOpts.OptimizationLevel == 0) {
Expand Down Expand Up @@ -996,7 +1006,7 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
break;

case Backend_EmitBC:
if (CodeGenOpts.EmitSummaryIndex) {
if (CodeGenOpts.PrepareForThinLTO) {
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
if (!ThinLinkOS)
Expand All @@ -1005,9 +1015,17 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
: nullptr));
} else {
// Emit a module summary by default for Regular LTO except for ld64
// targets
bool EmitLTOSummary =
(CodeGenOpts.PrepareForLTO &&
llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
llvm::Triple::Apple);
if (EmitLTOSummary && !TheModule->getModuleFlag("ThinLTO"))
TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));

MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
CodeGenOpts.EmitSummaryIndex,
CodeGenOpts.EmitSummaryIndex));
EmitLTOSummary));
}
break;

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGDebugInfo.cpp
Expand Up @@ -578,7 +578,7 @@ void CGDebugInfo::CreateCompileUnit() {
CSInfo,
getSource(SM, SM.getMainFileID())),
CGOpts.EmitVersionIdentMetadata ? Producer : "",
LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex,
LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO,
CGOpts.DwarfDebugFlags, RuntimeVers,
CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -749,11 +749,11 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
Opts.ProfileSampleAccurate = Args.hasArg(OPT_fprofile_sample_accurate);

Opts.PrepareForLTO = Args.hasArg(OPT_flto, OPT_flto_EQ);
Opts.EmitSummaryIndex = false;
Opts.PrepareForThinLTO = false;
if (Arg *A = Args.getLastArg(OPT_flto_EQ)) {
StringRef S = A->getValue();
if (S == "thin")
Opts.EmitSummaryIndex = true;
Opts.PrepareForThinLTO = true;
else if (S != "full")
Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << S;
}
Expand Down
17 changes: 17 additions & 0 deletions clang/test/CodeGen/emit-summary-index.c
@@ -0,0 +1,17 @@
// ; Check that the -flto=thin option emits a ThinLTO summary
// RUN: %clang_cc1 -flto=thin -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck %s
// CHECK: <GLOBALVAL_SUMMARY_BLOCK
//
// ; Check that we do not emit a summary for regular LTO on Apple platforms
// RUN: %clang_cc1 -flto -triple x86_64-apple-darwin -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck --check-prefix=LTO %s
// LTO-NOT: GLOBALVAL_SUMMARY_BLOCK
//
// ; Check that we emit a summary for regular LTO by default elsewhere
// RUN: %clang_cc1 -flto -triple x86_64-pc-linux-gnu -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck --check-prefix=LTOINDEX %s
// LTOINDEX: <FULL_LTO_GLOBALVAL_SUMMARY_BLOCK
//
// ; Simulate -save-temps and check that it works (!"ThinLTO" module flag not added multiple times)
// RUN: %clang_cc1 -flto -triple x86_64-pc-linux-gnu -emit-llvm-bc -disable-llvm-passes < %s -o %t.bc
// RUN: %clang_cc1 -flto -triple x86_64-pc-linux-gnu -emit-llvm-bc -x ir < %t.bc | llvm-bcanalyzer -dump | FileCheck --check-prefix=LTOINDEX %s

int main() {}
4 changes: 0 additions & 4 deletions clang/test/Misc/thinlto.c

This file was deleted.

0 comments on commit 9a8be60

Please sign in to comment.