llvm.org GIT mirror llvm / 4f7feb4
Revert r319794: [PGO] detect infinite loop and form MST properly: memory leak problem git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319841 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 1 year, 10 months ago
3 changed file(s) with 16 addition(s) and 76 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"
2322 #include "llvm/Support/BranchProbability.h"
2423 #include "llvm/Support/Debug.h"
2524 #include "llvm/Support/raw_ostream.h"
136135 << " w = " << BBWeight << "\n");
137136 }
138137 }
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 }
154138 }
155139
156140 // Sort CFG edges based on its weight.
227211
228212 BranchProbabilityInfo *BPI;
229213 BlockFrequencyInfo *BFI;
230 LoopInfo *LI;
231214
232215 public:
233216 CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,
234 BlockFrequencyInfo *BFI_ = nullptr,
235 LoopInfo *LI_ = nullptr)
236 : F(Func), BPI(BPI_), BFI(BFI_), LI(LI_) {
217 BlockFrequencyInfo *BFI_ = nullptr)
218 : F(Func), BPI(BPI_), BFI(BFI_) {
237219 buildEdges();
238220 sortEdgesByWeight();
239221 computeMinimumSpanningTree();
389389
390390 void getAnalysisUsage(AnalysisUsage &AU) const override {
391391 AU.addRequired();
392 AU.addRequired();
393392 }
394393 };
395394
415414
416415 void getAnalysisUsage(AnalysisUsage &AU) const override {
417416 AU.addRequired();
418 AU.addRequired();
419417 }
420418 };
421419
427425 "PGO instrumentation.", false, false)
428426 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
429427 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
430 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
431428 INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
432429 "PGO instrumentation.", false, false)
433430
441438 "Read PGO instrumentation profile.", false, false)
442439 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
443440 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
444 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
445441 INITIALIZE_PASS_END(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
446442 "Read PGO instrumentation profile.", false, false)
447443
533529 Function &Func,
534530 std::unordered_multimap &ComdatMembers,
535531 bool CreateGlobalVar = false, BranchProbabilityInfo *BPI = nullptr,
536 BlockFrequencyInfo *BFI = nullptr, LoopInfo *LI = nullptr)
532 BlockFrequencyInfo *BFI = nullptr)
537533 : F(Func), ComdatMembers(ComdatMembers), ValueSites(IPVK_Last + 1),
538 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI, LI) {
534 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI) {
539535 // This should be done before CFG hash computation.
540536 SIVisitor.countSelects(Func);
541537 MIVisitor.countMemIntrinsics(Func);
718714 // Critical edges will be split.
719715 static void instrumentOneFunc(
720716 Function &F, Module *M, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI,
721 LoopInfo *LI,
722717 std::unordered_multimap &ComdatMembers) {
723718 FuncPGOInstrumentation FuncInfo(F, ComdatMembers, true, BPI,
724 BFI, LI);
719 BFI);
725720 unsigned NumCounters = FuncInfo.getNumCounters();
726721
727722 uint32_t I = 0;
848843 PGOUseFunc(Function &Func, Module *Modu,
849844 std::unordered_multimap &ComdatMembers,
850845 BranchProbabilityInfo *BPI = nullptr,
851 BlockFrequencyInfo *BFIin = nullptr,
852 LoopInfo *LI = nullptr)
846 BlockFrequencyInfo *BFIin = nullptr)
853847 : F(Func), M(Modu), BFI(BFIin),
854 FuncInfo(Func, ComdatMembers, false, BPI, BFIin, LI),
848 FuncInfo(Func, ComdatMembers, false, BPI, BFIin),
855849 FreqAttr(FFA_Normal) {}
856850
857851 // Read counts for the instrumented BB from profile.
13841378
13851379 static bool InstrumentAllFunctions(
13861380 Module &M, function_ref LookupBPI,
1387 function_ref LookupBFI,
1388 function_ref<LoopInfo *(Function &)> LookupLI) {
1381 function_ref<BlockFrequencyInfo *(Function &)> LookupBFI) {
13891382 createIRLevelProfileFlagVariable(M);
13901383 std::unordered_multimap ComdatMembers;
13911384 collectComdatMembers(M, ComdatMembers);
13951388 continue;
13961389 auto *BPI = LookupBPI(F);
13971390 auto *BFI = LookupBFI(F);
1398 auto *LI = LookupLI(F);
1399 instrumentOneFunc(F, &M, BPI, BFI, LI, ComdatMembers);
1391 instrumentOneFunc(F, &M, BPI, BFI, ComdatMembers);
14001392 }
14011393 return true;
14021394 }
14111403 auto LookupBFI = [this](Function &F) {
14121404 return &this->getAnalysis(F).getBFI();
14131405 };
1414 auto LookupLI = [this](Function &F) {
1415 return &this->getAnalysis(F).getLoopInfo();
1416 };
1417 return InstrumentAllFunctions(M, LookupBPI, LookupBFI, LookupLI);
1406 return InstrumentAllFunctions(M, LookupBPI, LookupBFI);
14181407 }
14191408
14201409 PreservedAnalyses PGOInstrumentationGen::run(Module &M,
14281417 return &FAM.getResult(F);
14291418 };
14301419
1431 auto LookupLI = [&FAM](Function &F) {
1432 return &FAM.getResult(F);
1433 };
1434
1435 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI, LookupLI))
1420 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI))
14361421 return PreservedAnalyses::all();
14371422
14381423 return PreservedAnalyses::none();
14411426 static bool annotateAllFunctions(
14421427 Module &M, StringRef ProfileFileName,
14431428 function_ref LookupBPI,
1444 function_ref LookupBFI,
1445 function_ref<LoopInfo *(Function &)> LookupLI) {
1429 function_ref<BlockFrequencyInfo *(Function &)> LookupBFI) {
14461430 DEBUG(dbgs() << "Read in profile counters: ");
14471431 auto &Ctx = M.getContext();
14481432 // Read the counter array from file.
14781462 continue;
14791463 auto *BPI = LookupBPI(F);
14801464 auto *BFI = LookupBFI(F);
1481 auto *LI = LookupLI(F);
1482 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI, LI);
1465 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI);
14831466 if (!Func.readCounters(PGOReader.get()))
14841467 continue;
14851468 Func.populateCounters();
15551538 return &FAM.getResult(F);
15561539 };
15571540
1558 auto LookupLI = [&FAM](Function &F) {
1559 return &FAM.getResult(F);
1560 };
1561
1562 if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI, LookupLI))
1541 if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI))
15631542 return PreservedAnalyses::all();
15641543
15651544 return PreservedAnalyses::none();
15751554 auto LookupBFI = [this](Function &F) {
15761555 return &this->getAnalysis(F).getBFI();
15771556 };
1578 auto LookupLI = [this](Function &F) {
1579 return &this->getAnalysis(F).getLoopInfo();
1580 };
1581
1582 return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI, LookupLI);
1557
1558 return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI);
15831559 }
15841560
15851561 static std::string getSimpleNodeName(const BasicBlock *Node) {
+0
-18
test/Transforms/PGOProfile/infinite_loop_gen.ll less more
None ; 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