zequanwu updated this revision to Diff 276208. zequanwu marked an inline comment as done. zequanwu added a comment. Herald added subscribers: dexonsmith, steven_wu.
- Disable `enable-call-graph-profile` by default in opt. - Disable `CGProfilePass` by default in clang unless `-no-integrated-as` is not given and `-fprofile-instrument-use-path=` is given, as this pass only generates module metadata when profile data is given. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D83013/new/ https://reviews.llvm.org/D83013 Files: clang/lib/CodeGen/BackendUtil.cpp clang/lib/Frontend/CompilerInvocation.cpp llvm/include/llvm/InitializePasses.h llvm/include/llvm/Transforms/IPO.h llvm/include/llvm/Transforms/IPO/PassManagerBuilder.h llvm/include/llvm/Transforms/Instrumentation/CGProfile.h llvm/lib/Passes/PassBuilder.cpp llvm/lib/Transforms/IPO/PassManagerBuilder.cpp llvm/lib/Transforms/Instrumentation/CGProfile.cpp llvm/lib/Transforms/Instrumentation/Instrumentation.cpp llvm/test/Instrumentation/cgprofile.ll llvm/test/Other/new-pm-cgprofile.ll llvm/test/Other/new-pm-defaults.ll llvm/test/Other/new-pm-thinlto-defaults.ll llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll llvm/tools/opt/NewPMDriver.cpp llvm/tools/opt/NewPMDriver.h llvm/tools/opt/opt.cpp
Index: llvm/tools/opt/opt.cpp =================================================================== --- llvm/tools/opt/opt.cpp +++ llvm/tools/opt/opt.cpp @@ -278,6 +278,10 @@ cl::desc("Specify time trace file destination"), cl::value_desc("filename")); +static cl::opt<bool> EnableCallGraphProfile( + "enable-call-graph-profile", cl::init(false), cl::Hidden, + cl::desc("Enable call graph profile pass (default = on)")); + static cl::opt<bool> RemarksWithHotness( "pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), @@ -414,6 +418,8 @@ Builder.SLPVectorize = OptLevel > 1 && SizeLevel < 2; + Builder.CallGraphProfile = EnableCallGraphProfile; + if (TM) TM->adjustPassManager(Builder); @@ -767,7 +773,8 @@ RemarksFile.get(), PassPipeline, Passes, OK, VK, PreserveAssemblyUseListOrder, PreserveBitcodeUseListOrder, EmitSummaryIndex, - EmitModuleHash, EnableDebugify, Coroutines) + EmitModuleHash, EnableDebugify, Coroutines, + EnableCallGraphProfile) ? 0 : 1; } Index: llvm/tools/opt/NewPMDriver.h =================================================================== --- llvm/tools/opt/NewPMDriver.h +++ llvm/tools/opt/NewPMDriver.h @@ -66,7 +66,8 @@ bool ShouldPreserveAssemblyUseListOrder, bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex, bool EmitModuleHash, - bool EnableDebugify, bool Coroutines); + bool EnableDebugify, bool Coroutines, + bool CallGraphProfile); } // namespace llvm #endif Index: llvm/tools/opt/NewPMDriver.cpp =================================================================== --- llvm/tools/opt/NewPMDriver.cpp +++ llvm/tools/opt/NewPMDriver.cpp @@ -220,7 +220,8 @@ bool ShouldPreserveAssemblyUseListOrder, bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex, bool EmitModuleHash, - bool EnableDebugify, bool Coroutines) { + bool EnableDebugify, bool Coroutines, + bool CallGraphProfile) { bool VerifyEachPass = VK == VK_VerifyEachPass; Optional<PGOOptions> P; @@ -266,6 +267,7 @@ SI.registerCallbacks(PIC); PipelineTuningOptions PTO; + PTO.CallGraphProfile = CallGraphProfile; // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized // to false above so we shouldn't necessarily need to check whether or not the // option has been enabled. Index: llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll =================================================================== --- llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll +++ llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll @@ -227,7 +227,6 @@ ; CHECK-O-NEXT: Running pass: SimplifyCFGPass ; CHECK-O-NEXT: Running pass: SpeculateAroundPHIsPass ; CHECK-O-NEXT: Finished {{.*}}Function pass manager run. -; CHECK-O-NEXT: Running pass: CGProfilePass ; CHECK-O-NEXT: Running pass: GlobalDCEPass ; CHECK-O-NEXT: Running pass: ConstantMergePass ; CHECK-O-NEXT: Finished {{.*}}Module pass manager run. Index: llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll =================================================================== --- llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll +++ llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll @@ -216,7 +216,6 @@ ; CHECK-O-NEXT: Running pass: SimplifyCFGPass ; CHECK-O-NEXT: Running pass: SpeculateAroundPHIsPass ; CHECK-O-NEXT: Finished {{.*}}Function pass manager run. -; CHECK-O-NEXT: Running pass: CGProfilePass ; CHECK-O-NEXT: Running pass: GlobalDCEPass ; CHECK-O-NEXT: Running pass: ConstantMergePass ; CHECK-O-NEXT: Finished {{.*}}Module pass manager run. Index: llvm/test/Other/new-pm-thinlto-defaults.ll =================================================================== --- llvm/test/Other/new-pm-thinlto-defaults.ll +++ llvm/test/Other/new-pm-thinlto-defaults.ll @@ -248,7 +248,6 @@ ; CHECK-POSTLINK-O-NEXT: Running pass: SimplifyCFGPass ; CHECK-POSTLINK-O-NEXT: Running pass: SpeculateAroundPHIsPass ; CHECK-POSTLINK-O-NEXT: Finished llvm::Function pass manager run. -; CHECK-POSTLINK-O-NEXT: Running pass: CGProfilePass ; CHECK-POSTLINK-O-NEXT: Running pass: GlobalDCEPass ; CHECK-POSTLINK-O-NEXT: Running pass: ConstantMergePass ; CHECK-POSTLINK-O-NEXT: Finished llvm::Module pass manager run. Index: llvm/test/Other/new-pm-defaults.ll =================================================================== --- llvm/test/Other/new-pm-defaults.ll +++ llvm/test/Other/new-pm-defaults.ll @@ -279,7 +279,6 @@ ; CHECK-O-NEXT: Running pass: SpeculateAroundPHIsPass ; CHECK-EP-OPTIMIZER-LAST: Running pass: NoOpFunctionPass ; CHECK-O-NEXT: Finished llvm::Function pass manager run. -; CHECK-O-NEXT: Running pass: CGProfilePass ; CHECK-O-NEXT: Running pass: GlobalDCEPass ; CHECK-O-NEXT: Running pass: ConstantMergePass ; CHECK-O-NEXT: Finished llvm::Module pass manager run. Index: llvm/test/Other/new-pm-cgprofile.ll =================================================================== --- llvm/test/Other/new-pm-cgprofile.ll +++ llvm/test/Other/new-pm-cgprofile.ll @@ -1,8 +1,8 @@ ; RUN: opt -debug-pass-manager -passes='default<O2>' %s 2>&1 |FileCheck %s --check-prefixes=DEFAULT -; RUN: opt -debug-pass-manager -passes='default<O2>' -enable-npm-call-graph-profile=0 %s 2>&1 |FileCheck %s --check-prefixes=OFF -; RUN: opt -debug-pass-manager -passes='default<O2>' -enable-npm-call-graph-profile=1 %s 2>&1 |FileCheck %s --check-prefixes=ON +; RUN: opt -debug-pass-manager -passes='default<O2>' -enable-call-graph-profile=0 %s 2>&1 |FileCheck %s --check-prefixes=OFF +; RUN: opt -debug-pass-manager -passes='default<O2>' -enable-call-graph-profile=1 %s 2>&1 |FileCheck %s --check-prefixes=ON ; -; DEFAULT: Running pass: CGProfilePass +; DEFAULT-NOT: Running pass: CGProfilePass ; OFF-NOT: Running pass: CGProfilePass ; ON: Running pass: CGProfilePass Index: llvm/test/Instrumentation/cgprofile.ll =================================================================== --- llvm/test/Instrumentation/cgprofile.ll +++ llvm/test/Instrumentation/cgprofile.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -passes cg-profile -S | FileCheck %s +; RUN: opt < %s -cg-profile -S | FileCheck %s declare void @b() Index: llvm/lib/Transforms/Instrumentation/Instrumentation.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/Instrumentation.cpp +++ llvm/lib/Transforms/Instrumentation/Instrumentation.cpp @@ -112,6 +112,7 @@ initializePGOInstrumentationUseLegacyPassPass(Registry); initializePGOIndirectCallPromotionLegacyPassPass(Registry); initializePGOMemOPSizeOptLegacyPassPass(Registry); + initializeCGProfileLegacyPassPass(Registry); initializeInstrOrderFileLegacyPassPass(Registry); initializeInstrProfilingLegacyPassPass(Registry); initializeMemorySanitizerLegacyPassPass(Registry); Index: llvm/lib/Transforms/Instrumentation/CGProfile.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/CGProfile.cpp +++ llvm/lib/Transforms/Instrumentation/CGProfile.cpp @@ -15,17 +15,42 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/MDBuilder.h" #include "llvm/IR/PassManager.h" +#include "llvm/InitializePasses.h" #include "llvm/ProfileData/InstrProf.h" +#include "llvm/Transforms/IPO.h" #include "llvm/Transforms/Instrumentation.h" #include <array> using namespace llvm; -PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) { +static bool +addModuleFlags(Module &M, + MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) { + if (Counts.empty()) + return false; + + LLVMContext &Context = M.getContext(); + MDBuilder MDB(Context); + std::vector<Metadata *> Nodes; + + for (auto E : Counts) { + Metadata *Vals[] = {ValueAsMetadata::get(E.first.first), + ValueAsMetadata::get(E.first.second), + MDB.createConstant(ConstantInt::get( + Type::getInt64Ty(Context), E.second))}; + Nodes.push_back(MDNode::get(Context, Vals)); + } + + M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes)); + return true; +} + +static bool +runCGProfilePass(Module &M, + function_ref<BlockFrequencyInfo &(Function &)> GetBFI, + function_ref<TargetTransformInfo &(Function &)> GetTTI) { MapVector<std::pair<Function *, Function *>, uint64_t> Counts; - FunctionAnalysisManager &FAM = - MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); InstrProfSymtab Symtab; auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F, Function *CalledF, uint64_t NewCount) { @@ -35,14 +60,14 @@ Count = SaturatingAdd(Count, NewCount); }; // Ignore error here. Indirect calls are ignored if this fails. - (void)(bool)Symtab.create(M); + (void)(bool) Symtab.create(M); for (auto &F : M) { if (F.isDeclaration()) continue; - auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F); + auto &BFI = GetBFI(F); if (BFI.getEntryFreq() == 0) continue; - TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F); + TargetTransformInfo &TTI = GetTTI(F); for (auto &BB : F) { Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB); if (!BBCount) @@ -69,28 +94,56 @@ } } - addModuleFlags(M, Counts); - - return PreservedAnalyses::all(); + return addModuleFlags(M, Counts); } -void CGProfilePass::addModuleFlags( - Module &M, - MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const { - if (Counts.empty()) - return; +namespace { +struct CGProfileLegacyPass final : public ModulePass { + static char ID; + CGProfileLegacyPass() : ModulePass(ID) { + initializeCGProfileLegacyPassPass(*PassRegistry::getPassRegistry()); + } - LLVMContext &Context = M.getContext(); - MDBuilder MDB(Context); - std::vector<Metadata *> Nodes; + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesCFG(); + AU.addRequired<BlockFrequencyInfoWrapperPass>(); + AU.addRequired<TargetTransformInfoWrapperPass>(); + } - for (auto E : Counts) { - Metadata *Vals[] = {ValueAsMetadata::get(E.first.first), - ValueAsMetadata::get(E.first.second), - MDB.createConstant(ConstantInt::get( - Type::getInt64Ty(Context), E.second))}; - Nodes.push_back(MDNode::get(Context, Vals)); + bool runOnModule(Module &M) override { + auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & { + return this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); + }; + auto GetTTI = [this](Function &F) -> TargetTransformInfo & { + return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); + }; + + return runCGProfilePass(M, GetBFI, GetTTI); } +}; - M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes)); +} // namespace + +char CGProfileLegacyPass::ID = 0; + +INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false, + false) + +ModulePass *llvm::createCGProfileLegacyPass() { + return new CGProfileLegacyPass(); +} + +PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) { + FunctionAnalysisManager &FAM = + MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); + auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & { + return FAM.getResult<BlockFrequencyAnalysis>(F); + }; + auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & { + return FAM.getResult<TargetIRAnalysis>(F); + }; + + runCGProfilePass(M, GetBFI, GetTTI); + + return PreservedAnalyses::all(); } Index: llvm/lib/Transforms/IPO/PassManagerBuilder.cpp =================================================================== --- llvm/lib/Transforms/IPO/PassManagerBuilder.cpp +++ llvm/lib/Transforms/IPO/PassManagerBuilder.cpp @@ -195,6 +195,7 @@ PrepareForThinLTO = EnablePrepareForThinLTO; PerformThinLTO = EnablePerformThinLTO; DivergentTarget = false; + CallGraphProfile = false; } PassManagerBuilder::~PassManagerBuilder() { @@ -834,6 +835,10 @@ if (MergeFunctions) MPM.add(createMergeFunctionsPass()); + // Add Module flag "CG Profile" based on Branch Frequency Information. + if (CallGraphProfile) + MPM.add(createCGProfileLegacyPass()); + // LoopSink pass sinks instructions hoisted by LICM, which serves as a // canonicalization pass that enables other optimizations. As a result, // LoopSink pass needs to be a very late IR pass to avoid undoing LICM Index: llvm/lib/Passes/PassBuilder.cpp =================================================================== --- llvm/lib/Passes/PassBuilder.cpp +++ llvm/lib/Passes/PassBuilder.cpp @@ -248,10 +248,6 @@ EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden, cl::desc("Enable control height reduction optimization (CHR)")); -static cl::opt<bool> EnableCallGraphProfile( - "enable-npm-call-graph-profile", cl::init(true), cl::Hidden, - cl::desc("Enable call graph profile pass for the new PM (default = on)")); - /// Flag to enable inline deferral during PGO. static cl::opt<bool> EnablePGOInlineDeferral("enable-npm-pgo-inline-deferral", cl::init(true), @@ -267,7 +263,6 @@ Coroutines = false; LicmMssaOptCap = SetLicmMssaOptCap; LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; - CallGraphProfile = EnableCallGraphProfile; } extern cl::opt<bool> EnableHotColdSplit; Index: llvm/include/llvm/Transforms/Instrumentation/CGProfile.h =================================================================== --- llvm/include/llvm/Transforms/Instrumentation/CGProfile.h +++ llvm/include/llvm/Transforms/Instrumentation/CGProfile.h @@ -19,11 +19,6 @@ class CGProfilePass : public PassInfoMixin<CGProfilePass> { public: PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); - -private: - void addModuleFlags( - Module &M, - MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const; }; } // end namespace llvm Index: llvm/include/llvm/Transforms/IPO/PassManagerBuilder.h =================================================================== --- llvm/include/llvm/Transforms/IPO/PassManagerBuilder.h +++ llvm/include/llvm/Transforms/IPO/PassManagerBuilder.h @@ -156,6 +156,7 @@ bool DisableTailCalls; bool DisableUnrollLoops; + bool CallGraphProfile; bool SLPVectorize; bool LoopVectorize; bool LoopsInterleaved; Index: llvm/include/llvm/Transforms/IPO.h =================================================================== --- llvm/include/llvm/Transforms/IPO.h +++ llvm/include/llvm/Transforms/IPO.h @@ -282,6 +282,8 @@ ModulePass *createWriteThinLTOBitcodePass(raw_ostream &Str, raw_ostream *ThinLinkOS = nullptr); +ModulePass *createCGProfileLegacyPass(); + } // End llvm namespace #endif Index: llvm/include/llvm/InitializePasses.h =================================================================== --- llvm/include/llvm/InitializePasses.h +++ llvm/include/llvm/InitializePasses.h @@ -102,6 +102,7 @@ void initializeCFIInstrInserterPass(PassRegistry&); void initializeCFLAndersAAWrapperPassPass(PassRegistry&); void initializeCFLSteensAAWrapperPassPass(PassRegistry&); +void initializeCGProfileLegacyPassPass(PassRegistry &); void initializeCallGraphDOTPrinterPass(PassRegistry&); void initializeCallGraphPrinterLegacyPassPass(PassRegistry&); void initializeCallGraphViewerPass(PassRegistry&); Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -859,8 +859,6 @@ (Opts.OptimizationLevel > 1)); Opts.RerollLoops = Args.hasArg(OPT_freroll_loops); - Opts.DisableIntegratedAS = Args.hasArg(OPT_fno_integrated_as); - Opts.CallGraphProfile = !Opts.DisableIntegratedAS; Opts.Autolink = !Args.hasArg(OPT_fno_autolink); Opts.SampleProfileFile = std::string(Args.getLastArgValue(OPT_fprofile_sample_use_EQ)); @@ -881,6 +879,9 @@ std::string(Args.getLastArgValue(OPT_fprofile_instrument_use_path_EQ)); if (!Opts.ProfileInstrumentUsePath.empty()) setPGOUseInstrumentor(Opts, Opts.ProfileInstrumentUsePath); + Opts.DisableIntegratedAS = Args.hasArg(OPT_fno_integrated_as); + Opts.CallGraphProfile = Opts.getProfileUse() != CodeGenOptions::ProfileNone && + !Opts.DisableIntegratedAS; Opts.ProfileRemappingFile = std::string(Args.getLastArgValue(OPT_fprofile_remapping_file_EQ)); if (!Opts.ProfileRemappingFile.empty() && !Opts.ExperimentalNewPassManager) { Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -620,6 +620,7 @@ PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize; PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP; PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop; + PMBuilder.CallGraphProfile = CodeGenOpts.CallGraphProfile; PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; // Loop interleaving in the loop vectorizer has historically been set to be
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits