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 
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.
118  return passPipelineCallback(pm);
119  return success();
120  }
121 
122  /// Enable running the reproducer information stored in resources (if
123  /// present).
124  MlirOptMainConfig &runReproducer(bool enableReproducer) {
125  runReproducerFlag = enableReproducer;
126  return *this;
127  };
128 
129  /// Return true if the reproducer should be run.
130  bool shouldRunReproducer() const { return runReproducerFlag; }
131 
132  /// Show the registered dialects before trying to load the input file.
134  showDialectsFlag = show;
135  return *this;
136  }
137  bool shouldShowDialects() const { return showDialectsFlag; }
138 
139  /// Set whether to split the input file based on the `// -----` marker into
140  /// pieces and process each chunk independently.
141  MlirOptMainConfig &splitInputFile(bool split = true) {
142  splitInputFileFlag = split;
143  return *this;
144  }
145  bool shouldSplitInputFile() const { return splitInputFileFlag; }
146 
147  /// Disable implicit addition of a top-level module op during parsing.
150  return *this;
151  }
153 
154  /// Set whether to check that emitted diagnostics match `expected-*` lines on
155  /// the corresponding line. This is meant for implementing diagnostic tests.
158  return *this;
159  }
161 
162  /// Set whether to run the verifier after each transformation pass.
165  return *this;
166  }
167  bool shouldVerifyPasses() const { return verifyPassesFlag; }
168 
169  /// Set whether to run the verifier after each transformation pass.
172  return *this;
173  }
175 
176  /// Reproducer file generation (no crash required).
177  StringRef getReproducerFilename() const { return generateReproducerFileFlag; }
178 
179 protected:
180  /// Allow operation with no registered dialects.
181  /// This option is for convenience during testing only and discouraged in
182  /// general.
184 
185  /// Configuration for the debugging hooks.
187 
188  /// Print the pipeline that will be run.
189  bool dumpPassPipelineFlag = false;
190 
191  /// Emit bytecode instead of textual assembly when generating output.
192  bool emitBytecodeFlag = false;
193 
194  /// Elide resources when generating bytecode.
196 
197  /// Enable the Debugger action hook: Debugger can intercept MLIR Actions.
199 
200  /// IRDL file to register before processing the input.
201  std::string irdlFileFlag = "";
202 
203  /// Location Breakpoints to filter the action logging.
204  std::vector<tracing::BreakpointManager *> logActionLocationFilter;
205 
206  /// Emit bytecode at given version.
207  std::optional<int64_t> emitBytecodeVersion = std::nullopt;
208 
209  /// The callback to populate the pass manager.
211 
212  /// Enable running the reproducer.
213  bool runReproducerFlag = false;
214 
215  /// Show the registered dialects before trying to load the input file.
216  bool showDialectsFlag = false;
217 
218  /// Split the input file based on the `// -----` marker into pieces and
219  /// process each chunk independently.
220  bool splitInputFileFlag = false;
221 
222  /// Use an explicit top-level module op during parsing.
223  bool useExplicitModuleFlag = false;
224 
225  /// Set whether to check that emitted diagnostics match `expected-*` lines on
226  /// the corresponding line. This is meant for implementing diagnostic tests.
227  bool verifyDiagnosticsFlag = false;
228 
229  /// Run the verifier after each transformation pass.
230  bool verifyPassesFlag = true;
231 
232  /// Verify that the input IR round-trips perfectly.
233  bool verifyRoundtripFlag = false;
234 
235  /// The reproducer output filename (no crash required).
236  std::string generateReproducerFileFlag = "";
237 };
238 
239 /// This defines the function type used to setup the pass manager. This can be
240 /// used to pass in a callback to setup a default pass pipeline to be applied on
241 /// the loaded IR.
243 
244 /// Register and parse command line options.
245 /// - toolName is used for the header displayed by `--help`.
246 /// - registry should contain all the dialects that can be parsed in the source.
247 /// - return std::pair<std::string, std::string> for
248 /// inputFilename and outputFilename command line option values.
249 std::pair<std::string, std::string>
250 registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName,
251  DialectRegistry &registry);
252 
253 /// Perform the core processing behind `mlir-opt`.
254 /// - outputStream is the stream where the resulting IR is printed.
255 /// - buffer is the in-memory file to parser and process.
256 /// - registry should contain all the dialects that can be parsed in the source.
257 /// - config contains the configuration options for the tool.
258 LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
259  std::unique_ptr<llvm::MemoryBuffer> buffer,
260  DialectRegistry &registry,
261  const MlirOptMainConfig &config);
262 
263 /// Implementation for tools like `mlir-opt`.
264 /// - toolName is used for the header displayed by `--help`.
265 /// - registry should contain all the dialects that can be parsed in the source.
266 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
267  DialectRegistry &registry);
268 
269 /// Implementation for tools like `mlir-opt`.
270 /// This function can be used with registrationAndParseCLIOptions so that
271 /// CLI options can be accessed before running MlirOptMain.
272 /// - inputFilename is the name of the input mlir file.
273 /// - outputFilename is the name of the output file.
274 /// - registry should contain all the dialects that can be parsed in the source.
275 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef inputFilename,
276  llvm::StringRef outputFilename,
277  DialectRegistry &registry);
278 
279 /// Helper wrapper to return the result of MlirOptMain directly from main.
280 ///
281 /// Example:
282 ///
283 /// int main(int argc, char **argv) {
284 /// // ...
285 /// return mlir::asMainReturnCode(mlir::MlirOptMain(
286 /// argc, argv, /* ... */);
287 /// }
288 ///
290  return r.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE;
291 }
292 
293 } // namespace mlir
294 
295 #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.
bool shouldVerifyPasses() const
Definition: MlirOptMain.h:167
bool shouldSplitInputFile() const
Definition: MlirOptMain.h:145
bool shouldVerifyRoundtrip() const
Definition: MlirOptMain.h:174
std::optional< int64_t > bytecodeVersionToEmit() const
Definition: MlirOptMain.h:101
std::optional< int64_t > emitBytecodeVersion
Emit bytecode at given version.
Definition: MlirOptMain.h:207
StringRef getReproducerFilename() const
Reproducer file generation (no crash required).
Definition: MlirOptMain.h:177
std::string irdlFileFlag
IRDL file to register before processing the input.
Definition: MlirOptMain.h:201
std::vector< tracing::BreakpointManager * > logActionLocationFilter
Location Breakpoints to filter the action logging.
Definition: MlirOptMain.h:204
bool emitBytecodeFlag
Emit bytecode instead of textual assembly when generating output.
Definition: MlirOptMain.h:192
MlirOptMainConfig & splitInputFile(bool split=true)
Set whether to split the input file based on the // ----- marker into pieces and process each chunk i...
Definition: MlirOptMain.h:141
bool shouldShowDialects() const
Definition: MlirOptMain.h:137
std::string generateReproducerFileFlag
The reproducer output filename (no crash required).
Definition: MlirOptMain.h:236
std::function< LogicalResult(PassManager &)> passPipelineCallback
The callback to populate the pass manager.
Definition: MlirOptMain.h:210
bool dumpPassPipelineFlag
Print the pipeline that will be run.
Definition: MlirOptMain.h:189
bool verifyPassesFlag
Run the verifier after each transformation pass.
Definition: MlirOptMain.h:230
bool enableDebuggerActionHookFlag
Enable the Debugger action hook: Debugger can intercept MLIR Actions.
Definition: MlirOptMain.h:198
MlirOptMainConfig & verifyRoundtrip(bool verify)
Set whether to run the verifier after each transformation pass.
Definition: MlirOptMain.h:170
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:163
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:186
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:133
bool runReproducerFlag
Enable running the reproducer.
Definition: MlirOptMain.h:213
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:124
MlirOptMainConfig & verifyDiagnostics(bool verify)
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
Definition: MlirOptMain.h:156
bool verifyRoundtripFlag
Verify that the input IR round-trips perfectly.
Definition: MlirOptMain.h:233
bool verifyDiagnosticsFlag
Set whether to check that emitted diagnostics match expected-* lines on the corresponding line.
Definition: MlirOptMain.h:227
bool allowUnregisteredDialectsFlag
Allow operation with no registered dialects.
Definition: MlirOptMain.h:183
MlirOptMainConfig & useExplicitModule(bool useExplicitModule)
Disable implicit addition of a top-level module op during parsing.
Definition: MlirOptMain.h:148
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:152
bool shouldRunReproducer() const
Return true if the reproducer should be run.
Definition: MlirOptMain.h:130
bool elideResourceDataFromBytecodeFlag
Elide resources when generating bytecode.
Definition: MlirOptMain.h:195
MlirOptMainConfig & allowUnregisteredDialects(bool allow)
Options.
Definition: MlirOptMain.h:56
bool shouldDumpPassPipeline() const
Definition: MlirOptMain.h:77
bool shouldVerifyDiagnostics() const
Definition: MlirOptMain.h:160
bool useExplicitModuleFlag
Use an explicit top-level module op during parsing.
Definition: MlirOptMain.h:223
const tracing::DebugConfig & getDebugConfig() const
Definition: MlirOptMain.h:70
bool showDialectsFlag
Show the registered dialects before trying to load the input file.
Definition: MlirOptMain.h:216
MlirOptMainConfig & setDebugConfig(tracing::DebugConfig config)
Set the debug configuration to use.
Definition: MlirOptMain.h:65
StringRef getIrdlFile() const
Definition: MlirOptMain.h:94
bool splitInputFileFlag
Split the input file based on the // ----- marker into pieces and process each chunk independently.
Definition: MlirOptMain.h:220
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: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.
int asMainReturnCode(LogicalResult r)
Helper wrapper to return the result of MlirOptMain directly from main.
Definition: MlirOptMain.h:289
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