[PATCH] D116871: [llvm-reduce] optionally keep running delta passes until they stop making the IR smaller

John Regehr via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 8 14:21:42 PST 2022


regehr created this revision.
regehr added reviewers: aeubanks, reames, Meinersbur.
regehr requested review of this revision.
Herald added a project: LLVM.

Currently, llvm-reduce runs each of its delta passes once, in a fixed order, and then exits. This is a reasonable default behavior since it allows llvm-reduce to run fairly quickly, but it fails to account for the case where a pass later in the phase ordering creates opportunities for a pass earlier in the phase ordering to make more progress.

This patch does not change the default behavior of llvm-reduce, but adds a command line parameter "max-pass-iterations" that specifies the largest number of times that the requested set of delta passes will be run. It also adds logic to bail out of the top-level reduction loop as soon as running the entire collection of passes fails to make the IR module smaller.

I have a small collection of test inputs to llvm-reduce, the average size before reduction is 15 KB. After reduction by the current llvm-reduce, the average IR file is 388 bytes. After reduction using the llvm-reduce with this proposed patch (and max-pass-iterations=10, but it always bails out earlier than that) the average reduced IR file is 250 bytes. This seems like a pretty decent win for a very simple patch, and furthermore since the default value for max-pass-iterations is 1, llvm-reduce will not be slowed down for anyone who doesn't use this new option.

I was not able to convince myself that a test for this patch would be worthwhile, instead of being fragile and silly, given the utter simplicity of the patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116871

Files:
  llvm/tools/llvm-reduce/DeltaManager.cpp
  llvm/tools/llvm-reduce/DeltaManager.h
  llvm/tools/llvm-reduce/llvm-reduce.cpp


Index: llvm/tools/llvm-reduce/llvm-reduce.cpp
===================================================================
--- llvm/tools/llvm-reduce/llvm-reduce.cpp
+++ llvm/tools/llvm-reduce/llvm-reduce.cpp
@@ -88,6 +88,12 @@
                                          cl::desc("Set the target triple"),
                                          cl::cat(Options));
 
+static cl::opt<int>
+    MaxPassIterations("max-pass-iterations",
+                      cl::desc("Maximum number of times to run the full set "
+                               "of delta passes (default=1)"),
+                      cl::init(1), cl::cat(Options));
+
 static codegen::RegisterCodeGenFlags CGF;
 
 void writeOutput(ReducerWorkItem &M, StringRef Message) {
@@ -161,7 +167,7 @@
   TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram));
 
   // Try to reduce code
-  runDeltaPasses(Tester);
+  runDeltaPasses(Tester, MaxPassIterations);
 
   // Print reduced file to STDOUT
   if (OutputFilename == "-")
Index: llvm/tools/llvm-reduce/DeltaManager.h
===================================================================
--- llvm/tools/llvm-reduce/DeltaManager.h
+++ llvm/tools/llvm-reduce/DeltaManager.h
@@ -19,7 +19,7 @@
 class TestRunner;
 
 void printDeltaPasses(raw_ostream &OS);
-void runDeltaPasses(TestRunner &Tester);
+void runDeltaPasses(TestRunner &Tester, int MaxPassIterations);
 } // namespace llvm
 
 #endif
Index: llvm/tools/llvm-reduce/DeltaManager.cpp
===================================================================
--- llvm/tools/llvm-reduce/DeltaManager.cpp
+++ llvm/tools/llvm-reduce/DeltaManager.cpp
@@ -103,15 +103,29 @@
 #undef DELTA_PASS
 }
 
-void llvm::runDeltaPasses(TestRunner &Tester) {
-  if (DeltaPasses.empty()) {
-    runAllDeltaPasses(Tester);
-  } else {
-    StringRef Passes = DeltaPasses;
-    while (!Passes.empty()) {
-      auto Split = Passes.split(",");
-      runDeltaPassName(Tester, Split.first);
-      Passes = Split.second;
+static int getIRSize(TestRunner &Tester) {
+  std::string Str;
+  raw_string_ostream SS(Str);
+  Tester.getProgram().print(SS, /*AnnotationWriter=*/nullptr);
+  return SS.str().length();
+}
+
+void llvm::runDeltaPasses(TestRunner &Tester, int MaxPassIterations) {
+  int OldSize = getIRSize(Tester);
+  for (int Iter = 0; Iter < MaxPassIterations; ++Iter) {
+    if (DeltaPasses.empty()) {
+      runAllDeltaPasses(Tester);
+    } else {
+      StringRef Passes = DeltaPasses;
+      while (!Passes.empty()) {
+        auto Split = Passes.split(",");
+        runDeltaPassName(Tester, Split.first);
+        Passes = Split.second;
+      }
     }
+    int NewSize = getIRSize(Tester);
+    if (NewSize >= OldSize)
+      break;
+    OldSize = NewSize;
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116871.398378.patch
Type: text/x-patch
Size: 2727 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220108/c5ab3d61/attachment-0001.bin>


More information about the llvm-commits mailing list