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 
16 #include "mlir/Support/LogicalResult.h"
17 #include "llvm/ADT/StringRef.h"
18 
19 #include <cstdlib>
20 #include <memory>
21 
22 namespace llvm {
23 class raw_ostream;
24 class MemoryBuffer;
25 } // namespace llvm
26 
27 namespace mlir {
28 class DialectRegistry;
29 class PassPipelineCLParser;
30 class PassManager;
31 
32 /// This defines the function type used to setup the pass manager. This can be
33 /// used to pass in a callback to setup a default pass pipeline to be applied on
34 /// the loaded IR.
35 using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
36 
37 /// Perform the core processing behind `mlir-opt`:
38 /// - outputStream is the stream where the resulting IR is printed.
39 /// - buffer is the in-memory file to parser and process.
40 /// - passPipeline is the specification of the pipeline that will be applied.
41 /// - registry should contain all the dialects that can be parsed in the source.
42 /// - splitInputFile will look for a "-----" marker in the input file, and load
43 /// each chunk in an individual ModuleOp processed separately.
44 /// - verifyDiagnostics enables a verification mode where comments starting with
45 /// "expected-(error|note|remark|warning)" are parsed in the input and matched
46 /// against emitted diagnostics.
47 /// - verifyPasses enables the IR verifier in-between each pass in the pipeline.
48 /// - allowUnregisteredDialects allows to parse and create operation without
49 /// registering the Dialect in the MLIRContext.
50 /// - preloadDialectsInContext will trigger the upfront loading of all
51 ///   dialects from the global registry in the MLIRContext. This option is
52 ///   deprecated and will be removed soon.
53 LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
54                           std::unique_ptr<llvm::MemoryBuffer> buffer,
55                           const PassPipelineCLParser &passPipeline,
56                           DialectRegistry &registry, bool splitInputFile,
57                           bool verifyDiagnostics, bool verifyPasses,
58                           bool allowUnregisteredDialects,
59                           bool preloadDialectsInContext = false);
60 
61 /// Support a callback to setup the pass manager.
62 /// - passManagerSetupFn is the callback invoked to setup the pass manager to
63 ///   apply on the loaded IR.
64 LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
65                           std::unique_ptr<llvm::MemoryBuffer> buffer,
66                           PassPipelineFn passManagerSetupFn,
67                           DialectRegistry &registry, bool splitInputFile,
68                           bool verifyDiagnostics, bool verifyPasses,
69                           bool allowUnregisteredDialects,
70                           bool preloadDialectsInContext = false);
71 
72 /// Implementation for tools like `mlir-opt`.
73 /// - toolName is used for the header displayed by `--help`.
74 /// - registry should contain all the dialects that can be parsed in the source.
75 /// - preloadDialectsInContext will trigger the upfront loading of all
76 ///   dialects from the global registry in the MLIRContext. This option is
77 ///   deprecated and will be removed soon.
78 LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
79                           DialectRegistry &registry,
80                           bool preloadDialectsInContext = false);
81 
82 /// Helper wrapper to return the result of MlirOptMain directly from main.
83 ///
84 /// Example:
85 ///
86 ///     int main(int argc, char **argv) {
87 ///       // ...
88 ///       return mlir::asMainReturnCode(mlir::MlirOptMain(
89 ///           argc, argv, /* ... */);
90 ///     }
91 ///
asMainReturnCode(LogicalResult r)92 inline int asMainReturnCode(LogicalResult r) {
93   return r.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE;
94 }
95 
96 } // namespace mlir
97 
98 #endif // MLIR_TOOLS_MLIROPT_MLIROPTMAIN_H
99