Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-08-12 Thread Teresa Johnson via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL278541: CodeGen: Replace ThinLTO backend implementation with 
a client of LTO/Resolution. (authored by tejohnson).

Changed prior to commit:
  https://reviews.llvm.org/D21545?vs=64043=67868#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D21545

Files:
  cfe/trunk/lib/CodeGen/BackendUtil.cpp
  cfe/trunk/lib/CodeGen/CMakeLists.txt

Index: cfe/trunk/lib/CodeGen/BackendUtil.cpp
===
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp
@@ -29,9 +29,11 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
+#include "llvm/LTO/LTOBackend.h"
 #include "llvm/MC/SubtargetFeature.h"
 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/Timer.h"
@@ -74,8 +76,7 @@
   /// Set LLVM command line options passed through -backend-option.
   void setCommandLineOpts();
 
-  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager ,
-ModuleSummaryIndex *ModuleSummary);
+  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager );
 
   /// Generates the TargetMachine.
   /// Leaves TM unchanged if it is unable to create the target machine.
@@ -284,8 +285,7 @@
 }
 
 void EmitAssemblyHelper::CreatePasses(legacy::PassManager ,
-  legacy::FunctionPassManager ,
-  ModuleSummaryIndex *ModuleSummary) {
+  legacy::FunctionPassManager ) {
   if (CodeGenOpts.DisableLLVMPasses)
 return;
 
@@ -336,14 +336,6 @@
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
-  // If we are performing a ThinLTO importing compile, invoke the LTO
-  // pipeline and pass down the in-memory module summary index.
-  if (ModuleSummary) {
-PMBuilder.ModuleSummary = ModuleSummary;
-PMBuilder.populateThinLTOPassManager(MPM);
-return;
-  }
-
   // Add target-specific passes that need to run as early as possible.
   if (TM)
 PMBuilder.addExtension(
@@ -670,35 +662,15 @@
   if (TM)
 TheModule->setDataLayout(TM->createDataLayout());
 
-  // If we are performing a ThinLTO importing compile, load the function
-  // index into memory and pass it into CreatePasses, which will add it
-  // to the PassManagerBuilder and invoke LTO passes.
-  std::unique_ptr ModuleSummary;
-  if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
-ErrorOr IndexOrErr =
-llvm::getModuleSummaryIndexForFile(
-CodeGenOpts.ThinLTOIndexFile, [&](const DiagnosticInfo ) {
-  TheModule->getContext().diagnose(DI);
-});
-if (std::error_code EC = IndexOrErr.getError()) {
-  std::string Error = EC.message();
-  errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
- << "': " << Error << "\n";
-  return;
-}
-ModuleSummary = std::move(IndexOrErr.get());
-assert(ModuleSummary && "Expected non-empty module summary index");
-  }
-
   legacy::PassManager PerModulePasses;
   PerModulePasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
   legacy::FunctionPassManager PerFunctionPasses(TheModule);
   PerFunctionPasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
-  CreatePasses(PerModulePasses, PerFunctionPasses, ModuleSummary.get());
+  CreatePasses(PerModulePasses, PerFunctionPasses);
 
   legacy::PassManager CodeGenPasses;
   CodeGenPasses.add(
@@ -751,12 +723,71 @@
   }
 }
 
