[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
Fri Jan 23 10:40:12 PST 2026
https://github.com/adelejjeh created https://github.com/llvm/llvm-project/pull/177653
Currently, separate-const-offset-from-gep tries to combine both the offsets on the base address and the offsets on the current GEP itself when it tires to separate constant offsets. This results in the pass failing to separate the offset in cases where the base address has a large offset that would cause the total offset to be larger than what the back-end can represent for the respective addressing mode. However, in many cases we can still benefit from extracting the offset of the GEP without including the offset from the base-address-GEP when the base-address is used in multiple different places to help reduce register pressure and recalculating the base address.
This PR addresses the above by re-trying without accumulating the offset from the base-address GEP included if isLegalAddressingMode returns false the first time.
>From b43f311966cec34f3462b6e0b85f95feadba3df5 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] Update splitGEP to extract offsets without base
---
.../Scalar/SeparateConstOffsetFromGEP.cpp | 21 ++++-
.../AMDGPU/amdgpu-large-lds-offset.ll | 88 +++++++++++++++++++
2 files changed, 108 insertions(+), 1 deletion(-)
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..9cbb807b21da4 100644
--- a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1095,7 +1095,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 = ByteOffset;
+ 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
+}
More information about the llvm-commits
mailing list