1 //===- mlir-reduce.cpp - The MLIR reducer ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the general framework of the MLIR reducer tool. It
10 // parses the command line arguments, parses the initial MLIR test case and sets
11 // up the testing environment. It outputs the most reduced test case variant
12 // after executing the reduction passes.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include <vector>
17
18 #include "mlir/InitAllDialects.h"
19 #include "mlir/Parser.h"
20 #include "mlir/Pass/Pass.h"
21 #include "mlir/Pass/PassManager.h"
22 #include "mlir/Reducer/OptReductionPass.h"
23 #include "mlir/Reducer/Passes/OpReducer.h"
24 #include "mlir/Reducer/ReductionNode.h"
25 #include "mlir/Reducer/ReductionTreePass.h"
26 #include "mlir/Reducer/Tester.h"
27 #include "mlir/Support/FileUtilities.h"
28 #include "mlir/Support/LogicalResult.h"
29 #include "mlir/Transforms/Passes.h"
30 #include "llvm/Support/InitLLVM.h"
31 #include "llvm/Support/ToolOutputFile.h"
32
33 using namespace mlir;
34
35 namespace mlir {
36 namespace test {
37 void registerTestDialect(DialectRegistry &);
38 } // namespace test
39 } // namespace mlir
40
41 static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
42 llvm::cl::Required,
43 llvm::cl::desc("<input file>"));
44
45 static llvm::cl::opt<std::string>
46 testFilename("test", llvm::cl::Required, llvm::cl::desc("Testing script"));
47
48 static llvm::cl::list<std::string>
49 testArguments("test-args", llvm::cl::ZeroOrMore,
50 llvm::cl::desc("Testing script arguments"));
51
52 static llvm::cl::opt<std::string>
53 outputFilename("o",
54 llvm::cl::desc("Output filename for the reduced test case"),
55 llvm::cl::init("-"));
56
57 // TODO: Use PassPipelineCLParser to define pass pieplines in the command line.
58 static llvm::cl::opt<std::string>
59 passTestSpecifier("pass-test",
60 llvm::cl::desc("Indicate a specific pass to be tested"));
61
62 // Parse and verify the input MLIR file.
loadModule(MLIRContext & context,OwningModuleRef & module,StringRef inputFilename)63 static LogicalResult loadModule(MLIRContext &context, OwningModuleRef &module,
64 StringRef inputFilename) {
65 module = parseSourceFile(inputFilename, &context);
66 if (!module)
67 return failure();
68
69 return success();
70 }
71
main(int argc,char ** argv)72 int main(int argc, char **argv) {
73
74 llvm::InitLLVM y(argc, argv);
75
76 registerMLIRContextCLOptions();
77 registerPassManagerCLOptions();
78
79 llvm::cl::ParseCommandLineOptions(argc, argv,
80 "MLIR test case reduction tool.\n");
81
82 std::string errorMessage;
83
84 auto testscript = openInputFile(testFilename, &errorMessage);
85 if (!testscript)
86 llvm::report_fatal_error(errorMessage);
87
88 auto output = openOutputFile(outputFilename, &errorMessage);
89 if (!output)
90 llvm::report_fatal_error(errorMessage);
91
92 mlir::MLIRContext context;
93 registerAllDialects(context.getDialectRegistry());
94 #ifdef MLIR_INCLUDE_TESTS
95 mlir::test::registerTestDialect(context.getDialectRegistry());
96 #endif
97
98 mlir::OwningModuleRef moduleRef;
99 if (failed(loadModule(context, moduleRef, inputFilename)))
100 llvm::report_fatal_error("Input test case can't be parsed");
101
102 // Initialize test environment.
103 const Tester test(testFilename, testArguments);
104
105 if (!test.isInteresting(inputFilename))
106 llvm::report_fatal_error(
107 "Input test case does not exhibit interesting behavior");
108
109 // Reduction pass pipeline.
110 PassManager pm(&context);
111
112 if (passTestSpecifier == "DCE") {
113
114 // Opt Reduction Pass with SymbolDCEPass as opt pass.
115 pm.addPass(std::make_unique<OptReductionPass>(test, &context,
116 createSymbolDCEPass()));
117
118 } else if (passTestSpecifier == "function-reducer") {
119
120 // Reduction tree pass with OpReducer variant generation and single path
121 // traversal.
122 pm.addPass(
123 std::make_unique<ReductionTreePass<OpReducer<FuncOp>, SinglePath>>(
124 test));
125 }
126
127 ModuleOp m = moduleRef.get().clone();
128
129 if (failed(pm.run(m)))
130 llvm::report_fatal_error("Error running the reduction pass pipeline");
131
132 m.print(output->os());
133 output->keep();
134
135 return 0;
136 }
137