MLIR 24.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 static llvm::cl::OptionCategory mlirReduceCategory("mlir-reduce options");
32
33 static llvm::cl::opt<std::string> inputFilename(
34 llvm::cl::Positional, llvm::cl::desc("<input file>"),
35 llvm::cl::cat(mlirReduceCategory));
36
37 static llvm::cl::opt<std::string> outputFilename(
38 "o", llvm::cl::desc("Output filename for the reduced test case"),
39 llvm::cl::init("-"), llvm::cl::cat(mlirReduceCategory));
40
41 static llvm::cl::opt<bool> noImplicitModule{
42 "no-implicit-module",
43 llvm::cl::desc(
44 "Disable implicit addition of a top-level module op during parsing"),
45 llvm::cl::init(false)};
46
47 static llvm::cl::opt<bool> allowUnregisteredDialects(
48 "allow-unregistered-dialect",
49 llvm::cl::desc("Allow operation with no registered dialects"),
50 llvm::cl::init(false));
51
52 static llvm::cl::opt<std::string> splitInputFile(
53 "split-input-file", llvm::cl::ValueOptional,
54 llvm::cl::callback([&](const std::string &str) {
55 // Implicit value: use default marker if flag was used without
56 // value.
57 if (str.empty())
58 splitInputFile.setValue(kDefaultSplitMarker);
59 }),
60 llvm::cl::desc("Split the input file into chunks using the given or "
61 "default marker and process each chunk independently"),
62 llvm::cl::init(""));
63
64 llvm::cl::HideUnrelatedOptions(mlirReduceCategory);
65
66 llvm::InitLLVM y(argc, argv);
67
69
70 PassPipelineCLParser parser("", "Reduction Passes to Run");
71 llvm::cl::ParseCommandLineOptions(argc, argv,
72 "MLIR test case reduction tool.\n");
73
74 if (allowUnregisteredDialects)
76
77 std::string errorMessage;
78
79 auto output = openOutputFile(outputFilename, &errorMessage);
80 if (!output)
81 return failure();
82
83 std::unique_ptr<llvm::MemoryBuffer> input =
84 openInputFile(inputFilename, &errorMessage);
85 if (!input) {
86 llvm::errs() << errorMessage << "\n";
87 return failure();
88 }
89
90 auto errorHandler = [&](const Twine &msg) {
91 return emitError(UnknownLoc::get(&context)) << msg;
92 };
93
94 auto chunkFn = [&](std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
95 raw_ostream &os) {
96 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
97 sourceMgr->AddNewSourceBuffer(std::move(chunkBuffer), SMLoc());
99 parseSourceFileForTool(sourceMgr, &context, !noImplicitModule);
100 if (!opRef)
101 return failure();
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 op.get()->print(output->os());
112 output->keep();
113 return success();
114 };
115
116 auto &splitInputFileDelimiter = splitInputFile.getValue();
117 if (!splitInputFileDelimiter.empty())
118 return splitAndProcessBuffer(std::move(input), chunkFn, output->os(),
119 splitInputFileDelimiter,
120 splitInputFileDelimiter);
121
122 return chunkFn(std::move(input), output->os());
123}
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.
void registerReducerPasses()
Definition Passes.h:268