[llvm] 7db390c - Revert "[LTO] Use lto::backend for code generation."

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 3 14:56:42 PST 2021


Author: Florian Hahn
Date: 2021-02-03T22:49:30Z
New Revision: 7db390cc7738a9ba0ed7d4ca59ab6ea2e69c47e9

URL: https://github.com/llvm/llvm-project/commit/7db390cc7738a9ba0ed7d4ca59ab6ea2e69c47e9
DIFF: https://github.com/llvm/llvm-project/commit/7db390cc7738a9ba0ed7d4ca59ab6ea2e69c47e9.diff

LOG: Revert "[LTO] Use lto::backend for code generation."

This reverts commit 6a59f0560648b43324b5aed51b9ef996404a25e0, because
it is causing failures on green dragon.

Added: 
    

Modified: 
    llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
    llvm/lib/LTO/LTOCodeGenerator.cpp
    llvm/test/LTO/X86/disable-verify.ll
    llvm/test/tools/lto/print-stats.ll
    llvm/tools/llvm-lto/llvm-lto.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
index b57c7daf758a..fc7b8fc25bd9 100644
--- a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
+++ b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
@@ -41,8 +41,6 @@
 #include "llvm/ADT/StringSet.h"
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/IR/Module.h"
-#include "llvm/LTO/Config.h"
-#include "llvm/LTO/LTO.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ToolOutputFile.h"
@@ -75,10 +73,6 @@ struct LTOCodeGenerator {
   LTOCodeGenerator(LLVMContext &Context);
   ~LTOCodeGenerator();
 
-  /// Return a lto::Config object which contains the options set in
-  /// LTOCodeGenerator.
-  lto::Config toConfig() const;
-
   /// Merge given module.  Return true on success.
   ///
   /// Resets \a HasVerifiedInput.
@@ -171,14 +165,14 @@ struct LTOCodeGenerator {
   /// if the compilation was not successful.
   std::unique_ptr<MemoryBuffer> compileOptimized();
 
-  /// Compile the merged optimized module \p ParallelismLevel output files each
+  /// Compile the merged optimized module into out.size() output files each
   /// representing a linkable partition of the module. If out contains more
-  /// than one element, code generation is done in parallel with \p
-  /// ParallelismLevel threads.  Output files will be written to the streams
-  /// created using the \p AddStream callback. Returns true on success.
+  /// than one element, code generation is done in parallel with out.size()
+  /// threads.  Output files will be written to members of out. Returns true on
+  /// success.
   ///
   /// Calls \a verifyMergedModuleOnce().
-  bool compileOptimized(lto::AddStreamFn AddStream, unsigned ParallelismLevel);
+  bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out);
 
   /// Enable the Freestanding mode: indicate that the optimizer should not
   /// assume builtins are present on the target.
@@ -194,6 +188,8 @@ struct LTOCodeGenerator {
   void DiagnosticHandler(const DiagnosticInfo &DI);
 
 private:
+  void initializeLTOPasses();
+
   /// Verify the merged module on first call.
   ///
   /// Sets \a HasVerifiedInput on first call and doesn't run again on the same

diff  --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 180d906f506c..28cb66899469 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -37,7 +37,6 @@
 #include "llvm/IR/Verifier.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/LTO/LTO.h"
-#include "llvm/LTO/LTOBackend.h"
 #include "llvm/LTO/legacy/LTOModule.h"
 #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
 #include "llvm/Linker/Linker.h"
@@ -124,29 +123,41 @@ LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
       TheLinker(new Linker(*MergedModule)) {
   Context.setDiscardValueNames(LTODiscardValueNames);
   Context.enableDebugTypeODRUniquing();
+  initializeLTOPasses();
 }
 
 LTOCodeGenerator::~LTOCodeGenerator() {}
 
-lto::Config LTOCodeGenerator::toConfig() const {
-  lto::Config Conf;
-  Conf.CGFileType = FileType;
-  Conf.CPU = MCpu;
-  Conf.MAttrs = MAttrs;
-  Conf.RelocModel = RelocModel;
-  Conf.Options = Options;
-  Conf.CodeModel = None;
-  Conf.StatsFile = LTOStatsFile;
-  Conf.OptLevel = OptLevel;
-  Conf.Freestanding = Freestanding;
-  Conf.PTO.LoopVectorization = OptLevel > 1;
-  Conf.PTO.SLPVectorization = OptLevel > 1;
-  Conf.DisableVerify = DisableVerify;
-  Conf.PreCodeGenPassesHook = [](legacy::PassManager &PM) {
-    PM.add(createObjCARCContractPass());
-  };
-
-  return Conf;
+// Initialize LTO passes. Please keep this function in sync with
+// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
+// passes are initialized.
+void LTOCodeGenerator::initializeLTOPasses() {
+  PassRegistry &R = *PassRegistry::getPassRegistry();
+
+  initializeInternalizeLegacyPassPass(R);
+  initializeIPSCCPLegacyPassPass(R);
+  initializeGlobalOptLegacyPassPass(R);
+  initializeConstantMergeLegacyPassPass(R);
+  initializeDAHPass(R);
+  initializeInstructionCombiningPassPass(R);
+  initializeSimpleInlinerPass(R);
+  initializePruneEHPass(R);
+  initializeGlobalDCELegacyPassPass(R);
+  initializeOpenMPOptLegacyPassPass(R);
+  initializeArgPromotionPass(R);
+  initializeJumpThreadingPass(R);
+  initializeSROALegacyPassPass(R);
+  initializeAttributorLegacyPassPass(R);
+  initializeAttributorCGSCCLegacyPassPass(R);
+  initializePostOrderFunctionAttrsLegacyPassPass(R);
+  initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
+  initializeGlobalsAAWrapperPassPass(R);
+  initializeLegacyLICMPassPass(R);
+  initializeMergedLoadStoreMotionLegacyPassPass(R);
+  initializeGVNLegacyPassPass(R);
+  initializeMemCpyOptLegacyPassPass(R);
+  initializeDCELegacyPassPass(R);
+  initializeCFGSimplifyPassPass(R);
 }
 
 void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
@@ -257,35 +268,38 @@ bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
 bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
   // make unique temp output file to put generated code
   SmallString<128> Filename;
+  int FD;
 
-  auto AddStream =
-      [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
-    StringRef Extension(FileType == CGFT_AssemblyFile ? "s" : "o");
+  StringRef Extension
+      (FileType == CGFT_AssemblyFile ? "s" : "o");
 
-    int FD;
-    std::error_code EC =
-        sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
-    if (EC)
-      emitError(EC.message());
+  std::error_code EC =
+      sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
+  if (EC) {
+    emitError(EC.message());
+    return false;
+  }
 
-    return std::make_unique<lto::NativeObjectStream>(
-        std::make_unique<llvm::raw_fd_ostream>(FD, true));
-  };
+  // generate object file
+  ToolOutputFile objFile(Filename, FD);
 
-  bool genResult = compileOptimized(AddStream, 1);
+  bool genResult = compileOptimized(&objFile.os());
+  objFile.os().close();
+  if (objFile.os().has_error()) {
+    emitError((Twine("could not write object file: ") + Filename + ": " +
+               objFile.os().error().message())
+                  .str());
+    objFile.os().clear_error();
+    sys::fs::remove(Twine(Filename));
+    return false;
+  }
 
+  objFile.keep();
   if (!genResult) {
     sys::fs::remove(Twine(Filename));
     return false;
   }
 
-  // If statistics were requested, save them to the specified file or
-  // print them out after codegen.
-  if (StatsFile)
-    PrintStatisticsJSON(StatsFile->os());
-  else if (AreStatisticsEnabled())
-    PrintStatistics();
-
   NativeObjectPath = Filename.c_str();
   *Name = NativeObjectPath.c_str();
   return true;
@@ -554,25 +568,36 @@ bool LTOCodeGenerator::optimize() {
   // Write LTOPostLink flag for passes that require all the modules.
   MergedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
 
+  // Instantiate the pass manager to organize the passes.
+  legacy::PassManager passes;
+
   // Add an appropriate DataLayout instance for this module...
   MergedModule->setDataLayout(TargetMach->createDataLayout());
 
-  lto::Config Conf = toConfig();
+  passes.add(
+      createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
 
-  ModuleSummaryIndex CombinedIndex(false);
-  TargetMach = createTargetMachine();
-  if (!opt(Conf, TargetMach.get(), 0, *MergedModule, /*IsThinLTO=*/false,
-           /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
-           /*CmdArgs*/ std::vector<uint8_t>())) {
-    emitError("LTO middle-end optimizations failed");
-    return false;
-  }
+  Triple TargetTriple(TargetMach->getTargetTriple());
+  PassManagerBuilder PMB;
+  PMB.LoopVectorize = true;
+  PMB.SLPVectorize = true;
+  PMB.Inliner = createFunctionInliningPass();
+  PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
+  if (Freestanding)
+    PMB.LibraryInfo->disableAllFunctions();
+  PMB.OptLevel = OptLevel;
+  PMB.VerifyInput = !DisableVerify;
+  PMB.VerifyOutput = !DisableVerify;
+
+  PMB.populateLTOPassManager(passes);
+
+  // Run our queue of passes all at once now, efficiently.
+  passes.run(*MergedModule);
 
   return true;
 }
 
-bool LTOCodeGenerator::compileOptimized(lto::AddStreamFn AddStream,
-                                        unsigned ParallelismLevel) {
+bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
   if (!this->determineTarget())
     return false;
 
@@ -580,17 +605,20 @@ bool LTOCodeGenerator::compileOptimized(lto::AddStreamFn AddStream,
   // been called in optimize(), this call will return early.
   verifyMergedModuleOnce();
 
+  legacy::PassManager preCodeGenPasses;
+
+  // If the bitcode files contain ARC code and were compiled with optimization,
+  // the ObjCARCContractPass must be run, so do it unconditionally here.
+  preCodeGenPasses.add(createObjCARCContractPass());
+  preCodeGenPasses.run(*MergedModule);
+
   // Re-externalize globals that may have been internalized to increase scope
   // for splitting
   restoreLinkageForExternals();
 
-  lto::Config Conf = toConfig();
-  ModuleSummaryIndex CombinedIndex(false);
-
-  Error Err =
-      backend(Conf, AddStream, ParallelismLevel, *MergedModule, CombinedIndex);
-  assert(!Err && "unexpected code-generation failure");
-  (void)Err;
+  splitCodeGen(
+      *MergedModule, Out, {}, [&]() { return createTargetMachine(); }, FileType,
+      ShouldRestoreGlobalsLinkage);
 
   // If statistics were requested, save them to the specified file or
   // print them out after codegen.

diff  --git a/llvm/test/LTO/X86/disable-verify.ll b/llvm/test/LTO/X86/disable-verify.ll
index 0d507e67305e..68c2c9dd2127 100644
--- a/llvm/test/LTO/X86/disable-verify.ll
+++ b/llvm/test/LTO/X86/disable-verify.ll
@@ -7,7 +7,7 @@ target triple = "x86_64-apple-macosx10.10.0"
 
 ; -disable-verify should disable verification from the optimization pipeline.
 ; CHECK: Pass Arguments:
-; CHECK-NOT: -verify {{.*}} -verify
+; CHECK-NOT: -verify
 
 ; VERIFY: Pass Arguments: {{.*}} -verify {{.*}} -verify
 

diff  --git a/llvm/test/tools/lto/print-stats.ll b/llvm/test/tools/lto/print-stats.ll
index 22b6171c1974..125d7d051499 100644
--- a/llvm/test/tools/lto/print-stats.ll
+++ b/llvm/test/tools/lto/print-stats.ll
@@ -5,12 +5,5 @@
 
 target triple = "x86_64-apple-macosx10.8.0"
 
-define i32 @test(i32 %a) {
-  %r = add i32 %a, 1
-  %r.1 = add i32 1, %a
-  %r.2 = add i32 %r, %r.1
-  ret i32 %r.2
-}
-
 ; STATS: Statistics Collected
 ; NO_STATS-NOT: Statistics Collected

diff  --git a/llvm/tools/llvm-lto/llvm-lto.cpp b/llvm/tools/llvm-lto/llvm-lto.cpp
index e7b09a44d9c0..1745b7449714 100644
--- a/llvm/tools/llvm-lto/llvm-lto.cpp
+++ b/llvm/tools/llvm-lto/llvm-lto.cpp
@@ -1041,24 +1041,25 @@ int main(int argc, char **argv) {
         error("writing merged module failed.");
     }
 
-    auto AddStream =
-        [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
+    std::list<ToolOutputFile> OSs;
+    std::vector<raw_pwrite_stream *> OSPtrs;
+    for (unsigned I = 0; I != Parallelism; ++I) {
       std::string PartFilename = OutputFilename;
       if (Parallelism != 1)
-        PartFilename += "." + utostr(Task);
-
+        PartFilename += "." + utostr(I);
       std::error_code EC;
-      auto S =
-          std::make_unique<raw_fd_ostream>(PartFilename, EC, sys::fs::OF_None);
+      OSs.emplace_back(PartFilename, EC, sys::fs::OF_None);
       if (EC)
         error("error opening the file '" + PartFilename + "': " + EC.message());
-      return std::make_unique<lto::NativeObjectStream>(std::move(S));
-    };
+      OSPtrs.push_back(&OSs.back().os());
+    }
 
-    if (!CodeGen.compileOptimized(AddStream, Parallelism))
+    if (!CodeGen.compileOptimized(OSPtrs))
       // Diagnostic messages should have been printed by the handler.
       error("error compiling the code");
 
+    for (ToolOutputFile &OS : OSs)
+      OS.keep();
   } else {
     if (Parallelism != 1)
       error("-j must be specified together with -o");


        


More information about the llvm-commits mailing list