Skip to content

Commit 8e65b72

Browse files
authored
[RISCV] Fix double counting CSRs with Zcmp in RISCVFrameLowering::getFrameIndexReference. (#117207)
The Zcmp callee saved registers are already accounted for in getCalleeSavedStackSize(). Subtracting RVPushStackSize subtracts them a second time leading to incorrect stack offsets during frame index elimination. This should have been removed in 0de2b26 when Zcmp handling was changed. Prior to that, RVPushStackSize was not included in getCalleeSavedStackSize(). The commit message at the time noted that Zcmp+RVV was likely broken.
1 parent 1b413c8 commit 8e65b72

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,6 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
12051205
// alignment padding.
12061206
int ScalarLocalVarSize = MFI.getStackSize() -
12071207
RVFI->getCalleeSavedStackSize() -
1208-
RVFI->getRVPushStackSize() -
12091208
RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
12101209
Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
12111210
}

llvm/test/CodeGen/RISCV/rvv/rv32-spill-vector-csr.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
; RUN: | FileCheck --check-prefix=SPILL-O0 %s
44
; RUN: llc -mtriple=riscv32 -mattr=+v,+d -O2 < %s \
55
; RUN: | FileCheck --check-prefix=SPILL-O2 %s
6+
; RUN: llc -mtriple=riscv32 -mattr=+v,+d,+zcmp -O2 < %s \
7+
; RUN: | FileCheck --check-prefix=SPILL-O2-ZCMP %s
68

79
@.str = private unnamed_addr constant [6 x i8] c"hello\00", align 1
810

