[llvm] 08d1c43 - llvm-reduce: Add conditional reduction passes

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 23 15:44:38 PDT 2022


Author: Matt Arsenault
Date: 2022-10-23T15:39:39-07:00
New Revision: 08d1c43c7023a2e955c43fbf4c3f1635f91521e0

URL: https://github.com/llvm/llvm-project/commit/08d1c43c7023a2e955c43fbf4c3f1635f91521e0
DIFF: https://github.com/llvm/llvm-project/commit/08d1c43c7023a2e955c43fbf4c3f1635f91521e0.diff

LOG: llvm-reduce: Add conditional reduction passes

Copy this technique from bugpoint. Before trying to blindly
delete blocks, try to fold branch conditions. This intuitively
makes more sense for a faster reduction, since you can find
dead paths in the function to prune out before trying to bisect
blocks in source order.

Seems to provide some speedup on my multi-hour reduction samples.

This does have the potential to produce testcases with unreachable
blocks. This is already a problem with the existing block
reduction pass. I'm struggling dealing with invalid reductions
in these cases, so in the future this should probably start
deleting those. However, I do sometimes try to reduce failures
in code that becomes unreachable, so I'm not totally sure
what to do here.

Added: 
    llvm/test/tools/llvm-reduce/reduce-conditionals.ll

Modified: 
    llvm/tools/llvm-reduce/DeltaManager.cpp
    llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp
    llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.h

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-reduce/reduce-conditionals.ll b/llvm/test/tools/llvm-reduce/reduce-conditionals.ll
new file mode 100644
index 000000000000..a832673d7350
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-conditionals.ll
@@ -0,0 +1,39 @@
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=simplify-conditionals-true --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=RESULT-TRUE %s < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=simplify-conditionals-false --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=RESULT-FALSE %s < %t
+
+; CHECK-INTERESTINGNESS-LABEL: @func(
+; CHECK-INTERESTINGNESS: store i32 1,
+
+; RESULT-TRUE: bb0:
+; RESULT-TRUE: store i32 0, ptr null, align 4
+; RESULT-TRUE-NEXT: store i32 1, ptr null, align 4
+; RESULT-TRUE-NEXT: br label %bb2
+; RESULT-TRUE-NOT: bb1
+
+
+; RESULT-FALSE: bb0:
+; RESULT-FALSE: store i32 0, ptr null, align 4
+; RESULT-FALSE-NEXT: br label %bb2
+
+; RESULT-FALSE: bb1: ; No predecessors!
+; RESULT-FALSE-NEXT: store i32 1, ptr null, align 4
+; RESULT-FALSE-NEXT: br label %bb3
+define void @func(i1 %cond0, i1 %cond1) {
+bb0:
+  store i32 0, ptr null
+  br i1 %cond0, label %bb1, label %bb2
+
+bb1:
+  store i32 1, ptr null
+  br i1 %cond1, label %bb2, label %bb3
+
+bb2:
+  store i32 2, ptr null
+  br label %bb3
+
+bb3:
+  ret void
+}

diff  --git a/llvm/tools/llvm-reduce/DeltaManager.cpp b/llvm/tools/llvm-reduce/DeltaManager.cpp
index 048a83c5e75d..ee29b44f531e 100644
--- a/llvm/tools/llvm-reduce/DeltaManager.cpp
+++ b/llvm/tools/llvm-reduce/DeltaManager.cpp
@@ -74,6 +74,8 @@ static cl::list<std::string>
     DELTA_PASS("function-bodies", reduceFunctionBodiesDeltaPass)               \
     DELTA_PASS("special-globals", reduceSpecialGlobalsDeltaPass)               \
     DELTA_PASS("aliases", reduceAliasesDeltaPass)                              \
+    DELTA_PASS("simplify-conditionals-true", reduceConditionalsTrueDeltaPass)  \
+    DELTA_PASS("simplify-conditionals-false", reduceConditionalsFalseDeltaPass)\
     DELTA_PASS("basic-blocks", reduceBasicBlocksDeltaPass)                     \
     DELTA_PASS("global-values", reduceGlobalValuesDeltaPass)                   \
     DELTA_PASS("global-objects", reduceGlobalObjectsDeltaPass)                 \

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp b/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp
index a45d57128a93..416b1652eb99 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp
@@ -13,6 +13,8 @@
 
 #include "ReduceUsingSimplifyCFG.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/Transforms/Utils/Local.h"
 
 using namespace llvm;
@@ -31,3 +33,37 @@ static void reduceUsingSimplifyCFG(Oracle &O, Module &Program) {
 void llvm::reduceUsingSimplifyCFGDeltaPass(TestRunner &Test) {
   runDeltaPass(Test, reduceUsingSimplifyCFG, "Reducing using SimplifyCFG");
 }
+static void reduceConditionals(Oracle &O, Module &M, bool Direction) {
+  SmallVector<BasicBlock *, 16> ToSimplify;
+
+  for (auto &F : M) {
+    for (auto &BB : F) {
+      auto *BR = dyn_cast<BranchInst>(BB.getTerminator());
+      if (!BR || !BR->isConditional() || O.shouldKeep())
+        continue;
+
+      if (Direction)
+        BR->setCondition(ConstantInt::getTrue(BR->getContext()));
+      else
+        BR->setCondition(ConstantInt::getFalse(BR->getContext()));
+
+      ToSimplify.push_back(&BB);
+    }
+  }
+
+  TargetTransformInfo TTI(M.getDataLayout());
+  for (auto *BB : ToSimplify)
+    simplifyCFG(BB, TTI);
+}
+
+void llvm::reduceConditionalsTrueDeltaPass(TestRunner &Test) {
+  runDeltaPass(
+      Test, [](Oracle &O, Module &M) { reduceConditionals(O, M, true); },
+      "Reducing conditional branches to true");
+}
+
+void llvm::reduceConditionalsFalseDeltaPass(TestRunner &Test) {
+  runDeltaPass(
+      Test, [](Oracle &O, Module &M) { reduceConditionals(O, M, false); },
+      "Reducing conditional branches to false");
+}

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.h b/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.h
index 56cd15a1a135..01a14602909b 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.h
+++ b/llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.h
@@ -18,6 +18,8 @@
 
 namespace llvm {
 void reduceUsingSimplifyCFGDeltaPass(TestRunner &Test);
+void reduceConditionalsTrueDeltaPass(TestRunner &Test);
+void reduceConditionalsFalseDeltaPass(TestRunner &Test);
 } // namespace llvm
 
 #endif


        


More information about the llvm-commits mailing list