[llvm] [InferAddressSpaces] Do not rewrite index operands of a constant GEP (PR #229491)

Malavika Samak via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 9 13:56:18 PDT 2026


https://github.com/malavikasamak updated https://github.com/llvm/llvm-project/pull/229491

>From 86b23dd48f1a3a6c0911ebdba88783277084f69f Mon Sep 17 00:00:00 2001
From: mmalavika <mmalavika at nvidia.com>
Date: Wed, 23 Sep 2026 20:17:56 +0000
Subject: [PATCH 1/2] [InferAddressSpaces] Do not rewrite index operands of a
 constant GEP

cloneConstantExprWithNewAddressSpace() previously rewrote every
operand of a GetElementPtr constant expression, including its index
operands, using the address space being inferred for the GEP's
pointer operand. Index operands are unrelated to that address space
and may themselves be (or reference) pointers in a different address
space. Rewriting them produces an invalid constant expression and
trips the assertion that the pointer operand's address space matches
the new address space.

Skip index operands (operand index != 0) entirely; only the pointer
operand needs rewriting.

Update the clone_constexpr.ll test: the pointer operand (@g2) is
still correctly rewritten to addrspace(3), but the nested
getelementptrs inside the index expression keep their original
addrspacecast rather than also being narrowed, since index operands
are not required to relate to the pointer's address space.

Add a test whose index expression is built from a global in a
different address space than the GEP's pointer operand, which
previously hit the assertion.
---
 .../Transforms/Scalar/InferAddressSpaces.cpp  |  8 ++++
 .../NVPTX/clone_constexpr.ll                  | 40 ++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index d7ce521a5ef524..dcf01b81fb67dd 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -1033,6 +1033,14 @@ Value *InferAddressSpacesImpl::cloneConstantExprWithNewAddressSpace(
       NewOperands.push_back(cast<Constant>(NewOperand));
       continue;
     }
+
+    // Avoid rewriting operands that are not the pointer, because they are not
+    // related and NewAddrSpace for the expression may not be valid for them.
+    if (CE->getOpcode() == Instruction::GetElementPtr && Index != 0) {
+      NewOperands.push_back(Operand);
+      continue;
+    }
+
     if (auto *CExpr = dyn_cast<ConstantExpr>(Operand))
       if (Value *NewOperand = cloneConstantExprWithNewAddressSpace(
               CExpr, NewAddrSpace, ValueWithNewAddrSpace, DL, TTI)) {
diff --git a/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll b/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll
index 7c4d897afae231..1c1db8f6a1e2fc 100644
--- a/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll
+++ b/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll
@@ -36,12 +36,20 @@ declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() #1
 
 ; Make sure we can clone GEP which uses complex constant expressions as indices.
 ; https://bugs.llvm.org/show_bug.cgi?id=51099
+;
+; The pointer operand (@g2) is still rewritten to addrspace(3), but the index
+; operand -- itself a constant expression that happens to reference pointers
+; -- is left as-is rather than recursed into, since index operands are not
+; required to relate to the pointer's address space (see the GEP-index-operand
+; check in cloneConstantExprWithNewAddressSpace). This means the nested
+; getelementptrs inside the index keep their original addrspacecast rather
+; than also being narrowed to addrspace(3).
 @g2 = internal addrspace(3) global [128 x i8] poison, align 1
 
 define float @complex_ce(ptr nocapture readnone %a, ptr nocapture readnone %b, ptr nocapture readnone %c) local_unnamed_addr #0 {
 ; CHECK-LABEL: @complex_ce(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[TMP0:%.*]] = load float, ptr addrspace(3) getelementptr (i8, ptr addrspace(3) @g2, i64 sub (i64 ptrtoint (ptr addrspace(3) getelementptr inbounds ([128 x i8], ptr addrspace(3) @g2, i64 0, i64 123) to i64), i64 ptrtoint (ptr addrspace(3) getelementptr inbounds ([128 x i8], ptr addrspace(3) @g2, i64 2, i64 0) to i64))), align 4
+; CHECK-NEXT:    [[TMP0:%.*]] = load float, ptr addrspace(3) getelementptr (i8, ptr addrspace(3) @g2, i64 sub (i64 ptrtoint (ptr getelementptr inbounds ([128 x i8], ptr addrspacecast (ptr addrspace(3) @g2 to ptr), i64 0, i64 123) to i64), i64 ptrtoint (ptr getelementptr inbounds ([128 x i8], ptr addrspacecast (ptr addrspace(3) @g2 to ptr), i64 2, i64 0) to i64))), align 4
 ; CHECK-NEXT:    ret float [[TMP0]]
 ;
 entry:
@@ -67,7 +75,37 @@ entry:
   ret float %0
 }
 
