[llvm] [AMDGPU][SIRegisterInfo]: Fixing maxoffset calculation in buildSpillLoadStore (PR #179182)

Abhinav Garg via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 2 00:37:42 PST 2026


https://github.com/abhigargrepo created https://github.com/llvm/llvm-project/pull/179182

This PR addresses Maxoffset calculation bug in SIRegisterInfo. When RemSize is non-zero, maxoffset, that needs to be encoded in the offset field, will be equal to "Offset + Size".

>From b7ff2a34ead62cdf0dc979c1e24dc96b96425129 Mon Sep 17 00:00:00 2001
From: Abhinav Garg <abhigarg at amd.com>
Date: Mon, 2 Feb 2026 06:35:53 +0000
Subject: [PATCH] Fixing maxoffset calculation in PEI pass

---
 llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp     |  3 +-
 .../fix-maxoffset-calculation-peiPass.mir     | 34 +++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/AMDGPU/fix-maxoffset-calculation-peiPass.mir

diff --git a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
index 96c2f6530fe4c..005ded0502b5a 100644
--- a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
@@ -1548,8 +1548,7 @@ void SIRegisterInfo::buildSpillLoadStore(
   unsigned NumRemSubRegs = RemSize ? 1 : 0;
   int64_t Offset = InstOffset + MFI.getObjectOffset(Index);
   int64_t MaterializedOffset = Offset;
-
-  int64_t MaxOffset = Offset + Size + RemSize - EltSize;
+  int64_t MaxOffset = Offset + Size - (RemSize ? 0 : EltSize);
   int64_t ScratchOffsetRegDelta = 0;
 
   if (IsFlat && EltSize > 4) {
diff --git a/llvm/test/CodeGen/AMDGPU/fix-maxoffset-calculation-peiPass.mir b/llvm/test/CodeGen/AMDGPU/fix-maxoffset-calculation-peiPass.mir
new file mode 100644
index 0000000000000..4615207b5c0ac
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/fix-maxoffset-calculation-peiPass.mir
@@ -0,0 +1,34 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
+# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -verify-machineinstrs -simplify-mir -amdgpu-spill-vgpr-to-agpr=0 -run-pass=prologepilog -o - %s | FileCheck %s
+
+# Test that the PEI pass does correct calculations for Maxoffset in case of
+# spill instructions. Must emit offset within 13 bit signed number range.
+
+---
+name:            fix_maxoffset_PEI_pass
+tracksRegLiveness: true
+fixedStack:
+  - { id: 0, type: spill-slot, offset: 4084, size: 24, alignment: 4,
+      stack-id: default, callee-saved-register: '', callee-saved-restored: true,
+      debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
+stack:           []
+machineFunctionInfo:
+  hasSpilledVGPRs: true
+  stackPtrOffsetReg: '$sgpr32'
+body:             |
+  ; CHECK-LABEL: name: fix_maxoffset_PEI_pass
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   liveins: $vgpr162_vgpr163_vgpr164_vgpr165_vgpr166_vgpr167
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   $sgpr0 = S_ADD_I32 $sgpr32, 4084, implicit-def dead $scc
+  ; CHECK-NEXT:   SCRATCH_STORE_DWORDX4_SADDR $vgpr162_vgpr163_vgpr164_vgpr165, $sgpr0, 0, 0, implicit $exec, implicit $flat_scr, implicit-def $vgpr162_vgpr163_vgpr164_vgpr165_vgpr166_vgpr167, implicit $vgpr162_vgpr163_vgpr164_vgpr165_vgpr166_vgpr167 :: (store (s128) into %fixed-stack.0, align 4, addrspace 5)
+  ; CHECK-NEXT:   SCRATCH_STORE_DWORDX2_SADDR $vgpr166_vgpr167, killed $sgpr0, 16, 0, implicit $exec, implicit $flat_scr, implicit $vgpr162_vgpr163_vgpr164_vgpr165_vgpr166_vgpr167 :: (store (s64) into %fixed-stack.0 + 16, align 4, addrspace 5)
+  ; CHECK-NEXT:   S_ENDPGM 0
+  bb.0:
+    liveins: $vgpr162_vgpr163_vgpr164_vgpr165_vgpr166_vgpr167
+  bb.1:
+    SI_SPILL_AV192_SAVE $vgpr162_vgpr163_vgpr164_vgpr165_vgpr166_vgpr167, %fixed-stack.0, $sgpr32, 0, implicit $exec :: (store (s192) into %fixed-stack.0, align 4, addrspace 5)
+    S_ENDPGM 0
+
+...



More information about the llvm-commits mailing list