<div dir="ltr">Thanks for the fix!<div><br></div><div>-Rong</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 4, 2019 at 1:00 PM Evgenii Stepanov <<a href="mailto:eugeni.stepanov@gmail.com">eugeni.stepanov@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">tools/opt/opt.cpp:419:8: error: comparison of two values with<br>
different enumeration types in switch statement<br>
('llvm::opt_tool::CSPGOKind' and 'llvm::opt_tool::PGOKind')<br>
[-Werror,-Wenum-compare-switch]<br>
  case InstrUse:<br>
<br>
Fixed in r355338.<br>
<br>
On Mon, Mar 4, 2019 at 12:20 PM Rong Xu via llvm-commits<br>
<<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br>
><br>
> Author: xur<br>
> Date: Mon Mar  4 12:21:27 2019<br>
> New Revision: 355330<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=355330&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=355330&view=rev</a><br>
> Log:<br>
> [PGO] Context sensitive PGO (part 3)<br>
><br>
> Part 3 of CSPGO changes (mostly related to PassMananger).<br>
><br>
> Differential Revision: <a href="https://reviews.llvm.org/D54175" rel="noreferrer" target="_blank">https://reviews.llvm.org/D54175</a><br>
><br>
> Modified:<br>
>     llvm/trunk/include/llvm/Passes/PassBuilder.h<br>
>     llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h<br>
>     llvm/trunk/lib/LTO/LTOBackend.cpp<br>
>     llvm/trunk/lib/Passes/PassBuilder.cpp<br>
>     llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
>     llvm/trunk/tools/opt/NewPMDriver.cpp<br>
>     llvm/trunk/tools/opt/NewPMDriver.h<br>
>     llvm/trunk/tools/opt/opt.cpp<br>
><br>
> Modified: llvm/trunk/include/llvm/Passes/PassBuilder.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Passes/PassBuilder.h?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Passes/PassBuilder.h?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/include/llvm/Passes/PassBuilder.h (original)<br>
> +++ llvm/trunk/include/llvm/Passes/PassBuilder.h Mon Mar  4 12:21:27 2019<br>
> @@ -31,25 +31,38 @@ class ModuleSummaryIndex;<br>
><br>
>  /// A struct capturing PGO tunables.<br>
>  struct PGOOptions {<br>
> -  PGOOptions(std::string ProfileGenFile = "", std::string ProfileUseFile = "",<br>
> -             std::string SampleProfileFile = "",<br>
> -             std::string ProfileRemappingFile = "",<br>
> -             bool RunProfileGen = false, bool SamplePGOSupport = false)<br>
> -      : ProfileGenFile(ProfileGenFile), ProfileUseFile(ProfileUseFile),<br>
> -        SampleProfileFile(SampleProfileFile),<br>
> -        ProfileRemappingFile(ProfileRemappingFile),<br>
> -        RunProfileGen(RunProfileGen),<br>
> -        SamplePGOSupport(SamplePGOSupport || !SampleProfileFile.empty()) {<br>
> -    assert((RunProfileGen ||<br>
> -            !SampleProfileFile.empty() ||<br>
> -            !ProfileUseFile.empty() ||<br>
> -            SamplePGOSupport) && "Illegal PGOOptions.");<br>
> -  }<br>
> -  std::string ProfileGenFile;<br>
> -  std::string ProfileUseFile;<br>
> -  std::string SampleProfileFile;<br>
> +  enum PGOAction { NoAction, IRInstr, IRUse, SampleUse };<br>
> +  enum CSPGOAction { NoCSAction, CSIRInstr, CSIRUse };<br>
> +  PGOOptions(std::string ProfileFile = "", std::string CSProfileGenFile = "",<br>
> +             std::string ProfileRemappingFile = "", PGOAction Action = NoAction,<br>
> +             CSPGOAction CSAction = NoCSAction, bool SamplePGOSupport = false)<br>
> +      : ProfileFile(ProfileFile), CSProfileGenFile(CSProfileGenFile),<br>
> +        ProfileRemappingFile(ProfileRemappingFile), Action(Action),<br>
> +        CSAction(CSAction),<br>
> +        SamplePGOSupport(SamplePGOSupport || Action == SampleUse) {<br>
> +    // Note, we do allow ProfileFile.empty() for Action=IRUse LTO can<br>
> +    // callback with IRUse action without ProfileFile.<br>
> +<br>
> +    // If there is a CSAction, PGOAction cannot be IRInstr or SampleUse.<br>
> +    assert(this->CSAction == NoCSAction ||<br>
> +           (this->Action != IRInstr && this->Action != SampleUse));<br>
> +<br>
> +    // For CSIRInstr, CSProfileGenFile also needs to be nonempty.<br>
> +    assert(this->CSAction != CSIRInstr || !this->CSProfileGenFile.empty());<br>
> +<br>
> +    // If CSAction is CSIRUse, PGOAction needs to be IRUse as they share<br>
> +    // a profile.<br>
> +    assert(this->CSAction != CSIRUse || this->Action == IRUse);<br>
> +<br>
> +    // If neither CSAction nor CSAction, SamplePGOSupport needs to be true.<br>
> +    assert(this->Action != NoAction || this->CSAction != NoCSAction ||<br>
> +           this->SamplePGOSupport);<br>
> +  }<br>
> +  std::string ProfileFile;<br>
> +  std::string CSProfileGenFile;<br>
>    std::string ProfileRemappingFile;<br>
> -  bool RunProfileGen;<br>
> +  PGOAction Action;<br>
> +  CSPGOAction CSAction;<br>
>    bool SamplePGOSupport;<br>
>  };<br>
><br>
> @@ -607,9 +620,8 @@ private:<br>
>                                  bool VerifyEachPass, bool DebugLogging);<br>
><br>
>    void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,<br>
> -                         OptimizationLevel Level, bool RunProfileGen,<br>
> -                         std::string ProfileGenFile,<br>
> -                         std::string ProfileUseFile,<br>
> +                         OptimizationLevel Level, bool RunProfileGen, bool IsCS,<br>
> +                         std::string ProfileFile,<br>
>                           std::string ProfileRemappingFile);<br>
><br>
>    void invokePeepholeEPCallbacks(FunctionPassManager &, OptimizationLevel);<br>
><br>
> Modified: llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h (original)<br>
> +++ llvm/trunk/include/llvm/Transforms/IPO/PassManagerBuilder.h Mon Mar  4 12:21:27 2019<br>
> @@ -159,6 +159,10 @@ public:<br>
><br>
>    /// Enable profile instrumentation pass.<br>
>    bool EnablePGOInstrGen;<br>
> +  /// Enable profile context sensitive instrumentation pass.<br>
> +  bool EnablePGOCSInstrGen;<br>
> +  /// Enable profile context sensitive profile use pass.<br>
> +  bool EnablePGOCSInstrUse;<br>
>    /// Profile data file name that the instrumentation will be written to.<br>
>    std::string PGOInstrGen;<br>
>    /// Path of the profile data file.<br>
> @@ -185,7 +189,7 @@ private:<br>
>    void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;<br>
>    void addLTOOptimizationPasses(legacy::PassManagerBase &PM);<br>
>    void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);<br>
> -  void addPGOInstrPasses(legacy::PassManagerBase &MPM);<br>
> +  void addPGOInstrPasses(legacy::PassManagerBase &MPM, bool IsCS);<br>
>    void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);<br>
>    void addInstructionCombiningPass(legacy::PassManagerBase &MPM) const;<br>
><br>
><br>
> Modified: llvm/trunk/lib/LTO/LTOBackend.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOBackend.cpp?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOBackend.cpp?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/lib/LTO/LTOBackend.cpp (original)<br>
> +++ llvm/trunk/lib/LTO/LTOBackend.cpp Mon Mar  4 12:21:27 2019<br>
> @@ -154,8 +154,15 @@ static void runNewPMPasses(Config &Conf,<br>
>                             const ModuleSummaryIndex *ImportSummary) {<br>
>    Optional<PGOOptions> PGOOpt;<br>
>    if (!Conf.SampleProfile.empty())<br>
> -    PGOOpt = PGOOptions("", "", Conf.SampleProfile, Conf.ProfileRemapping,<br>
> -                        false, true);<br>
> +    PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,<br>
> +                        PGOOptions::SampleUse, PGOOptions::NoCSAction, true);<br>
> +  else if (Conf.RunCSIRInstr) {<br>
> +    PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,<br>
> +                        PGOOptions::IRUse, PGOOptions::CSIRInstr);<br>
> +  } else if (!Conf.CSIRProfile.empty()) {<br>
> +    PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,<br>
> +                        PGOOptions::IRUse, PGOOptions::CSIRUse);<br>
> +  }<br>
><br>
>    PassBuilder PB(TM, PGOOpt);<br>
>    AAManager AA;<br>
> @@ -273,6 +280,11 @@ static void runOldPMPasses(Config &Conf,<br>
>    PMB.SLPVectorize = true;<br>
>    PMB.OptLevel = Conf.OptLevel;<br>
>    PMB.PGOSampleUse = Conf.SampleProfile;<br>
> +  PMB.EnablePGOCSInstrGen = Conf.RunCSIRInstr;<br>
> +  if (!Conf.RunCSIRInstr && !Conf.CSIRProfile.empty()) {<br>
> +    PMB.EnablePGOCSInstrUse = true;<br>
> +    PMB.PGOInstrUse = Conf.CSIRProfile;<br>
> +  }<br>
>    if (IsThinLTO)<br>
>      PMB.populateThinLTOPassManager(passes);<br>
>    else<br>
><br>
> Modified: llvm/trunk/lib/Passes/PassBuilder.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassBuilder.cpp?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassBuilder.cpp?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/lib/Passes/PassBuilder.cpp (original)<br>
> +++ llvm/trunk/lib/Passes/PassBuilder.cpp Mon Mar  4 12:21:27 2019<br>
> @@ -406,7 +406,7 @@ PassBuilder::buildFunctionSimplification<br>
><br>
>    // For PGO use pipeline, try to optimize memory intrinsics such as memcpy<br>
>    // using the size value profile. Don't perform this when optimizing for size.<br>
> -  if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&<br>
> +  if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse &&<br>
>        !isOptimizingForSize(Level))<br>
>      FPM.addPass(PGOMemOPSizeOpt());<br>
><br>
> @@ -449,8 +449,8 @@ PassBuilder::buildFunctionSimplification<br>
>    // Do not enable unrolling in PreLinkThinLTO phase during sample PGO<br>
>    // because it changes IR to makes profile annotation in back compile<br>
>    // inaccurate.<br>
> -  if (Phase != ThinLTOPhase::PreLink ||<br>
> -      !PGOOpt || PGOOpt->SampleProfileFile.empty())<br>
> +  if (Phase != ThinLTOPhase::PreLink || !PGOOpt ||<br>
> +      PGOOpt->Action != PGOOptions::SampleUse)<br>
>      LPM2.addPass(LoopFullUnrollPass(Level));<br>
><br>
>    for (auto &C : LoopOptimizerEndEPCallbacks)<br>
> @@ -510,7 +510,8 @@ PassBuilder::buildFunctionSimplification<br>
>    invokePeepholeEPCallbacks(FPM, Level);<br>
><br>
>    if (EnableCHR && Level == O3 && PGOOpt &&<br>
> -      (!PGOOpt->ProfileUseFile.empty() || !PGOOpt->SampleProfileFile.empty()))<br>
> +      (PGOOpt->Action == PGOOptions::IRUse ||<br>
> +       PGOOpt->Action == PGOOptions::SampleUse))<br>
>      FPM.addPass(ControlHeightReductionPass());<br>
><br>
>    return FPM;<br>
> @@ -518,15 +519,15 @@ PassBuilder::buildFunctionSimplification<br>
><br>
>  void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,<br>
>                                      PassBuilder::OptimizationLevel Level,<br>
> -                                    bool RunProfileGen,<br>
> -                                    std::string ProfileGenFile,<br>
> -                                    std::string ProfileUseFile,<br>
> +                                    bool RunProfileGen, bool IsCS,<br>
> +                                    std::string ProfileFile,<br>
>                                      std::string ProfileRemappingFile) {<br>
>    // Generally running simplification passes and the inliner with an high<br>
>    // threshold results in smaller executables, but there may be cases where<br>
>    // the size grows, so let's be conservative here and skip this simplification<br>
> -  // at -Os/Oz.<br>
> -  if (!isOptimizingForSize(Level)) {<br>
> +  // at -Os/Oz. We will not do this  inline for context sensistive PGO (when<br>
> +  // IsCS is true).<br>
> +  if (!isOptimizingForSize(Level) && !IsCS) {<br>
>      InlineParams IP;<br>
><br>
>      // In the old pass manager, this is a cl::opt. Should still this be one?<br>
> @@ -559,7 +560,7 @@ void PassBuilder::addPGOInstrPasses(Modu<br>
>    MPM.addPass(GlobalDCEPass());<br>
><br>
>    if (RunProfileGen) {<br>
> -    MPM.addPass(PGOInstrumentationGen());<br>
> +    MPM.addPass(PGOInstrumentationGen(IsCS));<br>
><br>
>      FunctionPassManager FPM;<br>
>      FPM.addPass(<br>
> @@ -568,15 +569,13 @@ void PassBuilder::addPGOInstrPasses(Modu<br>
><br>
>      // Add the profile lowering pass.<br>
>      InstrProfOptions Options;<br>
> -    if (!ProfileGenFile.empty())<br>
> -      Options.InstrProfileOutput = ProfileGenFile;<br>
> +    if (!ProfileFile.empty())<br>
> +      Options.InstrProfileOutput = ProfileFile;<br>
>      Options.DoCounterPromotion = true;<br>
> -    Options.UseBFIInPromotion = false;<br>
> -    MPM.addPass(InstrProfiling(Options, false));<br>
> -  }<br>
> -<br>
> -  if (!ProfileUseFile.empty())<br>
> -    MPM.addPass(PGOInstrumentationUse(ProfileUseFile, ProfileRemappingFile));<br>
> +    Options.UseBFIInPromotion = IsCS;<br>
> +    MPM.addPass(InstrProfiling(Options, IsCS));<br>
> +  } else if (!ProfileFile.empty())<br>
> +    MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS));<br>
>  }<br>
><br>
>  static InlineParams<br>
> @@ -593,7 +592,7 @@ PassBuilder::buildModuleSimplificationPi<br>
>                                                 bool DebugLogging) {<br>
>    ModulePassManager MPM(DebugLogging);<br>
><br>
> -  bool HasSampleProfile = PGOOpt && !PGOOpt->SampleProfileFile.empty();<br>
> +  bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse);<br>
><br>
>    // In ThinLTO mode, when flattened profile is used, all the available<br>
>    // profile information will be annotated in PreLink phase so there is<br>
> @@ -646,7 +645,7 @@ PassBuilder::buildModuleSimplificationPi<br>
>    if (LoadSampleProfile) {<br>
>      // Annotate sample profile right after early FPM to ensure freshness of<br>
>      // the debug info.<br>
> -    MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,<br>
> +    MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,<br>
>                                          PGOOpt->ProfileRemappingFile,<br>
>                                          Phase == ThinLTOPhase::PreLink));<br>
>      // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard<br>
> @@ -695,12 +694,17 @@ PassBuilder::buildModuleSimplificationPi<br>
><br>
>    // Add all the requested passes for instrumentation PGO, if requested.<br>
>    if (PGOOpt && Phase != ThinLTOPhase::PostLink &&<br>
> -      (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {<br>
> -    addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,<br>
> -                      PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile,<br>
> +      (PGOOpt->Action == PGOOptions::IRInstr ||<br>
> +       PGOOpt->Action == PGOOptions::IRUse)) {<br>
> +    addPGOInstrPasses(MPM, DebugLogging, Level,<br>
> +                      /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr,<br>
> +                      /* IsCS */ false, PGOOpt->ProfileFile,<br>
>                        PGOOpt->ProfileRemappingFile);<br>
>      MPM.addPass(PGOIndirectCallPromotion(false, false));<br>
>    }<br>
> +  if (PGOOpt && Phase != ThinLTOPhase::PostLink &&<br>
> +      PGOOpt->CSAction == PGOOptions::CSIRInstr)<br>
> +    MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile));<br>
><br>
>    // Synthesize function entry counts for non-PGO compilation.<br>
>    if (EnableSyntheticCounts && !PGOOpt)<br>
> @@ -731,8 +735,8 @@ PassBuilder::buildModuleSimplificationPi<br>
>    // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO<br>
>    // because it makes profile annotation in the backend inaccurate.<br>
>    InlineParams IP = getInlineParamsFromOptLevel(Level);<br>
> -  if (Phase == ThinLTOPhase::PreLink &&<br>
> -      PGOOpt && !PGOOpt->SampleProfileFile.empty())<br>
> +  if (Phase == ThinLTOPhase::PreLink && PGOOpt &&<br>
> +      PGOOpt->Action == PGOOptions::SampleUse)<br>
>      IP.HotCallSiteThreshold = 0;<br>
>    MainCGPipeline.addPass(InlinerPass(IP));<br>
><br>
> @@ -795,6 +799,21 @@ ModulePassManager PassBuilder::buildModu<br>
>    // FIXME: Is this really an optimization rather than a canonicalization?<br>
>    MPM.addPass(ReversePostOrderFunctionAttrsPass());<br>
><br>
> +  // Do a post inline PGO instrumentation and use pass. This is a context<br>
> +  // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as<br>
> +  // cross-module inline has not been done yet. The context sensitive<br>
> +  // instrumentation is after all the inlines are done.<br>
> +  if (!LTOPreLink && PGOOpt) {<br>
> +    if (PGOOpt->CSAction == PGOOptions::CSIRInstr)<br>
> +      addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true,<br>
> +                        /* IsCS */ true, PGOOpt->CSProfileGenFile,<br>
> +                        PGOOpt->ProfileRemappingFile);<br>
> +    else if (PGOOpt->CSAction == PGOOptions::CSIRUse)<br>
> +      addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false,<br>
> +                        /* IsCS */ true, PGOOpt->ProfileFile,<br>
> +                        PGOOpt->ProfileRemappingFile);<br>
> +  }<br>
> +<br>
>    // Re-require GloblasAA here prior to function passes. This is particularly<br>
>    // useful as the above will have inlined, DCE'ed, and function-attr<br>
>    // propagated everything. We should at this point have a reasonably minimal<br>
> @@ -1031,7 +1050,7 @@ PassBuilder::buildLTOPreLinkDefaultPipel<br>
>    assert(Level != O0 && "Must request optimizations for the default pipeline!");<br>
>    // FIXME: We should use a customized pre-link pipeline!<br>
>    return buildPerModuleDefaultPipeline(Level, DebugLogging,<br>
> -                                       /*LTOPreLink=*/true);<br>
> +                                       /* LTOPreLink */true);<br>
>  }<br>
><br>
>  ModulePassManager<br>
> @@ -1040,9 +1059,9 @@ PassBuilder::buildLTODefaultPipeline(Opt<br>
>    assert(Level != O0 && "Must request optimizations for the default pipeline!");<br>
>    ModulePassManager MPM(DebugLogging);<br>
><br>
> -  if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {<br>
> +  if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {<br>
>      // Load sample profile before running the LTO optimization pipeline.<br>
> -    MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,<br>
> +    MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,<br>
>                                          PGOOpt->ProfileRemappingFile,<br>
>                                          false /* ThinLTOPhase::PreLink */));<br>
>    }<br>
> @@ -1068,7 +1087,7 @@ PassBuilder::buildLTODefaultPipeline(Opt<br>
>      // This two-step promotion is to save the compile time. For LTO, it should<br>
>      // produce the same result as if we only do promotion here.<br>
>      MPM.addPass(PGOIndirectCallPromotion(<br>
> -        true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));<br>
> +        true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse));<br>
>      // Propagate constants at call sites into the functions they call.  This<br>
>      // opens opportunities for globalopt (and inlining) by substituting function<br>
>      // pointers passed as arguments to direct uses of functions.<br>
> @@ -1150,6 +1169,19 @@ PassBuilder::buildLTODefaultPipeline(Opt<br>
><br>
>    FPM.addPass(JumpThreadingPass());<br>
><br>
> +  // Do a post inline PGO instrumentation and use pass. This is a context<br>
> +  // sensitive PGO pass.<br>
> +  if (PGOOpt) {<br>
> +    if (PGOOpt->CSAction == PGOOptions::CSIRInstr)<br>
> +      addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true,<br>
> +                        /* IsCS */ true, PGOOpt->CSProfileGenFile,<br>
> +                        PGOOpt->ProfileRemappingFile);<br>
> +    else if (PGOOpt->CSAction == PGOOptions::CSIRUse)<br>
> +      addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false,<br>
> +                        /* IsCS */ true, PGOOpt->ProfileFile,<br>
> +                        PGOOpt->ProfileRemappingFile);<br>
> +  }<br>
> +<br>
>    // Break up allocas<br>
>    FPM.addPass(SROA());<br>
><br>
><br>
> Modified: llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp (original)<br>
> +++ llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp Mon Mar  4 12:21:27 2019<br>
> @@ -174,6 +174,8 @@ PassManagerBuilder::PassManagerBuilder()<br>
>      MergeFunctions = false;<br>
>      PrepareForLTO = false;<br>
>      EnablePGOInstrGen = false;<br>
> +    EnablePGOCSInstrGen = false;<br>
> +    EnablePGOCSInstrUse = false;<br>
>      PGOInstrGen = "";<br>
>      PGOInstrUse = "";<br>
>      PGOSampleUse = "";<br>
> @@ -271,13 +273,19 @@ void PassManagerBuilder::populateFunctio<br>
>  }<br>
><br>
>  // Do PGO instrumentation generation or use pass as the option specified.<br>
> -void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {<br>
> -  if (!EnablePGOInstrGen && PGOInstrUse.empty() && PGOSampleUse.empty())<br>
> +void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM,<br>
> +                                           bool IsCS = false) {<br>
> +  if (IsCS) {<br>
> +    if (!EnablePGOCSInstrGen && !EnablePGOCSInstrUse)<br>
> +      return;<br>
> +  } else if (!EnablePGOInstrGen && PGOInstrUse.empty() && PGOSampleUse.empty())<br>
>      return;<br>
> +<br>
>    // Perform the preinline and cleanup passes for O1 and above.<br>
>    // And avoid doing them if optimizing for size.<br>
> +  // We will not do this inline for context sensitive PGO (when IsCS is true).<br>
>    if (OptLevel > 0 && SizeLevel == 0 && !DisablePreInliner &&<br>
> -      PGOSampleUse.empty()) {<br>
> +      PGOSampleUse.empty() && !IsCS) {<br>
>      // Create preinline pass. We construct an InlineParams object and specify<br>
>      // the threshold here to avoid the command line options of the regular<br>
>      // inliner to influence pre-inlining. The only fields of InlineParams we<br>
> @@ -295,22 +303,23 @@ void PassManagerBuilder::addPGOInstrPass<br>
>      MPM.add(createInstructionCombiningPass()); // Combine silly seq's<br>
>      addExtensionsToPM(EP_Peephole, MPM);<br>
>    }<br>
> -  if (EnablePGOInstrGen) {<br>
> -    MPM.add(createPGOInstrumentationGenLegacyPass());<br>
> +  if ((EnablePGOInstrGen && !IsCS) || (EnablePGOCSInstrGen && IsCS)) {<br>
> +    MPM.add(createPGOInstrumentationGenLegacyPass(IsCS));<br>
>      // Add the profile lowering pass.<br>
>      InstrProfOptions Options;<br>
>      if (!PGOInstrGen.empty())<br>
>        Options.InstrProfileOutput = PGOInstrGen;<br>
>      Options.DoCounterPromotion = true;<br>
> +    Options.UseBFIInPromotion = IsCS;<br>
>      MPM.add(createLoopRotatePass());<br>
> -    MPM.add(createInstrProfilingLegacyPass(Options));<br>
> +    MPM.add(createInstrProfilingLegacyPass(Options, IsCS));<br>
>    }<br>
>    if (!PGOInstrUse.empty())<br>
> -    MPM.add(createPGOInstrumentationUseLegacyPass(PGOInstrUse));<br>
> +    MPM.add(createPGOInstrumentationUseLegacyPass(PGOInstrUse, IsCS));<br>
>    // Indirect call promotion that promotes intra-module targets only.<br>
>    // For ThinLTO this is done earlier due to interactions with globalopt<br>
>    // for imported functions. We don't run this at -O0.<br>
> -  if (OptLevel > 0)<br>
> +  if (OptLevel > 0 && !IsCS)<br>
>      MPM.add(<br>
>          createPGOIndirectCallPromotionLegacyPass(false, !PGOSampleUse.empty()));<br>
>  }<br>
> @@ -418,7 +427,7 @@ void PassManagerBuilder::addFunctionSimp<br>
>    addExtensionsToPM(EP_Peephole, MPM);<br>
><br>
>    if (EnableCHR && OptLevel >= 3 &&<br>
> -      (!PGOInstrUse.empty() || !PGOSampleUse.empty()))<br>
> +      (!PGOInstrUse.empty() || !PGOSampleUse.empty() || EnablePGOCSInstrGen))<br>
>      MPM.add(createControlHeightReductionLegacyPass());<br>
>  }<br>
><br>
> @@ -533,6 +542,11 @@ void PassManagerBuilder::populateModuleP<br>
>    if (DefaultOrPreLinkPipeline && !PrepareForThinLTOUsingPGOSampleProfile)<br>
>      addPGOInstrPasses(MPM);<br>
><br>
> +  // Create profile COMDAT variables. Lld linker wants to see all variables<br>
> +  // before the LTO/ThinLTO link since it needs to resolve symbols/comdats.<br>
> +  if (!PerformThinLTO && EnablePGOCSInstrGen)<br>
> +    MPM.add(createPGOInstrumentationGenCreateVarLegacyPass(PGOInstrGen));<br>
> +<br>
>    // We add a module alias analysis pass here. In part due to bugs in the<br>
>    // analysis infrastructure this "works" in that the analysis stays alive<br>
>    // for the entire SCC pass run below.<br>
> @@ -574,6 +588,14 @@ void PassManagerBuilder::populateModuleP<br>
>      // and saves running remaining passes on the eliminated functions.<br>
>      MPM.add(createEliminateAvailableExternallyPass());<br>
><br>
> +  // CSFDO instrumentation and use pass. Don't invoke this for Prepare pass<br>
> +  // for LTO and ThinLTO -- The actual pass will be called after all inlines<br>
> +  // are performed.<br>
> +  // Need to do this after COMDAT variables have been eliminated,<br>
> +  // (i.e. after EliminateAvailableExternallyPass).<br>
> +  if (!(PrepareForLTO || PrepareForThinLTO))<br>
> +    addPGOInstrPasses(MPM, /* IsCS */ true);<br>
> +<br>
>    if (EnableOrderFileInstrumentation)<br>
>      MPM.add(createInstrOrderFilePass());<br>
><br>
> @@ -854,6 +876,9 @@ void PassManagerBuilder::addLTOOptimizat<br>
><br>
>    PM.add(createPruneEHPass());   // Remove dead EH info.<br>
><br>
> +  // CSFDO instrumentation and use pass.<br>
> +  addPGOInstrPasses(PM, /* IsCS */ true);<br>
> +<br>
>    // Optimize globals again if we ran the inliner.<br>
>    if (RunInliner)<br>
>      PM.add(createGlobalOptimizerPass());<br>
><br>
> Modified: llvm/trunk/tools/opt/NewPMDriver.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/NewPMDriver.cpp?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/NewPMDriver.cpp?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/tools/opt/NewPMDriver.cpp (original)<br>
> +++ llvm/trunk/tools/opt/NewPMDriver.cpp Mon Mar  4 12:21:27 2019<br>
> @@ -102,6 +102,9 @@ static cl::opt<std::string> OptimizerLas<br>
><br>
>  extern cl::opt<PGOKind> PGOKindFlag;<br>
>  extern cl::opt<std::string> ProfileFile;<br>
> +extern cl::opt<CSPGOKind> CSPGOKindFlag;<br>
> +extern cl::opt<std::string> CSProfileGenFile;<br>
> +<br>
>  static cl::opt<std::string><br>
>      ProfileRemappingFile("profile-remapping-file",<br>
>                           cl::desc("Path to the profile remapping file."),<br>
> @@ -219,20 +222,41 @@ bool llvm::runPassPipeline(StringRef Arg<br>
>    Optional<PGOOptions> P;<br>
>    switch (PGOKindFlag) {<br>
>      case InstrGen:<br>
> -      P = PGOOptions(ProfileFile, "", "", "", true);<br>
> +      P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);<br>
>        break;<br>
>      case InstrUse:<br>
> -      P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false);<br>
> +      P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);<br>
>        break;<br>
>      case SampleUse:<br>
> -      P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false);<br>
> +      P = PGOOptions(ProfileFile, "", ProfileRemappingFile,<br>
> +                     PGOOptions::SampleUse);<br>
>        break;<br>
>      case NoPGO:<br>
>        if (DebugInfoForProfiling)<br>
> -        P = PGOOptions("", "", "", "", false, true);<br>
> +        P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,<br>
> +                       true);<br>
>        else<br>
>          P = None;<br>
> -  }<br>
> +    }<br>
> +    if (CSPGOKindFlag != NoCSPGO) {<br>
> +      if (P && (P->Action == PGOOptions::IRInstr ||<br>
> +                P->Action == PGOOptions::SampleUse))<br>
> +        errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";<br>
> +      if (CSPGOKindFlag == CSInstrGen) {<br>
> +        if (CSProfileGenFile.empty())<br>
> +          errs() << "CSInstrGen needs to specify CSProfileGenFile";<br>
> +        if (P) {<br>
> +          P->CSAction = PGOOptions::CSIRInstr;<br>
> +          P->CSProfileGenFile = CSProfileGenFile;<br>
> +        } else<br>
> +          P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,<br>
> +                         PGOOptions::NoAction, PGOOptions::CSIRInstr);<br>
> +      } else /* CSPGOKindFlag == CSInstrUse */ {<br>
> +        if (!P)<br>
> +          errs() << "CSInstrUse needs to be together with InstrUse";<br>
> +        P->CSAction = PGOOptions::CSIRUse;<br>
> +      }<br>
> +    }<br>
>    PassInstrumentationCallbacks PIC;<br>
>    StandardInstrumentations SI;<br>
>    SI.registerCallbacks(PIC);<br>
><br>
> Modified: llvm/trunk/tools/opt/NewPMDriver.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/NewPMDriver.h?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/NewPMDriver.h?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/tools/opt/NewPMDriver.h (original)<br>
> +++ llvm/trunk/tools/opt/NewPMDriver.h Mon Mar  4 12:21:27 2019<br>
> @@ -45,6 +45,7 @@ enum PGOKind {<br>
>    InstrUse,<br>
>    SampleUse<br>
>  };<br>
> +enum CSPGOKind { NoCSPGO, CSInstrGen, CSInstrUse };<br>
>  }<br>
><br>
>  /// Driver function to run the new pass manager over a module.<br>
><br>
> Modified: llvm/trunk/tools/opt/opt.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=355330&r1=355329&r2=355330&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=355330&r1=355329&r2=355330&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/tools/opt/opt.cpp (original)<br>
> +++ llvm/trunk/tools/opt/opt.cpp Mon Mar  4 12:21:27 2019<br>
> @@ -287,6 +287,22 @@ cl::opt<PGOKind><br>
>  cl::opt<std::string> ProfileFile("profile-file",<br>
>                                   cl::desc("Path to the profile."), cl::Hidden);<br>
><br>
> +cl::opt<CSPGOKind> CSPGOKindFlag(<br>
> +    "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,<br>
> +    cl::desc("The kind of context sensitive profile guided optimization"),<br>
> +    cl::values(<br>
> +        clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),<br>
> +        clEnumValN(<br>
> +            CSInstrGen, "cspgo-instr-gen-pipeline",<br>
> +            "Instrument (context sensitive) the IR to generate profile."),<br>
> +        clEnumValN(<br>
> +            CSInstrUse, "cspgo-instr-use-pipeline",<br>
> +            "Use instrumented (context sensitive) profile to guide PGO.")));<br>
> +cl::opt<std::string> CSProfileGenFile(<br>
> +    "cs-profilegen-file",<br>
> +    cl::desc("Path to the instrumented context sensitive profile."),<br>
> +    cl::Hidden);<br>
> +<br>
>  class OptCustomPassManager : public legacy::PassManager {<br>
>    DebugifyStatsMap DIStatsMap;<br>
><br>
> @@ -394,6 +410,17 @@ static void AddOptimizationPasses(legacy<br>
>      break;<br>
>    default:<br>
>      break;<br>
> +  }<br>
> +<br>
> +  switch (CSPGOKindFlag) {<br>
> +  case CSInstrGen:<br>
> +    Builder.EnablePGOCSInstrGen = true;<br>
> +    break;<br>
> +  case InstrUse:<br>
> +    Builder.EnablePGOCSInstrUse = true;<br>
> +    break;<br>
> +  default:<br>
> +    break;<br>
>    }<br>
><br>
>    Builder.populateFunctionPassManager(FPM);<br>
><br>
><br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>