Skip to content

Commit 607c525

Browse files
authored
[ARM64] [Windows] Mark block address as taken when expanding catchrets (#109252)
This fixes issue #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.
1 parent bbdca53 commit 607c525

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,7 @@ bool AArch64InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
19941994
.addReg(AArch64::X0)
19951995
.addMBB(TargetMBB)
19961996
.addImm(0);
1997+
TargetMBB->setMachineBlockAddressTaken();
19971998
return true;
19981999
}
19992000

llvm/test/CodeGen/AArch64/pr58516.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ define void @osfx(ptr %this) comdat personality ptr @__CxxFrameHandler3 {
4040
; CHECK-NEXT: // %bb.1: // %invoke.cont12
4141
; CHECK-NEXT: str wzr, [x20]
4242
; CHECK-NEXT: str wzr, [x21]
43-
; CHECK-NEXT: .LBB0_2: // %try.cont
43+
; CHECK-NEXT: .LBB0_2: // Block address taken
44+
; CHECK-NEXT: // %try.cont
4445
; CHECK-NEXT: $ehgcr_0_2:
4546
; CHECK-NEXT: .seh_startepilogue
4647
; CHECK-NEXT: sub sp, x29, #24
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple aarch64-unknown-windows-msvc %s -o - | FileCheck %s
3+
4+
declare i32 @__CxxFrameHandler3(...)
5+
6+
declare void @llvm.seh.try.begin() #0
7+
8+
define fastcc ptr @test_function(i1 %0, ptr %_Fmtfl.i.i, i1 %1) personality ptr @__CxxFrameHandler3 {
9+
; CHECK-LABEL: test_function:
10+
; CHECK: .Lfunc_begin0:
11+
; CHECK-NEXT: .seh_proc test_function
12+
; CHECK-NEXT: .seh_handler __CxxFrameHandler3, @unwind, @except
13+
; CHECK-NEXT: // %bb.0: // %entry
14+
; CHECK-NEXT: stp x29, x30, [sp, #-32]! // 16-byte Folded Spill
15+
; CHECK-NEXT: .seh_save_fplr_x 32
16+
; CHECK-NEXT: mov x29, sp
17+
; CHECK-NEXT: .seh_set_fp
18+
; CHECK-NEXT: .seh_endprologue
19+
; CHECK-NEXT: mov x3, #-2 // =0xfffffffffffffffe
20+
; CHECK-NEXT: stur x3, [x29, #16]
21+
; CHECK-NEXT: tbz w0, #0, .LBB0_3
22+
; CHECK-NEXT: // %bb.1: // %invoke.cont.i124
23+
; CHECK-NEXT: mov x0, x1
24+
; CHECK-NEXT: b .LBB0_4
25+
; CHECK-NEXT: .LBB0_2: // Block address taken
26+
; CHECK-NEXT: // %some-block
27+
; CHECK-NEXT: $ehgcr_0_2:
28+
; CHECK-NEXT: .LBB0_3: // %left-block526
29+
; CHECK-NEXT: mov x0, xzr
30+
; CHECK-NEXT: .LBB0_4: // %common.ret1
31+
; CHECK-NEXT: .seh_startepilogue
32+
; CHECK-NEXT: ldp x29, x30, [sp], #32 // 16-byte Folded Reload
33+
; CHECK-NEXT: .seh_save_fplr_x 32
34+
; CHECK-NEXT: .seh_endepilogue
35+
; CHECK-NEXT: ret
36+
; CHECK-NEXT: .seh_endfunclet
37+
; CHECK-NEXT: .seh_handlerdata
38+
; CHECK-NEXT: .word ($cppxdata$test_function)@IMGREL
39+
; CHECK-NEXT: .text
40+
; CHECK-NEXT: .seh_endproc
41+
; CHECK-NEXT: .def "?catch$5@?0?test_function@4HA";
42+
; CHECK-NEXT: .scl 3;
43+
; CHECK-NEXT: .type 32;
44+
; CHECK-NEXT: .endef
45+
; CHECK-NEXT: .p2align 2
46+
; CHECK-NEXT: "?catch$5@?0?test_function@4HA":
47+
; CHECK-NEXT: .seh_proc "?catch$5@?0?test_function@4HA"
48+
; CHECK-NEXT: .seh_handler __CxxFrameHandler3, @unwind, @except
49+
; CHECK-NEXT: .LBB0_5: // %catch.i
50+
; CHECK-NEXT: stp x29, x30, [sp, #-16]! // 16-byte Folded Spill
51+
; CHECK-NEXT: .seh_save_fplr_x 16
52+
; CHECK-NEXT: .seh_endprologue
53+
; CHECK-NEXT: adrp x0, .LBB0_2
54+
; CHECK-NEXT: add x0, x0, .LBB0_2
55+
; CHECK-NEXT: .seh_startepilogue
56+
; CHECK-NEXT: ldp x29, x30, [sp], #16 // 16-byte Folded Reload
57+
; CHECK-NEXT: .seh_save_fplr_x 16
58+
; CHECK-NEXT: .seh_endepilogue
59+
; CHECK-NEXT: ret
60+
entry:
61+
br i1 %0, label %right-block527, label %left-block526
62+
63+
common.ret1:
64+
%common.ret1.op = phi ptr [ null, %left-block530 ], [ null, %some-block ], [ %_Fmtfl.i.i, %invoke.cont.i124 ], [ null, %left-block526 ]
65+
ret ptr %common.ret1.op
66+
67+
invoke.cont.i124:
68+
%.not657 = icmp eq i32 1, 0
69+
br i1 %.not657, label %some-block, label %common.ret1
70+
71+
catch.dispatch.i:
72+
%2 = catchswitch within none [label %catch.i] unwind to caller
73+
74+
catch.i:
75+
%3 = catchpad within %2 [ptr null, i32 0, ptr null]
76+
catchret from %3 to label %some-block
77+
78+
some-block:
79+
br label %common.ret1
80+
81+
left-block526:
82+
br i1 %1, label %common.ret1, label %left-block530
83+
84+
right-block527:
85+
invoke void @llvm.seh.try.begin()
86+
to label %invoke.cont.i124 unwind label %catch.dispatch.i
87+
88+
left-block530:
89+
%.not = icmp eq i32 0, 0
90+
br label %common.ret1
91+
}
92+
93+
attributes #0 = { nounwind willreturn memory(write) }
94+
95+
!llvm.module.flags = !{!0}
96+
97+
!0 = !{i32 2, !"eh-asynch", i32 1}

llvm/test/CodeGen/AArch64/wineh-try-catch.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
; CHECK-LABEL: .Ltmp0:
4242
; CHECK: bl "?func2@@YAHXZ
4343

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

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

0 commit comments

Comments
 (0)