Skip to content

Commit a33accd

Browse files
author
Andy Kaylor
committed
[PGO] Early detection regarding whether pgo counter promotion is possible
Patch by Chris Chrulski This fixes a problem with the current behavior when assertions are enabled. A loop that exits to a catchswitch instruction is skipped for the counter promotion, however this check was being done after the PGOCounterPromoter tried to collect an insertion point for the exit block. A call to getFirstInsertionPt() on a block that begins with a catchswitch instruction triggers an assertion. This change performs a check whether the counter promotion is possible prior to collecting the ExitBlocks and InsertPts. Differential Revision: https://reviews.llvm.org/D73222
1 parent 50a3ff3 commit a33accd

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,14 @@ class PGOCounterPromoter {
247247
: LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop),
248248
LI(LI), BFI(BFI) {
249249

250+
// Skip collection of ExitBlocks and InsertPts for loops that will not be
251+
// able to have counters promoted.
250252
SmallVector<BasicBlock *, 8> LoopExitBlocks;
251253
SmallPtrSet<BasicBlock *, 8> BlockSet;
254+
252255
L.getExitBlocks(LoopExitBlocks);
256+
if (!isPromotionPossible(&L, LoopExitBlocks))
257+
return;
253258

254259
for (BasicBlock *ExitBlock : LoopExitBlocks) {
255260
if (BlockSet.insert(ExitBlock).second) {
@@ -318,21 +323,31 @@ class PGOCounterPromoter {
318323
return true;
319324
}
320325

321-
// Returns the max number of Counter Promotions for LP.
322-
unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
326+
// Check whether the loop satisfies the basic conditions needed to perform
327+
// Counter Promotions.
328+
bool isPromotionPossible(Loop *LP,
329+
const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
323330
// We can't insert into a catchswitch.
324-
SmallVector<BasicBlock *, 8> LoopExitBlocks;
325-
LP->getExitBlocks(LoopExitBlocks);
326331
if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
327332
return isa<CatchSwitchInst>(Exit->getTerminator());
328333
}))
329-
return 0;
334+
return false;
330335

331336
if (!LP->hasDedicatedExits())
332-
return 0;
337+
return false;
333338

334339
BasicBlock *PH = LP->getLoopPreheader();
335340
if (!PH)
341+
return false;
342+
343+
return true;
344+
}
345+
346+
// Returns the max number of Counter Promotions for LP.
347+
unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
348+
SmallVector<BasicBlock *, 8> LoopExitBlocks;
349+
LP->getExitBlocks(LoopExitBlocks);
350+
if (!isPromotionPossible(LP, LoopExitBlocks))
336351
return 0;
337352

338353
SmallVector<BasicBlock *, 8> ExitingBlocks;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; Test that instrumentation counter promotion for loops does not fail during
2+
; compilation for loops that exit to a catchswitch block. In this case, counters
3+
; do not get promoted out of the loop body.
4+
5+
; RUN: opt < %s -pgo-instr-gen -instrprof -do-counter-promotion=true -S | FileCheck %s
6+
; RUN: opt < %s -passes=pgo-instr-gen,instrprof -do-counter-promotion=true -S | FileCheck %s
7+
8+
; Source used to create test:
9+
;
10+
; extern void may_throw(int);
11+
; char buffer[200];
12+
; void run(int count) {
13+
; try {
14+
; for (int i = 0; i < count; ++i) {
15+
; if (buffer[i] == 0)
16+
; break;
17+
; may_throw(i);
18+
; }
19+
; }
20+
; catch (...) {
21+
; throw;
22+
; }
23+
;}
24+
25+
%eh.ThrowInfo = type { i32, i32, i32, i32 }
26+
27+
@"?buffer@@3PADA" = dso_local local_unnamed_addr global [200 x i8] zeroinitializer, align 16
28+
define dso_local void @"?run@@YAXH@Z"(i32 %count) local_unnamed_addr personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
29+
entry:
30+
br label %for.cond
31+
32+
for.cond: ; preds = %for.inc, %entry
33+
%i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
34+
%cmp = icmp slt i32 %i.0, %count
35+
br i1 %cmp, label %for.body, label %cleanup
36+
37+
for.body: ; preds = %for.cond
38+
; CHECK: for.body:
39+
; CHECK: %pgocount1 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 0)
40+
; CHECK: %1 = add i64 %pgocount1, 1
41+
; CHECK: store i64 %1, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 0)
42+
%idxprom = zext i32 %i.0 to i64
43+
%arrayidx = getelementptr inbounds [200 x i8], [200 x i8]* @"?buffer@@3PADA", i64 0, i64 %idxprom
44+
%0 = load i8, i8* %arrayidx, align 1
45+
%cmp1 = icmp eq i8 %0, 0
46+
br i1 %cmp1, label %cleanup, label %if.end
47+
48+
if.end: ; preds = %for.body
49+
invoke void @"?may_throw@@YAXH@Z"(i32 %i.0)
50+
to label %for.inc unwind label %catch.dispatch
51+
52+
for.inc: ; preds = %if.end
53+
; CHECK: for.inc:
54+
; CHECK: %pgocount2 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 1)
55+
; CHECK: %3 = add i64 %pgocount2, 1
56+
; CHECK: store i64 %3, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH@Z", i64 0, i64 1)
57+
%inc = add nuw nsw i32 %i.0, 1
58+
br label %for.cond
59+
60+
cleanup: ; preds = %for.body, %for.cond
61+
ret void
62+
63+
catch.dispatch: ; preds = %if.end
64+
%1 = catchswitch within none [label %catch] unwind to caller
65+
66+
catch: ; preds = %catch.dispatch
67+
%2 = catchpad within %1 [i8* null, i32 64, i8* null]
68+
call void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #2 [ "funclet"(token %2) ]
69+
unreachable
70+
}
71+
declare dso_local void @"?may_throw@@YAXH@Z"(i32)
72+
declare dso_local void @_CxxThrowException(i8*, %eh.ThrowInfo*)
73+
declare dso_local i32 @__CxxFrameHandler3(...)

0 commit comments

Comments
 (0)