@@ -82,6 +84,37 @@ define <vscale x 1 x double> @foo(<vscale x 1 x double> %a, <vscale x 1 x double
8284
; SPILL-O2-NEXT: lw s0, 24(sp) # 4-byte Folded Reload
8385
; SPILL-O2-NEXT: addi sp, sp, 32
8486
; SPILL-O2-NEXT: ret
87+
;
88+
; SPILL-O2-ZCMP-LABEL: foo:
89+
; SPILL-O2-ZCMP: # %bb.0:
90+
; SPILL-O2-ZCMP-NEXT: cm.push {ra, s0}, -32
91+
; SPILL-O2-ZCMP-NEXT: csrr a1, vlenb
92+
; SPILL-O2-ZCMP-NEXT: slli a1, a1, 1
93+
; SPILL-O2-ZCMP-NEXT: sub sp, sp, a1
94+
; SPILL-O2-ZCMP-NEXT: mv s0, a0
95+
; SPILL-O2-ZCMP-NEXT: addi a1, sp, 16
96+
; SPILL-O2-ZCMP-NEXT: vs1r.v v8, (a1) # Unknown-size Folded Spill
97+
; SPILL-O2-ZCMP-NEXT: vsetvli zero, a0, e64, m1, ta, ma
98+
; SPILL-O2-ZCMP-NEXT: vfadd.vv v9, v8, v9
99+
; SPILL-O2-ZCMP-NEXT: csrr a0, vlenb
100+
; SPILL-O2-ZCMP-NEXT: add a0, a0, sp
101+
; SPILL-O2-ZCMP-NEXT: addi a0, a0, 16
102+
; SPILL-O2-ZCMP-NEXT: vs1r.v v9, (a0) # Unknown-size Folded Spill
103+
; SPILL-O2-ZCMP-NEXT: lui a0, %hi(.L.str)
104+
; SPILL-O2-ZCMP-NEXT: addi a0, a0, %lo(.L.str)
105+
; SPILL-O2-ZCMP-NEXT: call puts
106+
; SPILL-O2-ZCMP-NEXT: csrr a0, vlenb
107+
; SPILL-O2-ZCMP-NEXT: add a0, a0, sp
108+
; SPILL-O2-ZCMP-NEXT: addi a0, a0, 16
109+
; SPILL-O2-ZCMP-NEXT: vl1r.v v8, (a0) # Unknown-size Folded Reload
110+
; SPILL-O2-ZCMP-NEXT: addi a0, sp, 16
111+
; SPILL-O2-ZCMP-NEXT: vl1r.v v9, (a0) # Unknown-size Folded Reload
112+
; SPILL-O2-ZCMP-NEXT: vsetvli zero, s0, e64, m1, ta, ma
113+
; SPILL-O2-ZCMP-NEXT: vfadd.vv v8, v9, v8
114+
; SPILL-O2-ZCMP-NEXT: csrr a0, vlenb
115+
; SPILL-O2-ZCMP-NEXT: slli a0, a0, 1
116+
; SPILL-O2-ZCMP-NEXT: add sp, sp, a0
117+
; SPILL-O2-ZCMP-NEXT: cm.popret {ra, s0}, 32
85118
{
86119
%x = call <vscale x 1 x double> @llvm.riscv.vfadd.nxv1f64.nxv1f64(<vscale x 1 x double> undef, <vscale x 1 x double> %a, <vscale x 1 x double> %b, i32 7, i32 %gvl)
87120
%call = call signext i32 @puts(ptr @.str)

llvm/test/CodeGen/RISCV/rvv/rv64-spill-vector-csr.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
; RUN: | FileCheck --check-prefix=SPILL-O2 %s
66
; RUN: llc -mtriple=riscv64 -mattr=+v,+d -mattr=+d -riscv-v-vector-bits-max=128 -O2 < %s \
77
; RUN: | FileCheck --check-prefix=SPILL-O2-VLEN128 %s
8+
; RUN: llc -mtriple=riscv64 -mattr=+v,+d,+zcmp -O2 < %s \
9+
; RUN: | FileCheck --check-prefix=SPILL-O2-ZCMP %s
810

911

1012
@.str = private unnamed_addr constant [6 x i8] c"hello\00", align 1
@@ -113,6 +115,37 @@ define <vscale x 1 x double> @foo(<vscale x 1 x double> %a, <vscale x 1 x double
113115
; SPILL-O2-VLEN128-NEXT: ld s0, 16(sp) # 8-byte Folded Reload
114116
; SPILL-O2-VLEN128-NEXT: addi sp, sp, 32
115117
; SPILL-O2-VLEN128-NEXT: ret
118+
;
119+
; SPILL-O2-ZCMP-LABEL: foo:
120+
; SPILL-O2-ZCMP: # %bb.0:
121+
; SPILL-O2-ZCMP-NEXT: cm.push {ra, s0}, -32
122+
; SPILL-O2-ZCMP-NEXT: csrr a1, vlenb
123+
; SPILL-O2-ZCMP-NEXT: slli a1, a1, 1
124+
; SPILL-O2-ZCMP-NEXT: sub sp, sp, a1
125+
; SPILL-O2-ZCMP-NEXT: mv s0, a0
126+
; SPILL-O2-ZCMP-NEXT: addi a1, sp, 16
127+
; SPILL-O2-ZCMP-NEXT: vs1r.v v8, (a1) # Unknown-size Folded Spill
128+
; SPILL-O2-ZCMP-NEXT: vsetvli zero, a0, e64, m1, ta, ma
129+
; SPILL-O2-ZCMP-NEXT: vfadd.vv v9, v8, v9
130+
; SPILL-O2-ZCMP-NEXT: csrr a0, vlenb
131+
; SPILL-O2-ZCMP-NEXT: add a0, a0, sp
132+
; SPILL-O2-ZCMP-NEXT: addi a0, a0, 16
133+
; SPILL-O2-ZCMP-NEXT: vs1r.v v9, (a0) # Unknown-size Folded Spill
134+
; SPILL-O2-ZCMP-NEXT: lui a0, %hi(.L.str)
135+
; SPILL-O2-ZCMP-NEXT: addi a0, a0, %lo(.L.str)
136+
; SPILL-O2-ZCMP-NEXT: call puts
137+
; SPILL-O2-ZCMP-NEXT: csrr a0, vlenb
138+
; SPILL-O2-ZCMP-NEXT: add a0, a0, sp
139+
; SPILL-O2-ZCMP-NEXT: addi a0, a0, 16
140+
; SPILL-O2-ZCMP-NEXT: vl1r.v v8, (a0) # Unknown-size Folded Reload
141+
; SPILL-O2-ZCMP-NEXT: addi a0, sp, 16
142+
; SPILL-O2-ZCMP-NEXT: vl1r.v v9, (a0) # Unknown-size Folded Reload
143+
; SPILL-O2-ZCMP-NEXT: vsetvli zero, s0, e64, m1, ta, ma
144+
; SPILL-O2-ZCMP-NEXT: vfadd.vv v8, v9, v8
145+
; SPILL-O2-ZCMP-NEXT: csrr a0, vlenb
146+
; SPILL-O2-ZCMP-NEXT: slli a0, a0, 1
147+
; SPILL-O2-ZCMP-NEXT: add sp, sp, a0
148+
; SPILL-O2-ZCMP-NEXT: cm.popret {ra, s0}, 32
116149
{
117150
%x = call <vscale x 1 x double> @llvm.riscv.vfadd.nxv1f64.nxv1f64(<vscale x 1 x double> undef, <vscale x 1 x double> %a, <vscale x 1 x double> %b, i64 7, i64 %gvl)
118151
%call = call signext i32 @puts(ptr @.str)

0 commit comments

Comments
 (0)