[PATCH] D134279: [MustExec][LICM] Handle latch being part of an irreducible cycle (PR57780)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 20 05:57:34 PDT 2022


nikic created this revision.
nikic added reviewers: fhahn, mkazantsev, jdoerfert, reames.
Herald added subscribers: asbirlea, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The algorithm in allLoopPathsLeadToBlock() does not handle the case where the loop latch is part of the predecessor set correctly: In this case, we may take the backedge and not execute other latch successors. In practice, this only happens if the latch is part of an irreducible cycle.

TBH I am not fully confident in this fix, I have the feeling that I'm working around a problem rather than addressing the root cause.

Fixes https://github.com/llvm/llvm-project/issues/57780.


https://reviews.llvm.org/D134279

Files:
  llvm/lib/Analysis/MustExecute.cpp
  llvm/test/Transforms/LICM/pr57780.ll


Index: llvm/test/Transforms/LICM/pr57780.ll
===================================================================
--- llvm/test/Transforms/LICM/pr57780.ll
+++ llvm/test/Transforms/LICM/pr57780.ll
@@ -7,7 +7,6 @@
 define void @test() {
 ; CHECK-LABEL: @test(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    store i16 5, ptr @c, align 2
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
 ; CHECK-NEXT:    [[V:%.*]] = phi i32 [ 10, [[ENTRY:%.*]] ], [ 0, [[LOOP_LATCH:%.*]] ]
@@ -16,6 +15,7 @@
 ; CHECK:       loop.cont:
 ; CHECK-NEXT:    br i1 false, label [[LOOP_IRREDUCIBLE:%.*]], label [[LOOP_LATCH]]
 ; CHECK:       loop.irreducible:
+; CHECK-NEXT:    store i16 5, ptr @c, align 2
 ; CHECK-NEXT:    br label [[LOOP_LATCH]]
 ; CHECK:       loop.latch:
 ; CHECK-NEXT:    br i1 false, label [[LOOP_IRREDUCIBLE]], label [[LOOP]]
Index: llvm/lib/Analysis/MustExecute.cpp
===================================================================
--- llvm/lib/Analysis/MustExecute.cpp
+++ llvm/lib/Analysis/MustExecute.cpp
@@ -201,6 +201,14 @@
   SmallPtrSet<const BasicBlock *, 4> Predecessors;
   collectTransitivePredecessors(CurLoop, BB, Predecessors);
 
+  // Bail out if the latch block is part of the predecessor set. In this case
+  // we may take the backedge to the header and not execute other latch
+  // successors. For loops in loop simplify form, this only happens if the
+  // latch is part of an irreducible cycle.
+  for (const BasicBlock *Pred : predecessors(CurLoop->getHeader()))
+    if (Predecessors.contains(Pred))
+      return false;
+
   // Make sure that all successors of, all predecessors of BB which are not
   // dominated by BB, are either:
   // 1) BB,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134279.461548.patch
Type: text/x-patch
Size: 1678 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220920/3fc5fc48/attachment.bin>


More information about the llvm-commits mailing list