Skip to content

llvm-reduce: Fix assert on invokes with catchswitch #111838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions llvm/test/tools/llvm-reduce/issue111817-catchswitch-assert.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
; 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
; RUN: FileCheck --check-prefix=CHECK-FINAL %s < %t

; Make sure there's no assertion for invoke destinations that don't
; use landingpad (and use catchswitch instead)

; CHECK-INTERESTINGNESS: invoke

; CHECK-FINAL: bb:
; CHECK-FINAL-NEXT: invoke void @llvm.seh.try.begin()
; CHECK-FINAL-NEXT: to label %bb7 unwind label %bb1
; CHECK-FINAL: bb1:
; CHECK-FINAL-NEXT: %i = catchswitch within none [label %bb2] unwind to caller

; CHECK-FINAL: bb2:
; CHECK-FINAL-NEXT: %i3 = catchpad within %i [ptr null]
; CHECK-FINAL-NEXT: ret ptr null

; CHECK-FINAL-NOT: bb4
; CHECK-FINAL-NOT: bb5

; CHECK-FINAL: bb7:
; CHECK-FINAL-NEXT: ret ptr null
define ptr @func() personality ptr @__C_specific_handler {
bb:
invoke void @llvm.seh.try.begin()
to label %bb7 unwind label %bb1

bb1: ; preds = %bb
%i = catchswitch within none [label %bb2] unwind to caller

bb2: ; preds = %bb1
%i3 = catchpad within %i [ptr null]
catchret from %i3 to label %bb4

bb4: ; preds = %bb2
invoke void @llvm.seh.try.end()
to label %bb7 unwind label %bb5

bb5: ; preds = %bb4
%i6 = cleanuppad within none []
cleanupret from %i6 unwind to caller

bb7: ; preds = %bb4, %bb
ret ptr null
}

declare void @llvm.seh.try.begin() #0
declare void @llvm.seh.try.end() #0
declare i32 @__C_specific_handler(...)

attributes #0 = { nounwind willreturn memory(write) }

13 changes: 11 additions & 2 deletions llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ static void replaceBranchTerminator(BasicBlock &BB,
if (ChunkSuccessors.size() == Term->getNumSuccessors())
return;

// TODO: Handle these without failing verifier.
if (isa<CatchSwitchInst>(Term))
return;

bool IsBranch = isa<BranchInst>(Term);
if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Term)) {
LandingPadInst *LP = Invoke->getLandingPadInst();
BasicBlock *UnwindDest = Invoke->getUnwindDest();
Instruction *LP = UnwindDest->getFirstNonPHI();

// Remove landingpad instruction if the containing block isn't used by other
// invokes.
if (none_of(LP->getParent()->users(), [Invoke](User *U) {

// TODO: Handle catchswitch, catchpad, catchret, and cleanupret
if (isa<LandingPadInst>(LP) &&
none_of(UnwindDest->users(), [Invoke](User *U) {
return U != Invoke && isa<InvokeInst>(U);
})) {
LP->replaceAllUsesWith(getDefaultValue(LP->getType()));
Expand Down
Loading