+static void runThinLTOBackend(const CodeGenOptions , Module *M,
+  std::unique_ptr OS) {
+  // If we are performing a ThinLTO importing compile, load the function index
+  // into memory and pass it into thinBackend, which will run the function
+  // importer and invoke LTO passes.
+  ErrorOr IndexOrErr =
+  llvm::getModuleSummaryIndexForFile(
+  CGOpts.ThinLTOIndexFile,
+  [&](const DiagnosticInfo ) { M->getContext().diagnose(DI); });
+  if (std::error_code EC = IndexOrErr.getError()) {
+std::string Error = EC.message();
+errs() << "Error loading index file '" << CGOpts.ThinLTOIndexFile
+   << "': " << Error << "\n";
+return;
+  }
+  std::unique_ptr CombinedIndex = std::move(*IndexOrErr);
+
+  auto AddStream = [&](size_t Task) { return std::move(OS); };
+
+  StringMap>
+  ModuleToDefinedGVSummaries;
+  CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+  // FIXME: We could simply import the modules mentioned in the combined index
+  // here.
+  

Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-14 Thread Mehdi AMINI via cfe-commits
mehdi_amini accepted this revision.
mehdi_amini added a comment.

(I didn't mark it as accepted because Teresa did, but in case you're waiting 
for me, don't)


https://reviews.llvm.org/D21545



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


Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-14 Thread Peter Collingbourne via cfe-commits
pcc marked an inline comment as done.
pcc added a comment.

https://reviews.llvm.org/D21545



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


Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-14 Thread Peter Collingbourne via cfe-commits
pcc updated this revision to Diff 64043.
pcc added a comment.

- Add a FIXME


https://reviews.llvm.org/D21545

Files:
  lib/CodeGen/BackendUtil.cpp
  lib/CodeGen/CMakeLists.txt

Index: lib/CodeGen/CMakeLists.txt
===
--- lib/CodeGen/CMakeLists.txt
+++ lib/CodeGen/CMakeLists.txt
@@ -8,6 +8,7 @@
   IRReader
   InstCombine
   Instrumentation
+  LTO
   Linker
   MC
   ObjCARCOpts
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -29,9 +29,11 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
+#include "llvm/LTO/LTOBackend.h"
 #include "llvm/MC/SubtargetFeature.h"
 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/Timer.h"
@@ -74,8 +76,7 @@
   /// Set LLVM command line options passed through -backend-option.
   void setCommandLineOpts();
 
-  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager ,
-ModuleSummaryIndex *ModuleSummary);
+  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager );
 
   /// Generates the TargetMachine.
   /// Leaves TM unchanged if it is unable to create the target machine.
@@ -281,8 +282,7 @@
 }
 
 void EmitAssemblyHelper::CreatePasses(legacy::PassManager ,
-  legacy::FunctionPassManager ,
-  ModuleSummaryIndex *ModuleSummary) {
+  legacy::FunctionPassManager ) {
   if (CodeGenOpts.DisableLLVMPasses)
 return;
 
@@ -333,14 +333,6 @@
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
-  // If we are performing a ThinLTO importing compile, invoke the LTO
-  // pipeline and pass down the in-memory module summary index.
-  if (ModuleSummary) {
-PMBuilder.ModuleSummary = ModuleSummary;
-PMBuilder.populateThinLTOPassManager(MPM);
-return;
-  }
-
   // Add target-specific passes that need to run as early as possible.
   if (TM)
 PMBuilder.addExtension(
@@ -659,35 +651,15 @@
   if (TM)
 TheModule->setDataLayout(TM->createDataLayout());
 
-  // If we are performing a ThinLTO importing compile, load the function
-  // index into memory and pass it into CreatePasses, which will add it
-  // to the PassManagerBuilder and invoke LTO passes.
-  std::unique_ptr ModuleSummary;
-  if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
-ErrorOr IndexOrErr =
-llvm::getModuleSummaryIndexForFile(
-CodeGenOpts.ThinLTOIndexFile, [&](const DiagnosticInfo ) {
-  TheModule->getContext().diagnose(DI);
-});
-if (std::error_code EC = IndexOrErr.getError()) {
-  std::string Error = EC.message();
-  errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
- << "': " << Error << "\n";
-  return;
-}
-ModuleSummary = std::move(IndexOrErr.get());
-assert(ModuleSummary && "Expected non-empty module summary index");
-  }
-
   legacy::PassManager PerModulePasses;
   PerModulePasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
   legacy::FunctionPassManager PerFunctionPasses(TheModule);
   PerFunctionPasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
-  CreatePasses(PerModulePasses, PerFunctionPasses, ModuleSummary.get());
+  CreatePasses(PerModulePasses, PerFunctionPasses);
 
   legacy::PassManager CodeGenPasses;
   CodeGenPasses.add(
@@ -740,12 +712,71 @@
   }
 }
 
