Skip to content

Commit 06e2b44

Browse files
committed
[RISCV] Optimize scalable frame setup when VLEN is precisely known
If we know the exact value of VLEN, the frame offset adjustment for scalable stack slots becomes a fixed constant. This avoids the need to read vlenb, and may allow the offset to be folded into the immediate field of an add/sub. We could go further here, and fold the offset into a single larger frame adjustment - instead of having a separate scalable adjustment step - but that requires a bit more code reorganization. I may (or may not) return to that in a future patch. Differential Revision: https://reviews.llvm.org/D137593
1 parent 6705d94 commit 06e2b44

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,24 @@ void RISCVFrameLowering::adjustStackForRVV(MachineFunction &MF,
373373
assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
374374

375375
const RISCVInstrInfo *TII = STI.getInstrInfo();
376-
Register SPReg = getSPReg(STI);
376+
const Register SPReg = getSPReg(STI);
377+
378+
// Optimize compile time offset case
379+
if (STI.getRealMinVLen() == STI.getRealMaxVLen()) {
380+
// 1. Multiply the number of v-slots by the (constant) length of register
381+
const int64_t VLENB = STI.getRealMinVLen() / 8;
382+
assert(Amount % 8 == 0 &&
383+
"Reserve the stack by the multiple of one vector size.");
384+
const int64_t NumOfVReg = Amount / 8;
385+
const int64_t Offset = NumOfVReg * VLENB;
386+
if (!isInt<32>(Offset)) {
387+
report_fatal_error(
388+
"Frame size outside of the signed 32-bit range not supported");
389+
}
390+
adjustReg(MBB, MBBI, DL, SPReg, SPReg, Offset, Flag);
391+
return;
392+
}
393+
377394
unsigned Opc = RISCV::ADD;
378395
if (Amount < 0) {
379396
Amount = -Amount;

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ define <vscale x 1 x double> @foo(<vscale x 1 x double> %a, <vscale x 1 x double
8787
; SPILL-O2-VLEN128-NEXT: addi sp, sp, -32
8888
; SPILL-O2-VLEN128-NEXT: sd ra, 24(sp) # 8-byte Folded Spill
8989
; SPILL-O2-VLEN128-NEXT: sd s0, 16(sp) # 8-byte Folded Spill
90-
; SPILL-O2-VLEN128-NEXT: csrr a1, vlenb
91-
; SPILL-O2-VLEN128-NEXT: slli a1, a1, 1
92-
; SPILL-O2-VLEN128-NEXT: sub sp, sp, a1
90+
; SPILL-O2-VLEN128-NEXT: addi sp, sp, -32
9391
; SPILL-O2-VLEN128-NEXT: mv s0, a0
9492
; SPILL-O2-VLEN128-NEXT: addi a1, sp, 16
9593
; SPILL-O2-VLEN128-NEXT: vs1r.v v8, (a1) # Unknown-size Folded Spill
@@ -106,9 +104,7 @@ define <vscale x 1 x double> @foo(<vscale x 1 x double> %a, <vscale x 1 x double
106104
; SPILL-O2-VLEN128-NEXT: addi a0, sp, 16
107105
; SPILL-O2-VLEN128-NEXT: vl1r.v v9, (a0) # Unknown-size Folded Reload
108106
; SPILL-O2-VLEN128-NEXT: vfadd.vv v8, v9, v8
109-
; SPILL-O2-VLEN128-NEXT: csrr a0, vlenb
110-
; SPILL-O2-VLEN128-NEXT: slli a0, a0, 1
111-
; SPILL-O2-VLEN128-NEXT: add sp, sp, a0
107+
; SPILL-O2-VLEN128-NEXT: addi sp, sp, 32
112108
; SPILL-O2-VLEN128-NEXT: ld ra, 24(sp) # 8-byte Folded Reload
113109
; SPILL-O2-VLEN128-NEXT: ld s0, 16(sp) # 8-byte Folded Reload
114110
; SPILL-O2-VLEN128-NEXT: addi sp, sp, 32

0 commit comments

Comments
 (0)