Index: lib/Transforms/Instrumentation/CFGMST.h =================================================================== --- lib/Transforms/Instrumentation/CFGMST.h +++ lib/Transforms/Instrumentation/CFGMST.h @@ -53,8 +53,8 @@ // Union BB1 and BB2 into the same group and return true. // Returns false if BB1 and BB2 are already in the same group. bool unionGroups(const BasicBlock *BB1, const BasicBlock *BB2) { - BBInfo *BB1G = findAndCompressGroup(&getBBInfo(BB1)); - BBInfo *BB2G = findAndCompressGroup(&getBBInfo(BB2)); + BBInfo *BB1G = findAndCompressGroup(getBBInfo(BB1)); + BBInfo *BB2G = findAndCompressGroup(getBBInfo(BB2)); if (BB1G == BB2G) return false; @@ -72,10 +72,11 @@ } // Give BB, return the auxiliary information. - BBInfo &getBBInfo(const BasicBlock *BB) const { + BBInfo *getBBInfo(const BasicBlock *BB) const { auto It = BBInfos.find(BB); - assert(It->second.get() != nullptr); - return *It->second.get(); + if (It == BBInfos.end()) + return nullptr; + return It->second.get(); } // Traverse the CFG using a stack. Find all the edges and assign the weight. @@ -176,8 +177,8 @@ << " (*: Instrument, C: CriticalEdge, -: Removed)\n"; uint32_t Count = 0; for (auto &EI : AllEdges) - OS << " Edge " << Count++ << ": " << getBBInfo(EI->SrcBB).Index << "-->" - << getBBInfo(EI->DestBB).Index << EI->infoString() << "\n"; + OS << " Edge " << Count++ << ": " << getBBInfo(EI->SrcBB)->Index << "-->" + << getBBInfo(EI->DestBB)->Index << EI->infoString() << "\n"; } // Add an edge to AllEdges with weight W. Index: lib/Transforms/Instrumentation/PGOInstrumentation.cpp =================================================================== --- lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -325,7 +325,7 @@ BasicBlock *getInstrBB(Edge *E); // Return the auxiliary BB information. - BBInfo &getBBInfo(const BasicBlock *BB) const { return MST.getBBInfo(BB); } + BBInfo *getBBInfo(const BasicBlock *BB) const { return MST.getBBInfo(BB); } // Dump edges and BB information. void dumpInfo(std::string Str = "") const { @@ -386,7 +386,7 @@ const TerminatorInst *TI = BB.getTerminator(); for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) { BasicBlock *Succ = TI->getSuccessor(I); - uint32_t Index = getBBInfo(Succ).Index; + uint32_t Index = getBBInfo(Succ)->Index; for (int J = 0; J < 4; J++) Indexes.push_back((char)(Index >> (J * 8))); } @@ -507,8 +507,8 @@ // For a critical edge, we have to split. Instrument the newly // created BB. NumOfPGOSplit++; - DEBUG(dbgs() << "Split critical edge: " << getBBInfo(SrcBB).Index << " --> " - << getBBInfo(DestBB).Index << "\n"); + DEBUG(dbgs() << "Split critical edge: " << getBBInfo(SrcBB)->Index << " --> " + << getBBInfo(DestBB)->Index << "\n"); unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB); BasicBlock *InstrBB = SplitCriticalEdge(TI, SuccNum); assert(InstrBB && "Critical edge is not split"); @@ -668,7 +668,7 @@ InstrProfRecord &getProfileRecord() { return ProfileRecord; } // Return the auxiliary BB information. - UseBBInfo &getBBInfo(const BasicBlock *BB) const { + UseBBInfo *getBBInfo(const BasicBlock *BB) const { return FuncInfo.getBBInfo(BB); } @@ -739,7 +739,7 @@ continue; uint64_t CountValue = CountFromProfile[I++]; if (!E->Removed) { - getBBInfo(InstrBB).setBBInfoCount(CountValue); + getBBInfo(InstrBB)->setBBInfoCount(CountValue); E->setEdgeCount(CountValue); continue; } @@ -754,7 +754,7 @@ PGOUseEdge &NewEdge1 = FuncInfo.MST.addEdge(InstrBB, DestBB, 0); NewEdge1.setEdgeCount(CountValue); NewEdge1.InMST = true; - getBBInfo(InstrBB).setBBInfoCount(CountValue); + getBBInfo(InstrBB)->setBBInfoCount(CountValue); } ProfileCountSize = CountFromProfile.size(); CountPosition = I; @@ -768,8 +768,8 @@ continue; E->setEdgeCount(Value); - getBBInfo(E->SrcBB).UnknownCountOutEdge--; - getBBInfo(E->DestBB).UnknownCountInEdge--; + getBBInfo(E->SrcBB)->UnknownCountOutEdge--; + getBBInfo(E->DestBB)->UnknownCountInEdge--; return; } llvm_unreachable("Cannot find the unknown count edge"); @@ -817,8 +817,8 @@ DEBUG(dbgs() << "SUM = " << ValueSum << "\n"); - getBBInfo(nullptr).UnknownCountOutEdge = 2; - getBBInfo(nullptr).UnknownCountInEdge = 2; + getBBInfo(nullptr)->UnknownCountOutEdge = 2; + getBBInfo(nullptr)->UnknownCountInEdge = 2; setInstrumentedCounts(CountFromProfile); ProgramMaxCount = PGOReader->getMaximumFunctionCount(); @@ -835,17 +835,17 @@ const BasicBlock *SrcBB = E->SrcBB; const BasicBlock *DestBB = E->DestBB; - UseBBInfo &SrcInfo = getBBInfo(SrcBB); - UseBBInfo &DestInfo = getBBInfo(DestBB); - SrcInfo.OutEdges.push_back(E.get()); - DestInfo.InEdges.push_back(E.get()); - SrcInfo.UnknownCountOutEdge++; - DestInfo.UnknownCountInEdge++; + UseBBInfo *SrcInfo = getBBInfo(SrcBB); + UseBBInfo *DestInfo = getBBInfo(DestBB); + SrcInfo->OutEdges.push_back(E.get()); + DestInfo->InEdges.push_back(E.get()); + SrcInfo->UnknownCountOutEdge++; + DestInfo->UnknownCountInEdge++; if (!E->CountValid) continue; - DestInfo.UnknownCountInEdge--; - SrcInfo.UnknownCountOutEdge--; + DestInfo->UnknownCountInEdge--; + SrcInfo->UnknownCountOutEdge--; } bool Changes = true; @@ -857,27 +857,29 @@ // For efficient traversal, it's better to start from the end as most // of the instrumented edges are at the end. for (auto &BB : reverse(F)) { - UseBBInfo &Count = getBBInfo(&BB); - if (!Count.CountValid) { - if (Count.UnknownCountOutEdge == 0) { - Count.CountValue = sumEdgeCount(Count.OutEdges); - Count.CountValid = true; + UseBBInfo *Count = getBBInfo(&BB); + if (Count == nullptr) + continue; + if (!Count->CountValid) { + if (Count->UnknownCountOutEdge == 0) { + Count->CountValue = sumEdgeCount(Count->OutEdges); + Count->CountValid = true; Changes = true; - } else if (Count.UnknownCountInEdge == 0) { - Count.CountValue = sumEdgeCount(Count.InEdges); - Count.CountValid = true; + } else if (Count->UnknownCountInEdge == 0) { + Count->CountValue = sumEdgeCount(Count->InEdges); + Count->CountValid = true; Changes = true; } } - if (Count.CountValid) { - if (Count.UnknownCountOutEdge == 1) { - uint64_t Total = Count.CountValue - sumEdgeCount(Count.OutEdges); - setEdgeCount(Count.OutEdges, Total); + if (Count->CountValid) { + if (Count->UnknownCountOutEdge == 1) { + uint64_t Total = Count->CountValue - sumEdgeCount(Count->OutEdges); + setEdgeCount(Count->OutEdges, Total); Changes = true; } - if (Count.UnknownCountInEdge == 1) { - uint64_t Total = Count.CountValue - sumEdgeCount(Count.InEdges); - setEdgeCount(Count.InEdges, Total); + if (Count->UnknownCountInEdge == 1) { + uint64_t Total = Count->CountValue - sumEdgeCount(Count->InEdges); + setEdgeCount(Count->InEdges, Total); Changes = true; } } @@ -887,14 +889,21 @@ DEBUG(dbgs() << "Populate counts in " << NumPasses << " passes.\n"); #ifndef NDEBUG // Assert every BB has a valid counter. - for (auto &BB : F) - assert(getBBInfo(&BB).CountValid && "BB count is not valid"); + for (auto &BB : F) { + auto BI = getBBInfo(&BB); + if (BI != nullptr) + assert(BI->CountValid && "BB count is not valid"); + } #endif - uint64_t FuncEntryCount = getBBInfo(&*F.begin()).CountValue; + uint64_t FuncEntryCount = getBBInfo(&*F.begin())->CountValue; F.setEntryCount(FuncEntryCount); uint64_t FuncMaxCount = FuncEntryCount; - for (auto &BB : F) - FuncMaxCount = std::max(FuncMaxCount, getBBInfo(&BB).CountValue); + for (auto &BB : F) { + auto BI = getBBInfo(&BB); + if (BI == nullptr) + continue; + FuncMaxCount = std::max(FuncMaxCount, BI->CountValue); + } markFunctionAttributes(FuncEntryCount, FuncMaxCount); // Now annotate select instructions @@ -929,16 +938,16 @@ continue; if (!isa(TI) && !isa(TI)) continue; - if (getBBInfo(&BB).CountValue == 0) + if (getBBInfo(&BB)->CountValue == 0) continue; // We have a non-zero Branch BB. - const UseBBInfo &BBCountInfo = getBBInfo(&BB); - unsigned Size = BBCountInfo.OutEdges.size(); + const UseBBInfo *BBCountInfo = getBBInfo(&BB); + unsigned Size = BBCountInfo->OutEdges.size(); SmallVector EdgeCounts(Size, 0); uint64_t MaxCount = 0; for (unsigned s = 0; s < Size; s++) { - const PGOUseEdge *E = BBCountInfo.OutEdges[s]; + const PGOUseEdge *E = BBCountInfo->OutEdges[s]; const BasicBlock *SrcBB = E->SrcBB; const BasicBlock *DestBB = E->DestBB; if (DestBB == nullptr) @@ -974,7 +983,7 @@ uint64_t SCounts[2]; SCounts[0] = CountFromProfile[*CurCtrIdx]; // True count ++(*CurCtrIdx); - uint64_t TotalCount = UseFunc->getBBInfo(SI.getParent()).CountValue; + uint64_t TotalCount = UseFunc->getBBInfo(SI.getParent())->CountValue; // False Count SCounts[1] = (TotalCount > SCounts[0] ? TotalCount - SCounts[0] : 0); uint64_t MaxCount = std::max(SCounts[0], SCounts[1]); Index: test/Transforms/PGOProfile/Inputs/unreachable_bb.proftext =================================================================== --- /dev/null +++ test/Transforms/PGOProfile/Inputs/unreachable_bb.proftext @@ -0,0 +1,9 @@ +# IR level Instrumentation Flag +:ir +foo +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +0 Index: test/Transforms/PGOProfile/unreachable_bb.ll =================================================================== --- /dev/null +++ test/Transforms/PGOProfile/unreachable_bb.ll @@ -0,0 +1,23 @@ +; RUN: llvm-profdata merge %S/Inputs/unreachable_bb.proftext -o %t.profdata +; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE +; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @foo() { +entry: + call void @bar() + unreachable +return: + ret void +} + +declare void @bar() + +;USE: !0 = !{i32 1, !"ProfileSummary", !1} +;USE: !1 = !{!2, !3, !4, !5, !6, !7, !8, !9} +;USE: !2 = !{!"ProfileFormat", !"InstrProf"} +;USE: !3 = !{!"TotalCount", i64 0} + +