MLIR  20.0.0git
MlirReduceMain.cpp
Go to the documentation of this file.
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 
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"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/ToolOutputFile.h"
28 
29 using namespace mlir;
30 
31 // Parse and verify the input MLIR file. Returns null on error.
33  StringRef inputFilename,
34  bool insertImplictModule) {
35  // Set up the input file.
36  std::string errorMessage;
37  auto file = openInputFile(inputFilename, &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(std::move(file), SMLoc());
45  return parseSourceFileForTool(sourceMgr, &context, insertImplictModule);
46 }
47 
48 LogicalResult 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("-"), 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(false)};
70 
71  llvm::cl::HideUnrelatedOptions(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  "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);
89  if (!output)
90  return failure();
91 
93  loadModule(context, inputFilename, !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(parser.addToPipeline(pm, errorHandler)))
104  return failure();
105 
106  OwningOpRef<Operation *> op = opRef.get()->clone();
107 
108  if (failed(pm.run(op.get())))
109  return failure();
110 
111  op.get()->print(output->os());
112  output->keep();
113 
114  return success();
115 }
OwningOpRef< Operation * > loadModule(MLIRContext &context, StringRef inputFilename, bool insertImplictModule)
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
Definition: OwningOpRef.h:29
OpTy get() const
Allow accessing the internal op.
Definition: OwningOpRef.h:51
The main pass manager and pipeline builder.
Definition: PassManager.h:231
LogicalResult run(Operation *op)
Run the passes within this manager on the provided operation.
Definition: Pass.cpp:847
This class implements a command-line parser for MLIR passes.
Definition: PassRegistry.h:247
LogicalResult addToPipeline(OpPassManager &pm, function_ref< LogicalResult(const Twine &)> errorHandler) const
Adds the passes defined by this parser entry to the given pass manager.
Include the generated interface declarations.
std::unique_ptr< llvm::MemoryBuffer > openInputFile(llvm::StringRef inputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for reading.
std::unique_ptr< llvm::ToolOutputFile > openOutputFile(llvm::StringRef outputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for writing.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
OwningOpRef< Operation * > parseSourceFileForTool(const std::shared_ptr< llvm::SourceMgr > &sourceMgr, const ParserConfig &config, bool insertImplicitModule)
This parses the file specified by the indicated SourceMgr.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult mlirReduceMain(int argc, char **argv, MLIRContext &context)