[llvm] ded546a - [InferAddressSpaces] Insert fixup casts before cloned user during rewriting phase (#226987)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 23:49:25 PDT 2026


Author: Antonio Frighetto
Date: 2026-09-30T08:49:16+02:00
New Revision: ded546ad82656ce6605ee2f2aa60c78dec70d95f

URL: https://github.com/llvm/llvm-project/commit/ded546ad82656ce6605ee2f2aa60c78dec70d95f
DIFF: https://github.com/llvm/llvm-project/commit/ded546ad82656ce6605ee2f2aa60c78dec70d95f.diff

LOG: [InferAddressSpaces] Insert fixup casts before cloned user during rewriting phase (#226987)

After reworking `operandWithNewAddressSpaceOrCreatePoison` in #225161 in
order to handle joins of different address spaces in the lattice to a
more specific address space, ensure that casts created during the fix-up
phase of poison uses are placed before the cloned user, as the latter
has already been inserted above the original instruction in
`cloneValueWithNewAddressSpace`.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
    llvm/test/Transforms/InferAddressSpaces/NVPTX/address-space-join.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index c7516158c2b30..d7ce521a5ef52 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -807,7 +807,14 @@ static Value *operandWithNewAddressSpaceOrCreatePoison(
   if (LLVM_UNLIKELY(Inst->getOpcode() == Instruction::PHI))
     return phiNodeOperandWithNewAddressSpace(NewI, OperandUse.get());
 
-  NewI->insertBefore(Inst->getIterator());
+  // During cloning phase, the cast is placed before the original flat
+  // instruction, as its clone in the new address space has not been inserted
+  // yet. During poison fixup phase, the clone already exists, thus make sure
+  // the cast is inserted before it.
+  Instruction *InsertPt = Inst;
+  if (Value *NewUser = ValueWithNewAddrSpace.lookup(Inst))
+    InsertPt = cast<Instruction>(NewUser);
+  NewI->insertBefore(InsertPt->getIterator());
   NewI->setDebugLoc(Inst->getDebugLoc());
   return NewI;
 }

diff  --git a/llvm/test/Transforms/InferAddressSpaces/NVPTX/address-space-join.ll b/llvm/test/Transforms/InferAddressSpaces/NVPTX/address-space-join.ll
index 512b6867a2c84..0a70be4c0fa74 100644
--- a/llvm/test/Transforms/InferAddressSpaces/NVPTX/address-space-join.ll
+++ b/llvm/test/Transforms/InferAddressSpaces/NVPTX/address-space-join.ll
@@ -220,3 +220,25 @@ define i32 @cluster_expression(ptr addrspace(3) %local) {
   %value = load i32, ptr addrspace(7) %p, align 4
   ret i32 %value
 }
+
+; A deferred AS3 operand needs a cast placed before the newly-cloned AS7 select.
+define i32 @mixed_deferred_select(ptr addrspace(3) %local, ptr addrspace(7) %cluster, i1 %cond) {
+; CHECK-LABEL: define i32 @mixed_deferred_select(
+; CHECK-SAME: ptr addrspace(3) [[LOCAL:%.*]], ptr addrspace(7) [[CLUSTER:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT:    [[OFFSET:%.*]] = getelementptr inbounds i32, ptr addrspace(3) [[LOCAL]], i64 1
+; CHECK-NEXT:    [[VAL:%.*]] = load i32, ptr addrspace(3) [[OFFSET]], align 4
+; CHECK-NEXT:    [[TMP1:%.*]] = addrspacecast ptr addrspace(3) [[OFFSET]] to ptr addrspace(7)
+; CHECK-NEXT:    [[P:%.*]] = select i1 [[COND]], ptr addrspace(7) [[TMP1]], ptr addrspace(7) [[CLUSTER]]
+; CHECK-NEXT:    [[VAL_1:%.*]] = load i32, ptr addrspace(7) [[P]], align 4
+; CHECK-NEXT:    [[RV:%.*]] = add i32 [[VAL]], [[VAL_1]]
+; CHECK-NEXT:    ret i32 [[RV]]
+;
+  %lg = addrspacecast ptr addrspace(3) %local to ptr
+  %cg = addrspacecast ptr addrspace(7) %cluster to ptr
+  %offset = getelementptr inbounds i32, ptr %lg, i64 1
+  %val = load i32, ptr %offset, align 4
+  %p = select i1 %cond, ptr %offset, ptr %cg
+  %val.1 = load i32, ptr %p, align 4
+  %rv = add i32 %val, %val.1
+  ret i32 %rv
+}


        


More information about the llvm-commits mailing list