Skip to content

[AMDGPU] Support FLAT_SCRATCH in SILoadStoreOptimizer const offset promotion - #212507

Merged
aobolensk merged 16 commits into
llvm:mainfrom
aobolensk:llvm-amdgpu-FLAT_SCRATCH-32-bit-offset-promotion
Nov 4, 2026
Merged

aobolensk merged 16 commits into
llvm:mainfrom
aobolensk:llvm-amdgpu-FLAT_SCRATCH-32-bit-offset-promotion

Conversation

@aobolensk

Copy link
Copy Markdown
Contributor

Add a 32-bit base/offset extraction path for the V_ADD_U32_e64 pattern used by FLAT_SCRATCH vaddr and enable promoteConstantOffsetToImm for FLAT_SCRATCH instructions that have a vaddr operand.

…omotion

Add a 32-bit base/offset extraction path for the V_ADD_U32_e64 pattern used by FLAT_SCRATCH vaddr and enable promoteConstantOffsetToImm for FLAT_SCRATCH instructions that have a vaddr operand.
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-amdgpu

Author: Arseniy Obolenskiy (aobolensk)

Changes

Add a 32-bit base/offset extraction path for the V_ADD_U32_e64 pattern used by FLAT_SCRATCH vaddr and enable promoteConstantOffsetToImm for FLAT_SCRATCH instructions that have a vaddr operand.


Patch is 128.49 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/212507.diff

3 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp (+74-7)
  • (modified) llvm/test/CodeGen/AMDGPU/flat-scratch-svs.ll (+172-246)
  • (modified) llvm/test/CodeGen/AMDGPU/promote-constOffset-to-imm.ll (+1197-10)
