MLIR 22.0.0git
MlirOptMain.h
Go to the documentation of this file.
1//===- MlirOptMain.h - MLIR Optimizer Driver main ---------------*- C++ -*-===//
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// Main entry function for mlir-opt for when built as standalone binary.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_TOOLS_MLIROPT_MLIROPTMAIN_H
14#define MLIR_TOOLS_MLIROPT_MLIROPTMAIN_H
15
18#include "llvm/ADT/StringRef.h"
19
20#include <cstdlib>
21#include <functional>
22#include <memory>
23
24namespace llvm {
25class raw_ostream;
26class MemoryBuffer;
27} // namespace llvm
28
29namespace mlir {
30class DialectRegistry;
32class PassManager;
33
34/// enum class to indicate the verbosity level of the diagnostic filter.
40
46
51
52/// Configuration options for the mlir-opt tool.
53/// This is intended to help building tools like mlir-opt by collecting the
54/// supported options.
55/// The API is fluent, and the options are sorted in alphabetical order below.
56/// The options can be exposed to the LLVM command line by registering them
57/// with `MlirOptMainConfig::registerCLOptions(DialectRegistry &);` and creating
58/// a config using `auto config = MlirOptMainConfig::createFromCLOptions();`.
60public:
61 /// Register the options as global LLVM command line options.
62 static void registerCLOptions(DialectRegistry &dialectRegistry);
63
64 /// Create a new config with the default set from the CL options.
66
67 ///
68 /// Options.
69 ///
70
71 /// Allow operation with no registered dialects.
72 /// This option is for convenience during testing only and discouraged in
73 /// general.
76 return *this;
77 }
81
82 /// Set the debug configuration to use.
89
90 /// Print the pass-pipeline as text before executing.
93 return *this;
94 }
95
99
101
102 /// Set the output format to bytecode instead of textual IR.
105 return *this;
106 }
107 bool shouldEmitBytecode() const { return emitBytecodeFlag; }
108
112
114
115 /// Set the IRDL file to load before processing the input.
117 irdlFileFlag = file;
118 return *this;
119 }
120 StringRef getIrdlFile() const { return irdlFileFlag; }
121
122 /// Set the bytecode version to emit.
124 emitBytecodeVersion = version;
125 return *this;
126 }
127 std::optional<int64_t> bytecodeVersionToEmit() const {
128 return emitBytecodeVersion;
129 }
130
131 /// Set the callback to populate the pass manager.
133 setPassPipelineSetupFn(std::function<LogicalResult(PassManager &)> callback) {
134 passPipelineCallback = std::move(callback);
135 return *this;
136 }
137
138 /// Set the parser to use to populate the pass manager.
140
141 /// Populate the passmanager, if any callback was set.
142 LogicalResult setupPassPipeline(PassManager &pm) const {
144 return passPipelineCallback(pm);
145 return success();
146 }
147
148 /// List the registered passes and return.
150 listPassesFlag = list;
151 return *this;
152 }
153 bool shouldListPasses() const { return listPassesFlag; }
154
155 /// Enable running the reproducer information stored in resources (if
156 /// present).
157 MlirOptMainConfig &runReproducer(bool enableReproducer) {
158 runReproducerFlag = enableReproducer;
159 return *this;
160 };
161
162 /// Return true if the reproducer should be run.
163 bool shouldRunReproducer() const { return runReproducerFlag; }
164
165 /// Show the registered dialects before trying to load the input file.
167 showDialectsFlag = show;
168 return *this;
169 }
170 bool shouldShowDialects() const { return showDialectsFlag; }
171
172 /// Set the marker on which to split the input into chunks and process each
173 /// chunk independently. Input is not split if empty.
175 splitInputFile(std::string splitMarker = kDefaultSplitMarker) {
176 splitInputFileFlag = std::move(splitMarker);
177 return *this;
178 }
179 StringRef inputSplitMarker() const { return splitInputFileFlag; }
180
181 /// Set whether to merge the output chunks into one file using the given
182 /// marker.
184 outputSplitMarker(std::string splitMarker = kDefaultSplitMarker) {
185 outputSplitMarkerFlag = std::move(splitMarker);
186 return *this;
187 }
188 StringRef outputSplitMarker() const { return outputSplitMarkerFlag; }
189
190 /// Disable implicit addition of a top-level module op during parsing.
196
197 /// Set whether to check that emitted diagnostics match `expected-*` lines on
198 /// the corresponding line. This is meant for implementing diagnostic tests.
204
209
213
214 /// Set whether to run the verifier after each transformation pass.
217 return *this;
218 }
219 bool shouldVerifyPasses() const { return verifyPassesFlag; }
220
221 /// Set whether to run the verifier on parsing.
227
228 /// Set whether to run the verifier after each transformation pass.
231 return *this;
232 }
234
235 /// Checks if any remark filters are set.
236 bool shouldEmitRemarks() const {
237 // Emit all remarks only when no filters are specified.
238 const bool hasFilters =
239 !getRemarksAllFilter().empty() || !getRemarksPassedFilter().empty() ||
240 !getRemarksFailedFilter().empty() ||
241 !getRemarksMissedFilter().empty() || !getRemarksAnalyseFilter().empty();
242 return hasFilters;
243 }
244
245 /// Reproducer file generation (no crash required).
247
248 /// Set the reproducer output filename
250 /// Set the remark policy to use.
252 /// Set the remark format to use.
253 std::string getRemarksAllFilter() const { return remarksAllFilterFlag; }
254 /// Set the remark output file.
255 std::string getRemarksOutputFile() const { return remarksOutputFileFlag; }
256 /// Set the remark passed filters.
257 std::string getRemarksPassedFilter() const { return remarksPassedFilterFlag; }
258 /// Set the remark failed filters.
259 std::string getRemarksFailedFilter() const { return remarksFailedFilterFlag; }
260 /// Set the remark missed filters.
261 std::string getRemarksMissedFilter() const { return remarksMissedFilterFlag; }
262 /// Set the remark analyse filters.
263 std::string getRemarksAnalyseFilter() const {
265 }
266
267protected:
268 /// Allow operation with no registered dialects.
269 /// This option is for convenience during testing only and discouraged in
270 /// general.
272
273 /// Remark format
275 /// Remark policy
277 /// Remark file to output to
278 std::string remarksOutputFileFlag = "";
279 /// Remark filters
280 std::string remarksAllFilterFlag = "";
281 std::string remarksPassedFilterFlag = "";
282 std::string remarksFailedFilterFlag = "";
283 std::string remarksMissedFilterFlag = "";
284 std::string remarksAnalyseFilterFlag = "";
285
286 /// Configuration for the debugging hooks.
288
289 /// Verbosity level of diagnostic information. 0: Errors only,
290 /// 1: Errors and warnings, 2: Errors, warnings and remarks.
293
294 /// Print the pipeline that will be run.
296
297 /// Emit bytecode instead of textual assembly when generating output.
298 bool emitBytecodeFlag = false;
299
300 /// Elide resources when generating bytecode.
302
303 /// IRDL file to register before processing the input.
304 std::string irdlFileFlag = "";
305
306 /// Location Breakpoints to filter the action logging.
307 std::vector<tracing::BreakpointManager *> logActionLocationFilter;
308
309 /// Emit bytecode at given version.
310 std::optional<int64_t> emitBytecodeVersion = std::nullopt;
311
312 /// The callback to populate the pass manager.
313 std::function<LogicalResult(PassManager &)> passPipelineCallback;
314
315 /// List the registered passes and return.
316 bool listPassesFlag = false;
317
318 /// Enable running the reproducer.
319 bool runReproducerFlag = false;
320
321 /// Show the registered dialects before trying to load the input file.
322 bool showDialectsFlag = false;
323
324 /// Show the notes in diagnostic information. Notes can be included in
325 /// any diagnostic information, so it is not specified in the verbosity
326 /// level.
328
329 /// Split the input file based on the given marker into chunks and process
330 /// each chunk independently. Input is not split if empty.
331 std::string splitInputFileFlag = "";
332
333 /// Merge output chunks into one file using the given marker.
334 std::string outputSplitMarkerFlag = "";
335
336 /// Use an explicit top-level module op during parsing.
338
339 /// Set whether to check that emitted diagnostics match `expected-*` lines on
340 /// the corresponding line. This is meant for implementing diagnostic tests.
343
344 /// Run the verifier after each transformation pass.
345 bool verifyPassesFlag = true;
346
347 /// Disable the verifier on parsing.
349
350 /// Verify that the input IR round-trips perfectly.
352
353 /// The reproducer output filename (no crash required).
355};
356
357/// This defines the function type used to setup the pass manager. This can be
358/// used to pass in a callback to setup a default pass pipeline to be applied on
359/// the loaded IR.
361
362/// Register and parse command line options.
363/// - toolName is used for the header displayed by `--help`.
364/// - registry should contain all the dialects that can be parsed in the source.
365/// - return std::pair<std::string, std::string> for
366/// inputFilename and outputFilename command line option values.
367std::pair<std::string, std::string>
368registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName,
369 DialectRegistry &registry);
370
371/// Perform the core processing behind `mlir-opt`.
372/// - outputStream is the stream where the resulting IR is printed.
373/// - buffer is the in-memory file to parser and process.
374/// - registry should contain all the dialects that can be parsed in the source.
375/// - config contains the configuration options for the tool.
376LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
377 std::unique_ptr<llvm::MemoryBuffer> buffer,
378 DialectRegistry &registry,
380
381/// Implementation for tools like `mlir-opt`.
382/// - toolName is used for the header displayed by `--help`.
383/// - registry should contain all the dialects that can be parsed in the source.
384LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
385 DialectRegistry &registry);
386
387/// Implementation for tools like `mlir-opt`.
388/// This function can be used with registerAndParseCLIOptions so that
389/// CLI options can be accessed before running MlirOptMain.
390/// - inputFilename is the name of the input mlir file.
391/// - outputFilename is the name of the output file.
392/// - registry should contain all the dialects that can be parsed in the source.
393LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef inputFilename,
394 llvm::StringRef outputFilename,
395 DialectRegistry &registry);
396
397/// Helper wrapper to return the result of MlirOptMain directly from main.
398///
399/// Example:
400///
401/// int main(int argc, char **argv) {
402/// // ...
403/// return mlir::asMainReturnCode(mlir::MlirOptMain(
404/// argc, argv, /* ... */);
405/// }
406///
407inline int asMainReturnCode(LogicalResult r) {
408 return r.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE;
409}
410
411} // namespace mlir
412
413#endif // MLIR_TOOLS_MLIROPT_MLIROPTMAIN_H
return success()
static LogicalResult emit(SolverOp solver, const SMTEmissionOptions &options, mlir::raw_indented_ostream &stream)
Emit the SMT operations in the given 'solver' to the 'stream'.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Configuration options for the mlir-opt tool.
Definition MlirOptMain.h:59
MlirOptMainConfig & emitBytecode(bool emit)
Set the output format to bytecode instead of textual IR.
std::string getRemarksAnalyseFilter() const
Set the remark analyse filters.
std::string getRemarksMissedFilter() const
Set the remark missed filters.
bool shouldEmitRemarks() const
Checks if any remark filters are set.
static MlirOptMainConfig createFromCLOptions()
Create a new config with the default set from the CL options.
std::string splitInputFileFlag
Split the input file based on the given marker into chunks and process each chunk independently.
MlirOptMainConfig & runReproducer(bool enableReproducer)
Enable running the reproducer information stored in resources (if present).
bool shouldVerifyPasses() const
MlirOptMainConfig & splitInputFile(std::string splitMarker=kDefaultSplitMarker)
Set the marker on which to split the input into chunks and process each chunk independently.
MlirOptMainConfig & showDialects(bool show)
Show the registered dialects before trying to load the input file.
MlirOptMainConfig & allowUnregisteredDialects(bool allow)
Options.
Definition MlirOptMain.h:74
MlirOptMainConfig & verifyOnParsing(bool verify)
Set whether to run the verifier on parsing.
StringRef inputSplitMarker() const
MlirOptMainConfig & outputSplitMarker(std::string splitMarker=kDefaultSplitMarker)
Set whether to merge the output chunks into one file using the given marker.
MlirOptMainConfig & verifyDiagnostics(SourceMgrDiagnosticVerifierHandler::Level verify)
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
bool shouldVerifyRoundtrip() const
SourceMgrDiagnosticVerifierHandler::Level verifyDiagnosticsLevel() const
bool shouldShowNotes() const
bool disableVerifierOnParsingFlag
Disable the verifier on parsing.
MlirOptMainConfig & verifyRoundtrip(bool verify)
Set whether to run the verifier after each transformation pass.
std::optional< int64_t > emitBytecodeVersion
Emit bytecode at given version.
StringRef getReproducerFilename() const
Reproducer file generation (no crash required).
std::string irdlFileFlag
IRDL file to register before processing the input.
VerbosityLevel diagnosticVerbosityLevelFlag
Verbosity level of diagnostic information.
std::vector< tracing::BreakpointManager * > logActionLocationFilter
Location Breakpoints to filter the action logging.
bool emitBytecodeFlag
Emit bytecode instead of textual assembly when generating output.
std::string getRemarksOutputFile() const
Set the remark output file.
bool shouldShowDialects() const
MlirOptMainConfig & setEmitBytecodeVersion(int64_t version)
Set the bytecode version to emit.
std::string getRemarksFailedFilter() const
Set the remark failed filters.
bool disableDiagnosticNotesFlag
Show the notes in diagnostic information.
std::string generateReproducerFileFlag
The reproducer output filename (no crash required).
SourceMgrDiagnosticVerifierHandler::Level verifyDiagnosticsFlag
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
MlirOptMainConfig & useExplicitModule(bool useExplicitModule)
Disable implicit addition of a top-level module op during parsing.
MlirOptMainConfig & setPassPipelineSetupFn(std::function< LogicalResult(PassManager &)> callback)
Set the callback to populate the pass manager.
std::function< LogicalResult(PassManager &)> passPipelineCallback
The callback to populate the pass manager.
bool dumpPassPipelineFlag
Print the pipeline that will be run.
const tracing::DebugConfig & getDebugConfig() const
Definition MlirOptMain.h:88
bool verifyPassesFlag
Run the verifier after each transformation pass.
std::string remarksOutputFileFlag
Remark file to output to.
bool shouldVerifyOnParsing() const
std::string getRemarksAllFilter() const
Set the remark format to use.
RemarkPolicy getRemarkPolicy() const
Set the remark policy to use.
bool shouldElideResourceDataFromBytecode() const
bool shouldEmitBytecode() const
tracing::DebugConfig debugConfig
Configuration for the debugging hooks.
RemarkFormat remarkFormatFlag
Remark format.
MlirOptMainConfig & dumpPassPipeline(bool dump)
Print the pass-pipeline as text before executing.
Definition MlirOptMain.h:91
MlirOptMainConfig & verifyPasses(bool verify)
Set whether to run the verifier after each transformation pass.
StringRef outputSplitMarker() const
RemarkFormat getRemarkFormat() const
Set the reproducer output filename.
MlirOptMainConfig & setIrdlFile(StringRef file)
Set the IRDL file to load before processing the input.
bool runReproducerFlag
Enable running the reproducer.
RemarkPolicy remarkPolicyFlag
Remark policy.
std::string remarksAllFilterFlag
Remark filters.
bool verifyRoundtripFlag
Verify that the input IR round-trips perfectly.
std::string remarksPassedFilterFlag
bool allowUnregisteredDialectsFlag
Allow operation with no registered dialects.
MlirOptMainConfig & listPasses(bool list)
List the registered passes and return.
LogicalResult setupPassPipeline(PassManager &pm) const
Populate the passmanager, if any callback was set.
std::string remarksAnalyseFilterFlag
static void registerCLOptions(DialectRegistry &dialectRegistry)
Register the options as global LLVM command line options.
bool shouldAllowUnregisteredDialects() const
Definition MlirOptMain.h:78
bool shouldUseExplicitModule() const
VerbosityLevel getDiagnosticVerbosityLevel() const
Definition MlirOptMain.h:96
bool shouldRunReproducer() const
Return true if the reproducer should be run.
bool elideResourceDataFromBytecodeFlag
Elide resources when generating bytecode.
std::optional< int64_t > bytecodeVersionToEmit() const
std::string remarksFailedFilterFlag
bool shouldDumpPassPipeline() const
bool shouldVerifyDiagnostics() const
bool useExplicitModuleFlag
Use an explicit top-level module op during parsing.
std::string remarksMissedFilterFlag
tracing::DebugConfig & getDebugConfig()
Definition MlirOptMain.h:87
std::string getRemarksPassedFilter() const
Set the remark passed filters.
bool shouldListPasses() const
bool showDialectsFlag
Show the registered dialects before trying to load the input file.
StringRef getIrdlFile() const
MlirOptMainConfig & setDebugConfig(tracing::DebugConfig config)
Set the debug configuration to use.
Definition MlirOptMain.h:83
bool listPassesFlag
List the registered passes and return.
std::string outputSplitMarkerFlag
Merge output chunks into one file using the given marker.
MlirOptMainConfig & setPassPipelineParser(const PassPipelineCLParser &parser)
Set the parser to use to populate the pass manager.
The main pass manager and pipeline builder.
This class implements a command-line parser for MLIR passes.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
Include the generated interface declarations.
const char *const kDefaultSplitMarker
int asMainReturnCode(LogicalResult r)
Helper wrapper to return the result of MlirOptMain directly from main.
const FrozenRewritePatternSet GreedyRewriteConfig config
LogicalResult MlirOptMain(llvm::raw_ostream &outputStream, std::unique_ptr< llvm::MemoryBuffer > buffer, DialectRegistry &registry, const MlirOptMainConfig &config)
Perform the core processing behind mlir-opt.
llvm::function_ref< LogicalResult(PassManager &pm)> PassPipelineFn
This defines the function type used to setup the pass manager.
RemarkPolicy
Definition MlirOptMain.h:47
RemarkFormat
Definition MlirOptMain.h:41
std::pair< std::string, std::string > registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName, DialectRegistry &registry)
Register and parse command line options.
VerbosityLevel
enum class to indicate the verbosity level of the diagnostic filter.
Definition MlirOptMain.h:35
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition Verifier.cpp:423