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