Skip to content

Commit 8a9c170

Browse files
authored
[RISCV] Align stack size down to a multiple of 16 before using cm.push/pop. (#86073)
This an alternative to #84935 to fix the miscompile, but not be optimal. The immediate for cm.push/pop must be a multiple of 16. For RVE, it might not be. It's not easy to increase the stack size without messing up cfa directives and maybe other things. This patch rounds the stack size down to a multiple of 16 before clamping it to 48. This causes an extra addi to be emitted to handle the remainder. Once this commited, I can commit #84989 to add verification for these instructions being generated with valid offsets.
1 parent ecf6bb2 commit 8a9c170

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,10 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
554554
if (RVFI->isPushable(MF) && FirstFrameSetup != MBB.end() &&
555555
FirstFrameSetup->getOpcode() == RISCV::CM_PUSH) {
556556
// Use available stack adjustment in push instruction to allocate additional
557-
// stack space.
558-
uint64_t Spimm = std::min(StackSize, (uint64_t)48);
557+
// stack space. Align the stack size down to a multiple of 16. This is
558+
// needed for RVE.
559+
// FIXME: Can we increase the stack size to a multiple of 16 instead?
560+
uint64_t Spimm = std::min(alignDown(StackSize, 16), (uint64_t)48);
559561
FirstFrameSetup->getOperand(1).setImm(Spimm);
560562
StackSize -= Spimm;
561563
}
@@ -776,8 +778,10 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
776778
if (RVFI->isPushable(MF) && MBBI != MBB.end() &&
777779
MBBI->getOpcode() == RISCV::CM_POP) {
778780
// Use available stack adjustment in pop instruction to deallocate stack
779-
// space.
780-
uint64_t Spimm = std::min(StackSize, (uint64_t)48);
781+
// space. Align the stack size down to a multiple of 16. This is needed for
782+
// RVE.
783+
// FIXME: Can we increase the stack size to a multiple of 16 instead?
784+
uint64_t Spimm = std::min(alignDown(StackSize, 16), (uint64_t)48);
781785
MBBI->getOperand(1).setImm(Spimm);
782786
StackSize -= Spimm;
783787
}

llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 {
44
; RV32-LABEL: func:
55
; RV32: # %bb.0: # %entry
6-
; RV32-NEXT: cm.push {ra, s0-s1}, -24
6+
; RV32-NEXT: cm.push {ra, s0-s1}, -16
7+
; RV32-NEXT: addi sp, sp, -8
78
; RV32-NEXT: .cfi_def_cfa_offset 24
89
; RV32-NEXT: .cfi_offset ra, -12
910
; RV32-NEXT: .cfi_offset s0, -8
@@ -31,7 +32,8 @@ define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 {
3132
; RV32-NEXT: lw a0, 4(sp) # 4-byte Folded Reload
3233
; RV32-NEXT: sb a0, 0(s0)
3334
; RV32-NEXT: mv a0, s1
34-
; RV32-NEXT: cm.popret {ra, s0-s1}, 24
35+
; RV32-NEXT: addi sp, sp, 8
36+
; RV32-NEXT: cm.popret {ra, s0-s1}, 16
3537
entry:
3638
br label %while.body
3739

0 commit comments

Comments
 (0)