[PATCH] D34156: [LTO] Add -femit-summary-index flag to emit summary for regular LTO

2017-06-13 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk created this revision.
Herald added subscribers: eraman, inglorion.

With https://reviews.llvm.org/D33921, we gained the ability to have module 
summaries in regular
LTO modules without triggering ThinLTO compilation. There is however,
currently, no way to trigger the emission of a module summary with
regular LTO in Clang. This patch adds a flag -femit-summary-index which
makes that possible.

Why would you want a summary with regular LTO?

The Qualcomm Linker performs a two-phase garbage collection (a.k.a. dead
stripping): firstly, before LTO compilation, and then again afterwards.
The key advantage of already running GC before LTO is that in links that
involve a heavy mix of ELF objects and bitcode (a critical use case for
us), we can prune some of the dependences between bitcode and ELF and
thus internalize more symbols in LTO.

For this to work, the linker needs to be able to inspect the reference
graphs for bitcode modules and the module summary provides exactly that.


https://reviews.llvm.org/D34156

Files:
  clang/include/clang/Driver/Options.td
  clang/include/clang/Frontend/CodeGenOptions.def
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGen/emit-summary-index.c
  clang/test/Misc/thinlto.c

Index: clang/test/Misc/thinlto.c
===
--- clang/test/Misc/thinlto.c
+++ /dev/null
@@ -1,4 +0,0 @@
-// RUN: %clang_cc1 -flto=thin -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck %s
-// ; Check that the -flto=thin option emits a summary
-// CHECK: containsValue("thin");
+  if (A && A->containsValue("thin"))
+Opts.PrepareForThinLTO = Opts.EmitSummaryIndex = true;
   Opts.LTOUnit = Args.hasFlag(OPT_flto_unit, OPT_fno_lto_unit, false);
   if (Arg *A = Args.getLastArg(OPT_fthinlto_index_EQ)) {
 if (IK.getLanguage() != InputKind::LLVM_IR)
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2057,6 +2057,8 @@
 if (JA.getType() == types::TY_LLVM_BC)
   CmdArgs.push_back("-emit-llvm-uselists");
 
+Args.AddLastArg(CmdArgs, options::OPT_femit_summary_index);
+
 if (D.isUsingLTO()) {
   Args.AddLastArg(CmdArgs, options::OPT_flto, options::OPT_flto_EQ);
 
Index: clang/lib/CodeGen/BackendUtil.cpp
===
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -486,7 +486,7 @@
 PMBuilder.Inliner = createFunctionInliningPass(
 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
 (!CodeGenOpts.SampleProfileFile.empty() &&
- CodeGenOpts.EmitSummaryIndex));
+ CodeGenOpts.PrepareForThinLTO));
   }
 
   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
@@ -497,7 +497,7 @@
 
   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
-  PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
+  PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
@@ -738,7 +738,7 @@
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 std::error_code EC;
 ThinLinkOS.reset(new llvm::raw_fd_ostream(
@@ -752,10 +752,13 @@
   }
   PerModulePasses.add(
   createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get()));
-}
-else
+} else {
+  if (CodeGenOpts.EmitSummaryIndex)
+ TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
   PerModulePasses.add(
-  createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
+  createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
+  CodeGenOpts.EmitSummaryIndex));
+}
 break;
 
   case Backend_EmitLL:
@@ -907,7 +910,7 @@
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 std::error_code EC;
 ThinLinkOS.emplace(CodeGenOpts.ThinLinkBitcodeFile, EC,
@@ -921,8 +924,9 @@
   MPM.addPass(
   ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &*ThinLinkOS : nullptr));
 } else {
+  if (CodeGenOpts.EmitSummaryIndex)
+ TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
   MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
-CodeGenOpts.EmitSummaryIndex,
 CodeGenOpts.EmitSummaryIndex));
 }
 break;
Index: clang/include/clang/Frontend/CodeGenOptions.def
==

[PATCH] D34156: [LTO] Add -femit-summary-index flag to emit summary for regular LTO

2017-06-13 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk added a comment.

In https://reviews.llvm.org/D34156#779270, @pcc wrote:

> Have you considered writing the regular LTO summaries unconditionally if 
> `-flto` was specified? That was how I imagined that the interface would look.


