llvm.org GIT mirror llvm / 45f64ad
Revert r320104: infinite loop profiling bug fix Causes unexpected memory issue with New PM this time. The new PM invalidates BPI but not BFI, leaving the reference to BPI from BFI invalid. Abandon this patch. There is a more general solution which also handles runtime infinite loop (but not statically). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320180 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 1 year, 10 months ago
6 changed file(s) with 38 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
5555
5656 const Function *getFunction() const;
5757 const BranchProbabilityInfo *getBPI() const;
58 const LoopInfo *getLoopInfo() const;
5958 void view() const;
6059
6160 /// 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; }
996995
997996 /// \brief Print the frequencies for the current function.
998997 ///
263263 return BFI ? &BFI->getBPI() : nullptr;
264264 }
265265
266 const LoopInfo *BlockFrequencyInfo::getLoopInfo() const {
267 return BFI ? &BFI->getLoopInfo() : nullptr;
268 }
269
270266 raw_ostream &BlockFrequencyInfo::
271267 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
272268 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"
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 " << HB->getName()
151 << " to exit"
152 << " w = " << UINT64_MAX << "\n");
153 }
154138 }
155139
156140 // Sort CFG edges based on its weight.
225209 return *AllEdges.back();
226210 }
227211
228 const BranchProbabilityInfo *BPI;
212 BranchProbabilityInfo *BPI;
229213 BlockFrequencyInfo *BFI;
230 const LoopInfo *LI;
231214
232215 public:
233 CFGMST(Function &Func, BlockFrequencyInfo *BFI_ = nullptr)
234 : F(Func), BPI(BFI_->getBPI()), BFI(BFI_), LI(BFI_->getLoopInfo()) {
216 CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,
217 BlockFrequencyInfo *BFI_ = nullptr)
218 : F(Func), BPI(BPI_), BFI(BFI_) {
235219 buildEdges();
236220 sortEdgesByWeight();
237221 computeMinimumSpanningTree();
424424 INITIALIZE_PASS_BEGIN(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
425425 "PGO instrumentation.", false, false)
426426 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
427 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
427428 INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
428429 "PGO instrumentation.", false, false)
429430
436437 INITIALIZE_PASS_BEGIN(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
437438 "Read PGO instrumentation profile.", false, false)
438439 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
440 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
439441 INITIALIZE_PASS_END(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
440442 "Read PGO instrumentation profile.", false, false)
441443
526528 FuncPGOInstrumentation(
527529 Function &Func,
528530 std::unordered_multimap &ComdatMembers,
529 bool CreateGlobalVar = false, BlockFrequencyInfo *BFI = nullptr)
531 bool CreateGlobalVar = false, BranchProbabilityInfo *BPI = nullptr,
532 BlockFrequencyInfo *BFI = nullptr)
530533 : F(Func), ComdatMembers(ComdatMembers), ValueSites(IPVK_Last + 1),
531 SIVisitor(Func), MIVisitor(Func), MST(F, BFI) {
534 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI) {
532535 // This should be done before CFG hash computation.
533536 SIVisitor.countSelects(Func);
534537 MIVisitor.countMemIntrinsics(Func);
710713 // Visit all edge and instrument the edges not in MST, and do value profiling.
711714 // Critical edges will be split.
712715 static void instrumentOneFunc(
713 Function &F, Module *M, BlockFrequencyInfo *BFI,
716 Function &F, Module *M, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI,
714717 std::unordered_multimap &ComdatMembers) {
715 FuncPGOInstrumentation FuncInfo(F, ComdatMembers, true, BFI);
718 FuncPGOInstrumentation FuncInfo(F, ComdatMembers, true, BPI,
719 BFI);
716720 unsigned NumCounters = FuncInfo.getNumCounters();
717721
718722 uint32_t I = 0;
838842 public:
839843 PGOUseFunc(Function &Func, Module *Modu,
840844 std::unordered_multimap &ComdatMembers,
845 BranchProbabilityInfo *BPI = nullptr,
841846 BlockFrequencyInfo *BFIin = nullptr)
842847 : F(Func), M(Modu), BFI(BFIin),
843 FuncInfo(Func, ComdatMembers, false, BFIin), FreqAttr(FFA_Normal) {}
848 FuncInfo(Func, ComdatMembers, false, BPI, BFIin),
849 FreqAttr(FFA_Normal) {}
844850
845851 // Read counts for the instrumented BB from profile.
846852 bool readCounters(IndexedInstrProfReader *PGOReader);
13711377 }
13721378
13731379 static bool InstrumentAllFunctions(
1374 Module &M, function_reflockFrequencyInfo *(Function &)> LookupBFI) {
1380 Module &M, function_refranchProbabilityInfo *(Function &)> LookupBPI,
1381 function_ref LookupBFI) {
13751382 createIRLevelProfileFlagVariable(M);
13761383 std::unordered_multimap ComdatMembers;
13771384 collectComdatMembers(M, ComdatMembers);
13791386 for (auto &F : M) {
13801387 if (F.isDeclaration())
13811388 continue;
1389 auto *BPI = LookupBPI(F);
13821390 auto *BFI = LookupBFI(F);
1383 instrumentOneFunc(F, &M, BFI, ComdatMembers);
1391 instrumentOneFunc(F, &M, BPI, BFI, ComdatMembers);
13841392 }
13851393 return true;
13861394 }
13891397 if (skipModule(M))
13901398 return false;
13911399
1400 auto LookupBPI = [this](Function &F) {
1401 return &this->getAnalysis(F).getBPI();
1402 };
13921403 auto LookupBFI = [this](Function &F) {
13931404 return &this->getAnalysis(F).getBFI();
13941405 };
1395 return InstrumentAllFunctions(M, LookupBFI);
1406 return InstrumentAllFunctions(M, LookupBPI, LookupBFI);
13961407 }
13971408
13981409 PreservedAnalyses PGOInstrumentationGen::run(Module &M,
13991410 ModuleAnalysisManager &AM) {
14001411 auto &FAM = AM.getResult(M).getManager();
1412 auto LookupBPI = [&FAM](Function &F) {
1413 return &FAM.getResult(F);
1414 };
14011415
14021416 auto LookupBFI = [&FAM](Function &F) {
14031417 return &FAM.getResult(F);
14041418 };
14051419
1406 if (!InstrumentAllFunctions(M, LookupBFI))
1420 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI))
14071421 return PreservedAnalyses::all();
14081422
14091423 return PreservedAnalyses::none();
14111425
14121426 static bool annotateAllFunctions(
14131427 Module &M, StringRef ProfileFileName,
1428 function_ref LookupBPI,
14141429 function_ref LookupBFI) {
14151430 DEBUG(dbgs() << "Read in profile counters: ");
14161431 auto &Ctx = M.getContext();
14451460 for (auto &F : M) {
14461461 if (F.isDeclaration())
14471462 continue;
1463 auto *BPI = LookupBPI(F);
14481464 auto *BFI = LookupBFI(F);
1449 PGOUseFunc Func(F, &M, ComdatMembers, BFI);
1465 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI);
14501466 if (!Func.readCounters(PGOReader.get()))
14511467 continue;
14521468 Func.populateCounters();
15141530 ModuleAnalysisManager &AM) {
15151531
15161532 auto &FAM = AM.getResult(M).getManager();
1533 auto LookupBPI = [&FAM](Function &F) {
1534 return &FAM.getResult(F);
1535 };
15171536
15181537 auto LookupBFI = [&FAM](Function &F) {
15191538 return &FAM.getResult(F);
15201539 };
15211540
1522 if (!annotateAllFunctions(M, ProfileFileName, LookupBFI))
1541 if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI))
15231542 return PreservedAnalyses::all();
15241543
15251544 return PreservedAnalyses::none();
15291548 if (skipModule(M))
15301549 return false;
15311550
1551 auto LookupBPI = [this](Function &F) {
1552 return &this->getAnalysis(F).getBPI();
1553 };
15321554 auto LookupBFI = [this](Function &F) {
15331555 return &this->getAnalysis(F).getBFI();
15341556 };
15351557
1536 return annotateAllFunctions(M, ProfileFileName, LookupBFI);
1558 return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI);
15371559 }
15381560
15391561 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