Repository navigation
Conversation
…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.
|
@llvm/pr-subscribers-backend-amdgpu Author: Arseniy Obolenskiy (aobolensk) ChangesAdd 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:
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]
|
|
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 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. |
|
@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. |
In 8af0a21 the test to address that gap was added |
| ; 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Can you add new runlines with precommit to this PR so that we could see the diff?
| @@ -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 | |||
There was a problem hiding this comment.
Looks like can be added with precommit?
…TCH-32-bit-offset-promotion
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
| bool SILoadStoreOptimizer::processBaseWithConstOffset32( | ||
| MachineInstr *AddDef, const MachineOperand &Base, MemAddress &Addr) const { | ||
| if (!Base.isReg()) | ||
| return false; |
There was a problem hiding this comment.
Double checked, the caller already checks it, removed this check
|
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 wrongThe new 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: With 2. GFX11 SVS swizzle bugThe SVS forms are now handled, but there is no check for 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: 3. Base subregister is dropped
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 4. Smaller issuesClamp 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: 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: 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 |
Thanks, all five reproduced and are fixed:
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
left a comment
There was a problem hiding this comment.
LGTM, assuming Matt's issues are solved.
Thanks, could you please take a look at the pre-committed tests as well? #225397 |
…TCH-32-bit-offset-promotion
…omotion (NFC) (#225397) Prerequisite for llvm/llvm-project#212507
…omotion (NFC) (#225397) Prerequisite for llvm/llvm-project#212507
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.