1 //===- mlir-opt.cpp - MLIR Optimizer Driver -------------------------------===//
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 #include "mlir/InitAllDialects.h"
14 #include "mlir/InitAllPasses.h"
15 #include "mlir/IR/Dialect.h"
16 #include "mlir/IR/MLIRContext.h"
17 #include "mlir/Pass/Pass.h"
18 #include "mlir/Pass/PassManager.h"
19 #include "mlir/Support/FileUtilities.h"
20 #include "mlir/Support/MlirOptMain.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/InitLLVM.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/ToolOutputFile.h"
25 
26 using namespace llvm;
27 using namespace mlir;
28 
29 namespace mlir {
30 // Defined in the test directory, no public header.
31 void registerConvertToTargetEnvPass();
32 void registerInliner();
33 void registerMemRefBoundCheck();
34 void registerPassManagerTestPass();
35 void registerPatternsTestPass();
36 void registerPrintOpAvailabilityPass();
37 void registerSideEffectTestPasses();
38 void registerSimpleParametricTilingPass();
39 void registerSymbolTestPasses();
40 void registerTestAffineDataCopyPass();
41 void registerTestAllReduceLoweringPass();
42 void registerTestLoopPermutationPass();
43 void registerTestCallGraphPass();
44 void registerTestConstantFold();
45 void registerTestConvertGPUKernelToCubinPass();
46 void registerTestDominancePass();
47 void registerTestFunc();
48 void registerTestGpuMemoryPromotionPass();
49 void registerTestLinalgTransforms();
50 void registerTestLivenessPass();
51 void registerTestLoopFusion();
52 void registerTestLoopMappingPass();
53 void registerTestMatchers();
54 void registerTestMemRefDependenceCheck();
55 void registerTestMemRefStrideCalculation();
56 void registerTestOpaqueLoc();
57 void registerTestParallelismDetection();
58 void registerTestGpuParallelLoopMappingPass();
59 void registerTestVectorConversions();
60 void registerTestVectorToLoopsPass();
61 void registerVectorizerTestPass();
62 } // namespace mlir
63 
64 static cl::opt<std::string>
65     inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
66 
67 static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
68                                            cl::value_desc("filename"),
69                                            cl::init("-"));
70 
71 static cl::opt<bool>
72     splitInputFile("split-input-file",
73                    cl::desc("Split the input file into pieces and process each "
74                             "chunk independently"),
75                    cl::init(false));
76 
77 static cl::opt<bool>
78     verifyDiagnostics("verify-diagnostics",
79                       cl::desc("Check that emitted diagnostics match "
80                                "expected-* lines on the corresponding line"),
81                       cl::init(false));
82 
83 static cl::opt<bool>
84     verifyPasses("verify-each",
85                  cl::desc("Run the verifier after each transformation pass"),
86                  cl::init(true));
87 
88 static cl::opt<bool> allowUnregisteredDialects(
89     "allow-unregistered-dialect",
90     cl::desc("Allow operation with no registered dialects"), cl::init(false));
91 
92 void registerTestPasses() {
93   registerConvertToTargetEnvPass();
94   registerInliner();
95   registerMemRefBoundCheck();
96   registerPassManagerTestPass();
97   registerPatternsTestPass();
98   registerPrintOpAvailabilityPass();
99   registerSideEffectTestPasses();
100   registerSimpleParametricTilingPass();
101   registerSymbolTestPasses();
102   registerTestAffineDataCopyPass();
103   registerTestAllReduceLoweringPass();
104   registerTestLoopPermutationPass();
105   registerTestCallGraphPass();
106   registerTestConstantFold();
107 #if MLIR_CUDA_CONVERSIONS_ENABLED
108   registerTestConvertGPUKernelToCubinPass();
109 #endif
110   registerTestDominancePass();
111   registerTestFunc();
112   registerTestGpuMemoryPromotionPass();
113   registerTestLinalgTransforms();
114   registerTestLivenessPass();
115   registerTestLoopFusion();
116   registerTestLoopMappingPass();
117   registerTestMatchers();
118   registerTestMemRefDependenceCheck();
119   registerTestMemRefStrideCalculation();
120   registerTestOpaqueLoc();
121   registerTestParallelismDetection();
122   registerTestGpuParallelLoopMappingPass();
123   registerTestVectorConversions();
124   registerTestVectorToLoopsPass();
125   registerVectorizerTestPass();
126 }
127 
128 static cl::opt<bool>
129     showDialects("show-dialects",
130                  cl::desc("Print the list of registered dialects"),
131                  cl::init(false));
132 
133 int main(int argc, char **argv) {
134   registerAllDialects();
135   registerAllPasses();
136   registerTestPasses();
137   InitLLVM y(argc, argv);
138 
139   // Register any pass manager command line options.
140   registerPassManagerCLOptions();
141   PassPipelineCLParser passPipeline("", "Compiler passes to run");
142 
143   // Parse pass names in main to ensure static initialization completed.
144   cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
145 
146   MLIRContext context;
147   if(showDialects) {
148     llvm::outs() << "Registered Dialects:\n";
149     for(Dialect *dialect : context.getRegisteredDialects()) {
150       llvm::outs() << dialect->getNamespace() << "\n";
151     }
152     return 0;
153   }
154 
155   // Set up the input file.
156   std::string errorMessage;
157   auto file = openInputFile(inputFilename, &errorMessage);
158   if (!file) {
159     llvm::errs() << errorMessage << "\n";
160     return 1;
161   }
162 
163   auto output = openOutputFile(outputFilename, &errorMessage);
164   if (!output) {
165     llvm::errs() << errorMessage << "\n";
166     exit(1);
167   }
168 
169   return failed(MlirOptMain(output->os(), std::move(file), passPipeline,
170                             splitInputFile, verifyDiagnostics, verifyPasses,
171                             allowUnregisteredDialects));
172 }
173