llvm.org GIT mirror llvm / 134f1a8
[PGO] detect infinite loop and form MST properly Differential Revision: http://reviews.llvm.org/D40873 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320104 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 1 year, 10 months ago
6 changed file(s) with 56 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
5555
5656 const Function *getFunction() const;
5757 const BranchProbabilityInfo *getBPI() const;
58 const LoopInfo *getLoopInfo() const;
5859 void view() const;
5960
6061 /// getblockFreq - Return block frequency. Return 0 if we don't have the
992992 }
993993
994994 const BranchProbabilityInfoT &getBPI() const { return *BPI; }
995 const LoopInfoT &getLoopInfo() const { return *LI; }
995996
996997 /// \brief Print the frequencies for the current function.
997998 ///
263263 return BFI ? &BFI->getBPI() : nullptr;
264264 }
265265
266 const LoopInfo *BlockFrequencyInfo::getLoopInfo() const {
267 return BFI ? &BFI->getLoopInfo() : nullptr;
268 }
269
266270 raw_ostream &BlockFrequencyInfo::
267271 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
268272 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
1919 #include "llvm/Analysis/BlockFrequencyInfo.h"
2020 #include "llvm/Analysis/BranchProbabilityInfo.h"
2121 #include "llvm/Analysis/CFG.h"
22 #include "llvm/Analysis/LoopInfo.h"
2223 #include "llvm/Support/BranchProbability.h"
2324 #include "llvm/Support/Debug.h"
2425 #include "llvm/Support/raw_ostream.h"
135136 << " w = " << BBWeight << "\n");
136137 }
137138 }
139 // check if there is any infinite loop. If yes, add a fake edge from
140 // the header block to the fake node:
141 for (auto *L : *LI) {
142 SmallVector ExitingBlocks;
143 L->getExitingBlocks(ExitingBlocks);
144 if (!ExitingBlocks.empty())
145 continue;
146 auto *HB = L->getHeader();
147 if (!HB)
148 continue;
149 addEdge(HB, nullptr, UINT64_MAX);
150 DEBUG(dbgs() << " Edge: from infinite loop header " << HB->getName()
151 << " to exit"
152 << " w = " << UINT64_MAX << "\n");
153 }
138154 }
139155
140156 // Sort CFG edges based on its weight.
209225 return *AllEdges.back();
210226 }
211227
212 BranchProbabilityInfo *BPI;
228 const BranchProbabilityInfo *BPI;
213229 BlockFrequencyInfo *BFI;
230 const LoopInfo *LI;
214231
215232 public:
216 CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,
217 BlockFrequencyInfo *BFI_ = nullptr)
218 : F(Func), BPI(BPI_), BFI(BFI_) {
233 CFGMST(Function &Func, BlockFrequencyInfo *BFI_ = nullptr)
234 : F(Func), BPI(BFI_->getBPI()), BFI(BFI_), LI(BFI_->getLoopInfo()) {
219235 buildEdges();
220236 sortEdgesByWeight();
221237 computeMinimumSpanningTree();
424424 INITIALIZE_PASS_BEGIN(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
425425 "PGO instrumentation.", false, false)
426426 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
427 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
428427 INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
429428 "PGO instrumentation.", false, false)
430429
437436 INITIALIZE_PASS_BEGIN(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
438437 "Read PGO instrumentation profile.", false, false)
439438 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
440 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
441439 INITIALIZE_PASS_END(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
442440 "Read PGO instrumentation profile.", false, false)
443441
528526 FuncPGOInstrumentation(
529527 Function &Func,
530528 std::unordered_multimap &ComdatMembers,
531 bool CreateGlobalVar = false, BranchProbabilityInfo *BPI = nullptr,
532 BlockFrequencyInfo *BFI = nullptr)
529 bool CreateGlobalVar = false, BlockFrequencyInfo *BFI = nullptr)
533530 : F(Func), ComdatMembers(ComdatMembers), ValueSites(IPVK_Last + 1),
534 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI) {
531 SIVisitor(Func), MIVisitor(Func), MST(F, BFI) {
535532 // This should be done before CFG hash computation.
536533 SIVisitor.countSelects(Func);
537534 MIVisitor.countMemIntrinsics(Func);
713710 // Visit all edge and instrument the edges not in MST, and do value profiling.
714711 // Critical edges will be split.
715712 static void instrumentOneFunc(
716 Function &F, Module *M, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI,
713 Function &F, Module *M, BlockFrequencyInfo *BFI,
717714 std::unordered_multimap &ComdatMembers) {
718 FuncPGOInstrumentation FuncInfo(F, ComdatMembers, true, BPI,
719 BFI);
715 FuncPGOInstrumentation FuncInfo(F, ComdatMembers, true, BFI);
720716 unsigned NumCounters = FuncInfo.getNumCounters();
721717
722718 uint32_t I = 0;
842838 public:
843839 PGOUseFunc(Function &Func, Module *Modu,
844840 std::unordered_multimap &ComdatMembers,
845 BranchProbabilityInfo *BPI = nullptr,
846841 BlockFrequencyInfo *BFIin = nullptr)
847842 : F(Func), M(Modu), BFI(BFIin),
848 FuncInfo(Func, ComdatMembers, false, BPI, BFIin),
849 FreqAttr(FFA_Normal) {}
843 FuncInfo(Func, ComdatMembers, false, BFIin), FreqAttr(FFA_Normal) {}
850844
851845 // Read counts for the instrumented BB from profile.
852846 bool readCounters(IndexedInstrProfReader *PGOReader);
13771371 }
13781372
13791373 static bool InstrumentAllFunctions(
1380 Module &M, function_ref LookupBPI,
1381 function_ref LookupBFI) {
1374 Module &M, function_ref LookupBFI) {
13821375 createIRLevelProfileFlagVariable(M);
13831376 std::unordered_multimap ComdatMembers;
13841377 collectComdatMembers(M, ComdatMembers);
13861379 for (auto &F : M) {
13871380 if (F.isDeclaration())
13881381 continue;
1389 auto *BPI = LookupBPI(F);
13901382 auto *BFI = LookupBFI(F);
1391 instrumentOneFunc(F, &M, BPI, BFI, ComdatMembers);
1383 instrumentOneFunc(F, &M, BFI, ComdatMembers);
13921384 }
13931385 return true;
13941386 }
13971389 if (skipModule(M))
13981390 return false;
13991391
1400 auto LookupBPI = [this](Function &F) {
1401 return &this->getAnalysis(F).getBPI();
1402 };
14031392 auto LookupBFI = [this](Function &F) {
14041393 return &this->getAnalysis(F).getBFI();
14051394 };
1406 return InstrumentAllFunctions(M, LookupBPI, LookupBFI);
1395 return InstrumentAllFunctions(M, LookupBFI);
14071396 }
14081397
14091398 PreservedAnalyses PGOInstrumentationGen::run(Module &M,
14101399 ModuleAnalysisManager &AM) {
14111400 auto &FAM = AM.getResult(M).getManager();
1412 auto LookupBPI = [&FAM](Function &F) {
1413 return &FAM.getResult(F);
1414 };
14151401
14161402 auto LookupBFI = [&FAM](Function &F) {
14171403 return &FAM.getResult(F);
14181404 };
14191405
1420 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI))
1406 if (!InstrumentAllFunctions(M, LookupBFI))
14211407 return PreservedAnalyses::all();
14221408
14231409 return PreservedAnalyses::none();
14251411
14261412 static bool annotateAllFunctions(
14271413 Module &M, StringRef ProfileFileName,
1428 function_ref LookupBPI,
14291414 function_ref LookupBFI) {
14301415 DEBUG(dbgs() << "Read in profile counters: ");
14311416 auto &Ctx = M.getContext();
14601445 for (auto &F : M) {
14611446 if (F.isDeclaration())
14621447 continue;
1463 auto *BPI = LookupBPI(F);
14641448 auto *BFI = LookupBFI(F);
1465 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI);
1449 PGOUseFunc Func(F, &M, ComdatMembers, BFI);
14661450 if (!Func.readCounters(PGOReader.get()))
14671451 continue;
14681452 Func.populateCounters();
15301514 ModuleAnalysisManager &AM) {
15311515
15321516 auto &FAM = AM.getResult(M).getManager();
1533 auto LookupBPI = [&FAM](Function &F) {
1534 return &FAM.getResult(F);
1535 };
15361517
15371518 auto LookupBFI = [&FAM](Function &F) {
15381519 return &FAM.getResult(F);
15391520 };
15401521
1541 if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI))
1522 if (!annotateAllFunctions(M, ProfileFileName, LookupBFI))
15421523 return PreservedAnalyses::all();
15431524
15441525 return PreservedAnalyses::none();
15481529 if (skipModule(M))
15491530 return false;
15501531
1551 auto LookupBPI = [this](Function &F) {
1552 return &this->getAnalysis(F).getBPI();
1553 };
15541532 auto LookupBFI = [this](Function &F) {
15551533 return &this->getAnalysis(F).getBFI();
15561534 };
15571535
1558 return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI);
1536 return annotateAllFunctions(M, ProfileFileName, LookupBFI);
15591537 }
15601538
15611539 static std::string getSimpleNodeName(const BasicBlock *Node) {
0 ; RUN: opt < %s -pgo-instr-gen -S -o - | FileCheck %s
1
2 define void @foo() {
3 entry:
4 br label %while.body
5 ; CHECK: llvm.instrprof.increment
6
7 while.body: ; preds = %entry, %while.body
8 ; CHECK: llvm.instrprof.increment
9 call void (...) @bar() #2
10 br label %while.body
11 }
12
13 declare void @bar(...)
14
15
16 attributes #0 = { nounwind }
17