[llvm] [AMDGPU] Fix scratch address materialization in SVS frame index fallback (PR #208624)
Anshil Gandhi via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 9 22:30:28 PDT 2026
https://github.com/gandhi56 created https://github.com/llvm/llvm-project/pull/208624
The flat scratch SVS frame index fallback folded the offset in with
V_ADD_U32_e32 writing into FrameReg (an SGPR, an illegal VALU destination)
and added it to FrameReg instead of the scavenged VGPR used as vaddr, so the
offset never reached the address.
Materialize FrameReg + Offset into the scavenged VGPR instead, or just the
offset when there is no frame register.
NFC in practice: for flat scratch an SGPR saddr is always legal, so the
fallback is only entered with no frame register, where the behavior is
unchanged. The FrameReg != 0 path is currently unreachable.
Co-authored-by: Cursor <cursoragent at cursor.com>
>From f99f6e4dbd1cc3b1c2b2d8fc80b21d79cdecdd02 Mon Sep 17 00:00:00 2001
From: Anshil Gandhi <Anshil.Gandhi at amd.com>
Date: Fri, 10 Jul 2026 00:20:22 -0500
Subject: [PATCH] [AMDGPU] Fix scratch address materialization in SVS frame
index fallback
The flat scratch SVS frame index fallback folded the offset in with
V_ADD_U32_e32 writing into FrameReg (an SGPR, an illegal VALU destination)
and added it to FrameReg instead of the scavenged VGPR used as vaddr, so the
offset never reached the address.
Materialize FrameReg + Offset into the scavenged VGPR instead, or just the
offset when there is no frame register.
NFC in practice: for flat scratch an SGPR saddr is always legal, so the
fallback is only entered with no frame register, where the behavior is
unchanged. The FrameReg != 0 path is currently unreachable.
Co-authored-by: Cursor <cursoragent at cursor.com>
---
llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp | 28 +++++++++++++----------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
index de1cae51db1b5..61e89739bcf14 100644
--- a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
@@ -3245,19 +3245,23 @@ bool SIRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator MI,
Register TmpVGPR = RS->scavengeRegisterBackwards(
AMDGPU::VGPR_32RegClass, MI, false, 0, /*AllowSpill=*/true);
- // Materialize the frame register.
- auto MIB =
- BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), TmpVGPR);
- if (FrameReg)
- MIB.addReg(FrameReg);
- else
- MIB.addImm(Offset);
-
- // Add the offset to the frame register.
- if (FrameReg && Offset)
- BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_ADD_U32_e32), FrameReg)
- .addReg(FrameReg, RegState::Kill)
+ // Materialize the scratch address (FrameReg + Offset) into the
+ // scavenged VGPR for use as the SV form vaddr. FrameReg, when it is
+ // available, is an SGPR; when there is no frame register the address
+ // is simply the frame offset.
+ if (FrameReg) {
+ BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), TmpVGPR)
+ .addReg(FrameReg);
+ if (Offset)
+ // Offset is a literal and TmpVGPR is a VGPR, so only a single
+ // constant bus operand is used and this e32 form is legal.
+ BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_ADD_U32_e32), TmpVGPR)
+ .addImm(Offset)
+ .addReg(TmpVGPR, RegState::Kill);
+ } else {
+ BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), TmpVGPR)
.addImm(Offset);
+ }
BuildMI(*MBB, MI, DL, TII->get(SVOpcode))
.add(MI->getOperand(0)) // $vdata
More information about the llvm-commits
mailing list