MLIR 23.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"
19#include "mlir/Reducer/Passes.h"
23#include "llvm/Support/InitLLVM.h"
24#include "llvm/Support/SourceMgr.h"
25#include "llvm/Support/ToolOutputFile.h"
26
27using namespace mlir;
28
29LogicalResult mlir::mlirReduceMain(int argc, char **argv,
30 MLIRContext &context) {
31 // Override the default '-h' and use the default PrintHelpMessage() which
32 // won't print options in categories.
33 static llvm::cl::opt<bool> help("h", llvm::cl::desc("Alias for -help"),
34 llvm::cl::Hidden);
35
36 static llvm::cl::OptionCategory mlirReduceCategory("mlir-reduce options");
37
38 static llvm::cl::opt<std::string> inputFilename(
39 llvm::cl::Positional, llvm::cl::desc("<input file>"),
40 llvm::cl::cat(mlirReduceCategory));
41
42 static llvm::cl::opt<std::string> outputFilename(
43 "o", llvm::cl::desc("Output filename for the reduced test case"),
44 llvm::cl::init("-"), llvm::cl::cat(mlirReduceCategory));
45
46 static llvm::cl::opt<bool> noImplicitModule{
47 "no-implicit-module",
48 llvm::cl::desc(
49 "Disable implicit addition of a top-level module op during parsing"),
50 llvm::cl::init(false)};
51
52 static llvm::cl::opt<bool> allowUnregisteredDialects(
53 "allow-unregistered-dialect",
54 llvm::cl::desc("Allow operation with no registered dialects"),
55 llvm::cl::init(false));
56
57 static llvm::cl::opt<std::string> splitInputFile(
58 "split-input-file", llvm::cl::ValueOptional,
59 llvm::cl::callback([&](const std::string &str) {
60 // Implicit value: use default marker if flag was used without
61 // value.
62 if (str.empty())
63 splitInputFile.setValue(kDefaultSplitMarker);
64 }),
65 llvm::cl::desc("Split the input file into chunks using the given or "
66 "default marker and process each chunk independently"),
67 llvm::cl::init(""));
68
69 llvm::cl::HideUnrelatedOptions(mlirReduceCategory);
70
71 llvm::InitLLVM y(argc, argv);
72
73 registerReducerPasses();
74
75 PassPipelineCLParser parser("", "Reduction Passes to Run");
76 llvm::cl::ParseCommandLineOptions(argc, argv,
77 "MLIR test case reduction tool.\n");
78
79 if (help) {
80 llvm::cl::PrintHelpMessage();
81 return success();
82 }
83 if (allowUnregisteredDialects)
85
86 std::string errorMessage;
87
88 auto output = openOutputFile(outputFilename, &errorMessage);
89 if (!output)
90 return failure();
91
92 std::unique_ptr<llvm::MemoryBuffer> input =
93 openInputFile(inputFilename, &errorMessage);
94 if (!input) {
95 llvm::errs() << errorMessage << "\n";
96 return failure();
97 }
98
99 auto errorHandler = [&](const Twine &msg) {
100 return emitError(UnknownLoc::get(&context)) << msg;
101 };
102
103 auto chunkFn = [&](std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
104 raw_ostream &os) {
105 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
106 sourceMgr->AddNewSourceBuffer(std::move(chunkBuffer), SMLoc());
108 parseSourceFileForTool(sourceMgr, &context, !noImplicitModule);
109 if (!opRef)
110 return failure();
111 // Reduction pass pipeline.
112 PassManager pm(&context, opRef.get()->getName().getStringRef());
113 if (failed(parser.addToPipeline(pm, errorHandler)))
114 return failure();
115
116 OwningOpRef<Operation *> op = opRef.get()->clone();
117
118 if (failed(pm.run(op.get())))
119 return failure();
120 op.get()->print(output->os());
121 output->keep();
122 return success();
123 };
124
125 auto &splitInputFileDelimiter = splitInputFile.getValue();
126 if (!splitInputFileDelimiter.empty())
127 return splitAndProcessBuffer(std::move(input), chunkFn, output->os(),
128 splitInputFileDelimiter,
129 splitInputFileDelimiter);
130
131 return chunkFn(std::move(input), output->os());
132}
return success()
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.
LogicalResult run(Operation *op)
Run the passes within this manager on the provided operation.
Definition Pass.cpp:1035
This class implements a command-line parser for MLIR passes.
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.
const char *const kDefaultSplitMarker
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.
std::unique_ptr< llvm::MemoryBuffer > openInputFile(llvm::StringRef inputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for reading.
LogicalResult splitAndProcessBuffer(std::unique_ptr< llvm::MemoryBuffer > originalBuffer, ChunkBufferHandler processChunkBuffer, raw_ostream &os, llvm::StringRef inputSplitMarker=kDefaultSplitMarker, llvm::StringRef outputSplitMarker="")
Splits the specified buffer on a marker (// ----- by default), processes each chunk independently acc...
LogicalResult mlirReduceMain(int argc, char **argv, MLIRContext &context)
OwningOpRef< Operation * > parseSourceFileForTool(const std::shared_ptr< llvm::SourceMgr > &sourceMgr, const ParserConfig &config, bool insertImplicitModule)
This parses the file specified by the indicated SourceMgr.