Absolutely, if people are OK with that. I would have enabled it by default in 
our tree anyway. Let me know if you prefer this (and other people if you have 
objections).

> Also, how were you planning to expose the reference graph to the linker? I 
> gather from your message that you are teaching your linker to read the module 
> summary index directly from bitcode files.

Yep, pretty much. I have a layer between the linker and LLVM that implements a 
`getRefsBySymbol` API which lets the linker iterate the reference graph. We've 
had this out of tree for some time, it's just that your recent patch made it 
straightforward to implement upstream.

> I wonder whether it would be worth trying to avoid needing to read summaries 
> multiple times. The approach that I had in mind was to somehow teach the 
> linker to add regular object files to the combined summary index by creating 
> a "global value summary" for each section, with a reference for each 
> relocation. (This would be similar to how we add regular LTO inputs to the 
> combined summary in https://reviews.llvm.org/D33922.) Then LTO would run as 
> usual. Any regular object sections would then naturally participate in the 
> summary-based dead stripping that LTO already does.

It could be done but I'm skeptical about this from a cost/benefit perspective. 
We'd only save one additional read of the summaries, which is pretty cheap 
anyway. The GC logic in the linker is non-trivial and doesn't map particularly 
well to the combined summary (e.g. we'd have to deal with the liveness of 
groups of symbols rather than individual ones when symbols share an input 
section).

Thanks,
Tobias


https://reviews.llvm.org/D34156



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D34156: [LTO] Add -femit-summary-index flag to emit summary for regular LTO

2017-06-14 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk updated this revision to Diff 102585.
tobiasvk added a comment.

- Change patch to always emit a module summary for regular LTO

I don't see any real downside of having a summary given the marginal time and
space overhead it takes to construct and save it.


https://reviews.llvm.org/D34156

Files:
  clang/include/clang/Frontend/CodeGenOptions.def
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGen/emit-summary-index.c
  clang/test/Misc/thinlto.c

Index: clang/test/Misc/thinlto.c
===
--- clang/test/Misc/thinlto.c
+++ /dev/null
@@ -1,4 +0,0 @@
-// RUN: %clang_cc1 -flto=thin -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck %s
-// ; Check that the -flto=thin option emits a summary
-// CHECK: 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;
   }
Index: clang/lib/CodeGen/BackendUtil.cpp
===
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -486,7 +486,7 @@
 PMBuilder.Inliner = createFunctionInliningPass(
 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
 (!CodeGenOpts.SampleProfileFile.empty() &&
- CodeGenOpts.EmitSummaryIndex));
+ CodeGenOpts.PrepareForThinLTO));
   }
 
   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
@@ -497,7 +497,7 @@
 
   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
-  PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
+  PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
@@ -733,12 +733,20 @@
 
   std::unique_ptr ThinLinkOS;
 
+  // Emit a module summary by default for Regular LTO except for ld64 targets
+  bool EmitLTOSummary =
+  (CodeGenOpts.PrepareForLTO && !CodeGenOpts.PrepareForThinLTO &&
+   llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
+   llvm::Triple::Apple);
+  if (EmitLTOSummary)
+TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
+
   switch (Action) {
   case Backend_EmitNothing:
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 std::error_code EC;
 ThinLinkOS.reset(new llvm::raw_fd_ostream(
@@ -755,7 +763,8 @@
 }
 else
   PerModulePasses.add(
-  createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
+  createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
+  EmitLTOSummary));
 break;
 
   case Backend_EmitLL:
@@ -895,6 +904,14 @@
 }
   }
 
+  // Emit a module summary by default for Regular LTO except for ld64 targets
+  bool EmitLTOSummary =
+  (CodeGenOpts.PrepareForLTO && !CodeGenOpts.PrepareForThinLTO &&
+   llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
+   llvm::Triple::Apple);
+  if (EmitLTOSummary)
+TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
+
   // FIXME: We still use the legacy pass manager to do code generation. We
   // create that pass manager here and use it as needed below.
   legacy::PassManager CodeGenPasses;
@@ -907,7 +924,7 @@
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 std::error_code EC;
 ThinLinkOS.emplace(CodeGenOpts.ThinLinkBitcodeFile, EC,
@@ -922,8 +939,7 @@
   ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &*ThinLinkOS : nullptr));
 } else {
   MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
-CodeGenOpts.EmitSummaryIndex,
-CodeGenOpts.EmitSummaryIndex));
+EmitLTOSummary));
 }
 break;
 
