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 after each transformation pass.
189  return *this;
190  }
192 
193  /// Reproducer file generation (no crash required).
194  StringRef getReproducerFilename() const { return generateReproducerFileFlag; }
195 
196 protected:
197  /// Allow operation with no registered dialects.
198  /// This option is for convenience during testing only and discouraged in
199  /// general.
201 
202  /// Configuration for the debugging hooks.
204 
205  /// Print the pipeline that will be run.
206  bool dumpPassPipelineFlag = false;
207 
208  /// Emit bytecode instead of textual assembly when generating output.
209  bool emitBytecodeFlag = false;
210 
211  /// Elide resources when generating bytecode.
213 
214  /// Enable the Debugger action hook: Debugger can intercept MLIR Actions.
216 
217  /// IRDL file to register before processing the input.
218  std::string irdlFileFlag = "";
219 
220  /// Location Breakpoints to filter the action logging.
221  std::vector<tracing::BreakpointManager *> logActionLocationFilter;
222 
223  /// Emit bytecode at given version.
224  std::optional<int64_t> emitBytecodeVersion = std::nullopt;
225 
226  /// The callback to populate the pass manager.
227  std::function<LogicalResult(PassManager &)> passPipelineCallback;
228 
229  /// List the registered passes and return.
230  bool listPassesFlag = false;
231 
232  /// Enable running the reproducer.
233  bool runReproducerFlag = false;
234 
235  /// Show the registered dialects before trying to load the input file.
236  bool showDialectsFlag = false;
237 
238  /// Split the input file based on the given marker into chunks and process
239  /// each chunk independently. Input is not split if empty.
240  std::string splitInputFileFlag = "";
241 
242  /// Merge output chunks into one file using the given marker.
243  std::string outputSplitMarkerFlag = "";
244 
245  /// Use an explicit top-level module op during parsing.
246  bool useExplicitModuleFlag = false;
247 
248  /// Set whether to check that emitted diagnostics match `expected-*` lines on
249  /// the corresponding line. This is meant for implementing diagnostic tests.
250  bool verifyDiagnosticsFlag = false;
251 
252  /// Run the verifier after each transformation pass.
253  bool verifyPassesFlag = true;
254 
255  /// Verify that the input IR round-trips perfectly.
256  bool verifyRoundtripFlag = false;
257 
258  /// The reproducer output filename (no crash required).
259  std::string generateReproducerFileFlag = "";
260 };
261 
262 /// This defines the function type used to setup the pass manager. This can be
263 /// used to pass in a callback to setup a default pass pipeline to be applied on
264 /// the loaded IR.
265 using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
266 
267 /// Register and parse command line options.
268 /// - toolName is used for the header displayed by `--help`.
269 /// - registry should contain all the dialects that can be parsed in the source.
270 /// - return std::pair<std::string, std::string> for
271 /// inputFilename and outputFilename command line option values.
272 std::pair<std::string, std::string>
273 registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName,
274  DialectRegistry &registry);
275 
276 /// Perform the core processing behind `mlir-opt`.
277 /// - outputStream is the stream where the resulting IR is printed.
278 /// - buffer is the in-memory file to parser and process.
279 /// - registry should contain all the dialects that can be parsed in the source.
280 /// - config contains the configuration options for the tool.
281 LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
282  std::unique_ptr<llvm::MemoryBuffer> buffer,
283  DialectRegistry &registry,
284  const MlirOptMainConfig &config);
285 
286 /// Implementation for tools like `mlir-opt`.
287 /// - toolName is used for the header displayed by `--help`.
288 /// - registry should contain all the dialects that can be parsed in the source.
289 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
290  DialectRegistry &registry);
291 
292 /// Implementation for tools like `mlir-opt`.
293 /// This function can be used with registrationAndParseCLIOptions so that
294 /// CLI options can be accessed before running MlirOptMain.
295 /// - inputFilename is the name of the input mlir file.
296 /// - outputFilename is the name of the output file.
297 /// - registry should contain all the dialects that can be parsed in the source.
298 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef inputFilename,
299  llvm::StringRef outputFilename,
300  DialectRegistry &registry);
301 
302 /// Helper wrapper to return the result of MlirOptMain directly from main.
303 ///
304 /// Example:
305 ///
306 /// int main(int argc, char **argv) {
307 /// // ...
308 /// return mlir::asMainReturnCode(mlir::MlirOptMain(
309 /// argc, argv, /* ... */);
310 /// }
311 ///
312 inline int asMainReturnCode(LogicalResult r) {
313  return r.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE;
314 }
315 
316 } // namespace mlir
317 
318 #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:240
bool shouldVerifyPasses() const
Definition: MlirOptMain.h:184
StringRef inputSplitMarker() const
Definition: MlirOptMain.h:153
bool shouldVerifyRoundtrip() const
Definition: MlirOptMain.h:191
std::optional< int64_t > bytecodeVersionToEmit() const
Definition: MlirOptMain.h:101
std::optional< int64_t > emitBytecodeVersion
Emit bytecode at given version.
Definition: MlirOptMain.h:224
StringRef getReproducerFilename() const
Reproducer file generation (no crash required).
Definition: MlirOptMain.h:194
std::string irdlFileFlag
IRDL file to register before processing the input.
Definition: MlirOptMain.h:218
std::vector< tracing::BreakpointManager * > logActionLocationFilter
Location Breakpoints to filter the action logging.
Definition: MlirOptMain.h:221
bool emitBytecodeFlag
Emit bytecode instead of textual assembly when generating output.
Definition: MlirOptMain.h:209
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:259
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:227
bool dumpPassPipelineFlag
Print the pipeline that will be run.
Definition: MlirOptMain.h:206
bool verifyPassesFlag
Run the verifier after each transformation pass.
Definition: MlirOptMain.h:253
bool enableDebuggerActionHookFlag
Enable the Debugger action hook: Debugger can intercept MLIR Actions.
Definition: MlirOptMain.h:215
MlirOptMainConfig & verifyRoundtrip(bool verify)
Set whether to run the verifier after each transformation pass.
Definition: MlirOptMain.h:187
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 shouldElideResourceDataFromBytecode() const
Definition: MlirOptMain.h:85
bool shouldEmitBytecode() const
Definition: MlirOptMain.h:84
tracing::DebugConfig debugConfig
Configuration for the debugging hooks.
Definition: MlirOptMain.h:203
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:233
MlirOptMainConfig & dumpPassPipeline(bool dump)
Print the pass-pipeline as text before executing.
Definition: MlirOptMain.h:73
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:256
bool verifyDiagnosticsFlag
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
Definition: MlirOptMain.h:250
bool allowUnregisteredDialectsFlag
Allow operation with no registered dialects.
Definition: MlirOptMain.h:200
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:212
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:246
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:236
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:230
std::string outputSplitMarkerFlag
Merge output chunks into one file using the given marker.
Definition: MlirOptMain.h:243
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:312
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