[PATCH] D87709: InferAddressSpaces: Fix assert with unreachable code

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 11:14:43 PDT 2020


arsenm created this revision.
arsenm added reviewers: hliao, tra, jdoerfert, yaxunl, rampitec.
Herald added subscribers: kerbowa, hiraditya, nhaehnle, jvesely.
Herald added a project: LLVM.
arsenm requested review of this revision.
Herald added a subscriber: wdng.

Invalid IR in unreachable code is technically valid IR. In this case,
the address space of the value was never inferred, and we tried to
rewrite it with an invalid address space value which would assert.


https://reviews.llvm.org/D87709

Files:
  llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
  llvm/test/Transforms/InferAddressSpaces/AMDGPU/unreachable-code-assert.ll


Index: llvm/test/Transforms/InferAddressSpaces/AMDGPU/unreachable-code-assert.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InferAddressSpaces/AMDGPU/unreachable-code-assert.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -S -infer-address-spaces %s | FileCheck %s
+
+define amdgpu_kernel void @subclass_data_assert() {
+; CHECK-LABEL: @subclass_data_assert(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    unreachable
+; CHECK:       strlen.while11:
+; CHECK-NEXT:    [[I:%.*]] = getelementptr i8, i8* [[I]], i64 1
+; CHECK-NEXT:    [[I1:%.*]] = load i8, i8* [[I]], align 1
+; CHECK-NEXT:    [[I2:%.*]] = icmp eq i8 [[I1]], 0
+; CHECK-NEXT:    br i1 [[I2]], label [[STRLEN_WHILE_DONE12:%.*]], label [[STRLEN_WHILE11:%.*]]
+; CHECK:       strlen.while.done12:
+; CHECK-NEXT:    ret void
+;
+entry:
+  unreachable
+
+strlen.while11:                                   ; preds = %strlen.while11
+  %i = getelementptr i8, i8* %i, i64 1
+  %i1 = load i8, i8* %i, align 1
+  %i2 = icmp eq i8 %i1, 0
+  br i1 %i2, label %strlen.while.done12, label %strlen.while11
+
+strlen.while.done12:                              ; preds = %strlen.while11
+  ret void
+}
Index: llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -997,6 +997,12 @@
   SmallVector<const Use *, 32> UndefUsesToFix;
   for (Value* V : Postorder) {
     unsigned NewAddrSpace = InferredAddrSpace.lookup(V);
+
+    // In some degenerate cases (e.g. invalid IR in unreachable code), we may
+    // not even infer the value to have its original address space.
+    if (NewAddrSpace == UninitializedAddressSpace)
+      continue;
+
     if (V->getType()->getPointerAddressSpace() != NewAddrSpace) {
       Value *New = cloneValueWithNewAddressSpace(
           V, NewAddrSpace, ValueWithNewAddrSpace, &UndefUsesToFix);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87709.291978.patch
Type: text/x-patch
Size: 2107 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200915/0597f663/attachment.bin>


More information about the llvm-commits mailing list