+; The index operand of the GEP below is computed from pointers in an address
+; space (1) different from the one inferred for the GEP's pointer operand (3).
+; The index operand must not be rewritten for the inferred address space.
+ at g3 = internal addrspace(1) global [128 x i8] poison, align 1
 
+define float @complex_ce_mixed_addrspace_index(ptr nocapture readnone %a) local_unnamed_addr #0 {
+; CHECK-LABEL: @complex_ce_mixed_addrspace_index(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = load float, ptr addrspace(3) getelementptr (i8, ptr addrspace(3) @g2, i64 sub (i64 ptrtoint (ptr getelementptr inbounds ([128 x i8], ptr addrspacecast (ptr addrspace(1) @g3 to ptr), i64 0, i64 123) to i64), i64 ptrtoint (ptr getelementptr inbounds ([128 x i8], ptr addrspacecast (ptr addrspace(1) @g3 to ptr), i64 2, i64 0) to i64))), align 4
+; CHECK-NEXT:    ret float [[TMP0]]
+;
+entry:
+  %0 = load float, ptr getelementptr (
+         i8, ptr addrspacecast (ptr addrspace(3) @g2 to ptr),
+         i64 sub (
+           i64 ptrtoint (
+             ptr getelementptr inbounds (
+               [128 x i8],
+               ptr addrspacecast (ptr addrspace(1) @g3 to ptr),
+               i64 0,
+               i64 123)
+             to i64),
+           i64 ptrtoint (
+             ptr getelementptr inbounds (
+               [128 x i8],
+               ptr addrspacecast (ptr addrspace(1) @g3 to ptr),
+               i64 2,
+               i64 0)
+             to i64))), align 4
+  ret float %0
+}
 
 attributes #0 = { convergent nounwind }
 attributes #1 = { nounwind readnone }

>From 26072f6df1716495bd172b46e2534f0fb33ffc79 Mon Sep 17 00:00:00 2001
From: mmalavika <mmalavika at nvidia.com>
Date: Fri, 9 Oct 2026 20:23:03 +0000
Subject: [PATCH 2/2] [InferAddressSpaces][NFC] Simplify
 cloneConstantExprWithNewAddressSpace

After the addrspacecast, bitcast and inttoptr cases return, the only
constant expression that can reach the generic operand loop in
cloneConstantExprWithNewAddressSpace() is a getelementptr:
isAddressExpression() only accepts those four opcodes as constant
expressions, and the select constant expression it once also covered
was removed in bbfb13a5ffbc.

Replace the loop with a GEP-only path: rewrite the pointer operand,
keep the indices as-is, and return nullptr if the pointer operand is
unchanged. This drops the IsNew bookkeeping, the Index != 0 special
case and the unreachable generic getWithOperands call, and asserts
that no other opcode reaches this point.

No functional change.
---
 .../Transforms/Scalar/InferAddressSpaces.cpp  | 74 ++++++++-----------
 1 file changed, 29 insertions(+), 45 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index dcf01b81fb67dd..1c866adf7b52a6 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -1018,53 +1018,37 @@ Value *InferAddressSpacesImpl::cloneConstantExprWithNewAddressSpace(
     return ConstantExpr::getAddrSpaceCast(CE, TargetType);
   }
 
