Skip to content

Commit 9839b8c

Browse files
authored
llvm-reduce: Fix assert on invokes with catchswitch (#111838)
This is the minimal change to avoid the assert. There's an API flaw in invoke instructions where getLandingPad assumes all invoke unwind blocks have landingpads, when some have catchswitch instead. Fixes #111817
1 parent a3638f1 commit 9839b8c

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; RUN: llvm-reduce -abort-on-invalid-reduction --delta-passes=basic-blocks --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
2+
; RUN: FileCheck --check-prefix=CHECK-FINAL %s < %t
3+
4+
; Make sure there's no assertion for invoke destinations that don't
5+
; use landingpad (and use catchswitch instead)
6+
7+
; CHECK-INTERESTINGNESS: invoke
8+
9+
; CHECK-FINAL: bb:
10+
; CHECK-FINAL-NEXT: invoke void @llvm.seh.try.begin()
11+
; CHECK-FINAL-NEXT: to label %bb7 unwind label %bb1
12+
; CHECK-FINAL: bb1:
13+
; CHECK-FINAL-NEXT: %i = catchswitch within none [label %bb2] unwind to caller
14+
15+
; CHECK-FINAL: bb2:
16+
; CHECK-FINAL-NEXT: %i3 = catchpad within %i [ptr null]
17+
; CHECK-FINAL-NEXT: ret ptr null
18+
19+
; CHECK-FINAL-NOT: bb4
20+
; CHECK-FINAL-NOT: bb5
21+
22+
; CHECK-FINAL: bb7:
23+
; CHECK-FINAL-NEXT: ret ptr null
24+
define ptr @func() personality ptr @__C_specific_handler {
25+
bb:
26+
invoke void @llvm.seh.try.begin()
27+
to label %bb7 unwind label %bb1
28+
29+
bb1: ; preds = %bb
30+
%i = catchswitch within none [label %bb2] unwind to caller
31+
32+
bb2: ; preds = %bb1
33+
%i3 = catchpad within %i [ptr null]
34+
catchret from %i3 to label %bb4
35+
36+
bb4: ; preds = %bb2
37+
invoke void @llvm.seh.try.end()
38+
to label %bb7 unwind label %bb5
39+
40+
bb5: ; preds = %bb4
41+
%i6 = cleanuppad within none []
42+
cleanupret from %i6 unwind to caller
43+
44+
bb7: ; preds = %bb4, %bb
45+
ret ptr null
46+
}
47+
48+
declare void @llvm.seh.try.begin() #0
49+
declare void @llvm.seh.try.end() #0
50+
declare i32 @__C_specific_handler(...)
51+
52+
attributes #0 = { nounwind willreturn memory(write) }
53+

llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,21 @@ static void replaceBranchTerminator(BasicBlock &BB,
4545
if (ChunkSuccessors.size() == Term->getNumSuccessors())
4646
return;
4747

48+
// TODO: Handle these without failing verifier.
49+
if (isa<CatchSwitchInst>(Term))
50+
return;
51+
4852
bool IsBranch = isa<BranchInst>(Term);
4953
if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Term)) {
50-
LandingPadInst *LP = Invoke->getLandingPadInst();
54+
BasicBlock *UnwindDest = Invoke->getUnwindDest();
55+
Instruction *LP = UnwindDest->getFirstNonPHI();
56+
5157
// Remove landingpad instruction if the containing block isn't used by other
5258
// invokes.
53-
if (none_of(LP->getParent()->users(), [Invoke](User *U) {
59+
60+
// TODO: Handle catchswitch, catchpad, catchret, and cleanupret
61+
if (isa<LandingPadInst>(LP) &&
62+
none_of(UnwindDest->users(), [Invoke](User *U) {
5463
return U != Invoke && isa<InvokeInst>(U);
5564
})) {
5665
LP->replaceAllUsesWith(getDefaultValue(LP->getType()));

0 commit comments

Comments
 (0)