[llvm-reduce] Add delta pass to run IR passes
authorArthur Eubanks <aeubanks@google.com>
Thu, 14 Apr 2022 00:36:58 +0000 (17:36 -0700)
committerArthur Eubanks <aeubanks@google.com>
Fri, 12 Aug 2022 17:38:19 +0000 (10:38 -0700)
The exact IR passes run is customizable via `-ir-passes`.

Reviewed By: regehr

Differential Revision: https://reviews.llvm.org/D123749

llvm/test/tools/llvm-reduce/run-ir-passes.ll [new file with mode: 0644]
llvm/tools/llvm-reduce/CMakeLists.txt
llvm/tools/llvm-reduce/DeltaManager.cpp
llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp [new file with mode: 0644]
llvm/tools/llvm-reduce/deltas/RunIRPasses.h [new file with mode: 0644]
llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn

diff --git a/llvm/test/tools/llvm-reduce/run-ir-passes.ll b/llvm/test/tools/llvm-reduce/run-ir-passes.ll
new file mode 100644 (file)
index 0000000..d3dceec
--- /dev/null
@@ -0,0 +1,28 @@
+; RUN: llvm-reduce --delta-passes=ir-passes --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=CHECK-FINAL --input-file=%t %s
+
+; CHECK-INTERESTINGNESS-LABEL: @f1
+; CHECK-INTERESTINGNESS: add
+; CHECK-INTERESTINGNESS-LABEL: @f2
+; CHECK-INTERESTINGNESS: add
+; CHECK-INTERESTINGNESS: add
+
+; CHECK-FINAL-LABEL: @f1
+; CHECK-FINAL: add i32 %a, 10
+; CHECK-FINAL-LABEL: @f2
+; CHECK-FINAL: add i32 %a, 5
+; CHECK-FINAL: add i32 %b, 5
+
+define i32 @f1(i32 %a) {
+  %b = add i32 %a, 5
+  %c = add i32 %b, 5
+  ret i32 %c
+}
+
+define i32 @f2(i32 %a) {
+  %b = add i32 %a, 5
+  %c = add i32 %b, 5
+  ret i32 %c
+}
+
+declare void @f3()
\ No newline at end of file
index fe6619d..969da36 100644 (file)
@@ -11,6 +11,7 @@ set(LLVM_LINK_COMPONENTS
   IRReader
   MC
   MIRParser
+  Passes
   Support
   Target
   TransformUtils
@@ -48,6 +49,7 @@ add_llvm_tool(llvm-reduce
   deltas/ReduceRegisterMasks.cpp
   deltas/ReduceRegisterDefs.cpp
   deltas/ReduceRegisterUses.cpp
+  deltas/RunIRPasses.cpp
   deltas/SimplifyInstructions.cpp
   llvm-reduce.cpp
 
index 5601ee9..5fa0376 100644 (file)
@@ -40,6 +40,7 @@
 #include "deltas/ReduceRegisterUses.h"
 #include "deltas/ReduceSpecialGlobals.h"
 #include "deltas/ReduceVirtualRegisters.h"
+#include "deltas/RunIRPasses.h"
 #include "deltas/SimplifyInstructions.h"
 #include "llvm/Support/CommandLine.h"
 
@@ -67,6 +68,7 @@ static cl::opt<std::string>
     DELTA_PASS("arguments", reduceArgumentsDeltaPass)                          \
     DELTA_PASS("instructions", reduceInstructionsDeltaPass)                    \
     DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass)         \
+    DELTA_PASS("ir-passes", runIRPassesDeltaPass)                              \
     DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass)                   \
     DELTA_PASS("operands-one", reduceOperandsOneDeltaPass)                     \
     DELTA_PASS("operands-nan", reduceOperandsNaNDeltaPass)                     \
diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp
new file mode 100644 (file)
index 0000000..ba93ba7
--- /dev/null
@@ -0,0 +1,53 @@
+//===- RunIRPasses.cpp ----------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RunIRPasses.h"
+#include "Delta.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace llvm;
+
+static cl::opt<std::string> PassPipeline(
+    "ir-passes",
+    cl::desc("A textual description of the pass pipeline, same as "
+             "what's passed to `opt -passes`."),
+    cl::init(
+        "function(sroa,instcombine,gvn,simplifycfg,infer-address-spaces)"));
+
+static void runPasses(Oracle &O, Module &Program) {
+  LoopAnalysisManager LAM;
+  FunctionAnalysisManager FAM;
+  CGSCCAnalysisManager CGAM;
+  ModuleAnalysisManager MAM;
+
+  PassInstrumentationCallbacks PIC;
+  PIC.registerShouldRunOptionalPassCallback(
+      [&](StringRef, Any) { return !O.shouldKeep(); });
+  PassBuilder PB(nullptr, PipelineTuningOptions(), None, &PIC);
+
+  PB.registerModuleAnalyses(MAM);
+  PB.registerCGSCCAnalyses(CGAM);
+  PB.registerFunctionAnalyses(FAM);
+  PB.registerLoopAnalyses(LAM);
+  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
+
+  ModulePassManager MPM;
+  if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
+    errs() << toString(std::move(Err)) << "\n";
+    report_fatal_error("Error constructing pass pipeline");
+  }
+  MPM.run(Program, MAM);
+}
+
+void llvm::runIRPassesDeltaPass(TestRunner &Test) {
+  errs() << "*** Running passes ...\n";
+  runDeltaPass(Test, runPasses);
+  errs() << "----------------------------\n";
+}
diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.h b/llvm/tools/llvm-reduce/deltas/RunIRPasses.h
new file mode 100644 (file)
index 0000000..603363b
--- /dev/null
@@ -0,0 +1,18 @@
+//===- RunIRPasses.h ------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_RUNPASSES_H
+#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_RUNPASSES_H
+
+#include "Delta.h"
+
+namespace llvm {
+void runIRPassesDeltaPass(TestRunner &Test);
+} // namespace llvm
+
+#endif
index 40219ea..d0ee9a0 100644 (file)
@@ -3,6 +3,7 @@ executable("llvm-reduce") {
     "//llvm/lib/CodeGen/MIRParser",
     "//llvm/lib/IR",
     "//llvm/lib/IRReader",
+    "//llvm/lib/Passes",
     "//llvm/lib/Support",
     "//llvm/lib/Target",
     "//llvm/lib/Target:TargetsToBuild",
@@ -40,6 +41,7 @@ executable("llvm-reduce") {
     "deltas/ReduceRegisterUses.cpp",
     "deltas/ReduceSpecialGlobals.cpp",
     "deltas/ReduceVirtualRegisters.cpp",
+    "deltas/RunIRPasses.cpp",
     "deltas/SimplifyInstructions.cpp",
     "deltas/Utils.cpp",
     "llvm-reduce.cpp",