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