Skip to content

Commit 6072608

Browse files
committed
[Instrumentation] Fix EdgeCounts vector size in SetBranchWeights
SetBranchWeights() calculates the size of the EdgeCounts vector using OutEdges.Size(), but this is an under-estimate with coroutines. Use the number of successors, as the vector will be indexed by the result of the GetSuccessorNumber() function. Rename the Size local, to make it clear what it refers to. This crashes in 18.1, but somehow doesn't in main. Still, it looks like the fix is applicable to both. Fixes #97962
1 parent 986ceae commit 6072608

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,11 +1623,13 @@ void PGOUseFunc::setBranchWeights() {
16231623
continue;
16241624

16251625
// We have a non-zero Branch BB.
1626-
unsigned Size = BBCountInfo.OutEdges.size();
1627-
SmallVector<uint64_t, 2> EdgeCounts(Size, 0);
1626+
unsigned OutEdgesCount = BBCountInfo.OutEdges.size();
1627+
unsigned SuccessorCount = BB.getTerminator()->getNumSuccessors();
1628+
1629+
SmallVector<uint64_t, 2> EdgeCounts(SuccessorCount, 0);
16281630
uint64_t MaxCount = 0;
1629-
for (unsigned s = 0; s < Size; s++) {
1630-
const PGOUseEdge *E = BBCountInfo.OutEdges[s];
1631+
for (unsigned It = 0; It < OutEdgesCount; It++) {
1632+
const PGOUseEdge *E = BBCountInfo.OutEdges[It];
16311633
const BasicBlock *SrcBB = E->SrcBB;
16321634
const BasicBlock *DestBB = E->DestBB;
16331635
if (DestBB == nullptr)

0 commit comments

Comments
 (0)