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 "mlir/Tools/mlir-reduce/MlirReduceMain.h"
17 #include "mlir/IR/PatternMatch.h"
18 #include "mlir/Parser/Parser.h"
19 #include "mlir/Pass/Pass.h"
20 #include "mlir/Pass/PassManager.h"
21 #include "mlir/Reducer/Passes.h"
22 #include "mlir/Rewrite/FrozenRewritePatternSet.h"
23 #include "mlir/Support/FileUtilities.h"
24 #include "mlir/Support/LogicalResult.h"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/ToolOutputFile.h"
27
28 using namespace mlir;
29
30 // Parse and verify the input MLIR file.
loadModule(MLIRContext & context,OwningOpRef<ModuleOp> & module,StringRef inputFilename)31 static LogicalResult loadModule(MLIRContext &context,
32 OwningOpRef<ModuleOp> &module,
33 StringRef inputFilename) {
34 module = parseSourceFile<ModuleOp>(inputFilename, &context);
35 if (!module)
36 return failure();
37
38 return success();
39 }
40
mlirReduceMain(int argc,char ** argv,MLIRContext & context)41 LogicalResult mlir::mlirReduceMain(int argc, char **argv,
42 MLIRContext &context) {
43 // Override the default '-h' and use the default PrintHelpMessage() which
44 // won't print options in categories.
45 static llvm::cl::opt<bool> help("h", llvm::cl::desc("Alias for -help"),
46 llvm::cl::Hidden);
47
48 static llvm::cl::OptionCategory mlirReduceCategory("mlir-reduce options");
49
50 static llvm::cl::opt<std::string> inputFilename(
51 llvm::cl::Positional, llvm::cl::desc("<input file>"),
52 llvm::cl::cat(mlirReduceCategory));
53
54 static llvm::cl::opt<std::string> outputFilename(
55 "o", llvm::cl::desc("Output filename for the reduced test case"),
56 llvm::cl::init("-"), llvm::cl::cat(mlirReduceCategory));
57
58 llvm::cl::HideUnrelatedOptions(mlirReduceCategory);
59
60 llvm::InitLLVM y(argc, argv);
61
62 registerReducerPasses();
63
64 PassPipelineCLParser parser("", "Reduction Passes to Run");
65 llvm::cl::ParseCommandLineOptions(argc, argv,
66 "MLIR test case reduction tool.\n");
67
68 if (help) {
69 llvm::cl::PrintHelpMessage();
70 return success();
71 }
72
73 std::string errorMessage;
74
75 auto output = openOutputFile(outputFilename, &errorMessage);
76 if (!output)
77 return failure();
78
79 OwningOpRef<ModuleOp> moduleRef;
80 if (failed(loadModule(context, moduleRef, inputFilename)))
81 return failure();
82
83 auto errorHandler = [&](const Twine &msg) {
84 return emitError(UnknownLoc::get(&context)) << msg;
85 };
86
87 // Reduction pass pipeline.
88 PassManager pm(&context);
89 if (failed(parser.addToPipeline(pm, errorHandler)))
90 return failure();
91
92 OwningOpRef<ModuleOp> m = moduleRef.get().clone();
93
94 if (failed(pm.run(m.get())))
95 return failure();
96
97 m->print(output->os());
98 output->keep();
99
100 return success();
101 }
102