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>
50     showDialects("show-dialects",
51                  llvm::cl::desc("Print the list of registered dialects"),
52                  llvm::cl::init(false));
53 
54 int main(int argc, char **argv) {
55   mlir::registerAllDialects();
56   mlir::registerAllPasses();
57 
58   mlir::registerDialect<mlir::standalone::StandaloneDialect>();
59   // TODO: Register standalone passes here.
60 
61   llvm::InitLLVM y(argc, argv);
62 
63   // Register any pass manager command line options.
64   mlir::registerPassManagerCLOptions();
65   mlir::PassPipelineCLParser passPipeline("", "Compiler passes to run");
66 
67   // Parse pass names in main to ensure static initialization completed.
68   llvm::cl::ParseCommandLineOptions(argc, argv,
69                                     "MLIR modular optimizer driver\n");
70 
71   mlir::MLIRContext context;
72   if (showDialects) {
73     llvm::outs() << "Registered Dialects:\n";
74     for (mlir::Dialect *dialect : context.getRegisteredDialects()) {
75       llvm::outs() << dialect->getNamespace() << "\n";
76     }
77     return 0;
78   }
79 
80   // Set up the input file.
81   std::string errorMessage;
82   auto file = mlir::openInputFile(inputFilename, &errorMessage);
83   if (!file) {
84     llvm::errs() << errorMessage << "\n";
85     return 1;
86   }
87 
88   auto output = mlir::openOutputFile(outputFilename, &errorMessage);
89   if (!output) {
90     llvm::errs() << errorMessage << "\n";
91     exit(1);
92   }
93 
94   return failed(mlir::MlirOptMain(output->os(), std::move(file), passPipeline,
95                                   splitInputFile, verifyDiagnostics,
96                                   verifyPasses));
97 }
98