llvm.org GIT mirror llvm / 9a320d9
[PGO] detect infinite loop and form MST properly Differential Revision: http://reviews.llvm.org/D40702 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319794 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 1 year, 10 months ago
3 changed file(s) with 76 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
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 "
151 << HB->getName() << " to exit"
152 << " w = " << UINT64_MAX << "\n");
153 }
138154 }
139155
140156 // Sort CFG edges based on its weight.
211227
212228 BranchProbabilityInfo *BPI;
213229 BlockFrequencyInfo *BFI;
230 LoopInfo *LI;
214231
215232 public:
216233 CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,
217 BlockFrequencyInfo *BFI_ = nullptr)
218 : F(Func), BPI(BPI_), BFI(BFI_) {
234 BlockFrequencyInfo *BFI_ = nullptr,
235 LoopInfo *LI_ = nullptr)
236 : F(Func), BPI(BPI_), BFI(BFI_), LI(LI_) {
219237 buildEdges();
220238 sortEdgesByWeight();
221239 computeMinimumSpanningTree();
389389
390390 void getAnalysisUsage(AnalysisUsage &AU) const override {
391391 AU.addRequired();
392 AU.addRequired();
392393 }
393394 };
394395
414415
415416 void getAnalysisUsage(AnalysisUsage &AU) const override {
416417 AU.addRequired();
418 AU.addRequired();
417419 }
418420 };
419421
425427 "PGO instrumentation.", false, false)
426428 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
427429 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
430 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
428431 INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
429432 "PGO instrumentation.", false, false)
430433
438441 "Read PGO instrumentation profile.", false, false)
439442 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
440443 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
444 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
441445 INITIALIZE_PASS_END(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
442446 "Read PGO instrumentation profile.", false, false)
443447
529533 Function &Func,
530534 std::unordered_multimap &ComdatMembers,
531535 bool CreateGlobalVar = false, BranchProbabilityInfo *BPI = nullptr,
532 BlockFrequencyInfo *BFI = nullptr)
536 BlockFrequencyInfo *BFI = nullptr, LoopInfo *LI = nullptr)
533537 : F(Func), ComdatMembers(ComdatMembers), ValueSites(IPVK_Last + 1),
534 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI) {
538 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI, LI) {
535539 // This should be done before CFG hash computation.
536540 SIVisitor.countSelects(Func);
537541 MIVisitor.countMemIntrinsics(Func);
714718 // Critical edges will be split.
715719 static void instrumentOneFunc(
716720 Function &F, Module *M, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI,
721 LoopInfo *LI,
717722 std::unordered_multimap &ComdatMembers) {
718723 FuncPGOInstrumentation FuncInfo(F, ComdatMembers, true, BPI,
719 BFI);
724 BFI, LI);
720725 unsigned NumCounters = FuncInfo.getNumCounters();
721726
722727 uint32_t I = 0;
843848 PGOUseFunc(Function &Func, Module *Modu,
844849 std::unordered_multimap &ComdatMembers,
845850 BranchProbabilityInfo *BPI = nullptr,
846 BlockFrequencyInfo *BFIin = nullptr)
851 BlockFrequencyInfo *BFIin = nullptr,
852 LoopInfo *LI = nullptr)
847853 : F(Func), M(Modu), BFI(BFIin),
848 FuncInfo(Func, ComdatMembers, false, BPI, BFIin),
854 FuncInfo(Func, ComdatMembers, false, BPI, BFIin, LI),
849855 FreqAttr(FFA_Normal) {}
850856
851857 // Read counts for the instrumented BB from profile.
13781384
13791385 static bool InstrumentAllFunctions(
13801386 Module &M, function_ref LookupBPI,
1381 function_ref LookupBFI) {
1387 function_ref LookupBFI,
1388 function_ref LookupLI) {
13821389 createIRLevelProfileFlagVariable(M);
13831390 std::unordered_multimap ComdatMembers;
13841391 collectComdatMembers(M, ComdatMembers);
13881395 continue;
13891396 auto *BPI = LookupBPI(F);
13901397 auto *BFI = LookupBFI(F);
1391 instrumentOneFunc(F, &M, BPI, BFI, ComdatMembers);
1398 auto *LI = LookupLI(F);
1399 instrumentOneFunc(F, &M, BPI, BFI, LI, ComdatMembers);
13921400 }
13931401 return true;
13941402 }
14031411 auto LookupBFI = [this](Function &F) {
14041412 return &this->getAnalysis(F).getBFI();
14051413 };
1406 return InstrumentAllFunctions(M, LookupBPI, LookupBFI);
1414 auto LookupLI = [this](Function &F) {
1415 return &this->getAnalysis(F).getLoopInfo();
1416 };
1417 return InstrumentAllFunctions(M, LookupBPI, LookupBFI, LookupLI);
14071418 }
14081419
14091420 PreservedAnalyses PGOInstrumentationGen::run(Module &M,
14171428 return &FAM.getResult(F);
14181429 };
14191430
1420 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI))
1431 auto LookupLI = [&FAM](Function &F) {
1432 return &FAM.getResult(F);
1433 };
1434
1435 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI, LookupLI))
14211436 return PreservedAnalyses::all();
14221437
14231438 return PreservedAnalyses::none();
14261441 static bool annotateAllFunctions(
14271442 Module &M, StringRef ProfileFileName,
14281443 function_ref LookupBPI,
1429 function_ref LookupBFI) {
1444 function_ref LookupBFI,
1445 function_ref LookupLI) {
14301446 DEBUG(dbgs() << "Read in profile counters: ");
14311447 auto &Ctx = M.getContext();
14321448 // Read the counter array from file.
14621478 continue;
14631479 auto *BPI = LookupBPI(F);
14641480 auto *BFI = LookupBFI(F);
1465 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI);
1481 auto *LI = LookupLI(F);
1482 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI, LI);
14661483 if (!Func.readCounters(PGOReader.get()))
14671484 continue;
14681485 Func.populateCounters();
15381555 return &FAM.getResult(F);
15391556 };
15401557
1541 if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI))
1558 auto LookupLI = [&FAM](Function &F) {
1559 return &FAM.getResult(F);
1560 };
1561
1562 if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI, LookupLI))
15421563 return PreservedAnalyses::all();
15431564
15441565 return PreservedAnalyses::none();
15541575 auto LookupBFI = [this](Function &F) {
15551576 return &this->getAnalysis(F).getBFI();
15561577 };
1557
1558 return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI);
1578 auto LookupLI = [this](Function &F) {
1579 return &this->getAnalysis(F).getLoopInfo();
1580 };
1581
1582 return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI, LookupLI);
15591583 }
15601584
15611585 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