1 //===- standalone-opt.cpp ---------------------------------------*- C++ -*-===//
2 //
3 // This file is licensed 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 #include "mlir/IR/Dialect.h"
10 #include "mlir/IR/MLIRContext.h"
11 #include "mlir/InitAllDialects.h"
12 #include "mlir/InitAllPasses.h"
13 #include "mlir/Pass/Pass.h"
14 #include "mlir/Pass/PassManager.h"
15 #include "mlir/Support/FileUtilities.h"
16 #include "mlir/Support/MlirOptMain.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/InitLLVM.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 
22 #include "Standalone/StandaloneDialect.h"
23 
24 static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
25                                                 llvm::cl::desc("<input file>"),
26                                                 llvm::cl::init("-"));
27 
28 static llvm::cl::opt<std::string>
29     outputFilename("o", llvm::cl::desc("Output filename"),
30                    llvm::cl::value_desc("filename"), llvm::cl::init("-"));
31 
32 static llvm::cl::opt<bool> splitInputFile(
33     "split-input-file",
34     llvm::cl::desc("Split the input file into pieces and process each "
35                    "chunk independently"),
36     llvm::cl::init(false));
37 
38 static llvm::cl::opt<bool> verifyDiagnostics(
39     "verify-diagnostics",
40     llvm::cl::desc("Check that emitted diagnostics match "
41                    "expected-* lines on the corresponding line"),
42     llvm::cl::init(false));
43 
44 static llvm::cl::opt<bool> verifyPasses(
45     "verify-each",
46     llvm::cl::desc("Run the verifier after each transformation pass"),
47     llvm::cl::init(true));
48 
49 static llvm::cl::opt<bool> allowUnregisteredDialects(
50     "allow-unregistered-dialect",
51     llvm::cl::desc("Allow operation with no registered dialects"),
52     llvm::cl::init(false));
53 
54 static llvm::cl::opt<bool>
55     showDialects("show-dialects",
56                  llvm::cl::desc("Print the list of registered dialects"),
57                  llvm::cl::init(false));
58 
59 int main(int argc, char **argv) {
60   mlir::registerAllDialects();
61   mlir::registerAllPasses();
62 
63   mlir::registerDialect<mlir::standalone::StandaloneDialect>();
64   // TODO: Register standalone passes here.
65 
66   llvm::InitLLVM y(argc, argv);
67 
68   // Register any pass manager command line options.
69   mlir::registerPassManagerCLOptions();
70   mlir::PassPipelineCLParser passPipeline("", "Compiler passes to run");
71 
72   // Parse pass names in main to ensure static initialization completed.
73   llvm::cl::ParseCommandLineOptions(argc, argv,
74                                     "MLIR modular optimizer driver\n");
75 
76   mlir::MLIRContext context;
77   if (showDialects) {
78     llvm::outs() << "Registered Dialects:\n";
79     for (mlir::Dialect *dialect : context.getRegisteredDialects()) {
80       llvm::outs() << dialect->getNamespace() << "\n";
81     }
82     return 0;
83   }
84 
85   // Set up the input file.
86   std::string errorMessage;
87   auto file = mlir::openInputFile(inputFilename, &errorMessage);
88   if (!file) {
89     llvm::errs() << errorMessage << "\n";
90     return 1;
91   }
92 
93   auto output = mlir::openOutputFile(outputFilename, &errorMessage);
94   if (!output) {
95     llvm::errs() << errorMessage << "\n";
96     exit(1);
97   }
98 
99   if (failed(MlirOptMain(output->os(), std::move(file), passPipeline,
100                          splitInputFile, verifyDiagnostics, verifyPasses,
101                          allowUnregisteredDialects))) {
102     return 1;
103   }
104   // Keep the output file if the invocation of MlirOptMain was successful.
105   output->keep();
106   return 0;
107 }
108