diff --git a/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp b/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp
index 897be1d6739e1..fc124c240a56f 100644
--- a/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp
+++ b/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp
@@ -196,6 +196,8 @@ class SILoadStoreOptimizer {
     unsigned HiSubReg = 0;
     // True when using V_ADD_U64_e64 pattern
     bool UseV64Pattern = false;
+    // True when using V_ADD_U32_e64 pattern (32-bit FLAT_SCRATCH vaddr)
+    bool UseScratchVGPRPattern = false;
   };
 
   struct MemAddress {
@@ -288,6 +290,9 @@ class SILoadStoreOptimizer {
   bool processBaseWithConstOffset64(MachineInstr *AddDef,
                                     const MachineOperand &Base,
                                     MemAddress &Addr) const;
+  bool processBaseWithConstOffset32(MachineInstr *AddDef,
+                                    const MachineOperand &Base,
+                                    MemAddress &Addr) const;
   void processBaseWithConstOffset(const MachineOperand &Base, MemAddress &Addr) const;
   /// Promotes constant offset to the immediate by adjusting the base. It
   /// tries to use a base from the nearby instructions that allows it to have
@@ -2198,6 +2203,23 @@ Register SILoadStoreOptimizer::computeBase(MachineInstr &MI,
     return FullDestReg;
   }
 
+  // Use V_ADD_U32_e64 for the 32-bit FLAT_SCRATCH vaddr base.
+  if (Addr.Base.UseScratchVGPRPattern) {
+    MachineOperand OffsetOp =
+        createRegOrImm(static_cast<int32_t>(Addr.Offset), MI);
+
+    Register FullDestReg = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
+    MachineInstr *Add32 =
+        BuildMI(*MBB, MBBI, DL, TII->get(AMDGPU::V_ADD_U32_e64), FullDestReg)
+            .addReg(Addr.Base.LoReg)
+            .add(OffsetOp)
+            .addImm(0); // clamp bit
+    (void)Add32;
+    LLVM_DEBUG(dbgs() << "    " << *Add32 << "\n\n";);
+
+    return FullDestReg;
+  }
+
   // Original carry-chain pattern (V_ADD_CO_U32 + V_ADDC_U32)
   assert((TRI->getRegSizeInBits(Addr.Base.LoReg, *MRI) == 32 ||
           Addr.Base.LoSubReg) &&
@@ -2289,6 +2311,39 @@ bool SILoadStoreOptimizer::processBaseWithConstOffset64(
   return true;
 }
 
+// Helper to extract a 32-bit constant offset from a V_ADD_U32_e64 instruction.
+// This is the pattern used for the 32-bit vaddr of FLAT_SCRATCH instructions.
+// Returns true if successful, populating Addr with base register info and
+// offset.
+bool SILoadStoreOptimizer::processBaseWithConstOffset32(
+    MachineInstr *AddDef, const MachineOperand &Base, MemAddress &Addr) const {
+  if (!Base.isReg())
+    return false;
+
+  MachineOperand *Src0 = TII->getNamedOperand(*AddDef, AMDGPU::OpName::src0);
+  MachineOperand *Src1 = TII->getNamedOperand(*AddDef, AMDGPU::OpName::src1);
+
+  const MachineOperand *BaseOp = nullptr;
+
+  if (auto Offset = TII->getImmOrMaterializedImm(*Src1)) {
+    BaseOp = Src0;
+    Addr.Offset = *Offset;
+  } else if (auto Offset0 = TII->getImmOrMaterializedImm(*Src0)) {
+    BaseOp = Src1;
+    Addr.Offset = *Offset0;
+  } else {
+    // Both or neither are constants - can't handle this pattern
+    return false;
+  }
+
+  if (!BaseOp->isReg())
+    return false;
+
+  Addr.Base.LoReg = BaseOp->getReg();
+  Addr.Base.UseScratchVGPRPattern = true;
+  return true;
+}
+
 // Analyze Base and extracts:
 //  - 32bit base registers, subregisters
 //  - 64bit constant offset
@@ -2303,6 +2358,10 @@ bool SILoadStoreOptimizer::processBaseWithConstOffset64(
 // Also handles V_ADD_U64_e64 pattern (gfx1250+):
 //   %OFFSET:sreg_64 = S_MOV_B64_IMM_PSEUDO 256
 //   %Base:vreg_64 = V_ADD_U64_e64 %BASE:vreg_64, %OFFSET:sreg_64, 0
+//
+// Also handles the 32-bit VGPR pattern used for FLAT_SCRATCH vaddr:
+//   %OFFSET:sreg_32 = S_MOV_B32 4096
+//   %Base:vgpr_32 = V_ADD_U32_e64 %BASE:vgpr_32, %OFFSET:sreg_32, 0
 void SILoadStoreOptimizer::processBaseWithConstOffset(const MachineOperand &Base,
                                                       MemAddress &Addr) const {
   if (!Base.isReg())
@@ -2318,6 +2377,12 @@ void SILoadStoreOptimizer::processBaseWithConstOffset(const MachineOperand &Base
       return;
   }
 
+  // Try V_ADD_U32_e64 pattern (32-bit base, used for FLAT_SCRATCH vaddr)
+  if (Def->getOpcode() == AMDGPU::V_ADD_U32_e64) {
+    if (processBaseWithConstOffset32(Def, Base, Addr))
+      return;
+  }
+
   // Fall through to REG_SEQUENCE + V_ADD_CO_U32 + V_ADDC_U32 pattern
   if (Def->getOpcode() != AMDGPU::REG_SEQUENCE || Def->getNumOperands() != 5)
     return;
@@ -2405,16 +2470,18 @@ bool SILoadStoreOptimizer::promoteConstantOffsetToImm(
   if (!STM->hasFlatInstOffsets() || !SIInstrInfo::isFLAT(MI))
     return false;
 
-  // TODO: Support FLAT_SCRATCH. Currently code expects 64-bit pointers.
-  if (SIInstrInfo::isFLATScratch(MI))
+  if (SIInstrInfo::isFLATScratch(MI) &&
+      !TII->getNamedOperand(MI, AMDGPU::OpName::vaddr))
     return false;
 
-  unsigned AS = SIInstrInfo::isFLATGlobal(MI) ? AMDGPUAS::GLOBAL_ADDRESS
-                                              : AMDGPUAS::FLAT_ADDRESS;
+  unsigned AS = SIInstrInfo::isFLATGlobal(MI)    ? AMDGPUAS::GLOBAL_ADDRESS
+                : SIInstrInfo::isFLATScratch(MI) ? AMDGPUAS::PRIVATE_ADDRESS
+                                                 : AMDGPUAS::FLAT_ADDRESS;
 
-  AMDGPU::FlatAddrSpace FlatVariant = AS == AMDGPUAS::GLOBAL_ADDRESS
-                                          ? AMDGPU::FlatAddrSpace::FlatGlobal
-                                          : AMDGPU::FlatAddrSpace::FLAT;
+  AMDGPU::FlatAddrSpace FlatVariant =
+      AS == AMDGPUAS::GLOBAL_ADDRESS    ? AMDGPU::FlatAddrSpace::FlatGlobal
+      : AS == AMDGPUAS::PRIVATE_ADDRESS ? AMDGPU::FlatAddrSpace::FlatScratch
+                                        : AMDGPU::FlatAddrSpace::FLAT;
   bool AllowNegativeOffset =
       TII->allowNegativeFlatOffset(FlatVariant) && !TII->usesASYNC_CNT(MI);
   // The async global instructions use i24 offset for global address but u16
diff --git a/llvm/test/CodeGen/AMDGPU/flat-scratch-svs.ll b/llvm/test/CodeGen/AMDGPU/flat-scratch-svs.ll
index d5a03b11d4cb4..469cb8da7a138 100644
--- a/llvm/test/CodeGen/AMDGPU/flat-scratch-svs.ll
+++ b/llvm/test/CodeGen/AMDGPU/flat-scratch-svs.ll
@@ -21,18 +21,16 @@ define amdgpu_kernel void @soff1_voff1(i32 %soff) {
 ; GFX942-SDAG-NEXT:    s_load_dword s0, s[4:5], 0x24
 ; GFX942-SDAG-NEXT:    v_and_b32_e32 v0, 0x3ff, v0
 ; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 1
+; GFX942-SDAG-NEXT:    v_mov_b32_e32 v2, 2
 ; GFX942-SDAG-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, s0, v0
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v2, 1, v0
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v3, 2, v0
-; GFX942-SDAG-NEXT:    scratch_store_byte v2, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, 1, v0
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 2
-; GFX942-SDAG-NEXT:    scratch_store_byte v3, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v2, off offset:1 sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, 4, v0
 ; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 4
-; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off offset:3 sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
 ; GFX942-SDAG-NEXT:    s_endpgm
 ;
@@ -41,18 +39,16 @@ define amdgpu_kernel void @soff1_voff1(i32 %soff) {
 ; GFX942-GISEL-NEXT:    s_load_dword s0, s[4:5], 0x24
 ; GFX942-GISEL-NEXT:    v_and_b32_e32 v0, 0x3ff, v0
 ; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 1
+; GFX942-GISEL-NEXT:    v_mov_b32_e32 v2, 2
 ; GFX942-GISEL-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, s0, v0
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v2, 1, v0
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v3, 2, v0
-; GFX942-GISEL-NEXT:    scratch_store_byte v2, v1, off sc0 sc1
+; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, 1, v0
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 2
-; GFX942-GISEL-NEXT:    scratch_store_byte v3, v1, off sc0 sc1
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v2, off offset:1 sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, 4, v0
 ; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 4
-; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off offset:3 sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
 ; GFX942-GISEL-NEXT:    s_endpgm
 ;
@@ -60,18 +56,16 @@ define amdgpu_kernel void @soff1_voff1(i32 %soff) {
 ; GFX11-SDAG-TRUE16:       ; %bb.0: ; %bb
 ; GFX11-SDAG-TRUE16-NEXT:    s_load_b32 s0, s[4:5], 0x24
 ; GFX11-SDAG-TRUE16-NEXT:    v_and_b32_e32 v0, 0x3ff, v0
-; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v1.l, 4
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt lgkmcnt(0)
-; GFX11-SDAG-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
-; GFX11-SDAG-TRUE16-NEXT:    v_add3_u32 v2, 0, s0, v0
+; GFX11-SDAG-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
+; GFX11-SDAG-TRUE16-NEXT:    v_add3_u32 v1, 0, s0, v0
 ; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v0.l, 1
 ; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v0.h, 2
-; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v3, 1, v2
-; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v4, 2, v2
-; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v2, 4, v2
-; GFX11-SDAG-TRUE16-NEXT:    scratch_store_b8 v3, v0, off dlc
+; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v2, 4, v1
+; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v1.l, 4
+; GFX11-SDAG-TRUE16-NEXT:    scratch_store_b8 v2, v0, off offset:-3 dlc
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt_vscnt null, 0x0
-; GFX11-SDAG-TRUE16-NEXT:    scratch_store_d16_hi_b8 v4, v0, off dlc
+; GFX11-SDAG-TRUE16-NEXT:    scratch_store_d16_hi_b8 v2, v0, off offset:-2 dlc
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt_vscnt null, 0x0
 ; GFX11-SDAG-TRUE16-NEXT:    scratch_store_b8 v2, v1, off dlc
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt_vscnt null, 0x0
@@ -85,12 +79,10 @@ define amdgpu_kernel void @soff1_voff1(i32 %soff) {
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX11-SDAG-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
 ; GFX11-SDAG-FAKE16-NEXT:    v_add3_u32 v0, 0, s0, v0
-; GFX11-SDAG-FAKE16-NEXT:    v_add_nc_u32_e32 v4, 1, v0
-; GFX11-SDAG-FAKE16-NEXT:    v_add_nc_u32_e32 v5, 2, v0
 ; GFX11-SDAG-FAKE16-NEXT:    v_add_nc_u32_e32 v0, 4, v0
-; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v4, v1, off dlc
+; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v0, v1, off offset:-3 dlc
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt_vscnt null, 0x0
-; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v5, v2, off dlc
+; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v0, v2, off offset:-2 dlc
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt_vscnt null, 0x0
 ; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v0, v3, off dlc
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt_vscnt null, 0x0
@@ -100,15 +92,14 @@ define amdgpu_kernel void @soff1_voff1(i32 %soff) {
 ; GFX11-GISEL:       ; %bb.0: ; %bb
 ; GFX11-GISEL-NEXT:    s_load_b32 s0, s[4:5], 0x24
 ; GFX11-GISEL-NEXT:    v_dual_mov_b32 v1, 1 :: v_dual_and_b32 v0, 0x3ff, v0
+; GFX11-GISEL-NEXT:    v_dual_mov_b32 v2, 2 :: v_dual_mov_b32 v3, 4
 ; GFX11-GISEL-NEXT:    s_waitcnt lgkmcnt(0)
-; GFX11-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
-; GFX11-GISEL-NEXT:    v_dual_mov_b32 v3, 4 :: v_dual_add_nc_u32 v0, s0, v0
-; GFX11-GISEL-NEXT:    v_dual_mov_b32 v2, 2 :: v_dual_add_nc_u32 v5, 2, v0
-; GFX11-GISEL-NEXT:    v_add_nc_u32_e32 v4, 1, v0
+; GFX11-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
+; GFX11-GISEL-NEXT:    v_add_nc_u32_e32 v0, s0, v0
 ; GFX11-GISEL-NEXT:    v_add_nc_u32_e32 v0, 4, v0
-; GFX11-GISEL-NEXT:    scratch_store_b8 v4, v1, off dlc
+; GFX11-GISEL-NEXT:    scratch_store_b8 v0, v1, off offset:-3 dlc
 ; GFX11-GISEL-NEXT:    s_waitcnt_vscnt null, 0x0
-; GFX11-GISEL-NEXT:    scratch_store_b8 v5, v2, off dlc
+; GFX11-GISEL-NEXT:    scratch_store_b8 v0, v2, off offset:-2 dlc
 ; GFX11-GISEL-NEXT:    s_waitcnt_vscnt null, 0x0
 ; GFX11-GISEL-NEXT:    scratch_store_b8 v0, v3, off dlc
 ; GFX11-GISEL-NEXT:    s_waitcnt_vscnt null, 0x0
@@ -179,18 +170,16 @@ define amdgpu_kernel void @soff1_voff2(i32 %soff) {
 ; GFX942-SDAG-NEXT:    s_load_dword s0, s[4:5], 0x24
 ; GFX942-SDAG-NEXT:    v_and_b32_e32 v0, 0x3ff, v0
 ; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 1
+; GFX942-SDAG-NEXT:    v_mov_b32_e32 v2, 2
 ; GFX942-SDAG-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX942-SDAG-NEXT:    v_mad_u32_u24 v0, v0, 2, s0
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v2, 1, v0
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v3, 2, v0
-; GFX942-SDAG-NEXT:    scratch_store_byte v2, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, 1, v0
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 2
-; GFX942-SDAG-NEXT:    scratch_store_byte v3, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v2, off offset:1 sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, 4, v0
 ; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 4
-; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off offset:3 sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
 ; GFX942-SDAG-NEXT:    s_endpgm
 ;
@@ -202,16 +191,14 @@ define amdgpu_kernel void @soff1_voff2(i32 %soff) {
 ; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 1
 ; GFX942-GISEL-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, s0, v0
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v2, 1, v0
-; GFX942-GISEL-NEXT:    scratch_store_byte v2, v1, off sc0 sc1
+; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, 1, v0
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v1, 2, v0
-; GFX942-GISEL-NEXT:    v_mov_b32_e32 v2, 2
-; GFX942-GISEL-NEXT:    scratch_store_byte v1, v2, off sc0 sc1
+; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 2
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off offset:1 sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, 4, v0
 ; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 4
-; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off offset:3 sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
 ; GFX942-GISEL-NEXT:    s_endpgm
 ;
@@ -219,18 +206,16 @@ define amdgpu_kernel void @soff1_voff2(i32 %soff) {
 ; GFX11-SDAG-TRUE16:       ; %bb.0: ; %bb
 ; GFX11-SDAG-TRUE16-NEXT:    s_load_b32 s0, s[4:5], 0x24
 ; GFX11-SDAG-TRUE16-NEXT:    v_and_b32_e32 v0, 0x3ff, v0
-; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v1.l, 4
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt lgkmcnt(0)
-; GFX11-SDAG-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
-; GFX11-SDAG-TRUE16-NEXT:    v_mad_u32_u24 v2, v0, 2, s0
+; GFX11-SDAG-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
+; GFX11-SDAG-TRUE16-NEXT:    v_mad_u32_u24 v1, v0, 2, s0
 ; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v0.l, 1
 ; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v0.h, 2
-; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v3, 1, v2
-; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v4, 2, v2
-; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v2, 4, v2
-; GFX11-SDAG-TRUE16-NEXT:    scratch_store_b8 v3, v0, off dlc
+; GFX11-SDAG-TRUE16-NEXT:    v_add_nc_u32_e32 v2, 4, v1
+; GFX11-SDAG-TRUE16-NEXT:    v_mov_b16_e32 v1.l, 4
+; GFX11-SDAG-TRUE16-NEXT:    scratch_store_b8 v2, v0, off offset:-3 dlc
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt_vscnt null, 0x0
-; GFX11-SDAG-TRUE16-NEXT:    scratch_store_d16_hi_b8 v4, v0, off dlc
+; GFX11-SDAG-TRUE16-NEXT:    scratch_store_d16_hi_b8 v2, v0, off offset:-2 dlc
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt_vscnt null, 0x0
 ; GFX11-SDAG-TRUE16-NEXT:    scratch_store_b8 v2, v1, off dlc
 ; GFX11-SDAG-TRUE16-NEXT:    s_waitcnt_vscnt null, 0x0
@@ -244,12 +229,10 @@ define amdgpu_kernel void @soff1_voff2(i32 %soff) {
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX11-SDAG-FAKE16-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
 ; GFX11-SDAG-FAKE16-NEXT:    v_mad_u32_u24 v0, v0, 2, s0
-; GFX11-SDAG-FAKE16-NEXT:    v_add_nc_u32_e32 v4, 1, v0
-; GFX11-SDAG-FAKE16-NEXT:    v_add_nc_u32_e32 v5, 2, v0
 ; GFX11-SDAG-FAKE16-NEXT:    v_add_nc_u32_e32 v0, 4, v0
-; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v4, v1, off dlc
+; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v0, v1, off offset:-3 dlc
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt_vscnt null, 0x0
-; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v5, v2, off dlc
+; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v0, v2, off offset:-2 dlc
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt_vscnt null, 0x0
 ; GFX11-SDAG-FAKE16-NEXT:    scratch_store_b8 v0, v3, off dlc
 ; GFX11-SDAG-FAKE16-NEXT:    s_waitcnt_vscnt null, 0x0
@@ -259,18 +242,16 @@ define amdgpu_kernel void @soff1_voff2(i32 %soff) {
 ; GFX11-GISEL:       ; %bb.0: ; %bb
 ; GFX11-GISEL-NEXT:    s_load_b32 s0, s[4:5], 0x24
 ; GFX11-GISEL-NEXT:    v_dual_mov_b32 v1, 1 :: v_dual_and_b32 v0, 0x3ff, v0
-; GFX11-GISEL-NEXT:    v_mov_b32_e32 v3, 4
+; GFX11-GISEL-NEXT:    v_dual_mov_b32 v2, 2 :: v_dual_mov_b32 v3, 4
 ; GFX11-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
 ; GFX11-GISEL-NEXT:    v_mul_u32_u24_e32 v0, 2, v0
 ; GFX11-GISEL-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX11-GISEL-NEXT:    v_add_nc_u32_e32 v0, s0, v0
 ; GFX11-GISEL-NEXT:    s_delay_alu instid0(VALU_DEP_1)
-; GFX11-GISEL-NEXT:    v_dual_mov_b32 v2, 2 :: v_dual_add_nc_u32 v5, 2, v0
-; GFX11-GISEL-NEXT:    v_add_nc_u32_e32 v4, 1, v0
 ; GFX11-GISEL-NEXT:    v_add_nc_u32_e32 v0, 4, v0
-; GFX11-GISEL-NEXT:    scratch_store_b8 v4, v1, off dlc
+; GFX11-GISEL-NEXT:    scratch_store_b8 v0, v1, off offset:-3 dlc
 ; GFX11-GISEL-NEXT:    s_waitcnt_vscnt null, 0x0
-; GFX11-GISEL-NEXT:    scratch_store_b8 v5, v2, off dlc
+; GFX11-GISEL-NEXT:    scratch_store_b8 v0, v2, off offset:-2 dlc
 ; GFX11-GISEL-NEXT:    s_waitcnt_vscnt null, 0x0
 ; GFX11-GISEL-NEXT:    scratch_store_b8 v0, v3, off dlc
 ; GFX11-GISEL-NEXT:    s_waitcnt_vscnt null, 0x0
@@ -347,18 +328,16 @@ define amdgpu_kernel void @soff1_voff4(i32 %soff) {
 ; GFX942-SDAG-NEXT:    s_load_dword s0, s[4:5], 0x24
 ; GFX942-SDAG-NEXT:    v_and_b32_e32 v0, 0x3ff, v0
 ; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 1
+; GFX942-SDAG-NEXT:    v_mov_b32_e32 v2, 2
 ; GFX942-SDAG-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX942-SDAG-NEXT:    v_mad_u32_u24 v0, v0, 4, s0
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v2, 1, v0
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v3, 2, v0
-; GFX942-SDAG-NEXT:    scratch_store_byte v2, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, 1, v0
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 2
-; GFX942-SDAG-NEXT:    scratch_store_byte v3, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v2, off offset:1 sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-SDAG-NEXT:    v_add_u32_e32 v0, 4, v0
 ; GFX942-SDAG-NEXT:    v_mov_b32_e32 v1, 4
-; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
+; GFX942-SDAG-NEXT:    scratch_store_byte v0, v1, off offset:3 sc0 sc1
 ; GFX942-SDAG-NEXT:    s_waitcnt vmcnt(0)
 ; GFX942-SDAG-NEXT:    s_endpgm
 ;
@@ -370,16 +349,14 @@ define amdgpu_kernel void @soff1_voff4(i32 %soff) {
 ; GFX942-GISEL-NEXT:    v_mov_b32_e32 v1, 1
 ; GFX942-GISEL-NEXT:    s_waitcnt lgkmcnt(0)
 ; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, s0, v0
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v2, 1, v0
-; GFX942-GISEL-NEXT:    scratch_store_byte v2, v1, off sc0 sc1
+; GFX942-GISEL-NEXT:    v_add_u32_e32 v0, 1, v0
+; GFX942-GISEL-NEXT:    scratch_store_byte v0, v1, off sc0 sc1
 ; GFX942-GISEL-NEXT:    s_waitcnt vmcnt(0)
-; GFX942-GISEL-NEXT:    v_add_u32_e32 v1, 2, v0
-; GFX942-GISEL-NEXT:    v_mov_b32_e32 v2, 2
-; GFX942-GISEL-NEXT:    scratch_store_byte v1, v2, off sc0 sc1
+; GFX942-GISEL-NEXT:    v_mov_b32_...
[truncated]

@mgcarrasco

Copy link
Copy Markdown
Contributor

Hi! This comment was generated using fuzz-fill, a new tool we're building to help improve LLVM test coverage by spotting lines introduced in a PR that don't yet appear to be exercised by the full test suite, including any tests added in this PR.

Looking at PR head 8ce99a9cb76b, we found 1 line added or modified here that we couldn't find a test for:

If it makes sense, adding a regression test that validates these lines would be great. And if any of them are already tested and we missed it, we'd love to hear so we can improve the tool. Feel free to give us some feedback on whether this review was useful from your perspective. Thanks!

@aobolensk

Copy link
Copy Markdown
Contributor Author

Hi! This comment was generated using fuzz-fill, a new tool we're building to help improve LLVM test coverage by spotting lines introduced in a PR that don't yet appear to be exercised by the full test suite, including any tests added in this PR.

Looking at PR head 8ce99a9cb76b, we found 1 line added or modified here that we couldn't find a test for:

If it makes sense, adding a regression test that validates these lines would be great. And if any of them are already tested and we missed it, we'd love to hear so we can improve the tool. Feel free to give us some feedback on whether this review was useful from your perspective. Thanks!

I have confirmed it locally that this line is used in diffoporder_add_scratch_vgpr test. There is a mistake in the detection

@mgcarrasco

Copy link
Copy Markdown
Contributor

Hi! This comment was generated using fuzz-fill, a new tool we're building to help improve LLVM test coverage by spotting lines introduced in a PR that don't yet appear to be exercised by the full test suite, including any tests added in this PR.
Looking at PR head 8ce99a9cb76b, we found 1 line added or modified here that we couldn't find a test for:

If it makes sense, adding a regression test that validates these lines would be great. And if any of them are already tested and we missed it, we'd love to hear so we can improve the tool. Feel free to give us some feedback on whether this review was useful from your perspective. Thanks!

I have confirmed it locally that this line is used in diffoporder_add_scratch_vgpr test. There is a mistake in the detection

Thanks! We will check on it.

@mgcarrasco

Copy link
Copy Markdown
Contributor

@aobolensk thanks again for the feedback. Just checking, the detection was done in 8ce99a9, but that test was added after in 8af0a21, right?

@aobolensk

Copy link
Copy Markdown
Contributor Author

@aobolensk thanks again for the feedback. Just checking, the detection was done in 8ce99a9, but that test was added after in 8af0a21, right?

Ah, that's my fault. Sorry for the confusion

@mgcarrasco

Copy link
Copy Markdown
Contributor

@aobolensk thanks again for the feedback. Just checking, the detection was done in 8ce99a9, but that test was added after in 8af0a21, right?

Ah, that's my fault. Sorry for the confusion

No worries, huge thanks again, this is super helpful.

@aobolensk

Copy link
Copy Markdown
Contributor Author

@aobolensk thanks again for the feedback. Just checking, the detection was done in 8ce99a9, but that test was added after in 8af0a21, right?

Ah, that's my fault. Sorry for the confusion

In 8af0a21 the test to address that gap was added

@aobolensk
aobolensk requested review from arsenm and vpykhtin and removed request for vpykhtin August 6, 2026 14:03
@aobolensk

Copy link
Copy Markdown
Contributor Author

@arsenm @vpykhtin could you please take a look?

@aobolensk

Copy link
Copy Markdown
Contributor Author

@arsenm @vpykhtin ping

; GFX942-SDAG-NEXT: v_add_u32_e32 v3, 2, v0
; GFX942-SDAG-NEXT: scratch_store_byte v2, v1, off sc0 sc1
; GFX942-SDAG-NEXT: v_add_u32_e32 v0, 1, v0
; GFX942-SDAG-NEXT: scratch_store_byte v0, v1, off sc0 sc1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this belongs to this patch but it choosed to increment base by 1 instead of using 1 as offset (but then v0 is reused with 1 and 3 offsets)

; RUN: llc -mtriple=amdgpu9.0a < %s | FileCheck -check-prefixes=GFX9,GFX90A %s
; RUN: llc -mtriple=amdgpu11.00 -mattr=+real-true16 < %s | FileCheck -check-prefixes=GFX11,GFX11-TRUE16 %s
; RUN: llc -mtriple=amdgpu11.00 -mattr=-real-true16 < %s | FileCheck -check-prefixes=GFX11,GFX11-FAKE16 %s
; RUN: llc -mtriple=amdgpu9.00 -mattr=+enable-flat-scratch < %s | FileCheck -check-prefixes=GFX9-FLATSCR %s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add new runlines with precommit to this PR so that we could see the diff?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #225397

@@ -1,5 +1,5 @@
# RUN: llc -mtriple=amdgpu9.00 -verify-machineinstrs -run-pass si-load-store-opt -o - %s | FileCheck -check-prefixes=GCN,GFX9 %s
# RUN: llc -mtriple=amdgpu8.03 -verify-machineinstrs -run-pass si-load-store-opt -o - %s | FileCheck -check-prefixes=GCN,GFX8 %s
# RUN: llc -mtriple=amdgpu9.00 -mattr=+enable-flat-scratch -verify-machineinstrs -run-pass si-load-store-opt -o - %s | FileCheck -check-prefixes=GCN,GFX9 %s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like can be added with precommit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #225397

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vpykhtin ping, pre-commit is there: #225397

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 147072 tests passed
  • 2297 tests skipped

✅ The build succeeded and all tests passed.

bool SILoadStoreOptimizer::processBaseWithConstOffset32(
MachineInstr *AddDef, const MachineOperand &Base, MemAddress &Addr) const {
if (!Base.isReg())
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be possible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked, the caller already checks it, removed this check

@vpykhtin

vpykhtin commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Claude 5.5: I applied it on current main and found a few problems. Repros are below; each one fails with the patch.

1. GLOBAL saddr loads are now changed, and the result can be wrong

The new V_ADD_U32_e64 match is in processBaseWithConstOffset, which runs for every FLAT instruction. GLOBAL_*_SADDR also has a 32-bit vaddr, so it gets promoted too. That isn't safe: the hardware zero-extends vaddr, and the 32-bit add can wrap.

Testcase (llc -mtriple=amdgpu9.00; same with amdgpu11.00, amdgpu12.00)
define amdgpu_kernel void @g(ptr addrspace(1) inreg %p, ptr addrspace(1) %out, i32 %x) {
  %tid = call i32 @llvm.amdgcn.workitem.id.x()
  %b = add i32 %tid, %x
  %o1 = add i32 %b, 512
  %o2 = add i32 %b, 640
  %z1 = zext i32 %o1 to i64
  %z2 = zext i32 %o2 to i64
  %a1 = getelementptr i8, ptr addrspace(1) %p, i64 %z1
  %a2 = getelementptr i8, ptr addrspace(1) %p, i64 %z2
  %v1 = load i32, ptr addrspace(1) %a1
  %v2 = load i32, ptr addrspace(1) %a2
  %s = add i32 %v1, %v2
  store i32 %s, ptr addrspace(1) %out
  ret void
}
declare i32 @llvm.amdgcn.workitem.id.x()

With the patch:

v_add_u32_e32 v0, 0x280, v0
global_load_dword v1, v0, s[0:1] offset:-128
global_load_dword v2, v0, s[0:1]

With b = -640, the first load should read p + 0xFFFFFF80, but now it reads p - 128. I think the 32-bit path should be limited to FLAT_SCRATCH.

2. GFX11 SVS swizzle bug

The SVS forms are now handled, but there is no check for hasFlatScratchSVSSwizzleBug(). Moving part of the constant from vaddr into the immediate changes the carry out of the low 2 bits. For example, with v&3 == 3 and s&3 == 0, the original access is fine, but the new one hits the bug. ISel avoids this with checkFlatScratchSVSSwizzleBug. Skipping SVS on GFX11 would be the simplest fix.

Testcase (llc -mtriple=amdgpu11.00 -run-pass si-load-store-opt)
---
name: svs_swizzle
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $vgpr0, $sgpr0
    %0:vgpr_32 = COPY $vgpr0
    %1:sreg_32_xexec_hi = COPY $sgpr0
    %2:vgpr_32 = V_ADD_U32_e64 %0, 1, 0, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD_SVS %2, %1, 0, 0, implicit $exec, implicit $flat_scr
    %4:vgpr_32 = V_ADD_U32_e64 %0, 4, 0, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD_SVS %4, %1, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result:

%6:vgpr_32 = V_ADD_U32_e64 %0, 4, 0, implicit $exec
%3:vgpr_32 = SCRATCH_LOAD_DWORD_SVS %6, %1, -3, 0, implicit $exec, implicit $flat_scr

3. Base subregister is dropped

processBaseWithConstOffset32 doesn't set LoSubReg, and computeBase doesn't use it. As a result, %r.sub0 and %r.sub1 are treated as the same base.

Testcase (llc -mtriple=amdgpu11.00 -run-pass si-load-store-opt -verify-machineinstrs)
---
name: subreg_base
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $vgpr0_vgpr1
    %0:vreg_64 = COPY $vgpr0_vgpr1
    %2:vgpr_32 = V_ADD_U32_e64 %0.sub0, 100, 0, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD %2, 0, 0, implicit $exec, implicit $flat_scr
    %4:vgpr_32 = V_ADD_U32_e64 %0.sub1, 200, 0, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD %4, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result: both loads use the new base, and the sub1 base is lost:

%6:vgpr_32 = V_ADD_U32_e64 %0:vreg_64, %5:sreg_32, 0, implicit $exec
%2:vgpr_32 = SCRATCH_LOAD_DWORD %6:vgpr_32, -100, 0, implicit $exec, implicit $flat_scr
%4:vgpr_32 = SCRATCH_LOAD_DWORD %6:vgpr_32, 0, 0, implicit $exec, implicit $flat_scr

*** Bad machine code: Illegal virtual register for instruction ***

4. Smaller issues

Clamp isn't checked. An add with clamp=1 saturates, so its constant can't be split off. The pass replaces it with a non-clamped add plus an immediate.

Testcase (llc -mtriple=amdgpu11.00 -run-pass si-load-store-opt)
---
name: clamp_set
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $vgpr0
    %0:vgpr_32 = COPY $vgpr0
    %2:vgpr_32 = V_ADD_U32_e64 %0, 1, 1, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD %2, 0, 0, implicit $exec, implicit $flat_scr
    %4:vgpr_32 = V_ADD_U32_e64 %0, 4, 1, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD %4, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result:

%5:vgpr_32 = V_ADD_U32_e64 %0, 4, 0, implicit $exec
%2:vgpr_32 = SCRATCH_LOAD_DWORD %5, -3, 0, implicit $exec, implicit $flat_scr

SGPR base on gfx9. If the base is an SGPR, the rebuilt add can have two SGPR operands, which breaks the constant bus limit. Requiring a VGPR base would avoid this.

Testcase (llc -mtriple=amdgpu9.00 -run-pass si-load-store-opt -verify-machineinstrs)
---
name: sgpr_base
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $sgpr0
    %0:sreg_32 = COPY $sgpr0
    %2:vgpr_32 = V_ADD_U32_e64 %0, 64, 0, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD %2, 0, 0, implicit $exec, implicit $flat_scr
    %6:sreg_32 = S_MOV_B32 1000
    %4:vgpr_32 = V_ADD_U32_e64 %0, %6, 0, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD %4, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result:

*** Bad machine code: VOP* instruction violates constant bus restriction ***
- instruction: %4:vgpr_32 = V_ADD_U32_e64 %0:sreg_32, %3:sreg_32, 0, implicit $exec

MIR test. The GFX8 run line doesn't test anything, since gfx803 has no scratch instructions. A case where negative offsets aren't allowed (so the smallest offset becomes the anchor) would be more useful.


The scratch changes themselves look correct to me. The new vaddr is always the real address of another access in the same block, so it stays within the non-negative base rule from isFlatScratchBaseLegal.

@aobolensk

Copy link
Copy Markdown
Contributor Author

Claude 5.5: I applied it on current main and found a few problems. Repros are below; each one fails with the patch.

1. GLOBAL saddr loads are now changed, and the result can be wrong

The new V_ADD_U32_e64 match is in processBaseWithConstOffset, which runs for every FLAT instruction. GLOBAL_*_SADDR also has a 32-bit vaddr, so it gets promoted too. That isn't safe: the hardware zero-extends vaddr, and the 32-bit add can wrap.

Testcase (llc -mtriple=amdgpu9.00; same with amdgpu11.00, amdgpu12.00)

define amdgpu_kernel void @g(ptr addrspace(1) inreg %p, ptr addrspace(1) %out, i32 %x) {
  %tid = call i32 @llvm.amdgcn.workitem.id.x()
  %b = add i32 %tid, %x
  %o1 = add i32 %b, 512
  %o2 = add i32 %b, 640
  %z1 = zext i32 %o1 to i64
  %z2 = zext i32 %o2 to i64
  %a1 = getelementptr i8, ptr addrspace(1) %p, i64 %z1
  %a2 = getelementptr i8, ptr addrspace(1) %p, i64 %z2
  %v1 = load i32, ptr addrspace(1) %a1
  %v2 = load i32, ptr addrspace(1) %a2
  %s = add i32 %v1, %v2
  store i32 %s, ptr addrspace(1) %out
  ret void
}
declare i32 @llvm.amdgcn.workitem.id.x()

With the patch:

v_add_u32_e32 v0, 0x280, v0
global_load_dword v1, v0, s[0:1] offset:-128
global_load_dword v2, v0, s[0:1]

With b = -640, the first load should read p + 0xFFFFFF80, but now it reads p - 128. I think the 32-bit path should be limited to FLAT_SCRATCH.

2. GFX11 SVS swizzle bug

The SVS forms are now handled, but there is no check for hasFlatScratchSVSSwizzleBug(). Moving part of the constant from vaddr into the immediate changes the carry out of the low 2 bits. For example, with v&3 == 3 and s&3 == 0, the original access is fine, but the new one hits the bug. ISel avoids this with checkFlatScratchSVSSwizzleBug. Skipping SVS on GFX11 would be the simplest fix.

Testcase (llc -mtriple=amdgpu11.00 -run-pass si-load-store-opt)

---
name: svs_swizzle
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $vgpr0, $sgpr0
    %0:vgpr_32 = COPY $vgpr0
    %1:sreg_32_xexec_hi = COPY $sgpr0
    %2:vgpr_32 = V_ADD_U32_e64 %0, 1, 0, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD_SVS %2, %1, 0, 0, implicit $exec, implicit $flat_scr
    %4:vgpr_32 = V_ADD_U32_e64 %0, 4, 0, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD_SVS %4, %1, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result:

%6:vgpr_32 = V_ADD_U32_e64 %0, 4, 0, implicit $exec
%3:vgpr_32 = SCRATCH_LOAD_DWORD_SVS %6, %1, -3, 0, implicit $exec, implicit $flat_scr

3. Base subregister is dropped

processBaseWithConstOffset32 doesn't set LoSubReg, and computeBase doesn't use it. As a result, %r.sub0 and %r.sub1 are treated as the same base.

Testcase (llc -mtriple=amdgpu11.00 -run-pass si-load-store-opt -verify-machineinstrs)

---
name: subreg_base
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $vgpr0_vgpr1
    %0:vreg_64 = COPY $vgpr0_vgpr1
    %2:vgpr_32 = V_ADD_U32_e64 %0.sub0, 100, 0, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD %2, 0, 0, implicit $exec, implicit $flat_scr
    %4:vgpr_32 = V_ADD_U32_e64 %0.sub1, 200, 0, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD %4, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result: both loads use the new base, and the sub1 base is lost:

%6:vgpr_32 = V_ADD_U32_e64 %0:vreg_64, %5:sreg_32, 0, implicit $exec
%2:vgpr_32 = SCRATCH_LOAD_DWORD %6:vgpr_32, -100, 0, implicit $exec, implicit $flat_scr
%4:vgpr_32 = SCRATCH_LOAD_DWORD %6:vgpr_32, 0, 0, implicit $exec, implicit $flat_scr

*** Bad machine code: Illegal virtual register for instruction ***

4. Smaller issues

Clamp isn't checked. An add with clamp=1 saturates, so its constant can't be split off. The pass replaces it with a non-clamped add plus an immediate.

Testcase (llc -mtriple=amdgpu11.00 -run-pass si-load-store-opt)

---
name: clamp_set
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $vgpr0
    %0:vgpr_32 = COPY $vgpr0
    %2:vgpr_32 = V_ADD_U32_e64 %0, 1, 1, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD %2, 0, 0, implicit $exec, implicit $flat_scr
    %4:vgpr_32 = V_ADD_U32_e64 %0, 4, 1, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD %4, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result:

%5:vgpr_32 = V_ADD_U32_e64 %0, 4, 0, implicit $exec
%2:vgpr_32 = SCRATCH_LOAD_DWORD %5, -3, 0, implicit $exec, implicit $flat_scr

SGPR base on gfx9. If the base is an SGPR, the rebuilt add can have two SGPR operands, which breaks the constant bus limit. Requiring a VGPR base would avoid this.

Testcase (llc -mtriple=amdgpu9.00 -run-pass si-load-store-opt -verify-machineinstrs)

---
name: sgpr_base
tracksRegLiveness: true
body: |
  bb.0:
    liveins: $sgpr0
    %0:sreg_32 = COPY $sgpr0
    %2:vgpr_32 = V_ADD_U32_e64 %0, 64, 0, implicit $exec
    %3:vgpr_32 = SCRATCH_LOAD_DWORD %2, 0, 0, implicit $exec, implicit $flat_scr
    %6:sreg_32 = S_MOV_B32 1000
    %4:vgpr_32 = V_ADD_U32_e64 %0, %6, 0, implicit $exec
    %5:vgpr_32 = SCRATCH_LOAD_DWORD %4, 0, 0, implicit $exec, implicit $flat_scr
    S_ENDPGM 0, implicit %3, implicit %5
...

Result:

*** Bad machine code: VOP* instruction violates constant bus restriction ***
- instruction: %4:vgpr_32 = V_ADD_U32_e64 %0:sreg_32, %3:sreg_32, 0, implicit $exec

MIR test. The GFX8 run line doesn't test anything, since gfx803 has no scratch instructions. A case where negative offsets aren't allowed (so the smallest offset becomes the anchor) would be more useful.

The scratch changes themselves look correct to me. The new vaddr is always the real address of another access in the same block, so it stays within the non-negative base rule from isFlatScratchBaseLegal.

Thanks, all five reproduced and are fixed:

  1. 32-bit path is now scratch-only
  2. SVS is now skipped on GFX11
  3. base keeps its subregister
  4. Clamped adds and SGPR bases are rejected. SGPR repro already fails the verifier before the pass runs, so the test uses V_MOV_B32 offsets instead, GFX8 scratch checks are gone

The new promote-constOffset-to-imm-scratch.mir covers each case on gfx942 and gfx1100, plus one where the first access has the smallest offset

@vpykhtin vpykhtin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, assuming Matt's issues are solved.

@aobolensk

Copy link
Copy Markdown
Contributor Author

LGTM, assuming Matt's issues are solved.

Thanks, could you please take a look at the pre-committed tests as well? #225397

aobolensk added a commit that referenced this pull request Nov 3, 2026
llvm-upstreamsync Bot pushed a commit to qualcomm/cpullvm-toolchain that referenced this pull request Nov 3, 2026
llvm-sync Bot pushed a commit to arm/arm-toolchain that referenced this pull request Nov 3, 2026
@aobolensk
aobolensk merged commit 030f8ab into llvm:main Nov 4, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants