[PATCH] D119524: [InferAddressSpaces] Fix assert on invalid cast ordering

Austin Kerbow via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 11 10:03:18 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG0bb25b46034a: [InferAddressSpaces] Fix assert on invalid cast ordering (authored by kerbowa).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119524/new/

https://reviews.llvm.org/D119524

Files:
  llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
  llvm/test/Transforms/InferAddressSpaces/AMDGPU/insert-pos-assert.ll


Index: llvm/test/Transforms/InferAddressSpaces/AMDGPU/insert-pos-assert.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InferAddressSpaces/AMDGPU/insert-pos-assert.ll
@@ -0,0 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -infer-address-spaces %s | FileCheck %s
+
+; Addrspacecasts must be inserted after the instructions that define their uses.
+
+%struct.s0 = type { i32*, i32 }
+%struct.s1 = type { %struct.s0 }
+
+ at global0 = protected addrspace(4) externally_initialized global %struct.s1 zeroinitializer
+
+declare i32 @func(i32* %arg)
+
+define i32 @insert_pos_assert() {
+; CHECK-LABEL: @insert_pos_assert(
+; CHECK-NEXT:    [[ALLOCA:%.*]] = alloca i32, align 4, addrspace(5)
+; CHECK-NEXT:    [[LOAD0:%.*]] = load i32*, i32* addrspace(4)* getelementptr inbounds ([[STRUCT_S1:%.*]], [[STRUCT_S1]] addrspace(4)* @global0, i32 0, i32 0, i32 0), align 8
+; CHECK-NEXT:    [[TMP1:%.*]] = addrspacecast i32* [[LOAD0]] to i32 addrspace(1)*
+; CHECK-NEXT:    [[TMP2:%.*]] = addrspacecast i32 addrspace(1)* [[TMP1]] to i32*
+; CHECK-NEXT:    [[LOAD1:%.*]] = load i32, i32 addrspace(5)* [[ALLOCA]], align 4
+; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[LOAD1]] to i64
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds i32, i32* [[TMP2]], i64 [[SEXT]]
+; CHECK-NEXT:    [[CALL:%.*]] = call i32 @func(i32* [[GEP]])
+; CHECK-NEXT:    ret i32 [[CALL]]
+;
+  %alloca = alloca i32, align 4, addrspace(5)
+  %cast = addrspacecast i32 addrspace(5)* %alloca to i32*
+  %load0 = load i32*, i32* addrspace(4)* getelementptr inbounds (%struct.s1, %struct.s1 addrspace(4)* @global0, i32 0, i32 0, i32 0)
+  %load1 = load i32, i32* %cast
+  %sext = sext i32 %load1 to i64
+  %gep = getelementptr inbounds i32, i32* %load0, i64 %sext
+  %call = call i32 @func(i32* %gep)
+  ret i32 %call
+}
Index: llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -1252,12 +1252,18 @@
         }
 
         // Otherwise, replaces the use with flat(NewV).
-        if (Instruction *Inst = dyn_cast<Instruction>(V)) {
+        if (Instruction *VInst = dyn_cast<Instruction>(V)) {
           // Don't create a copy of the original addrspacecast.
           if (U == V && isa<AddrSpaceCastInst>(V))
             continue;
 
-          BasicBlock::iterator InsertPos = std::next(Inst->getIterator());
+          // Insert the addrspacecast after NewV.
+          BasicBlock::iterator InsertPos;
+          if (Instruction *NewVInst = dyn_cast<Instruction>(NewV))
+            InsertPos = std::next(NewVInst->getIterator());
+          else
+            InsertPos = std::next(VInst->getIterator());
+
           while (isa<PHINode>(InsertPos))
             ++InsertPos;
           U.set(new AddrSpaceCastInst(NewV, V->getType(), "", &*InsertPos));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119524.407932.patch
Type: text/x-patch
Size: 3032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220211/24a7c835/attachment.bin>


More information about the llvm-commits mailing list