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 "mlir/Tools/ParseUtilities.h"
26#include "llvm/Support/InitLLVM.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/ToolOutputFile.h"
29
30using namespace mlir;
31
32// Parse and verify the input MLIR file. Returns null on error.
33OwningOpRef<Operation *> loadModule(MLIRContext &context,
34 StringRef inputFilename,
35 bool insertImplictModule) {
36 // Set up the input file.
37 std::string errorMessage;
38 auto file = openInputFile(inputFilename, errorMessage: &errorMessage);
39 if (!file) {
40 llvm::errs() << errorMessage << "\n";
41 return nullptr;
42 }
43
44 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
45 sourceMgr->AddNewSourceBuffer(F: std::move(file), IncludeLoc: SMLoc());
46 return parseSourceFileForTool(sourceMgr, config: &context, insertImplicitModule: insertImplictModule);
47}
48
49LogicalResult mlir::mlirReduceMain(int argc, char **argv,
50 MLIRContext &context) {
51 // Override the default '-h' and use the default PrintHelpMessage() which
52 // won't print options in categories.
53 static llvm::cl::opt<bool> help("h", llvm::cl::desc("Alias for -help"),
54 llvm::cl::Hidden);
55
56 static llvm::cl::OptionCategory mlirReduceCategory("mlir-reduce options");
57
58 static llvm::cl::opt<std::string> inputFilename(
59 llvm::cl::Positional, llvm::cl::desc("<input file>"),
60 llvm::cl::cat(mlirReduceCategory));
61
62 static llvm::cl::opt<std::string> outputFilename(
63 "o", llvm::cl::desc("Output filename for the reduced test case"),
64 llvm::cl::init(Val: "-"), llvm::cl::cat(mlirReduceCategory));
65
66 static llvm::cl::opt<bool> noImplicitModule{
67 "no-implicit-module",
68 llvm::cl::desc(
69 "Disable implicit addition of a top-level module op during parsing"),
70 llvm::cl::init(Val: false)};
71
72 llvm::cl::HideUnrelatedOptions(Category&: mlirReduceCategory);
73
74 llvm::InitLLVM y(argc, argv);
75
76 registerReducerPasses();
77
78 PassPipelineCLParser parser("", "Reduction Passes to Run");
79 llvm::cl::ParseCommandLineOptions(argc, argv,
80 Overview: "MLIR test case reduction tool.\n");
81
82 if (help) {
83 llvm::cl::PrintHelpMessage();
84 return success();
85 }
86
87 std::string errorMessage;
88
89 auto output = openOutputFile(outputFilename, errorMessage: &errorMessage);
90 if (!output)
91 return failure();
92
93 OwningOpRef<Operation *> opRef =
94 loadModule(context, inputFilename, insertImplictModule: !noImplicitModule);
95 if (!opRef)
96 return failure();
97
98 auto errorHandler = [&](const Twine &msg) {
99 return emitError(UnknownLoc::get(&context)) << msg;
100 };
101
102 // Reduction pass pipeline.
103 PassManager pm(&context, opRef.get()->getName().getStringRef());
104 if (failed(result: parser.addToPipeline(pm, errorHandler)))
105 return failure();
106
107 OwningOpRef<Operation *> op = opRef.get()->clone();
108
109 if (failed(result: pm.run(op: op.get())))
110 return failure();
111
112 op.get()->print(os&: output->os());
113 output->keep();
114
115 return success();
116}
117

source code of mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp