Skip to content

Commit d0f27fb

Browse files
kito-chengtstellar
authored andcommitted
[RISCV] Fixing stack offset for RVV object with vararg in stack.
We found LLVM generate wrong stack offset for RVV object when stack having variable argument, that cause by we didn't count vaarg part during calculate RVV stack objects. Also update the stack layout diagram for including vaarg in the diagram. Stack layout ref: https://github.com/gcc-mirror/gcc/blob/master/gcc/config/riscv/riscv.cc#L3941 Reviewed By: rogfer01 Differential Revision: https://reviews.llvm.org/D123180
1 parent b6e91d4 commit d0f27fb

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,10 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
674674
if (hasBP(MF)) {
675675
FrameReg = RISCVABI::getBPReg();
676676
// |--------------------------| -- <-- FP
677-
// | callee-saved registers | | <----.
677+
// | callee-allocated save | | <----|
678+
// | area for register varargs| | |
679+
// |--------------------------| | |
680+
// | callee-saved registers | | |
678681
// |--------------------------| -- |
679682
// | realignment (the size of | | |
680683
// | this area is not counted | | |
@@ -699,7 +702,10 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
699702
} else {
700703
FrameReg = RISCV::X2;
701704
// |--------------------------| -- <-- FP
702-
// | callee-saved registers | | <----.
705+
// | callee-allocated save | | <----|
706+
// | area for register varargs| | |
707+
// |--------------------------| | |
708+
// | callee-saved registers | | |
703709
// |--------------------------| -- |
704710
// | realignment (the size of | | |
705711
// | this area is not counted | | |
@@ -742,6 +748,9 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
742748
// the frame size.
743749
//
744750
// |--------------------------| -- <-- FP
751+
// | callee-allocated save | |
752+
// | area for register varargs| |
753+
// |--------------------------| |
745754
// | callee-saved registers | |
746755
// |--------------------------| | MFI.getStackSize()
747756
// | scalar local variables | |
@@ -756,7 +765,10 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
756765
// When using SP to access frame objects, we need to add RVV stack size.
757766
//
758767
// |--------------------------| -- <-- FP
759-
// | callee-saved registers | | <----.
768+
// | callee-allocated save | | <----|
769+
// | area for register varargs| | |
770+
// |--------------------------| | |
771+
// | callee-saved registers | | |
760772
// |--------------------------| -- |
761773
// | Padding after RVV | | |
762774
// | (not counted in | | |
@@ -786,8 +798,11 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
786798
Offset += StackOffset::getFixed(MFI.getStackSize());
787799
}
788800
} else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
801+
int ScalarLocalVarSize = MFI.getStackSize() -
802+
RVFI->getCalleeSavedStackSize() -
803+
RVFI->getVarArgsSaveSize();
789804
Offset += StackOffset::get(
790-
alignTo(MFI.getStackSize() - RVFI->getCalleeSavedStackSize(), 8),
805+
alignTo(ScalarLocalVarSize, 8),
791806
RVFI->getRVVStackSize());
792807
}
793808
}

llvm/test/CodeGen/RISCV/rvv/wrong-stack-offset-for-rvv-object.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ body: |
164164
; CHECK-NEXT: SD killed renamable $x16, killed $x10, 48 :: (store (s64) into %fixed-stack.1, align 16)
165165
; CHECK-NEXT: dead $x0 = PseudoVSETIVLI 2, 69, implicit-def $vl, implicit-def $vtype
166166
; CHECK-NEXT: renamable $v8 = PseudoVMV_V_I_MF8 0, 2, 3, implicit $vl, implicit $vtype
167-
; CHECK-NEXT: $x10 = ADDI $x2, 40
167+
; CHECK-NEXT: $x10 = ADDI $x2, 24
168168
; CHECK-NEXT: PseudoVSPILL_M1 killed renamable $v8, killed $x10 :: (store unknown-size into %stack.1, align 8)
169169
; CHECK-NEXT: {{ $}}
170170
; CHECK-NEXT: bb.1.while.cond:
@@ -179,7 +179,7 @@ body: |
179179
; CHECK-NEXT: liveins: $x8, $x9
180180
; CHECK-NEXT: {{ $}}
181181
; CHECK-NEXT: dead $x0 = PseudoVSETIVLI 2, 69, implicit-def $vl, implicit-def $vtype
182-
; CHECK-NEXT: $x10 = ADDI $x2, 40
182+
; CHECK-NEXT: $x10 = ADDI $x2, 24
183183
; CHECK-NEXT: renamable $v8 = PseudoVRELOAD_M1 killed $x10 :: (load unknown-size from %stack.1, align 8)
184184
; CHECK-NEXT: PseudoVSE8_V_MF8 killed renamable $v8, renamable $x8, 2, 3, implicit $vl, implicit $vtype :: (store (s16) into %ir.0, align 1)
185185
; CHECK-NEXT: $x10 = COPY renamable $x9

0 commit comments

Comments
 (0)