From a837603c4732380fff19d8fee72b0217d3f6af9a Mon Sep 17 00:00:00 2001 From: Alina Sbirlea Date: Fri, 19 Apr 2019 16:11:59 +0000 Subject: [PATCH] [NewPassManager] Adding pass tuning options: loop vectorize. Summary: Trying to add the plumbing necessary to add tuning options to the new pass manager. Testing with the flags for loop vectorize. Reviewers: chandlerc Subscribers: sanjoy, mehdi_amini, jlebar, steven_wu, dexonsmith, dang, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59723 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358763 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Passes/PassBuilder.h | 20 ++++++++++++++++++- include/llvm/Transforms/Vectorize.h | 5 +++-- .../llvm/Transforms/Vectorize/LoopVectorize.h | 16 +++++++++++++++ lib/LTO/LTOBackend.cpp | 2 +- lib/Passes/PassBuilder.cpp | 8 +++++++- lib/Transforms/IPO/PassManagerBuilder.cpp | 10 +++++----- lib/Transforms/Vectorize/LoopVectorize.cpp | 9 +++++++++ tools/opt/NewPMDriver.cpp | 2 +- unittests/IR/PassBuilderCallbacksTest.cpp | 3 ++- 9 files changed, 63 insertions(+), 12 deletions(-) diff --git a/include/llvm/Passes/PassBuilder.h b/include/llvm/Passes/PassBuilder.h index 3003e8247e1..029ae64cb7a 100644 --- a/include/llvm/Passes/PassBuilder.h +++ b/include/llvm/Passes/PassBuilder.h @@ -66,6 +66,22 @@ struct PGOOptions { bool SamplePGOSupport; }; +/// Tunable parameters for passes in the default pipelines. +class PipelineTuningOptions { +public: + /// Constructor sets pipeline tuning defaults based on cl::opts. Each option + /// can be set in the PassBuilder when using a LLVM as a library. + PipelineTuningOptions(); + + /// Tuning option to set loop interleaving on/off. Its default value is that + /// of the flag: `-interleave-loops`. + bool LoopInterleaving; + + /// Tuning option to enable/disable loop vectorization. Its default value is + /// that of the flag: `-vectorize-loops`. + bool LoopVectorization; +}; + /// This class provides access to building LLVM's passes. /// /// Its members provide the baseline state available to passes during their @@ -74,6 +90,7 @@ struct PGOOptions { /// construction. class PassBuilder { TargetMachine *TM; + PipelineTuningOptions PTO; Optional PGOOpt; PassInstrumentationCallbacks *PIC; @@ -190,9 +207,10 @@ public: }; explicit PassBuilder(TargetMachine *TM = nullptr, + PipelineTuningOptions PTO = PipelineTuningOptions(), Optional PGOOpt = None, PassInstrumentationCallbacks *PIC = nullptr) - : TM(TM), PGOOpt(PGOOpt), PIC(PIC) {} + : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) {} /// Cross register the analysis managers through their proxies. /// diff --git a/include/llvm/Transforms/Vectorize.h b/include/llvm/Transforms/Vectorize.h index a411b5664dd..88a0e49d0fa 100644 --- a/include/llvm/Transforms/Vectorize.h +++ b/include/llvm/Transforms/Vectorize.h @@ -109,8 +109,9 @@ struct VectorizeConfig { // // LoopVectorize - Create a loop vectorization pass. // -Pass *createLoopVectorizePass(bool InterleaveOnlyWhenForced = false, - bool VectorizeOnlyWhenForced = false); +Pass *createLoopVectorizePass(); +Pass *createLoopVectorizePass(bool InterleaveOnlyWhenForced, + bool VectorizeOnlyWhenForced); //===----------------------------------------------------------------------===// // diff --git a/include/llvm/Transforms/Vectorize/LoopVectorize.h b/include/llvm/Transforms/Vectorize/LoopVectorize.h index daca286f018..d1ec06afb02 100644 --- a/include/llvm/Transforms/Vectorize/LoopVectorize.h +++ b/include/llvm/Transforms/Vectorize/LoopVectorize.h @@ -76,6 +76,9 @@ class ScalarEvolution; class TargetLibraryInfo; class TargetTransformInfo; +extern cl::opt EnableLoopInterleaving; +extern cl::opt EnableLoopVectorization; + struct LoopVectorizeOptions { /// If false, consider all loops for interleaving. /// If true, only loops that explicitly request interleaving are considered. @@ -85,8 +88,21 @@ struct LoopVectorizeOptions { /// If true, only loops that explicitly request vectorization are considered. bool VectorizeOnlyWhenForced; + /// The current defaults when creating the pass with no arguments are: + /// EnableLoopInterleaving = true and EnableLoopVectorization = true. This + /// means that interleaving default is consistent with the cl::opt flag, while + /// vectorization is not. + /// FIXME: The default for EnableLoopVectorization in the cl::opt should be + /// set to true, and the corresponding change to account for this be made in + /// opt.cpp. The initializations below will become: + /// InterleaveOnlyWhenForced(!EnableLoopInterleaving) + /// VectorizeOnlyWhenForced(!EnableLoopVectorization). LoopVectorizeOptions() : InterleaveOnlyWhenForced(false), VectorizeOnlyWhenForced(false) {} + LoopVectorizeOptions(bool InterleaveOnlyWhenForced, + bool VectorizeOnlyWhenForced) + : InterleaveOnlyWhenForced(InterleaveOnlyWhenForced), + VectorizeOnlyWhenForced(VectorizeOnlyWhenForced) {} LoopVectorizeOptions &setInterleaveOnlyWhenForced(bool Value) { InterleaveOnlyWhenForced = Value; diff --git a/lib/LTO/LTOBackend.cpp b/lib/LTO/LTOBackend.cpp index 64c596931fa..9c2d0ed5d54 100644 --- a/lib/LTO/LTOBackend.cpp +++ b/lib/LTO/LTOBackend.cpp @@ -164,7 +164,7 @@ static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM, PGOOptions::IRUse, PGOOptions::CSIRUse); } - PassBuilder PB(TM, PGOOpt); + PassBuilder PB(TM, PipelineTuningOptions(), PGOOpt); AAManager AA; // Parse a custom AA pipeline if asked to. diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index 66936d68cb9..75a73efb906 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -215,6 +215,11 @@ static cl::opt EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden, cl::desc("Enable control height reduction optimization (CHR)")); +PipelineTuningOptions::PipelineTuningOptions() { + LoopInterleaving = EnableLoopInterleaving; + LoopVectorization = EnableLoopVectorization; +} + extern cl::opt EnableHotColdSplit; extern cl::opt EnableOrderFileInstrumentation; @@ -857,7 +862,8 @@ ModulePassManager PassBuilder::buildModuleOptimizationPipeline( OptimizePM.addPass(LoopDistributePass()); // Now run the core loop vectorizer. - OptimizePM.addPass(LoopVectorizePass()); + OptimizePM.addPass(LoopVectorizePass( + LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization))); // Eliminate loads by forwarding stores from the previous iteration to loads // of the current iteration. diff --git a/lib/Transforms/IPO/PassManagerBuilder.cpp b/lib/Transforms/IPO/PassManagerBuilder.cpp index 745f663ebdc..1a12d77e54a 100644 --- a/lib/Transforms/IPO/PassManagerBuilder.cpp +++ b/lib/Transforms/IPO/PassManagerBuilder.cpp @@ -41,6 +41,7 @@ #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" #include "llvm/Transforms/Utils.h" #include "llvm/Transforms/Vectorize.h" +#include "llvm/Transforms/Vectorize/LoopVectorize.h" using namespace llvm; @@ -48,10 +49,6 @@ static cl::opt RunPartialInlining("enable-partial-inlining", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Run Partial inlinining pass")); -static cl::opt - RunLoopVectorization("vectorize-loops", cl::Hidden, - cl::desc("Run the Loop vectorization passes")); - static cl::opt RunSLPVectorization("vectorize-slp", cl::Hidden, cl::desc("Run the SLP vectorization passes")); @@ -167,7 +164,10 @@ PassManagerBuilder::PassManagerBuilder() { Inliner = nullptr; DisableUnrollLoops = false; SLPVectorize = RunSLPVectorization; - LoopVectorize = RunLoopVectorization; + LoopVectorize = EnableLoopVectorization; + // FIXME: Add: LoopsInterleaved = EnableLoopInterleaving; + // Replace usage of DisableUnrollLoops with LoopsInterleaved when creating + // the LoopVectorize pass, to be consistent with the new pass manager. RerollLoops = RunLoopRerolling; NewGVN = RunNewGVN; DisableGVNLoadPRE = false; diff --git a/lib/Transforms/Vectorize/LoopVectorize.cpp b/lib/Transforms/Vectorize/LoopVectorize.cpp index 06b70b3aea6..c82c92f7d2a 100644 --- a/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -277,6 +277,13 @@ static cl::opt VPlanBuildStressTest( "out right after the build (stress test the VPlan H-CFG construction " "in the VPlan-native vectorization path).")); +cl::opt llvm::EnableLoopInterleaving( + "interleave-loops", cl::init(true), cl::Hidden, + cl::desc("Enable loop interleaving in Loop vectorization passes")); +cl::opt llvm::EnableLoopVectorization( + "vectorize-loops", cl::init(false), cl::Hidden, + cl::desc("Run the Loop vectorization passes")); + /// A helper function for converting Scalar types to vector types. /// If the incoming type is void, we return void. If the VF is 1, we return /// the scalar type. @@ -6063,6 +6070,8 @@ INITIALIZE_PASS_END(LoopVectorize, LV_NAME, lv_name, false, false) namespace llvm { +Pass *createLoopVectorizePass() { return new LoopVectorize(); } + Pass *createLoopVectorizePass(bool InterleaveOnlyWhenForced, bool VectorizeOnlyWhenForced) { return new LoopVectorize(InterleaveOnlyWhenForced, VectorizeOnlyWhenForced); diff --git a/tools/opt/NewPMDriver.cpp b/tools/opt/NewPMDriver.cpp index cbea7317c20..efe0bec35d7 100644 --- a/tools/opt/NewPMDriver.cpp +++ b/tools/opt/NewPMDriver.cpp @@ -261,7 +261,7 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, StandardInstrumentations SI; SI.registerCallbacks(PIC); - PassBuilder PB(TM, P, &PIC); + PassBuilder PB(TM, PipelineTuningOptions(), P, &PIC); registerEPCallbacks(PB, VerifyEachPass, DebugPM); // Load requested pass plugins and let them register pass builder callbacks diff --git a/unittests/IR/PassBuilderCallbacksTest.cpp b/unittests/IR/PassBuilderCallbacksTest.cpp index 7cd938fcddb..bb7e227075e 100644 --- a/unittests/IR/PassBuilderCallbacksTest.cpp +++ b/unittests/IR/PassBuilderCallbacksTest.cpp @@ -414,7 +414,8 @@ protected: "exit:\n" " ret void\n" "}\n")), - CallbacksHandle(), PB(nullptr, None, &CallbacksHandle.Callbacks), + CallbacksHandle(), + PB(nullptr, PipelineTuningOptions(), None, &CallbacksHandle.Callbacks), PM(true), LAM(true), FAM(true), CGAM(true), AM(true) { /// Register a callback for analysis registration. -- 2.50.1