MLIR  20.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 
24 namespace llvm {
25 class raw_ostream;
26 class MemoryBuffer;
27 } // namespace llvm
28 
29 namespace mlir {
30 class DialectRegistry;
31 class PassPipelineCLParser;
32 class PassManager;
33 
34 /// Configuration options for the mlir-opt tool.
35 /// This is intended to help building tools like mlir-opt by collecting the
36 /// supported options.
37 /// The API is fluent, and the options are sorted in alphabetical order below.
38 /// The options can be exposed to the LLVM command line by registering them
39 /// with `MlirOptMainConfig::registerCLOptions(DialectRegistry &);` and creating
40 /// a config using `auto config = MlirOptMainConfig::createFromCLOptions();`.
42 public:
43  /// Register the options as global LLVM command line options.
44  static void registerCLOptions(DialectRegistry &dialectRegistry);
45 
46  /// Create a new config with the default set from the CL options.
48 
49  ///
50  /// Options.
51  ///
52 
53  /// Allow operation with no registered dialects.
54  /// This option is for convenience during testing only and discouraged in
55  /// general.
58  return *this;
59  }
62  }
63 
64  /// Set the debug configuration to use.
66  debugConfig = std::move(config);
67  return *this;
68  }
70  const tracing::DebugConfig &getDebugConfig() const { return debugConfig; }
71 
72  /// Print the pass-pipeline as text before executing.
74  dumpPassPipelineFlag = dump;
75  return *this;
76  }
78 
79  /// Set the output format to bytecode instead of textual IR.
81  emitBytecodeFlag = emit;
82  return *this;
83  }
84  bool shouldEmitBytecode() const { return emitBytecodeFlag; }
87  }
88 
89  /// Set the IRDL file to load before processing the input.
90  MlirOptMainConfig &setIrdlFile(StringRef file) {
91  irdlFileFlag = file;
92  return *this;
93  }
94  StringRef getIrdlFile() const { return irdlFileFlag; }
95 
96  /// Set the bytecode version to emit.
98  emitBytecodeVersion = version;
99  return *this;
100  }
101  std::optional<int64_t> bytecodeVersionToEmit() const {
102  return emitBytecodeVersion;
103  }
104 
105  /// Set the callback to populate the pass manager.
107  setPassPipelineSetupFn(std::function<LogicalResult(PassManager &)> callback) {
108  passPipelineCallback = std::move(callback);
109  return *this;
110  }
111 
112  /// Set the parser to use to populate the pass manager.
114 
115  /// Populate the passmanager, if any callback was set.
116  LogicalResult setupPassPipeline(PassManager &pm) const {
118  return passPipelineCallback(pm);
119  return success();
120  }
121 
122  /// List the registered passes and return.
124  listPassesFlag = list;
125  return *this;
126  }
127  bool shouldListPasses() const { return listPassesFlag; }
128 
129  /// Enable running the reproducer information stored in resources (if
130  /// present).
131  MlirOptMainConfig &runReproducer(bool enableReproducer) {
132  runReproducerFlag = enableReproducer;
133  return *this;
134  };
135 
136  /// Return true if the reproducer should be run.
137  bool shouldRunReproducer() const { return runReproducerFlag; }
138 
139  /// Show the registered dialects before trying to load the input file.
141  showDialectsFlag = show;
142  return *this;
143  }
144  bool shouldShowDialects() const { return showDialectsFlag; }
145 
146  /// Set the marker on which to split the input into chunks and process each
147  /// chunk independently. Input is not split if empty.
149  splitInputFile(std::string splitMarker = kDefaultSplitMarker) {
150  splitInputFileFlag = std::move(splitMarker);
151  return *this;
152  }
153  StringRef inputSplitMarker() const { return splitInputFileFlag; }
154 
155  /// Set whether to merge the output chunks into one file using the given
156  /// marker.
158  outputSplitMarker(std::string splitMarker = kDefaultSplitMarker) {
159  outputSplitMarkerFlag = std::move(splitMarker);
160  return *this;
161  }
162  StringRef outputSplitMarker() const { return outputSplitMarkerFlag; }
163 
164  /// Disable implicit addition of a top-level module op during parsing.
167  return *this;
168  }
170 
171  /// Set whether to check that emitted diagnostics match `expected-*` lines on
172  /// the corresponding line. This is meant for implementing diagnostic tests.
175  return *this;
176  }
178 
179  /// Set whether to run the verifier after each transformation pass.
182  return *this;
183  }
184  bool shouldVerifyPasses() const { return verifyPassesFlag; }
185 
186  /// Set whether to run the verifier on parsing.
189  return *this;
190  }
192 
193  /// Set whether to run the verifier after each transformation pass.
196  return *this;
197  }
199 
200  /// Reproducer file generation (no crash required).
201  StringRef getReproducerFilename() const { return generateReproducerFileFlag; }
202 
203 protected:
204  /// Allow operation with no registered dialects.
205  /// This option is for convenience during testing only and discouraged in
206  /// general.
208 
209  /// Configuration for the debugging hooks.
211 
212  /// Print the pipeline that will be run.
213  bool dumpPassPipelineFlag = false;
214 
215  /// Emit bytecode instead of textual assembly when generating output.
216  bool emitBytecodeFlag = false;
217 
218  /// Elide resources when generating bytecode.
220 
221  /// Enable the Debugger action hook: Debugger can intercept MLIR Actions.
223 
224  /// IRDL file to register before processing the input.
225  std::string irdlFileFlag = "";
226 
227  /// Location Breakpoints to filter the action logging.
228  std::vector<tracing::BreakpointManager *> logActionLocationFilter;
229 
230  /// Emit bytecode at given version.
231  std::optional<int64_t> emitBytecodeVersion = std::nullopt;
232 
233  /// The callback to populate the pass manager.
234  std::function<LogicalResult(PassManager &)> passPipelineCallback;
235 
236  /// List the registered passes and return.
237  bool listPassesFlag = false;
238 
239  /// Enable running the reproducer.
240  bool runReproducerFlag = false;
241 
242  /// Show the registered dialects before trying to load the input file.
243  bool showDialectsFlag = false;
244 
245  /// Split the input file based on the given marker into chunks and process
246  /// each chunk independently. Input is not split if empty.
247  std::string splitInputFileFlag = "";
248 
249  /// Merge output chunks into one file using the given marker.
250  std::string outputSplitMarkerFlag = "";
251 
252  /// Use an explicit top-level module op during parsing.
253  bool useExplicitModuleFlag = false;
254 
255  /// Set whether to check that emitted diagnostics match `expected-*` lines on
256  /// the corresponding line. This is meant for implementing diagnostic tests.
257  bool verifyDiagnosticsFlag = false;
258 
259  /// Run the verifier after each transformation pass.
260  bool verifyPassesFlag = true;
261 
262  /// Disable the verifier on parsing.
264 
265  /// Verify that the input IR round-trips perfectly.
266  bool verifyRoundtripFlag = false;
267 
268  /// The reproducer output filename (no crash required).
269  std::string generateReproducerFileFlag = "";
270 };
271 
272 /// This defines the function type used to setup the pass manager. This can be
273 /// used to pass in a callback to setup a default pass pipeline to be applied on
274 /// the loaded IR.
275 using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
276 
277 /// Register and parse command line options.
278 /// - toolName is used for the header displayed by `--help`.
279 /// - registry should contain all the dialects that can be parsed in the source.
280 /// - return std::pair<std::string, std::string> for
281 /// inputFilename and outputFilename command line option values.
282 std::pair<std::string, std::string>
283 registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName,
284  DialectRegistry &registry);
285 
286 /// Perform the core processing behind `mlir-opt`.
287 /// - outputStream is the stream where the resulting IR is printed.
288 /// - buffer is the in-memory file to parser and process.
289 /// - registry should contain all the dialects that can be parsed in the source.
290 /// - config contains the configuration options for the tool.
291 LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
292  std::unique_ptr<llvm::MemoryBuffer> buffer,
293  DialectRegistry &registry,
294  const MlirOptMainConfig &config);
295 
296 /// Implementation for tools like `mlir-opt`.
297 /// - toolName is used for the header displayed by `--help`.
298 /// - registry should contain all the dialects that can be parsed in the source.
299 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
300  DialectRegistry &registry);
301 
302 /// Implementation for tools like `mlir-opt`.
303 /// This function can be used with registrationAndParseCLIOptions so that
304 /// CLI options can be accessed before running MlirOptMain.
305 /// - inputFilename is the name of the input mlir file.
306 /// - outputFilename is the name of the output file.
307 /// - registry should contain all the dialects that can be parsed in the source.
308 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef inputFilename,
309  llvm::StringRef outputFilename,
310  DialectRegistry &registry);
311 
312 /// Helper wrapper to return the result of MlirOptMain directly from main.
313 ///
314 /// Example:
315 ///
316 /// int main(int argc, char **argv) {
317 /// // ...
318 /// return mlir::asMainReturnCode(mlir::MlirOptMain(
319 /// argc, argv, /* ... */);
320 /// }
321 ///
322 inline int asMainReturnCode(LogicalResult r) {
323  return r.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE;
324 }
325 
326 } // namespace mlir
327 
328 #endif // MLIR_TOOLS_MLIROPT_MLIROPTMAIN_H
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Configuration options for the mlir-opt tool.
Definition: MlirOptMain.h:41
tracing::DebugConfig & getDebugConfig()
Definition: MlirOptMain.h:69
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.
Definition: MlirOptMain.h:247
bool shouldVerifyPasses() const
Definition: MlirOptMain.h:184
StringRef inputSplitMarker() const
Definition: MlirOptMain.h:153
bool shouldVerifyRoundtrip() const
Definition: MlirOptMain.h:198
std::optional< int64_t > bytecodeVersionToEmit() const
Definition: MlirOptMain.h:101
bool disableVerifierOnParsingFlag
Disable the verifier on parsing.
Definition: MlirOptMain.h:263
std::optional< int64_t > emitBytecodeVersion
Emit bytecode at given version.
Definition: MlirOptMain.h:231
StringRef getReproducerFilename() const
Reproducer file generation (no crash required).
Definition: MlirOptMain.h:201
std::string irdlFileFlag
IRDL file to register before processing the input.
Definition: MlirOptMain.h:225
std::vector< tracing::BreakpointManager * > logActionLocationFilter
Location Breakpoints to filter the action logging.
Definition: MlirOptMain.h:228
bool emitBytecodeFlag
Emit bytecode instead of textual assembly when generating output.
Definition: MlirOptMain.h:216
bool shouldShowDialects() const
Definition: MlirOptMain.h:144
MlirOptMainConfig & splitInputFile(std::string splitMarker=kDefaultSplitMarker)
Set the marker on which to split the input into chunks and process each chunk independently.
Definition: MlirOptMain.h:149
std::string generateReproducerFileFlag
The reproducer output filename (no crash required).
Definition: MlirOptMain.h:269
MlirOptMainConfig & outputSplitMarker(std::string splitMarker=kDefaultSplitMarker)
Set whether to merge the output chunks into one file using the given marker.
Definition: MlirOptMain.h:158
std::function< LogicalResult(PassManager &)> passPipelineCallback
The callback to populate the pass manager.
Definition: MlirOptMain.h:234
bool dumpPassPipelineFlag
Print the pipeline that will be run.
Definition: MlirOptMain.h:213
bool verifyPassesFlag
Run the verifier after each transformation pass.
Definition: MlirOptMain.h:260
bool enableDebuggerActionHookFlag
Enable the Debugger action hook: Debugger can intercept MLIR Actions.
Definition: MlirOptMain.h:222
MlirOptMainConfig & verifyRoundtrip(bool verify)
Set whether to run the verifier after each transformation pass.
Definition: MlirOptMain.h:194
MlirOptMainConfig & setIrdlFile(StringRef file)
Set the IRDL file to load before processing the input.
Definition: MlirOptMain.h:90
MlirOptMainConfig & verifyPasses(bool verify)
Set whether to run the verifier after each transformation pass.
Definition: MlirOptMain.h:180
bool shouldVerifyOnParsing() const
Definition: MlirOptMain.h:191
bool shouldElideResourceDataFromBytecode() const
Definition: MlirOptMain.h:85
bool shouldEmitBytecode() const
Definition: MlirOptMain.h:84
tracing::DebugConfig debugConfig
Configuration for the debugging hooks.
Definition: MlirOptMain.h:210
StringRef outputSplitMarker() const
Definition: MlirOptMain.h:162
MlirOptMainConfig & setPassPipelineSetupFn(std::function< LogicalResult(PassManager &)> callback)
Set the callback to populate the pass manager.
Definition: MlirOptMain.h:107
MlirOptMainConfig & showDialects(bool show)
Show the registered dialects before trying to load the input file.
Definition: MlirOptMain.h:140
bool runReproducerFlag
Enable running the reproducer.
Definition: MlirOptMain.h:240
MlirOptMainConfig & dumpPassPipeline(bool dump)
Print the pass-pipeline as text before executing.
Definition: MlirOptMain.h:73
MlirOptMainConfig & verifyOnParsing(bool verify)
Set whether to run the verifier on parsing.
Definition: MlirOptMain.h:187
MlirOptMainConfig & runReproducer(bool enableReproducer)
Enable running the reproducer information stored in resources (if present).
Definition: MlirOptMain.h:131
MlirOptMainConfig & verifyDiagnostics(bool verify)
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
Definition: MlirOptMain.h:173
bool verifyRoundtripFlag
Verify that the input IR round-trips perfectly.
Definition: MlirOptMain.h:266
bool verifyDiagnosticsFlag
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
Definition: MlirOptMain.h:257
bool allowUnregisteredDialectsFlag
Allow operation with no registered dialects.
Definition: MlirOptMain.h:207
MlirOptMainConfig & useExplicitModule(bool useExplicitModule)
Disable implicit addition of a top-level module op during parsing.
Definition: MlirOptMain.h:165
LogicalResult setupPassPipeline(PassManager &pm) const
Populate the passmanager, if any callback was set.
Definition: MlirOptMain.h:116
MlirOptMainConfig & setEmitBytecodeVersion(int64_t version)
Set the bytecode version to emit.
Definition: MlirOptMain.h:97
static void registerCLOptions(DialectRegistry &dialectRegistry)
Register the options as global LLVM command line options.
bool shouldAllowUnregisteredDialects() const
Definition: MlirOptMain.h:60
bool shouldUseExplicitModule() const
Definition: MlirOptMain.h:169
bool shouldRunReproducer() const
Return true if the reproducer should be run.
Definition: MlirOptMain.h:137
bool elideResourceDataFromBytecodeFlag
Elide resources when generating bytecode.
Definition: MlirOptMain.h:219
MlirOptMainConfig & allowUnregisteredDialects(bool allow)
Options.
Definition: MlirOptMain.h:56
bool shouldDumpPassPipeline() const
Definition: MlirOptMain.h:77
bool shouldVerifyDiagnostics() const
Definition: MlirOptMain.h:177
bool useExplicitModuleFlag
Use an explicit top-level module op during parsing.
Definition: MlirOptMain.h:253
const tracing::DebugConfig & getDebugConfig() const
Definition: MlirOptMain.h:70
bool shouldListPasses() const
Definition: MlirOptMain.h:127
bool showDialectsFlag
Show the registered dialects before trying to load the input file.
Definition: MlirOptMain.h:243
MlirOptMainConfig & listPasses(bool list)
List the registered passes and return.
Definition: MlirOptMain.h:123
MlirOptMainConfig & setDebugConfig(tracing::DebugConfig config)
Set the debug configuration to use.
Definition: MlirOptMain.h:65
StringRef getIrdlFile() const
Definition: MlirOptMain.h:94
bool listPassesFlag
List the registered passes and return.
Definition: MlirOptMain.h:237
std::string outputSplitMarkerFlag
Merge output chunks into one file using the given marker.
Definition: MlirOptMain.h:250
MlirOptMainConfig & emitBytecode(bool emit)
Set the output format to bytecode instead of textual IR.
Definition: MlirOptMain.h:80
MlirOptMainConfig & setPassPipelineParser(const PassPipelineCLParser &parser)
Set the parser to use to populate the pass manager.
The main pass manager and pipeline builder.
Definition: PassManager.h:231
This class implements a command-line parser for MLIR passes.
Definition: PassRegistry.h:247
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.
Definition: MlirOptMain.h:322
LogicalResult MlirOptMain(llvm::raw_ostream &outputStream, std::unique_ptr< llvm::MemoryBuffer > buffer, DialectRegistry &registry, const MlirOptMainConfig &config)
Perform the core processing behind mlir-opt.
std::pair< std::string, std::string > registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName, DialectRegistry &registry)
Register and parse command line options.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition: Verifier.cpp:426