From 72f4a4c810aeda0b029f33d04f7d906d39c2e4f9 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Thu, 11 Feb 2016 22:00:31 +0000 Subject: [PATCH] Define the ThinLTO Pipeline Summary: On the contrary to Full LTO, ThinLTO can afford to shift compile time from the frontend to the linker: both phases are parallel. This pipeline is based on the proposal in D13443 for full LTO. We ] didn't move forward on this proposal because the link was far too long after that. This patch refactor the "function simplification" passes that are part of the inliner loop in a helper function (this part is NFC and can be commited separately to simplify the diff). The ThinLTO pipeline integrates in the regular O2/O3 flow: - The compile phase perform the inliner with a somehow lighter function simplification. (TODO: tune the inliner thresholds here) This is intendend to simplify the IR and get rid of obvious things like linkonce_odr that will be inlined. - The link phase will run the pipeline from the start, extended with some specific passes that leverage the augmented knowledge we have during LTO. Especially after the inliner is done, a sequence of globalDCE/globalOpt is performed, followed by another run of the "function simplification" passes. The measurements on the public test suite as well as on our internal suite show an overall net improvement. The binary size for the clang executable is reduced by 5%. We're still tuning it with the bringup of ThinLTO but this should provide a good starting point. Reviewers: tejohnson Subscribers: joker.eph, llvm-commits, dexonsmith Differential Revision: http://reviews.llvm.org/D17115 From: Mehdi Amini llvm-svn: 260604 --- .../llvm/Transforms/IPO/PassManagerBuilder.h | 3 ++ lib/Transforms/IPO/PassManagerBuilder.cpp | 44 ++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/include/llvm/Transforms/IPO/PassManagerBuilder.h b/include/llvm/Transforms/IPO/PassManagerBuilder.h index 2f96a58f88b..035343945e6 100644 --- a/include/llvm/Transforms/IPO/PassManagerBuilder.h +++ b/include/llvm/Transforms/IPO/PassManagerBuilder.h @@ -133,6 +133,8 @@ public: bool VerifyOutput; bool MergeFunctions; bool PrepareForLTO; + bool PrepareForThinLTO; + bool PerformThinLTO; /// Profile data file name that the instrumentation will be written to. std::string PGOInstrGen; @@ -170,6 +172,7 @@ public: /// populateModulePassManager - This sets up the primary pass manager. void populateModulePassManager(legacy::PassManagerBase &MPM); void populateLTOPassManager(legacy::PassManagerBase &PM); + void populateThinLTOPassManager(legacy::PassManagerBase &PM); }; /// Registers a function for adding a standard set of passes. This should be diff --git a/lib/Transforms/IPO/PassManagerBuilder.cpp b/lib/Transforms/IPO/PassManagerBuilder.cpp index 46a0d1d15a4..5bb82ef8b9c 100644 --- a/lib/Transforms/IPO/PassManagerBuilder.cpp +++ b/lib/Transforms/IPO/PassManagerBuilder.cpp @@ -140,6 +140,8 @@ PassManagerBuilder::PassManagerBuilder() { PrepareForLTO = false; PGOInstrGen = RunPGOInstrGen; PGOInstrUse = RunPGOInstrUse; + PrepareForThinLTO = false; + PerformThinLTO = false; } PassManagerBuilder::~PassManagerBuilder() { @@ -233,6 +235,11 @@ void PassManagerBuilder::addFunctionSimplificationPasses( MPM.add(createTailCallEliminationPass()); // Eliminate tail calls MPM.add(createCFGSimplificationPass()); // Merge & remove BBs MPM.add(createReassociatePass()); // Reassociate expressions + if (PrepareForThinLTO) { + MPM.add(createAggressiveDCEPass()); // Delete dead instructions + MPM.add(createInstructionCombiningPass()); // Combine silly seq's + return; + } // Rotate Loop - disable header duplication at -Oz MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); MPM.add(createLICMPass()); // Hoist loop invariants @@ -346,6 +353,12 @@ void PassManagerBuilder::populateModulePassManager( MPM.add(createIPSCCPPass()); // IP SCCP MPM.add(createGlobalOptimizerPass()); // Optimize out global vars + + if (PerformThinLTO) + // Linking modules together can lead to duplicated global constants, only + // keep one copy of each constant. + MPM.add(createConstantMergePass()); + // Promote any localized global vars MPM.add(createPromoteMemoryToRegisterPass()); @@ -378,6 +391,12 @@ void PassManagerBuilder::populateModulePassManager( addFunctionSimplificationPasses(MPM); + // If we are planning to perform ThinLTO later, let's not bloat the code with + // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes + // during ThinLTO and performs the rest of the optimizations afterward. + if (PrepareForThinLTO) + return; + // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC // pass manager that we are specifically trying to avoid. To prevent this // we must insert a no-op module pass to reset the pass manager. @@ -396,7 +415,7 @@ void PassManagerBuilder::populateModulePassManager( if (!DisableUnitAtATime) MPM.add(createReversePostOrderFunctionAttrsPass()); - if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) { + if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) // Remove avail extern fns and globals definitions if we aren't // compiling an object file for later LTO. For LTO we want to preserve // these so they are eligible for inlining at link-time. Note if they @@ -407,6 +426,15 @@ void PassManagerBuilder::populateModulePassManager( // globals referenced by available external functions dead // and saves running remaining passes on the eliminated functions. MPM.add(createEliminateAvailableExternallyPass()); + + if (PerformThinLTO) { + // Remove dead fns and globals. Removing unreferenced functions could lead + // to more opportunities for globalopt + MPM.add(createGlobalDCEPass()); + MPM.add(createGlobalOptimizerPass()); + // Remove dead fns and globals after globalopt + MPM.add(createGlobalDCEPass()); + addFunctionSimplificationPasses(MPM); } if (EnableNonLTOGlobalsModRef) @@ -682,6 +710,20 @@ void PassManagerBuilder::addLateLTOOptimizationPasses( PM.add(createMergeFunctionsPass()); } +void PassManagerBuilder::populateThinLTOPassManager( + legacy::PassManagerBase &PM) { + PerformThinLTO = true; + + if (VerifyInput) + PM.add(createVerifierPass()); + + populateModulePassManager(PM); + + if (VerifyOutput) + PM.add(createVerifierPass()); + PerformThinLTO = false; +} + void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) { if (LibraryInfo) PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));