[RISCV] Scalable-vector stack objects can be placed at misaligned addresses
A scalable-vector stack object whose alignment is larger than vlenb gets a misaligned address when a less aligned RVV stack object follows it in frame index order.
; ABI alignment: 64 for %a, 8 for %b.
define void @f() nounwind {
%a = alloca <vscale x 8 x i64>
%a.val = load volatile <vscale x 8 x i64>, ptr %a
%b = alloca <vscale x 1 x i64>
%b.val = load volatile <vscale x 1 x i64>, ptr %b
ret void
}
llc -mtriple=riscv64 -mattr=+m,+v (comments added):
andi sp, sp, -64 # sp is 64-byte aligned
csrr a0, vlenb
add a0, sp, a0
addi a0, a0, 64
vl8re64.v v8, (a0) # %a = sp + 64 + vlenb
addi a0, sp, 64
vl1re64.v v8, (a0) # %b = sp + 64
%a is 64-byte aligned only if vlenb is a multiple of 64. It is 16 bytes off at VLEN=128 and 32 bytes off at VLEN=256. With the two allocas swapped, %a is aligned.
RISCVFrameLowering::assignRVVStackObjectOffsets() assigns offsets from the top of the RVV area in frame index order, then moves all objects down by the padding needed to align the size of the area. That aligns the bottom of the area, so only the lowest object is guaranteed its alignment.
@llvm/issue-subscribers-backend-risc-v
Author: Petar Jovanovic (petar-jovanovic)
; ABI alignment: 64 for %a, 8 for %b.
define void @<!-- -->f() nounwind {
%a = alloca <vscale x 8 x i64>
%a.val = load volatile <vscale x 8 x i64>, ptr %a
%b = alloca <vscale x 1 x i64>
%b.val = load volatile <vscale x 1 x i64>, ptr %b
ret void
}
llc -mtriple=riscv64 -mattr=+m,+v (comments added):
andi sp, sp, -64 # sp is 64-byte aligned
csrr a0, vlenb
add a0, sp, a0
addi a0, a0, 64
vl8re64.v v8, (a0) # %a = sp + 64 + vlenb
addi a0, sp, 64
vl1re64.v v8, (a0) # %b = sp + 64
%a is 64-byte aligned only if vlenb is a multiple of 64. It is 16 bytes off at VLEN=128 and 32 bytes off at VLEN=256. With the two allocas swapped, %a is aligned.
RISCVFrameLowering::assignRVVStackObjectOffsets() assigns offsets from the top of the RVV area in frame index order, then moves all objects down by the padding needed to align the size of the area. That aligns the bottom of the area, so only the lowest object is guaranteed its alignment.