+static void runThinLTOBackend(const CodeGenOptions , Module *M,
+  std::unique_ptr OS) {
+  // If we are performing a ThinLTO importing compile, load the function index
+  // into memory and pass it into thinBackend, which will run the function
+  // importer and invoke LTO passes.
+  ErrorOr IndexOrErr =
+  llvm::getModuleSummaryIndexForFile(
+  CGOpts.ThinLTOIndexFile,
+  [&](const DiagnosticInfo ) { M->getContext().diagnose(DI); });
+  if (std::error_code EC = IndexOrErr.getError()) {
+std::string Error = EC.message();
+errs() << "Error loading index file '" << CGOpts.ThinLTOIndexFile
+   << "': " << Error << "\n";
+return;
+  }
+  std::unique_ptr CombinedIndex = std::move(*IndexOrErr);
+
+  auto AddStream = [&](size_t Task) { return std::move(OS); };
+
+  StringMap>
+  ModuleToDefinedGVSummaries;
+  CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+  // FIXME: We could simply import the modules mentioned in the combined index
+  // here.
+  

Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-14 Thread Teresa Johnson via cfe-commits
tejohnson added inline comments.


Comment at: lib/CodeGen/BackendUtil.cpp:740
@@ +739,3 @@
+  ComputeCrossModuleImportForModule(M->getModuleIdentifier(), *CombinedIndex,
+ImportList);
+

mehdi_amini wrote:
> This should go away at some point right?
> Just to double check if my memory is correct: the linker plugin will emit the 
> import decision and the backend will take it as an input instead of 
> recomputing anything?
The way they are passed down is via the individual combined index. So it simply 
computes via what is in the index, which will only include summaries for what 
should be imported. I suppose we could make the logic even simpler tho and just 
blindly import what is in the index. 

Peter, can you add a fixme? 


https://reviews.llvm.org/D21545



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


Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-14 Thread Mehdi AMINI via cfe-commits
mehdi_amini added inline comments.


Comment at: lib/CodeGen/BackendUtil.cpp:740
@@ +739,3 @@
+  ComputeCrossModuleImportForModule(M->getModuleIdentifier(), *CombinedIndex,
+ImportList);
+

This should go away at some point right?
Just to double check if my memory is correct: the linker plugin will emit the 
import decision and the backend will take it as an input instead of recomputing 
anything?


https://reviews.llvm.org/D21545



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


Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-13 Thread Peter Collingbourne via cfe-commits
pcc updated this revision to Diff 63908.
pcc added a comment.

Refresh


http://reviews.llvm.org/D21545

Files:
  lib/CodeGen/BackendUtil.cpp
  lib/CodeGen/CMakeLists.txt

Index: lib/CodeGen/CMakeLists.txt
===
--- lib/CodeGen/CMakeLists.txt
+++ lib/CodeGen/CMakeLists.txt
@@ -8,6 +8,7 @@
   IRReader
   InstCombine
   Instrumentation
+  LTO
   Linker
   MC
   ObjCARCOpts
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -29,9 +29,11 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
+#include "llvm/LTO/LTOBackend.h"
 #include "llvm/MC/SubtargetFeature.h"
 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/Timer.h"
@@ -74,8 +76,7 @@
   /// Set LLVM command line options passed through -backend-option.
   void setCommandLineOpts();
 
-  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager ,
-ModuleSummaryIndex *ModuleSummary);
+  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager );
 
   /// Generates the TargetMachine.
   /// Leaves TM unchanged if it is unable to create the target machine.
