Skip to content

Commit f131edf

Browse files
avikivitytru
authored andcommitted
[Instrumentation] Fix EdgeCounts vector size in SetBranchWeights (#99064)
(cherry picked from commit 46a4132)
1 parent 1ccd19c commit f131edf

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -1625,11 +1625,17 @@ void PGOUseFunc::setBranchWeights() {
16251625
continue;
16261626

16271627
// We have a non-zero Branch BB.
1628-
unsigned Size = BBCountInfo.OutEdges.size();
1629-
SmallVector<uint64_t, 2> EdgeCounts(Size, 0);
1628+
1629+
// SuccessorCount can be greater than OutEdgesCount, because
1630+
// removed edges don't appear in OutEdges.
1631+
unsigned OutEdgesCount = BBCountInfo.OutEdges.size();
1632+
unsigned SuccessorCount = BB.getTerminator()->getNumSuccessors();
1633+
assert(OutEdgesCount <= SuccessorCount);
1634+
1635+
SmallVector<uint64_t, 2> EdgeCounts(SuccessorCount, 0);
16301636
uint64_t MaxCount = 0;
1631-
for (unsigned s = 0; s < Size; s++) {
1632-
const PGOUseEdge *E = BBCountInfo.OutEdges[s];
1637+
for (unsigned It = 0; It < OutEdgesCount; It++) {
1638+
const PGOUseEdge *E = BBCountInfo.OutEdges[It];
16331639
const BasicBlock *SrcBB = E->SrcBB;
16341640
const BasicBlock *DestBB = E->DestBB;
16351641
if (DestBB == nullptr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: rm -rf %t && split-file %s %t
2+
3+
; RUN: llvm-profdata merge %t/a.proftext -o %t/a.profdata
4+
; RUN: opt < %t/a.ll --passes=pgo-instr-use -pgo-test-profile-file=%t/a.profdata
5+
6+
;--- a.ll
7+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
8+
target triple = "x86_64-redhat-linux-gnu"
9+
10+
define void @_bar() presplitcoroutine personality ptr null {
11+
%1 = call token @llvm.coro.save(ptr null)
12+
%2 = call i8 @llvm.coro.suspend(token none, i1 false)
13+
switch i8 %2, label %5 [
14+
i8 0, label %3
15+
i8 1, label %4
16+
]
17+
18+
3: ; preds = %0
19+
ret void
20+
21+
4: ; preds = %0
22+
ret void
23+
24+
5: ; preds = %0
25+
ret void
26+
}
27+
28+
declare token @llvm.coro.save(ptr)
29+
30+
declare i8 @llvm.coro.suspend(token, i1)
31+
32+
;--- a.proftext
33+
# IR level Instrumentation Flag
34+
:ir
35+
36+
_bar
37+
# Func Hash:
38+
1063705160175073211
39+
# Num Counters:
40+
2
41+
1
42+
0

0 commit comments

Comments
 (0)