-  // Computes the operands of the new constant expression.
-  bool IsNew = false;
-  SmallVector<Constant *, 4> NewOperands;
-  for (unsigned Index = 0; Index < CE->getNumOperands(); ++Index) {
-    Constant *Operand = CE->getOperand(Index);
-    // If the address space of `Operand` needs to be modified, the new operand
-    // with the new address space should already be in ValueWithNewAddrSpace
-    // because (1) the constant expressions we consider (i.e. addrspacecast,
-    // bitcast, and getelementptr) do not incur cycles in the data flow graph
-    // and (2) this function is called on constant expressions in postorder.
-    if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) {
-      IsNew = true;
-      NewOperands.push_back(cast<Constant>(NewOperand));
-      continue;
-    }
-
-    // Avoid rewriting operands that are not the pointer, because they are not
-    // related and NewAddrSpace for the expression may not be valid for them.
-    if (CE->getOpcode() == Instruction::GetElementPtr && Index != 0) {
-      NewOperands.push_back(Operand);
-      continue;
-    }
-
-    if (auto *CExpr = dyn_cast<ConstantExpr>(Operand))
-      if (Value *NewOperand = cloneConstantExprWithNewAddressSpace(
-              CExpr, NewAddrSpace, ValueWithNewAddrSpace, DL, TTI)) {
-        IsNew = true;
-        NewOperands.push_back(cast<Constant>(NewOperand));
-        continue;
-      }
-    // Otherwise, reuses the old operand.
-    NewOperands.push_back(Operand);
-  }
-
-  // If !IsNew, we will replace the Value with itself. However, replaced values
-  // are assumed to wrapped in an addrspacecast cast later so drop it now.
-  if (!IsNew)
+  // The remaining constant expression is a getelementptr. Only its pointer
+  // operand is rewritten: the indices are not related to the pointer and
+  // NewAddrSpace may not be valid for them.
+  assert(CE->getOpcode() == Instruction::GetElementPtr &&
+         "unexpected constant expression");
+  // If the address space of the base needs to be modified, the new base with
+  // the new address space should already be in ValueWithNewAddrSpace because
+  // (1) the constant expressions we consider do not incur cycles in the data
+  // flow graph and (2) this function is called on constant expressions in
+  // postorder.
+  Constant *Base = CE->getOperand(0);
+  Value *NewBase = ValueWithNewAddrSpace.lookup(Base);
+  if (!NewBase)
+    if (auto *BaseCE = dyn_cast<ConstantExpr>(Base))
+      NewBase = cloneConstantExprWithNewAddressSpace(
+          BaseCE, NewAddrSpace, ValueWithNewAddrSpace, DL, TTI);
+
+  // If the base is unchanged, we will replace the Value with itself. However,
+  // replaced values are assumed to wrapped in an addrspacecast cast later so
+  // drop it now.
+  if (!NewBase)
     return nullptr;
 
-  if (CE->getOpcode() == Instruction::GetElementPtr) {
-    // Needs to specify the source type while constructing a getelementptr
-    // constant expression.
-    return CE->getWithOperands(NewOperands, TargetType, /*OnlyIfReduced=*/false,
-                               cast<GEPOperator>(CE)->getSourceElementType());
-  }
-
-  return CE->getWithOperands(NewOperands, TargetType);
+  SmallVector<Constant *, 4> NewOperands;
+  NewOperands.push_back(cast<Constant>(NewBase));
+  for (unsigned Index = 1; Index < CE->getNumOperands(); ++Index)
+    NewOperands.push_back(CE->getOperand(Index));
+  // Needs to specify the source type while constructing a getelementptr
+  // constant expression.
+  return CE->getWithOperands(NewOperands, TargetType, /*OnlyIfReduced=*/false,
+                             cast<GEPOperator>(CE)->getSourceElementType());
 }
 
 // Returns a clone of the value `V`, with its operands replaced as specified in



More information about the llvm-commits mailing list