[PATCH] D125766: [llvm-reduce] improve bb removal

Markus Lavin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 17 05:13:16 PDT 2022


markus created this revision.
markus added reviewers: fhahn, aeubanks.
Herald added a project: All.
markus requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently the `llvm-reduce` `basic-blocks` pass has the habit of replacing branches with `ret` instructions if the sole branch target of a block has been removed. Replacing a `br` with a `ret` results in a radically different CFG that might very well fail to reproduce what we are currently trying to reduce, especially if it is loop dependent. As a result going down these paths is seldom fruitful and we are left with block sequences like the following in the final reduction

  cont5830:                                         ; preds = %for.body5828
    br label %for.cond5831
  
  for.cond5831:                                     ; preds = %cont5830
    br label %cont5833
  
  cont5833:                                         ; preds = %for.cond5831
    br label %for.end6075
  
  for.end6075:                                      ; preds = %cont5833
    br label %cont6078
  
  cont6078:                                         ; preds = %for.end6075
    br label %cont6080
  
  cont6080:                                         ; preds = %cont6078
    br label %for.cond5821

This patch instead tries to update the branch to target the next block in sequence that is not to be removed thus increasing the likelihood of still maintaining a loop (while getting rid of some useless sequential flow). Probably we should go further than this and use loop analysis to make sure not to break loop structures but until then this small change seems like a step in the right direction.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125766

Files:
  llvm/test/tools/llvm-reduce/remove-bbs-sequence.ll
  llvm/test/tools/llvm-reduce/remove-bbs-sequence.py
  llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp


Index: llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
===================================================================
--- llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
@@ -47,6 +47,19 @@
   Term->eraseFromParent();
 
   if (ChunkSuccessors.empty()) {
+    // Scan forward in BB list to try find a block that is kept.
+    Function &F = *BB.getParent();
+    Function::iterator FI = BB.getIterator();
+    FI++;
+    while (FI != F.end()) {
+      auto &FIB = *FI;
+      if (BBsToKeep.count(&FIB)) {
+        BranchInst::Create(&FIB, &BB);
+        return;
+      }
+      FI++;
+    }
+    // If that fails then resort to replacing with a ret.
     auto *FnRetTy = BB.getParent()->getReturnType();
     ReturnInst::Create(BB.getContext(),
                        FnRetTy->isVoidTy() ? nullptr : UndefValue::get(FnRetTy),
Index: llvm/test/tools/llvm-reduce/remove-bbs-sequence.py
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-reduce/remove-bbs-sequence.py
@@ -0,0 +1,15 @@
+import subprocess
+import sys
+
+opt = subprocess.run( [ 'opt', '-passes=print<loops>','-disable-output', sys.argv[1]], stdout=subprocess.PIPE, stderr=subprocess.PIPE )
+
+stdout = opt.stdout.decode()
+
+pattern = 'Loop at depth 1 containing'
+
+if (pattern in opt.stderr.decode()):
+  print('This is interesting!')
+  sys.exit(0)
+else:
+  print('This is NOT interesting!')
+  sys.exit(1)
Index: llvm/test/tools/llvm-reduce/remove-bbs-sequence.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-reduce/remove-bbs-sequence.ll
@@ -0,0 +1,27 @@
+; RUN: llvm-reduce --delta-passes=basic-blocks --test %python --test-arg %p/remove-bbs-sequence.py %s -o %t
+; RUN: cat %t | FileCheck %s
+
+; The interestingness test is that the CFG contains a loop. Verify that the
+; unnecessary %bb2 is removed while still maintaining a loop.
+
+define void @main() {
+  bb0:
+  br label %bb1
+  bb1:
+  br label %bb2
+  bb2:
+  br label %bb3
+  bb3:
+  br label %bb1
+}
+
+; CHECK:define void @main() {
+; CHECK-NEXT: bb0:
+; CHECK-NEXT:   br label %bb1
+; CHECK-EMPTY:
+; CHECK-NEXT: bb1:
+; CHECK-NEXT:   br label %bb3
+; CHECK-EMPTY:
+; CHECK-NEXT: bb3:
+; CHECK-NEXT:   br label %bb1
+; CHECK-NEXT:}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125766.430000.patch
Type: text/x-patch
Size: 2351 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220517/21847812/attachment.bin>


More information about the llvm-commits mailing list