Skip to content

Commit

Permalink
[ARM64] [Windows] Mark block address as taken when expanding catchrets
Browse files Browse the repository at this point in the history
This fixes issue llvm#109250

The issue happens during the `MachineBlockPlacement` pass.
The block, whose address was previously not taken, is deemed redundant
by the pass and subsequently replaced using
`MachineBasicBlock::ReplaceUsesOfBlockWith` in `BranchFolding`.

ReplaceUsesOfBlockWith only replaces uses in the terminator.
However, `expandPostRAPseudo` introduces new block uses
when expanding catchrets. These uses do not get replaced, which results
in undefined label errors later on.

Marking the block addresss as taken prevents the replacement of the block,
without also replacing non-terminator uses.
  • Loading branch information
momo5502 committed Sep 20, 2024
1 parent 4c50112 commit 20a099c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,7 @@ bool AArch64InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
.addReg(AArch64::X0)
.addMBB(TargetMBB)
.addImm(0);
TargetMBB->setMachineBlockAddressTaken();
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion llvm/test/CodeGen/AArch64/pr58516.ll
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ define void @osfx(ptr %this) comdat personality ptr @__CxxFrameHandler3 {
; CHECK-NEXT: // %bb.1: // %invoke.cont12
; CHECK-NEXT: str wzr, [x20]
; CHECK-NEXT: str wzr, [x21]
; CHECK-NEXT: .LBB0_2: // %try.cont
; CHECK-NEXT: .LBB0_2: // Block address taken
; CHECK-NEXT: // %try.cont
; CHECK-NEXT: $ehgcr_0_2:
; CHECK-NEXT: .seh_startepilogue
; CHECK-NEXT: sub sp, x29, #24
Expand Down
45 changes: 45 additions & 0 deletions llvm/test/CodeGen/AArch64/wineh-catchret-obj-generation.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; RUN: llc -mtriple aarch64-unknown-windows-msvc -filetype=obj %s -o %t.o

declare i32 @__CxxFrameHandler3(...)

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

define fastcc ptr @test_function(i1 %0, ptr %_Fmtfl.i.i, i1 %1) personality ptr @__CxxFrameHandler3 {
entry:
br i1 %0, label %right-block527, label %left-block526

common.ret1:
%common.ret1.op = phi ptr [ null, %left-block530 ], [ null, %some-block ], [ %_Fmtfl.i.i, %invoke.cont.i124 ], [ null, %left-block526 ]
ret ptr %common.ret1.op

invoke.cont.i124:
%.not657 = icmp eq i32 1, 0
br i1 %.not657, label %some-block, label %common.ret1

catch.dispatch.i:
%2 = catchswitch within none [label %catch.i] unwind to caller

catch.i:
%3 = catchpad within %2 [ptr null, i32 0, ptr null]
catchret from %3 to label %some-block

some-block:
br label %common.ret1

left-block526:
br i1 %1, label %common.ret1, label %left-block530

right-block527:
invoke void @llvm.seh.try.begin()
to label %invoke.cont.i124 unwind label %catch.dispatch.i

left-block530:
%.not = icmp eq i32 0, 0
br label %common.ret1
}

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

!llvm.module.flags = !{!0}

!0 = !{i32 2, !"eh-asynch", i32 1}
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AArch64/wineh-try-catch.ll
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
; CHECK-LABEL: .Ltmp0:
; CHECK: bl "?func2@@YAHXZ

; CHECK: [[CATCHRETDEST:.LBB0_[0-9]+]]: // %catchret.dest
; CHECK: [[CATCHRETDEST:.LBB0_[0-9]+]]: // Block address taken

; Check the catch funclet.
; CHECK-LABEL: "?catch$4@?0??func@@YAHXZ@4HA":
Expand Down

0 comments on commit 20a099c

Please sign in to comment.