[llvm] [SeperateConstOffsetFromGEP] Update splitGEP to handle case where including base offset results in an offset that's too large (PR #177653)

Adel Ejjeh via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 3 08:57:40 PST 2026


https://github.com/adelejjeh updated https://github.com/llvm/llvm-project/pull/177653

>From fc9de36b6487941090173750bfc4022228bb7603 Mon Sep 17 00:00:00 2001
From: Adel Ejjeh <adel.ejjeh at amd.com>
Date: Fri, 23 Jan 2026 12:34:46 -0600
Subject: [PATCH 1/2] Update splitGEP to extract offsets without base

---
 .../Scalar/SeparateConstOffsetFromGEP.cpp     | 32 +++++--
 .../AMDGPU/amdgpu-large-lds-offset.ll         | 88 +++++++++++++++++++
 2 files changed, 113 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll

diff --git a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
index 67b22ec33ef61..7c5e6fff9465d 100644
--- a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1056,12 +1056,11 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
   // offset from each is accumulated.
   Value *NewBase;
   const APInt *BaseOffset;
-  const bool ExtractBase =
-      match(GEP->getPointerOperand(),
-            m_PtrAdd(m_Value(NewBase), m_APInt(BaseOffset)));
+  bool ExtractBase = match(GEP->getPointerOperand(),
+                           m_PtrAdd(m_Value(NewBase), m_APInt(BaseOffset)));
 
   unsigned IdxWidth = DL->getIndexTypeSizeInBits(GEP->getType());
-  const APInt BaseByteOffset =
+  APInt BaseByteOffset =
       ExtractBase ? BaseOffset->sextOrTrunc(IdxWidth) : APInt(IdxWidth, 0);
 
   // The backend can already nicely handle the case where all indices are
@@ -1072,8 +1071,8 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
   bool Changed = canonicalizeArrayIndicesToIndexSize(GEP);
 
   bool NeedsExtraction;
-  APInt AccumulativeByteOffset =
-      BaseByteOffset + accumulateByteOffset(GEP, NeedsExtraction);
+  APInt NonBaseByteOffset = accumulateByteOffset(GEP, NeedsExtraction);
+  APInt AccumulativeByteOffset = BaseByteOffset + NonBaseByteOffset;
 
   TargetTransformInfo &TTI = GetTTI(*GEP->getFunction());
 
@@ -1095,7 +1094,26 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
             GEP->getResultElementType(),
             /*BaseGV=*/nullptr, AccumulativeByteOffset.getSExtValue(),
             /*HasBaseReg=*/true, /*Scale=*/0, AddrSpace)) {
-      return Changed;
+      // If the addressing mode was not legal and the base byte offset was not
+      // 0, it could be a case where the total offset became too large to what
+      // the addressing mode can represent. Try again without extracting the
+      // base offset.
+      if (ExtractBase) {
+        ExtractBase = false;
+        BaseByteOffset = APInt(IdxWidth, 0);
+        AccumulativeByteOffset = NonBaseByteOffset;
+        if (TTI.isLegalAddressingMode(
+                GEP->getResultElementType(),
+                /*BaseGV=*/nullptr, AccumulativeByteOffset.getSExtValue(),
+                /*HasBaseReg=*/true, /*Scale=*/0, AddrSpace)) {
+          // We can proceed with just extracting the other (non-base) offsets.
+          NeedsExtraction = true;
+        } else {
+          return Changed;
+        }
+      } else {
+        return Changed;
+      }
     }
   }
 
