[llvm-branch-commits] [llvm] [InferAddressSpaces] Use a new value map for each rewritten constant (PR #240662)
Shilei Tian via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Nov 9 17:58:40 PST 2026
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/240662
>From 7e39d7383279eac25d142a2e36e90874281dafbe Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Mon, 9 Nov 2026 14:31:30 -0500
Subject: [PATCH] [InferAddressSpaces] Use a new value map for each rewritten
constant
When the pass rewrites a flat constant expression, it remaps the
instructions that use it through other constant expressions with a
`ValueMapper`. All rewritten constants shared one map, and the mapper
caches every constant that it visits, including the ones that it maps to
themselves. So when one instruction uses two rewritten constants, the
remap for the first one caches the part with the second one as
unchanged, and the remap for the second one reuses that entry. For
example, if a call has `addrspacecast`s of two flat pointers that are
based on the same `addrspace(3)` global, only one argument is folded.
This PR uses a new map for each rewritten constant.
This PR was assisted by AI but I reviewed all the changes.
---
.../Transforms/Scalar/InferAddressSpaces.cpp | 8 ++++--
.../AMDGPU/constexpr-two-casts-one-call.ll | 25 +++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/Transforms/InferAddressSpaces/AMDGPU/constexpr-two-casts-one-call.ll
diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index e603bccdcb74f0..9c87d85287142d 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -1648,8 +1648,6 @@ bool InferAddressSpacesImpl::rewriteWithNewAddressSpaces(
}
SmallVector<Instruction *, 16> DeadInstructions;
- ValueToValueMapTy VMap;
- ValueMapper VMapper(VMap, RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
// Replaces the uses of the old address expressions with the new ones.
for (const WeakTrackingVH &WVH : Postorder) {
@@ -1678,7 +1676,13 @@ bool InferAddressSpacesImpl::rewriteWithNewAddressSpaces(
}
}
if (!WorkList.empty()) {
+ // Use a new map for each constant. The mapper caches every constant
+ // that it visits, so an entry from an earlier constant could stop
+ // this remap, e.g. when one instruction uses two rewritten constants.
+ ValueToValueMapTy VMap;
VMap[C] = Replace;
+ ValueMapper VMapper(VMap,
+ RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
DenseSet<User *> Visited{WorkList.begin(), WorkList.end()};
while (!WorkList.empty()) {
User *U = WorkList.pop_back_val();
diff --git a/llvm/test/Transforms/InferAddressSpaces/AMDGPU/constexpr-two-casts-one-call.ll b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/constexpr-two-casts-one-call.ll
new file mode 100644
index 00000000000000..d02dd12239e9bd
--- /dev/null
+++ b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/constexpr-two-casts-one-call.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -mtriple=amdgpu9.00-amd-amdhsa -passes=infer-address-spaces < %s | FileCheck %s
+
+; The loads make the pass rewrite both flat pointers. The call uses both of
+; them, so both arguments must fold. The remap for the first flat pointer must
+; not stop the remap for the second one.
+
+ at lds = addrspace(3) global [4 x i32] poison
+
+declare void @use_local_local(ptr addrspace(3), ptr addrspace(3))
+
+define i32 @two_casts_in_one_call() {
+; CHECK-LABEL: define i32 @two_casts_in_one_call() {
+; CHECK-NEXT: [[VAL_4:%.*]] = load i32, ptr addrspace(3) getelementptr inbounds (i8, ptr addrspace(3) @lds, i64 4), align 4
+; CHECK-NEXT: [[VAL_8:%.*]] = load i32, ptr addrspace(3) getelementptr inbounds (i8, ptr addrspace(3) @lds, i64 8), align 4
+; CHECK-NEXT: call void @use_local_local(ptr addrspace(3) getelementptr inbounds (i8, ptr addrspace(3) @lds, i64 4), ptr addrspace(3) getelementptr inbounds (i8, ptr addrspace(3) @lds, i64 8))
+; CHECK-NEXT: [[SUM:%.*]] = add i32 [[VAL_4]], [[VAL_8]]
+; CHECK-NEXT: ret i32 [[SUM]]
+;
+ %val.4 = load i32, ptr getelementptr inbounds (i8, ptr addrspacecast (ptr addrspace(3) @lds to ptr), i64 4), align 4
+ %val.8 = load i32, ptr getelementptr inbounds (i8, ptr addrspacecast (ptr addrspace(3) @lds to ptr), i64 8), align 4
+ call void @use_local_local(ptr addrspace(3) addrspacecast (ptr getelementptr inbounds (i8, ptr addrspacecast (ptr addrspace(3) @lds to ptr), i64 4) to ptr addrspace(3)), ptr addrspace(3) addrspacecast (ptr getelementptr inbounds (i8, ptr addrspacecast (ptr addrspace(3) @lds to ptr), i64 8) to ptr addrspace(3)))
+ %sum = add i32 %val.4, %val.8
+ ret i32 %sum
+}
More information about the llvm-branch-commits
mailing list