@@ -281,8 +282,7 @@
 }
 
 void EmitAssemblyHelper::CreatePasses(legacy::PassManager ,
-  legacy::FunctionPassManager ,
-  ModuleSummaryIndex *ModuleSummary) {
+  legacy::FunctionPassManager ) {
   if (CodeGenOpts.DisableLLVMPasses)
 return;
 
@@ -333,14 +333,6 @@
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
-  // If we are performing a ThinLTO importing compile, invoke the LTO
-  // pipeline and pass down the in-memory module summary index.
-  if (ModuleSummary) {
-PMBuilder.ModuleSummary = ModuleSummary;
-PMBuilder.populateThinLTOPassManager(MPM);
-return;
-  }
-
   // Add target-specific passes that need to run as early as possible.
   if (TM)
 PMBuilder.addExtension(
@@ -659,35 +651,15 @@
   if (TM)
 TheModule->setDataLayout(TM->createDataLayout());
 
-  // If we are performing a ThinLTO importing compile, load the function
-  // index into memory and pass it into CreatePasses, which will add it
-  // to the PassManagerBuilder and invoke LTO passes.
-  std::unique_ptr ModuleSummary;
-  if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
-ErrorOr IndexOrErr =
-llvm::getModuleSummaryIndexForFile(
-CodeGenOpts.ThinLTOIndexFile, [&](const DiagnosticInfo ) {
-  TheModule->getContext().diagnose(DI);
-});
-if (std::error_code EC = IndexOrErr.getError()) {
-  std::string Error = EC.message();
-  errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
- << "': " << Error << "\n";
-  return;
-}
-ModuleSummary = std::move(IndexOrErr.get());
-assert(ModuleSummary && "Expected non-empty module summary index");
-  }
-
   legacy::PassManager PerModulePasses;
   PerModulePasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
   legacy::FunctionPassManager PerFunctionPasses(TheModule);
   PerFunctionPasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
-  CreatePasses(PerModulePasses, PerFunctionPasses, ModuleSummary.get());
+  CreatePasses(PerModulePasses, PerFunctionPasses);
 
   legacy::PassManager CodeGenPasses;
   CodeGenPasses.add(
@@ -740,12 +712,69 @@
   }
 }
 