diff --git a/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll b/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll
new file mode 100644
index 0000000000000..c2f462a15effc
--- /dev/null
+++ b/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll
@@ -0,0 +1,88 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1250 -passes=separate-const-offset-from-gep -S %s | FileCheck %s
+
+; This test is intended to test that the sepaerate-const-offset-from-gep
+; pass will still separate offsets even if the base offset is large.
+
+ at global_smem = external addrspace(3) global [0 x i8], align 16
+
+define void @large_base_offset(ptr addrspace(1) writeonly %1, i32 %35) {
+; CHECK-LABEL: define void @large_base_offset(
+; CHECK-SAME: ptr addrspace(1) writeonly [[TMP0:%.*]], i32 [[TMP1:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw nsw i32 [[TMP2]], 10
+; CHECK-NEXT:    [[TMP4:%.*]] = and i32 [[TMP1]], 16
+; CHECK-NEXT:    [[TMP5:%.*]] = shl nuw nsw i32 [[TMP4]], 1
+; CHECK-NEXT:    [[TMP6:%.*]] = or disjoint i32 [[TMP5]], [[TMP3]]
+; CHECK-NEXT:    [[TMP7:%.*]] = shl nuw nsw i32 [[TMP2]], 5
+; CHECK-NEXT:    [[TMP8:%.*]] = getelementptr i8, ptr addrspace(3) @global_smem, i32 67584
+; CHECK-NEXT:    [[TMP9:%.*]] = or disjoint i32 [[TMP6]], 16384
+; CHECK-NEXT:    [[TMP10:%.*]] = lshr exact i32 [[TMP9]], 5
+; CHECK-NEXT:    [[TMP11:%.*]] = and i32 [[TMP10]], 992
+; CHECK-NEXT:    [[TMP12:%.*]] = getelementptr inbounds nuw i8, ptr addrspace(3) [[TMP8]], i32 [[TMP6]]
+; CHECK-NEXT:    [[TMP13:%.*]] = getelementptr inbounds nuw i8, ptr addrspace(3) [[TMP12]], i32 [[TMP11]]
+; CHECK-NEXT:    [[TMP14:%.*]] = getelementptr inbounds nuw i8, ptr addrspace(3) [[TMP13]], i32 16384
+; CHECK-NEXT:    [[TMP15:%.*]] = load <16 x half>, ptr addrspace(3) [[TMP14]], align 32
+; CHECK-NEXT:    [[TMP16:%.*]] = shufflevector <16 x half> [[TMP15]], <16 x half> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; CHECK-NEXT:    store <8 x half> [[TMP16]], ptr addrspace(1) [[TMP0]], align 16
+; CHECK-NEXT:    ret void
+;
+entry:
+  %36 = and i32 %35, 15
+  %37 = shl nuw nsw i32 %36, 10
+  %38 = and i32 %35, 16
+  %39 = shl nuw nsw i32 %38, 1
+  %40 = or disjoint i32 %39, %37
+  %41 = shl nuw nsw i32 %36, 5
+  %42 = getelementptr i8, ptr addrspace(3) @global_smem, i32 67584
+  %67 = or disjoint i32 %40, 16384
+  %68 = lshr exact i32 %67, 5
+  %69 = and i32 %68, 992
+  %70 = getelementptr inbounds nuw i8, ptr addrspace(3) %42, i32 %67
+  %71 = getelementptr inbounds nuw i8, ptr addrspace(3) %70, i32 %69
+  %72 = load <16 x half>, ptr addrspace(3) %71, align 32
+  %424 = shufflevector <16 x half> %72, <16 x half> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+  store <8 x half> %424, ptr addrspace(1) %1, align 16
+  ret void
+}
+
+define void @small_base_offset(ptr addrspace(1) writeonly %1, i32 %35) {
+; CHECK-LABEL: define void @small_base_offset(
+; CHECK-SAME: ptr addrspace(1) writeonly [[TMP0:%.*]], i32 [[TMP1:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw nsw i32 [[TMP2]], 10
+; CHECK-NEXT:    [[TMP4:%.*]] = and i32 [[TMP1]], 16
+; CHECK-NEXT:    [[TMP5:%.*]] = shl nuw nsw i32 [[TMP4]], 1
+; CHECK-NEXT:    [[TMP6:%.*]] = or disjoint i32 [[TMP5]], [[TMP3]]
+; CHECK-NEXT:    [[TMP7:%.*]] = shl nuw nsw i32 [[TMP2]], 5
+; CHECK-NEXT:    [[TMP8:%.*]] = or disjoint i32 [[TMP6]], 16384
+; CHECK-NEXT:    [[TMP9:%.*]] = lshr exact i32 [[TMP8]], 5
+; CHECK-NEXT:    [[TMP10:%.*]] = and i32 [[TMP9]], 992
+; CHECK-NEXT:    [[TMP11:%.*]] = getelementptr i8, ptr addrspace(3) @global_smem, i32 [[TMP6]]
+; CHECK-NEXT:    [[TMP12:%.*]] = getelementptr i8, ptr addrspace(3) [[TMP11]], i32 [[TMP10]]
+; CHECK-NEXT:    [[TMP13:%.*]] = getelementptr i8, ptr addrspace(3) [[TMP12]], i32 16388
+; CHECK-NEXT:    [[TMP14:%.*]] = load <16 x half>, ptr addrspace(3) [[TMP13]], align 32
+; CHECK-NEXT:    [[TMP15:%.*]] = shufflevector <16 x half> [[TMP14]], <16 x half> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; CHECK-NEXT:    store <8 x half> [[TMP15]], ptr addrspace(1) [[TMP0]], align 16
+; CHECK-NEXT:    ret void
+;
+entry:
+  %36 = and i32 %35, 15
+  %37 = shl nuw nsw i32 %36, 10
+  %38 = and i32 %35, 16
+  %39 = shl nuw nsw i32 %38, 1
+  %40 = or disjoint i32 %39, %37
+  %41 = shl nuw nsw i32 %36, 5
+  %42 = getelementptr i8, ptr addrspace(3) @global_smem, i32 4
+  %67 = or disjoint i32 %40, 16384
+  %68 = lshr exact i32 %67, 5
+  %69 = and i32 %68, 992
+  %70 = getelementptr inbounds nuw i8, ptr addrspace(3) %42, i32 %67
+  %71 = getelementptr inbounds nuw i8, ptr addrspace(3) %70, i32 %69
+  %72 = load <16 x half>, ptr addrspace(3) %71, align 32
+  %424 = shufflevector <16 x half> %72, <16 x half> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+  store <8 x half> %424, ptr addrspace(1) %1, align 16
+  ret void
+}

>From 03690acc716ed175900de707320f0ae25ea637d8 Mon Sep 17 00:00:00 2001
From: Adel Ejjeh <adel.ejjeh at amd.com>
Date: Tue, 3 Feb 2026 10:56:46 -0600
Subject: [PATCH 2/2] handle reviewer comments

---
 .../Scalar/SeparateConstOffsetFromGEP.cpp      |  5 ++---
 .../AMDGPU/amdgpu-large-lds-offset.ll          | 18 +++++++++---------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
index 7c5e6fff9465d..b2d818179214a 100644
--- a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1095,9 +1095,8 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
             /*BaseGV=*/nullptr, AccumulativeByteOffset.getSExtValue(),
             /*HasBaseReg=*/true, /*Scale=*/0, AddrSpace)) {
       // If the addressing mode was not legal and the base byte offset was not
-      // 0, it could be a case where the total offset became too large to what
-      // the addressing mode can represent. Try again without extracting the
-      // base offset.
+      // 0, it could be a case where the total offset became too large for
+      // the addressing mode. Try again without extracting the base offset.
       if (ExtractBase) {
         ExtractBase = false;
         BaseByteOffset = APInt(IdxWidth, 0);
diff --git a/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll b/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll
index c2f462a15effc..d709814c95752 100644
--- a/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll
+++ b/llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/amdgpu-large-lds-offset.ll
@@ -1,12 +1,12 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1250 -passes=separate-const-offset-from-gep -S %s | FileCheck %s
 
-; This test is intended to test that the sepaerate-const-offset-from-gep
+; This test is intended to test that the separate-const-offset-from-gep
 ; pass will still separate offsets even if the base offset is large.
 
 @global_smem = external addrspace(3) global [0 x i8], align 16
 
-define void @large_base_offset(ptr addrspace(1) writeonly %1, i32 %35) {
+define void @large_base_offset(ptr addrspace(1) writeonly %out, i32 %idx) {
 ; CHECK-LABEL: define void @large_base_offset(
 ; CHECK-SAME: ptr addrspace(1) writeonly [[TMP0:%.*]], i32 [[TMP1:%.*]]) #[[ATTR0:[0-9]+]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
@@ -29,9 +29,9 @@ define void @large_base_offset(ptr addrspace(1) writeonly %1, i32 %35) {
 ; CHECK-NEXT:    ret void
 ;
 entry:
-  %36 = and i32 %35, 15
+  %36 = and i32 %idx, 15
   %37 = shl nuw nsw i32 %36, 10
-  %38 = and i32 %35, 16
+  %38 = and i32 %idx, 16
   %39 = shl nuw nsw i32 %38, 1
   %40 = or disjoint i32 %39, %37
   %41 = shl nuw nsw i32 %36, 5
@@ -43,11 +43,11 @@ entry:
   %71 = getelementptr inbounds nuw i8, ptr addrspace(3) %70, i32 %69
   %72 = load <16 x half>, ptr addrspace(3) %71, align 32
   %424 = shufflevector <16 x half> %72, <16 x half> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
-  store <8 x half> %424, ptr addrspace(1) %1, align 16
+  store <8 x half> %424, ptr addrspace(1) %out, align 16
   ret void
 }
 
-define void @small_base_offset(ptr addrspace(1) writeonly %1, i32 %35) {
+define void @small_base_offset(ptr addrspace(1) writeonly %out, i32 %idx) {
 ; CHECK-LABEL: define void @small_base_offset(
 ; CHECK-SAME: ptr addrspace(1) writeonly [[TMP0:%.*]], i32 [[TMP1:%.*]]) #[[ATTR0]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
@@ -69,9 +69,9 @@ define void @small_base_offset(ptr addrspace(1) writeonly %1, i32 %35) {
 ; CHECK-NEXT:    ret void
 ;
 entry:
-  %36 = and i32 %35, 15
+  %36 = and i32 %idx, 15
   %37 = shl nuw nsw i32 %36, 10
-  %38 = and i32 %35, 16
+  %38 = and i32 %idx, 16
   %39 = shl nuw nsw i32 %38, 1
   %40 = or disjoint i32 %39, %37
   %41 = shl nuw nsw i32 %36, 5
@@ -83,6 +83,6 @@ entry:
   %71 = getelementptr inbounds nuw i8, ptr addrspace(3) %70, i32 %69
   %72 = load <16 x half>, ptr addrspace(3) %71, align 32
   %424 = shufflevector <16 x half> %72, <16 x half> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
-  store <8 x half> %424, ptr addrspace(1) %1, align 16
+  store <8 x half> %424, ptr addrspace(1) %out, align 16
   ret void
 }



More information about the llvm-commits mailing list