Index: clang/include/clang/Frontend/CodeGenOptions.def
===
--- clang/include/clang/Frontend/CodeGenOptions.def
+++ clang/include/clang/Frontend/CodeGenOptions.def
@@ -88,7 +88,7 @@
  ///< 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

[PATCH] D34156: [LTO] Add -femit-summary-index flag to emit summary for regular LTO

2017-06-14 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk added a comment.

In https://reviews.llvm.org/D34156#780714, @mehdi_amini wrote:

> In https://reviews.llvm.org/D34156#780570, @tobiasvk wrote:
>
> > - Change patch to always emit a module summary for regular LTO
> >
> >   I don't see any real downside of having a summary given the marginal time 
> > and space overhead it takes to construct and save it.
>
>
> I think some code/logic needs to be changed in LLVM first:
>
>   bool LTOModule::isThinLTO() {
> // Right now the detection is only based on the summary presence. We may 
> want
> // to add a dedicated flag at some point.
>


No, that check still works. `hasGlobalValueSummary` returns false for regular 
LTO summaries (since they a have different block ID in the bitcode). I believe 
@pcc is refactoring these APIs in a separate patch, since the name 
`hasGlobalValueSummary` doesn't quite reflect its purpose anymore.


https://reviews.llvm.org/D34156



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D34156: [LTO] Enable module summary emission by default for regular LTO

2017-06-15 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk added a comment.

In https://reviews.llvm.org/D34156#781415, @pcc wrote:

> Please confirm that we can still self host with full LTO now that 
> https://reviews.llvm.org/D33922 has landed.


Good point. And the answer seems to be no :/

  ld.lld: .../llvm/lib/Linker/IRMover.cpp:242: llvm::Type *(anonymous 
namespace)::TypeMapTy::get(llvm::Type *, SmallPtrSet &): 
Assertion `!(Pair.first != Ty && Pair.second == Ty) && "mapping to a source 
type"' failed.

This is with `cmake 
-DCLANG_ENABLE_BOOTSTRAP=1-DBOOTSTRAP_LLVM_ENABLE_LLD=1-DBOOTSTRAP_LLVM_ENABLE_LTO=1
 (...) && ninja stage2`.


https://reviews.llvm.org/D34156



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D34156: [LTO] Enable module summary emission by default for regular LTO

2018-06-11 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk added a comment.

In https://reviews.llvm.org/D34156#1125489, @vlad.tsyrklevich wrote:

> Hi Tobias, I tracked down the failure self-hosting LLVM with LTO with this 
> revision to https://bugs.llvm.org/show_bug.cgi?id=37684#c2 and have a fix 
> under review in https://reviews.llvm.org/D47898.


Fantastic! That sounds exactly like the problem I was seeing back then. Thanks 
for tracking this down!

> Are you still interested in landing this?

Sure, I'll push an update to the patch.

Tobias


https://reviews.llvm.org/D34156



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D34156: [LTO] Enable module summary emission by default for regular LTO

2018-06-12 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk updated this revision to Diff 150965.
tobiasvk added a comment.

- Rebase for current tree
- Fix -flto -save-temps which the previous patch broke


https://reviews.llvm.org/D34156

Files:
  clang/include/clang/Frontend/CodeGenOptions.def
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGen/emit-summary-index.c
  clang/test/Misc/thinlto.c

Index: clang/test/Misc/thinlto.c
===
--- clang/test/Misc/thinlto.c
+++ /dev/null
@@ -1,4 +0,0 @@
-// RUN: %clang_cc1 -flto=thin -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck %s
-// ; Check that the -flto=thin option emits a summary
-// CHECK: 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;
   }
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -578,7 +578,7 @@
   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,
Index: clang/lib/CodeGen/BackendUtil.cpp
===
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -524,7 +524,7 @@
 PMBuilder.Inliner = createFunctionInliningPass(
 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
 (!CodeGenOpts.SampleProfileFile.empty() &&
- CodeGenOpts.EmitSummaryIndex));
+ CodeGenOpts.PrepareForThinLTO));
   }
 
   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
@@ -534,7 +534,7 @@
 
   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
-  PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
+  PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
@@ -771,12 +771,20 @@
 
   std::unique_ptr ThinLinkOS, DwoOS;
 
+  // Emit a module summary by default for Regular LTO except for ld64 targets
+  bool EmitLTOSummary =
+  (CodeGenOpts.PrepareForLTO && !CodeGenOpts.PrepareForThinLTO &&
+   llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
+   llvm::Triple::Apple);
+  if (EmitLTOSummary && !TheModule->getModuleFlag("ThinLTO"))
+TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
+
   switch (Action) {
   case Backend_EmitNothing:
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
 if (!ThinLinkOS)
@@ -787,7 +795,8 @@
 }
 else
   PerModulePasses.add(
-  createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
+  createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
+  EmitLTOSummary));
 break;
 
   case Backend_EmitLL:
@@ -935,7 +944,7 @@
   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
 
   if (!CodeGenOpts.DisableLLVMPasses) {
-bool IsThinLTO = CodeGenOpts.EmitSummaryIndex;
+bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
 bool IsLTO = CodeGenOpts.PrepareForLTO;
 
 if (CodeGenOpts.OptimizationLevel == 0) {
@@ -984,6 +993,14 @@
 }
   }
 
+  // Emit a module summary by default for Regular LTO except for ld64 targets
+  bool EmitLTOSummary =
+  (CodeGenOpts.PrepareForLTO && !CodeGenOpts.PrepareForThinLTO &&
+   llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
+   llvm::Triple::Apple);
+  if (EmitLTOSummary && !TheModule->getModuleFlag("ThinLTO"))
+TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
+
   // FIXME: We still use the legacy pass manager to do code generation. We
   // create that pass manager here and use it as needed below.
   legacy::PassManager CodeGenPasses;
@@ -996,7 +1013,7 @@
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
 if (!ThinLinkOS)
@@ -1006,8 +1023,7 @@
: nullptr));

[PATCH] D34156: [LTO] Enable module summary emission by default for regular LTO

2018-06-20 Thread Tobias Edler von Koch via Phabricator via cfe-commits
tobiasvk added a comment.

@pcc, @tejohnson: Are you OK with this going in now that 
https://reviews.llvm.org/D47898 has merged?


https://reviews.llvm.org/D34156



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D34156: [LTO] Enable module summary emission by default for regular LTO

2018-06-21 Thread Tobias Edler von Koch via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL335284: [LTO] Enable module summary emission by default for 
regular LTO (authored by tobiasvk, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D34156?vs=150965&id=152367#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D34156

Files:
  cfe/trunk/include/clang/Frontend/CodeGenOptions.def
  cfe/trunk/lib/CodeGen/BackendUtil.cpp
  cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
  cfe/trunk/lib/Frontend/CompilerInvocation.cpp
  cfe/trunk/test/CodeGen/emit-summary-index.c
  cfe/trunk/test/Misc/thinlto.c

Index: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
===
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp
@@ -749,11 +749,11 @@
   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;
   }
Index: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
===
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
@@ -578,7 +578,7 @@
   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,
Index: cfe/trunk/lib/CodeGen/BackendUtil.cpp
===
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp
@@ -524,7 +524,7 @@
 PMBuilder.Inliner = createFunctionInliningPass(
 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
 (!CodeGenOpts.SampleProfileFile.empty() &&
- CodeGenOpts.EmitSummaryIndex));
+ CodeGenOpts.PrepareForThinLTO));
   }
 
   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
@@ -534,7 +534,7 @@
 
   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
-  PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
+  PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
@@ -776,18 +776,28 @@
 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:
@@ -935,7 +945,7 @@
   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
 
   if (!CodeGenOpts.DisableLLVMPasses) {
-bool IsThinLTO = CodeGenOpts.EmitSummaryIndex;
+bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
 bool IsLTO = CodeGenOpts.PrepareForLTO;
 
 if (CodeGenOpts.OptimizationLevel == 0) {
@@ -996,18 +1006,26 @@
 break;
 
   case Backend_EmitBC:
-if (CodeGenOpts.EmitSummaryIndex) {
+if (CodeGenOpts.PrepareForThinLTO) {
   if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
 ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
 if (!ThinLinkOS)
   return;
   }
   MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
: nullptr));
 } else {
+  // Emit a m