1 //===- mlir-reduce.cpp - The MLIR reducer ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
37 void registerTestDialect(DialectRegistry &);
41 static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
43 llvm::cl::desc("<input file>"));
45 static llvm::cl::opt<std::string>
46 testFilename("test", llvm::cl::Required, llvm::cl::desc("Testing script"));
48 static llvm::cl::list<std::string>
49 testArguments("test-args", llvm::cl::ZeroOrMore,
50 llvm::cl::desc("Testing script arguments"));
52 static llvm::cl::opt<std::string>
54 llvm::cl::desc("Output filename for the reduced test case"),
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"));
62 // Parse and verify the input MLIR file.
63 static LogicalResult loadModule(MLIRContext &context, OwningModuleRef &module,
64 StringRef inputFilename) {
65 module = parseSourceFile(inputFilename, &context);
72 int main(int argc, char **argv) {
74 llvm::InitLLVM y(argc, argv);
76 registerMLIRContextCLOptions();
77 registerPassManagerCLOptions();
79 llvm::cl::ParseCommandLineOptions(argc, argv,
80 "MLIR test case reduction tool.\n");
82 std::string errorMessage;
84 auto testscript = openInputFile(testFilename, &errorMessage);
86 llvm::report_fatal_error(errorMessage);
88 auto output = openOutputFile(outputFilename, &errorMessage);
90 llvm::report_fatal_error(errorMessage);
92 mlir::MLIRContext context;
93 registerAllDialects(context.getDialectRegistry());
94 #ifdef MLIR_INCLUDE_TESTS
95 mlir::test::registerTestDialect(context.getDialectRegistry());
98 mlir::OwningModuleRef moduleRef;
99 if (failed(loadModule(context, moduleRef, inputFilename)))
100 llvm::report_fatal_error("Input test case can't be parsed");
102 // Initialize test environment.
103 const Tester test(testFilename, testArguments);
105 if (!test.isInteresting(inputFilename))
106 llvm::report_fatal_error(
107 "Input test case does not exhibit interesting behavior");
109 // Reduction pass pipeline.
110 PassManager pm(&context);
112 if (passTestSpecifier == "DCE") {
114 // Opt Reduction Pass with SymbolDCEPass as opt pass.
115 pm.addPass(std::make_unique<OptReductionPass>(test, &context,
116 createSymbolDCEPass()));
118 } else if (passTestSpecifier == "function-reducer") {
120 // Reduction tree pass with OpReducer variant generation and single path
123 std::make_unique<ReductionTreePass<OpReducer<FuncOp>, SinglePath>>(
127 ModuleOp m = moduleRef.get().clone();
129 if (failed(pm.run(m)))
130 llvm::report_fatal_error("Error running the reduction pass pipeline");
132 m.print(output->os());