[PATCH] D109735: SeparateConstOffsetFromGEP: Fix stack overflow in unreachable code

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 13 19:44:30 PDT 2021


arsenm created this revision.
arsenm added reviewers: jingyue, lebedev.ri, jdoerfert, aqjune, jroelofs, tra.
Herald added a subscriber: hiraditya.
arsenm requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.

ConstantOffsetExtractor::Find was infinitely recursing on the add
referencing itself.


https://reviews.llvm.org/D109735

Files:
  llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
  llvm/test/Transforms/SeparateConstOffsetFromGEP/crash-in-unreachable-code.ll


Index: llvm/test/Transforms/SeparateConstOffsetFromGEP/crash-in-unreachable-code.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SeparateConstOffsetFromGEP/crash-in-unreachable-code.ll
@@ -0,0 +1,14 @@
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -separate-const-offset-from-gep %s
+
+ at gv = external local_unnamed_addr addrspace(3) global [16 x i8], align 16
+
+; The add referencing itself is illegal, except it's in an unreachable block.
+define weak amdgpu_kernel void @foo() {
+entry:
+  ret void
+
+for.body28.i:                                     ; preds = %for.body28.i
+  %arrayidx3389.i = getelementptr inbounds [16 x i8], [16 x i8] addrspace(3)* @gv, i32 0, i32 %inc38.7.i.1
+  %inc38.7.i.1 = add nuw nsw i32 %inc38.7.i.1, 16
+  br label %for.body28.i
+}
Index: llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1164,6 +1164,9 @@
   DL = &F.getParent()->getDataLayout();
   bool Changed = false;
   for (BasicBlock &B : F) {
+    if (!DT->isReachableFromEntry(&B))
+      continue;
+
     for (BasicBlock::iterator I = B.begin(), IE = B.end(); I != IE;)
       if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I++))
         Changed |= splitGEP(GEP);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109735.372389.patch
Type: text/x-patch
Size: 1430 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210914/50e33c39/attachment.bin>


More information about the llvm-commits mailing list