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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

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