+static void runThinLTOBackend(const CodeGenOptions , Module *M,
+  std::unique_ptr OS) {
+  // If we are performing a ThinLTO importing compile, load the function index
+  // into memory and pass it into thinBackend, which will run the function
+  // importer and invoke LTO passes.
+  ErrorOr IndexOrErr =
+  llvm::getModuleSummaryIndexForFile(
+  CGOpts.ThinLTOIndexFile,
+  [&](const DiagnosticInfo ) { M->getContext().diagnose(DI); });
+  if (std::error_code EC = IndexOrErr.getError()) {
+std::string Error = EC.message();
+errs() << "Error loading index file '" << CGOpts.ThinLTOIndexFile
+   << "': " << Error << "\n";
+return;
+  }
+  std::unique_ptr CombinedIndex = std::move(*IndexOrErr);
+
+  auto AddStream = [&](size_t Task) { return std::move(OS); };
+
+  StringMap>
+  ModuleToDefinedGVSummaries;
+  CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+  FunctionImporter::ImportMapTy ImportList;
+  ComputeCrossModuleImportForModule(M->getModuleIdentifier(), *CombinedIndex,

Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-07-01 Thread Teresa Johnson via cfe-commits
tejohnson added inline comments.


Comment at: lib/CodeGen/BackendUtil.cpp:758
@@ +757,3 @@
+
+  lto::Config Conf;
+  if (Error E = thinBackend(

Can we pass along -save-temps here (i.e. invoke Conf.addSaveTemps)?


http://reviews.llvm.org/D21545



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


Re: [PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-06-28 Thread Peter Collingbourne via cfe-commits
pcc updated this revision to Diff 62160.
pcc marked 2 inline comments as done.
pcc added a comment.

- Use ComputeCrossModuleImportForModule


http://reviews.llvm.org/D21545

Files:
  lib/CodeGen/BackendUtil.cpp
  lib/CodeGen/CMakeLists.txt

Index: lib/CodeGen/CMakeLists.txt
===
--- lib/CodeGen/CMakeLists.txt
+++ lib/CodeGen/CMakeLists.txt
@@ -8,6 +8,7 @@
   IRReader
   InstCombine
   Instrumentation
+  LTOResolution
   Linker
   MC
   ObjCARCOpts
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -29,9 +29,11 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
+#include "llvm/LTO/Resolution/LTOBackend.h"
 #include "llvm/MC/SubtargetFeature.h"
 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/Timer.h"
@@ -74,8 +76,7 @@
   /// Set LLVM command line options passed through -backend-option.
   void setCommandLineOpts();
 
-  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager ,
-ModuleSummaryIndex *ModuleSummary);
+  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager );
 
   /// Generates the TargetMachine.
   /// Leaves TM unchanged if it is unable to create the target machine.
@@ -281,8 +282,7 @@
 }
 
 void EmitAssemblyHelper::CreatePasses(legacy::PassManager ,
-  legacy::FunctionPassManager ,
-  ModuleSummaryIndex *ModuleSummary) {
+  legacy::FunctionPassManager ) {
   if (CodeGenOpts.DisableLLVMPasses)
 return;
 
@@ -334,14 +334,6 @@
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
-  // If we are performing a ThinLTO importing compile, invoke the LTO
-  // pipeline and pass down the in-memory module summary index.
-  if (ModuleSummary) {
-PMBuilder.ModuleSummary = ModuleSummary;
-PMBuilder.populateThinLTOPassManager(MPM);
-return;
-  }
-
   // Add target-specific passes that need to run as early as possible.
   if (TM)
 PMBuilder.addExtension(
@@ -660,35 +652,15 @@
   if (TM)
 TheModule->setDataLayout(TM->createDataLayout());
 
-  // If we are performing a ThinLTO importing compile, load the function
-  // index into memory and pass it into CreatePasses, which will add it
-  // to the PassManagerBuilder and invoke LTO passes.
-  std::unique_ptr ModuleSummary;
-  if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
-ErrorOr IndexOrErr =
-llvm::getModuleSummaryIndexForFile(
-CodeGenOpts.ThinLTOIndexFile, [&](const DiagnosticInfo ) {
-  TheModule->getContext().diagnose(DI);
-});
-if (std::error_code EC = IndexOrErr.getError()) {
-  std::string Error = EC.message();
-  errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
- << "': " << Error << "\n";
-  return;
-}
-ModuleSummary = std::move(IndexOrErr.get());
-assert(ModuleSummary && "Expected non-empty module summary index");
-  }
-
   legacy::PassManager PerModulePasses;
   PerModulePasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
   legacy::FunctionPassManager PerFunctionPasses(TheModule);
   PerFunctionPasses.add(
   createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
 
-  CreatePasses(PerModulePasses, PerFunctionPasses, ModuleSummary.get());
+  CreatePasses(PerModulePasses, PerFunctionPasses);
 
   legacy::PassManager CodeGenPasses;
   CodeGenPasses.add(
@@ -741,12 +713,69 @@
   }
 }
 
+static void runThinLTOBackend(const CodeGenOptions , Module *M,
+  std::unique_ptr OS) {
+  // If we are performing a ThinLTO importing compile, load the function index
+  // into memory and pass it into thinBackend, which will run the function
+  // importer and invoke LTO passes.
+  ErrorOr IndexOrErr =
+  llvm::getModuleSummaryIndexForFile(
+  CGOpts.ThinLTOIndexFile,
+  [&](const DiagnosticInfo ) { M->getContext().diagnose(DI); });
+  if (std::error_code EC = IndexOrErr.getError()) {
+std::string Error = EC.message();
+errs() << "Error loading index file '" << CGOpts.ThinLTOIndexFile
+   << "': " << Error << "\n";
+return;
+  }
+  std::unique_ptr CombinedIndex = std::move(*IndexOrErr);
+
+  auto AddStream = [&](size_t Task) { return std::move(OS); };
+
+  StringMap>
+  ModuleToDefinedGVSummaries;
+  CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+  FunctionImporter::ImportMapTy 

[PATCH] D21545: CodeGen: Replace ThinLTO backend implementation with a client of LTO/Resolution.

2016-06-20 Thread Peter Collingbourne via cfe-commits
pcc created this revision.
pcc added reviewers: tejohnson, mehdi_amini.
pcc added a subscriber: cfe-commits.
pcc added dependencies: D21542: CodeGen: Replace test/CodeGen/thinlto_backend.c 
with a functional test., D20268: [wip] Resolution-based LTO API., D21537: 
Frontend: Simplify ownership model for clang's output streams..
Herald added a subscriber: mehdi_amini.

This changes clang to use the llvm::lto::thinBackend function instead of
its own less comprehensive ThinLTO backend implementation.

Depends on D20268

Depends on D21537

Depends on D21542

http://reviews.llvm.org/D21545

Files:
  lib/CodeGen/BackendUtil.cpp
  lib/CodeGen/CMakeLists.txt
  lib/Driver/Tools.cpp
  test/Driver/thinlto_backend.c

Index: test/Driver/thinlto_backend.c
===
--- test/Driver/thinlto_backend.c
+++ test/Driver/thinlto_backend.c
@@ -6,5 +6,9 @@
 // CHECK-THINLTOBE-ACTION: -fthinlto-index=
 
 // Ensure clang driver gives the expected error for incorrect input type
-// RUN: not %clang -O2 -o %t1.o %s -c -fthinlto-index=%t.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-WARNING
-// CHECK-WARNING: error: invalid argument '-fthinlto-index={{.*}}' only allowed with '-x ir'
+// RUN: not %clang -O2 -o %t1.o %s -c -fthinlto-index=%t.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-ERR1
+// CHECK-ERR1: error: invalid argument '-fthinlto-index={{.*}}' only allowed with '-x ir'
+
+// And for incorrect output type
+// RUN: not %clang -O2 -o %t1.o -x ir %s -S -fthinlto-index=%t.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-ERR2
+// CHECK-ERR2: error: invalid argument '-fthinlto-index={{.*}}' only allowed with '-c'
Index: lib/Driver/Tools.cpp
===
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -3891,6 +3891,9 @@
 if (!types::isLLVMIR(Input.getType()))
   D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
<< "-x ir";
+if (!isa(JA))
+  D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
+   << "-c";
 Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
   }
 
Index: lib/CodeGen/CMakeLists.txt
===
--- lib/CodeGen/CMakeLists.txt
+++ lib/CodeGen/CMakeLists.txt
@@ -8,6 +8,7 @@
   IRReader
   InstCombine
   Instrumentation
+  LTOResolution
   Linker
   MC
   ObjCARCOpts
Index: lib/CodeGen/BackendUtil.cpp
===
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -29,9 +29,11 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
+#include "llvm/LTO/Resolution/LTOBackend.h"
 #include "llvm/MC/SubtargetFeature.h"
 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/Timer.h"
@@ -74,8 +76,7 @@
   /// Set LLVM command line options passed through -backend-option.
   void setCommandLineOpts();
 
-  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager ,
-ModuleSummaryIndex *ModuleSummary);
+  void CreatePasses(legacy::PassManager , legacy::FunctionPassManager );
 
   /// Generates the TargetMachine.
   /// Leaves TM unchanged if it is unable to create the target machine.
@@ -275,8 +276,7 @@
 }
 
 void EmitAssemblyHelper::CreatePasses(legacy::PassManager ,
-  legacy::FunctionPassManager ,
-  ModuleSummaryIndex *ModuleSummary) {
+  legacy::FunctionPassManager ) {
   if (CodeGenOpts.DisableLLVMPasses)
 return;
 
@@ -327,14 +327,6 @@
   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
 
-  // If we are performing a ThinLTO importing compile, invoke the LTO
-  // pipeline and pass down the in-memory module summary index.
-  if (ModuleSummary) {
-PMBuilder.ModuleSummary = ModuleSummary;
-PMBuilder.populateThinLTOPassManager(MPM);
-return;
-  }
-
   // Add target-specific passes that need to run as early as possible.
   if (TM)
 PMBuilder.addExtension(
@@ -652,35 +644,15 @@
   if (TM)
 TheModule->setDataLayout(TM->createDataLayout());
 
-  // If we are performing a ThinLTO importing compile, load the function
-  // index into memory and pass it into CreatePasses, which will add it
-  // to the PassManagerBuilder and invoke LTO passes.
-  std::unique_ptr ModuleSummary;
-  if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
-ErrorOr IndexOrErr =
-llvm::getModuleSummaryIndexForFile(
-CodeGenOpts.ThinLTOIndexFile, [&](const