[llvm] [InferAddressSpaces] Do not rewrite index operands of a constant GEP (PR #229491)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 12:49:19 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-nvptx
Author: Malavika Samak (malavikasamak)
<details>
<summary>Changes</summary>
InferAddressSpaces rewrites generic pointers into specific address spaces when it can infer where they point. cloneConstantExprWithNewAddressSpace() does this for constant expressions, such as a GEP over an addrspacecast of a global, by cloning the expression and recursing through its operands.
For a getelementptr constant expression, it previously rewrote every operand, including the index operands, using the address space inferred for the GEP's pointer operand. Only the pointer operand (operand 0) belongs to that address space. Index operands are independent values, and they can be computed from pointers in a different address space, for example in ptrtoint/sub offset computations. Rewriting them for the wrong address space produces an invalid constant expression. On an assertions build it aborts:
Assertion `CE->getOperand(0)->getType()->getPointerAddressSpace() == NewAddrSpace' failed.
Skip index operands (operand index . Only the pointer operand is
rewritten.
Tests, in clone_constexpr.ll:
- Add complex_ce_mixed_addrspace_inand is in addrspace(3) and whose
index is built from a global in artion above without this change and
passes with it.
- Update complex_ce: the pointer opn to addrspace(3), but the nestedGEPs in the index keep their orig's index shares the base's address
space, so it never hit the assert changes.
---
Full diff: https://github.com/llvm/llvm-project/pull/229491.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp (+8)
- (modified) llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll (+39-1)
``````````diff
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 }
``````````
</details>
https://github.com/llvm/llvm-project/pull/229491
More information about the llvm-commits
mailing list