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