Skip to content

Commit bc1fe6f

Browse files
authored
RegisterCoalescer: Fix producing malformed IMPLICIT_DEFs (#73784)
If this was coalescing a SUBREG_TO_REG as a copy, the resulting instruction would be an IMPLICIT_DEF with an unexpected 2 immediate operands, which need to be dropped. Until recently the verifier did not catch this error, and an assert would fire if later the broken IMPLICIT_DEF was rematerialized P #73758 is related, it changes the failure mode to a verifier error.
1 parent fe0d629 commit bc1fe6f

File tree

2 files changed

+129
-2
lines changed

2 files changed

+129
-2
lines changed

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,12 +1694,19 @@ MachineInstr *RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI) {
16941694
// The source interval may also have been on an undef use, in which case the
16951695
// copy introduced a live value.
16961696
if (((V && V->isPHIDef()) || (!V && !DstLI.liveAt(Idx)))) {
1697-
CopyMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
16981697
for (unsigned i = CopyMI->getNumOperands(); i != 0; --i) {
16991698
MachineOperand &MO = CopyMI->getOperand(i-1);
1700-
if (MO.isReg() && MO.isUse())
1699+
if (MO.isReg()) {
1700+
if (MO.isUse())
1701+
CopyMI->removeOperand(i - 1);
1702+
} else {
1703+
assert(MO.isImm() &&
1704+
CopyMI->getOpcode() == TargetOpcode::SUBREG_TO_REG);
17011705
CopyMI->removeOperand(i-1);
1706+
}
17021707
}
1708+
1709+
CopyMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
17031710
LLVM_DEBUG(dbgs() << "\tReplaced copy of <undef> value with an "
17041711
"implicit def\n");
17051712
return CopyMI;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
2+
# RUN: llc -mtriple=arm64-apple-macosx -mcpu=apple-m1 -verify-coalescing -run-pass=register-coalescer -o - %s | FileCheck %s
3+
4+
# Hits assert "Trying to add an operand to a machine instr that is
5+
# already done!" when rematerializing during greedy. This was because
6+
# an IMPLICIT_DEF ended up with some immediate operands during
7+
# coalescing. A SUBREG_TO_REG was not dropping the immediate operands
8+
# when mutating to IMPLICIT_DEF, and would later fail the assert when
9+
# creating a new IMPLICIT_DEF copy during rematerialization.
10+
11+
--- |
12+
define void @_ZN38SanitizerCommonInterceptors_Scanf_Test8TestBodyEv() {
13+
ret void
14+
}
15+
16+
declare void @_ZL9testScanfPKcjz(ptr, i32, ...)
17+
18+
...
19+
---
20+
name: _ZN38SanitizerCommonInterceptors_Scanf_Test8TestBodyEv
21+
alignment: 4
22+
tracksRegLiveness: true
23+
frameInfo:
24+
maxAlignment: 8
25+
adjustsStack: true
26+
hasCalls: true
27+
maxCallFrameSize: 24
28+
body: |
29+
bb.0:
30+
liveins: $x0, $x1, $x2, $x3, $x4, $x5, $x6
31+
32+
; CHECK-LABEL: name: _ZN38SanitizerCommonInterceptors_Scanf_Test8TestBodyEv
33+
; CHECK: liveins: $x0, $x1, $x2, $x3, $x4, $x5, $x6
34+
; CHECK-NEXT: {{ $}}
35+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr64sp = IMPLICIT_DEF
36+
; CHECK-NEXT: dead [[DEF1:%[0-9]+]]:gpr32 = IMPLICIT_DEF
37+
; CHECK-NEXT: [[DEF2:%[0-9]+]]:gpr64common = IMPLICIT_DEF
38+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x5
39+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x4
40+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64 = COPY $x3
41+
; CHECK-NEXT: [[COPY3:%[0-9]+]]:gpr64 = COPY $x2
42+
; CHECK-NEXT: [[COPY4:%[0-9]+]]:gpr64 = COPY $x1
43+
; CHECK-NEXT: [[COPY5:%[0-9]+]]:gpr64 = COPY $x0
44+
; CHECK-NEXT: [[DEF3:%[0-9]+]]:gpr64 = IMPLICIT_DEF
45+
; CHECK-NEXT: [[DEF4:%[0-9]+]]:gpr64 = IMPLICIT_DEF
46+
; CHECK-NEXT: [[DEF5:%[0-9]+]]:gpr64 = IMPLICIT_DEF
47+
; CHECK-NEXT: ADJCALLSTACKDOWN 16, 0, implicit-def dead $sp, implicit $sp
48+
; CHECK-NEXT: BL @_ZL9testScanfPKcjz, csr_darwin_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
49+
; CHECK-NEXT: ADJCALLSTACKUP 16, 0, implicit-def dead $sp, implicit $sp
50+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
51+
; CHECK-NEXT: STRXui [[DEF3]], [[DEF]], 0 :: (store (s64) into stack)
52+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
53+
; CHECK-NEXT: STRWui undef [[DEF1]], [[DEF2]], 0 :: (store (s32))
54+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
55+
; CHECK-NEXT: STRXui [[DEF4]], undef [[DEF]], 0 :: (store (s64) into stack)
56+
; CHECK-NEXT: $x0 = COPY [[COPY5]]
57+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
58+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
59+
; CHECK-NEXT: $x0 = COPY [[COPY4]]
60+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
61+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
62+
; CHECK-NEXT: $x0 = COPY [[COPY3]]
63+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
64+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
65+
; CHECK-NEXT: $x0 = COPY [[COPY2]]
66+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
67+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
68+
; CHECK-NEXT: $x0 = COPY [[COPY1]]
69+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
70+
; CHECK-NEXT: ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
71+
; CHECK-NEXT: $x0 = COPY [[COPY]]
72+
; CHECK-NEXT: ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
73+
; CHECK-NEXT: ADJCALLSTACKDOWN 24, 0, implicit-def dead $sp, implicit $sp
74+
; CHECK-NEXT: STRXui [[DEF5]], undef [[DEF]], 1 :: (store (s64) into stack + 8)
75+
; CHECK-NEXT: ADJCALLSTACKUP 24, 0, implicit-def dead $sp, implicit $sp
76+
; CHECK-NEXT: RET_ReallyLR
77+
%0:gpr64sp = IMPLICIT_DEF
78+
%1:gpr32 = IMPLICIT_DEF
79+
%2:gpr64common = IMPLICIT_DEF
80+
%3:gpr64 = COPY killed $x5
81+
%4:gpr64 = COPY killed $x4
82+
%5:gpr64 = COPY killed $x3
83+
%6:gpr64 = COPY killed $x2
84+
%7:gpr64 = COPY killed $x1
85+
%8:gpr64 = COPY killed $x0
86+
%9:gpr64 = IMPLICIT_DEF
87+
%10:gpr64 = IMPLICIT_DEF
88+
%11:gpr64 = SUBREG_TO_REG 0, killed undef %1, %subreg.sub_32
89+
ADJCALLSTACKDOWN 16, 0, implicit-def dead $sp, implicit $sp
90+
BL @_ZL9testScanfPKcjz, csr_darwin_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
91+
ADJCALLSTACKUP 16, 0, implicit-def dead $sp, implicit $sp
92+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
93+
STRXui %9, killed %0, 0 :: (store (s64) into stack)
94+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
95+
STRWui undef %1, killed %2, 0 :: (store (s32))
96+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
97+
STRXui killed %10, killed undef %0, 0 :: (store (s64) into stack)
98+
$x0 = COPY killed %8
99+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
100+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
101+
$x0 = COPY killed %7
102+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
103+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
104+
$x0 = COPY killed %6
105+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
106+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
107+
$x0 = COPY killed %5
108+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
109+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
110+
$x0 = COPY killed %4
111+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
112+
ADJCALLSTACKDOWN 8, 0, implicit-def dead $sp, implicit $sp
113+
$x0 = COPY killed %3
114+
ADJCALLSTACKUP 8, 0, implicit-def dead $sp, implicit $sp
115+
ADJCALLSTACKDOWN 24, 0, implicit-def dead $sp, implicit $sp
116+
STRXui killed %11, undef %0, 1 :: (store (s64) into stack + 8)
117+
ADJCALLSTACKUP 24, 0, implicit-def dead $sp, implicit $sp
118+
RET_ReallyLR
119+
120+
...

0 commit comments

Comments
 (0)