1 | //===- toyc.cpp - The Toy Compiler ----------------------------------------===// |
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 | // This file implements the entry point for the Toy compiler. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "mlir/Dialect/Func/Extensions/AllExtensions.h" |
14 | #include "mlir/IR/Diagnostics.h" |
15 | #include "mlir/Support/LogicalResult.h" |
16 | #include "toy/AST.h" |
17 | #include "toy/Dialect.h" |
18 | #include "toy/Lexer.h" |
19 | #include "toy/MLIRGen.h" |
20 | #include "toy/Parser.h" |
21 | #include "toy/Passes.h" |
22 | |
23 | #include "mlir/Dialect/Affine/Passes.h" |
24 | #include "mlir/IR/AsmState.h" |
25 | #include "mlir/IR/BuiltinOps.h" |
26 | #include "mlir/IR/MLIRContext.h" |
27 | #include "mlir/IR/Verifier.h" |
28 | #include "mlir/InitAllDialects.h" |
29 | #include "mlir/Parser/Parser.h" |
30 | #include "mlir/Pass/PassManager.h" |
31 | #include "mlir/Transforms/Passes.h" |
32 | |
33 | #include "llvm/ADT/StringRef.h" |
34 | #include "llvm/Support/CommandLine.h" |
35 | #include "llvm/Support/ErrorOr.h" |
36 | #include "llvm/Support/MemoryBuffer.h" |
37 | #include "llvm/Support/SourceMgr.h" |
38 | #include "llvm/Support/raw_ostream.h" |
39 | #include <memory> |
40 | #include <string> |
41 | #include <system_error> |
42 | #include <utility> |
43 | |
44 | using namespace toy; |
45 | namespace cl = llvm::cl; |
46 | |
47 | static cl::opt<std::string> inputFilename(cl::Positional, |
48 | cl::desc("<input toy file>" ), |
49 | cl::init(Val: "-" ), |
50 | cl::value_desc("filename" )); |
51 | |
52 | namespace { |
53 | enum InputType { Toy, MLIR }; |
54 | } // namespace |
55 | static cl::opt<enum InputType> inputType( |
56 | "x" , cl::init(Val: Toy), cl::desc("Decided the kind of output desired" ), |
57 | cl::values(clEnumValN(Toy, "toy" , "load the input file as a Toy source." )), |
58 | cl::values(clEnumValN(MLIR, "mlir" , |
59 | "load the input file as an MLIR file" ))); |
60 | |
61 | namespace { |
62 | enum Action { None, DumpAST, DumpMLIR, DumpMLIRAffine }; |
63 | } // namespace |
64 | static cl::opt<enum Action> emitAction( |
65 | "emit" , cl::desc("Select the kind of output desired" ), |
66 | cl::values(clEnumValN(DumpAST, "ast" , "output the AST dump" )), |
67 | cl::values(clEnumValN(DumpMLIR, "mlir" , "output the MLIR dump" )), |
68 | cl::values(clEnumValN(DumpMLIRAffine, "mlir-affine" , |
69 | "output the MLIR dump after affine lowering" ))); |
70 | |
71 | static cl::opt<bool> enableOpt("opt" , cl::desc("Enable optimizations" )); |
72 | |
73 | /// Returns a Toy AST resulting from parsing the file or a nullptr on error. |
74 | std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) { |
75 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr = |
76 | llvm::MemoryBuffer::getFileOrSTDIN(Filename: filename); |
77 | if (std::error_code ec = fileOrErr.getError()) { |
78 | llvm::errs() << "Could not open input file: " << ec.message() << "\n" ; |
79 | return nullptr; |
80 | } |
81 | auto buffer = fileOrErr.get()->getBuffer(); |
82 | LexerBuffer lexer(buffer.begin(), buffer.end(), std::string(filename)); |
83 | Parser parser(lexer); |
84 | return parser.parseModule(); |
85 | } |
86 | |
87 | int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context, |
88 | mlir::OwningOpRef<mlir::ModuleOp> &module) { |
89 | // Handle '.toy' input to the compiler. |
90 | if (inputType != InputType::MLIR && |
91 | !llvm::StringRef(inputFilename).ends_with(Suffix: ".mlir" )) { |
92 | auto moduleAST = parseInputFile(filename: inputFilename); |
93 | if (!moduleAST) |
94 | return 6; |
95 | module = mlirGen(context, moduleAST&: *moduleAST); |
96 | return !module ? 1 : 0; |
97 | } |
98 | |
99 | // Otherwise, the input is '.mlir'. |
100 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr = |
101 | llvm::MemoryBuffer::getFileOrSTDIN(Filename: inputFilename); |
102 | if (std::error_code ec = fileOrErr.getError()) { |
103 | llvm::errs() << "Could not open input file: " << ec.message() << "\n" ; |
104 | return -1; |
105 | } |
106 | |
107 | // Parse the input mlir. |
108 | sourceMgr.AddNewSourceBuffer(F: std::move(*fileOrErr), IncludeLoc: llvm::SMLoc()); |
109 | module = mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, config: &context); |
110 | if (!module) { |
111 | llvm::errs() << "Error can't load file " << inputFilename << "\n" ; |
112 | return 3; |
113 | } |
114 | return 0; |
115 | } |
116 | |
117 | int dumpMLIR() { |
118 | mlir::DialectRegistry registry; |
119 | mlir::func::registerAllExtensions(registry); |
120 | |
121 | mlir::MLIRContext context(registry); |
122 | // Load our Dialect in this MLIR Context. |
123 | context.getOrLoadDialect<mlir::toy::ToyDialect>(); |
124 | |
125 | mlir::OwningOpRef<mlir::ModuleOp> module; |
126 | llvm::SourceMgr sourceMgr; |
127 | mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context); |
128 | if (int error = loadMLIR(sourceMgr, context, module)) |
129 | return error; |
130 | |
131 | mlir::PassManager pm(module.get()->getName()); |
132 | // Apply any generic pass manager command line options and run the pipeline. |
133 | if (mlir::failed(result: mlir::applyPassManagerCLOptions(pm))) |
134 | return 4; |
135 | |
136 | // Check to see what granularity of MLIR we are compiling to. |
137 | bool isLoweringToAffine = emitAction >= Action::DumpMLIRAffine; |
138 | |
139 | if (enableOpt || isLoweringToAffine) { |
140 | // Inline all functions into main and then delete them. |
141 | pm.addPass(mlir::pass: createInlinerPass()); |
142 | |
143 | // Now that there is only one function, we can infer the shapes of each of |
144 | // the operations. |
145 | mlir::OpPassManager &optPM = pm.nest<mlir::toy::FuncOp>(); |
146 | optPM.addPass(pass: mlir::toy::createShapeInferencePass()); |
147 | optPM.addPass(pass: mlir::createCanonicalizerPass()); |
148 | optPM.addPass(pass: mlir::createCSEPass()); |
149 | } |
150 | |
151 | if (isLoweringToAffine) { |
152 | // Partially lower the toy dialect. |
153 | pm.addPass(pass: mlir::toy::createLowerToAffinePass()); |
154 | |
155 | // Add a few cleanups post lowering. |
156 | mlir::OpPassManager &optPM = pm.nest<mlir::func::FuncOp>(); |
157 | optPM.addPass(pass: mlir::createCanonicalizerPass()); |
158 | optPM.addPass(pass: mlir::createCSEPass()); |
159 | |
160 | // Add optimizations if enabled. |
161 | if (enableOpt) { |
162 | optPM.addPass(mlir::affine::pass: createLoopFusionPass()); |
163 | optPM.addPass(mlir::affine::pass: createAffineScalarReplacementPass()); |
164 | } |
165 | } |
166 | |
167 | if (mlir::failed(result: pm.run(op: *module))) |
168 | return 4; |
169 | |
170 | module->dump(); |
171 | return 0; |
172 | } |
173 | |
174 | int dumpAST() { |
175 | if (inputType == InputType::MLIR) { |
176 | llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n" ; |
177 | return 5; |
178 | } |
179 | |
180 | auto moduleAST = parseInputFile(filename: inputFilename); |
181 | if (!moduleAST) |
182 | return 1; |
183 | |
184 | dump(*moduleAST); |
185 | return 0; |
186 | } |
187 | |
188 | int main(int argc, char **argv) { |
189 | // Register any command line options. |
190 | mlir::registerAsmPrinterCLOptions(); |
191 | mlir::registerMLIRContextCLOptions(); |
192 | mlir::registerPassManagerCLOptions(); |
193 | |
194 | cl::ParseCommandLineOptions(argc, argv, Overview: "toy compiler\n" ); |
195 | |
196 | switch (emitAction) { |
197 | case Action::DumpAST: |
198 | return dumpAST(); |
199 | case Action::DumpMLIR: |
200 | case Action::DumpMLIRAffine: |
201 | return dumpMLIR(); |
202 | default: |
203 | llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n" ; |
204 | } |
205 | |
206 | return 0; |
207 | } |
208 | |