[PATCH] D105002: [InferAddressSpaces] NFC: Check type before casting operands to PtrToIntInst

Reshabh Sharma via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 27 23:02:55 PDT 2021


rksharma created this revision.
rksharma added reviewers: arsenm, hliao.
Herald added subscribers: kerbowa, hiraditya, arichardson, nhaehnle, jvesely.
rksharma requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

Compiler crashes at an assertion while casting operands to PtrToIntInst at some cases when ptrtoint is present as an explicit operand to inttoptr. Explicit instruction operator as operand can not be casted to an Instruction.

This patch replaces cast with a dyn_cast and avoids the crash.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105002

Files:
  llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
  llvm/test/Transforms/InferAddressSpaces/AMDGPU/noop-ptrint-pair.ll


Index: llvm/test/Transforms/InferAddressSpaces/AMDGPU/noop-ptrint-pair.ll
===================================================================
--- llvm/test/Transforms/InferAddressSpaces/AMDGPU/noop-ptrint-pair.ll
+++ llvm/test/Transforms/InferAddressSpaces/AMDGPU/noop-ptrint-pair.ll
@@ -69,6 +69,16 @@
   ret i32* inttoptr (i64 ptrtoint (i32 addrspace(1)* @g to i64) to i32*)
 }
 
+; COMMON-LABEL: @noop_ptrint_pair_ce3(
+; AMDGCN-NEXT: %i = inttoptr i64 ptrtoint (i32 addrspace(1)* @g to i64) to i32*
+; AMDGCN-NEXT: ret void
+; NOTTI-NEXT: %i = inttoptr i64 ptrtoint (i32 addrspace(1)* @g to i64) to i32*
+; NOTTI-NEXT: ret void
+define void @noop_ptrint_pair_ce3() {
+  %i = inttoptr i64 ptrtoint (i32 addrspace(1)* @g to i64) to i32*
+  ret void
+}
+
 ; COMMON-LABEL: @non_noop_ptrint_pair_ce(
 ; AMDGCN-NEXT: store i32 0, i32* inttoptr (i64 ptrtoint (i32 addrspace(3)* @l to i64) to i32*)
 ; AMDGCN-NEXT: ret void
Index: llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -470,8 +470,8 @@
         PushPtrOperand(ASC->getPointerOperand());
     } else if (auto *I2P = dyn_cast<IntToPtrInst>(&I)) {
       if (isNoopPtrIntCastPair(cast<Operator>(I2P), *DL, TTI))
-        PushPtrOperand(
-            cast<PtrToIntInst>(I2P->getOperand(0))->getPointerOperand());
+        if (PtrToIntInst *P2I = dyn_cast<PtrToIntInst>(I2P->getOperand(0)))
+          PushPtrOperand(P2I->getPointerOperand());
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105002.354785.patch
Type: text/x-patch
Size: 1590 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210628/2a508279/attachment.bin>


More information about the llvm-commits mailing list