MLIR  19.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"
26 #include "llvm/Support/InitLLVM.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/ToolOutputFile.h"
29 
30 using namespace mlir;
31 
32 // Parse and verify the input MLIR file. Returns null on error.
34  StringRef inputFilename,
35  bool insertImplictModule) {
36  // Set up the input file.
37  std::string errorMessage;
38  auto file = openInputFile(inputFilename, &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(std::move(file), SMLoc());
46  return parseSourceFileForTool(sourceMgr, &context, insertImplictModule);
47 }
48 
49 LogicalResult 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("-"), 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(false)};
71 
72  llvm::cl::HideUnrelatedOptions(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  "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);
90  if (!output)
91  return failure();
92 
94  loadModule(context, inputFilename, !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(parser.addToPipeline(pm, errorHandler)))
105  return failure();
106 
107  OwningOpRef<Operation *> op = opRef.get()->clone();
108 
109  if (failed(pm.run(op.get())))
110  return failure();
111 
112  op.get()->print(output->os());
113  output->keep();
114 
115  return success();
116 }
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
void print(raw_ostream &os, const OpPrintingFlags &flags=std::nullopt)
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:846
This class implements a command-line parser for MLIR passes.
Definition: PassRegistry.h:244
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.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
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.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
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)
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26