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 "toy/Dialect.h"
14 #include "toy/MLIRGen.h"
15 #include "toy/Parser.h"
16 #include "toy/Passes.h"
17
18 #include "mlir/ExecutionEngine/ExecutionEngine.h"
19 #include "mlir/ExecutionEngine/OptUtils.h"
20 #include "mlir/IR/AsmState.h"
21 #include "mlir/IR/BuiltinOps.h"
22 #include "mlir/IR/MLIRContext.h"
23 #include "mlir/IR/Verifier.h"
24 #include "mlir/InitAllDialects.h"
25 #include "mlir/Parser.h"
26 #include "mlir/Pass/Pass.h"
27 #include "mlir/Pass/PassManager.h"
28 #include "mlir/Target/LLVMIR.h"
29 #include "mlir/Transforms/Passes.h"
30
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorOr.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/SourceMgr.h"
37 #include "llvm/Support/TargetSelect.h"
38 #include "llvm/Support/raw_ostream.h"
39
40 using namespace toy;
41 namespace cl = llvm::cl;
42
43 static cl::opt<std::string> inputFilename(cl::Positional,
44 cl::desc("<input toy file>"),
45 cl::init("-"),
46 cl::value_desc("filename"));
47
48 namespace {
49 enum InputType { Toy, MLIR };
50 }
51 static cl::opt<enum InputType> inputType(
52 "x", cl::init(Toy), cl::desc("Decided the kind of output desired"),
53 cl::values(clEnumValN(Toy, "toy", "load the input file as a Toy source.")),
54 cl::values(clEnumValN(MLIR, "mlir",
55 "load the input file as an MLIR file")));
56
57 namespace {
58 enum Action {
59 None,
60 DumpAST,
61 DumpMLIR,
62 DumpMLIRAffine,
63 DumpMLIRLLVM,
64 DumpLLVMIR,
65 RunJIT
66 };
67 }
68 static cl::opt<enum Action> emitAction(
69 "emit", cl::desc("Select the kind of output desired"),
70 cl::values(clEnumValN(DumpAST, "ast", "output the AST dump")),
71 cl::values(clEnumValN(DumpMLIR, "mlir", "output the MLIR dump")),
72 cl::values(clEnumValN(DumpMLIRAffine, "mlir-affine",
73 "output the MLIR dump after affine lowering")),
74 cl::values(clEnumValN(DumpMLIRLLVM, "mlir-llvm",
75 "output the MLIR dump after llvm lowering")),
76 cl::values(clEnumValN(DumpLLVMIR, "llvm", "output the LLVM IR dump")),
77 cl::values(
78 clEnumValN(RunJIT, "jit",
79 "JIT the code and run it by invoking the main function")));
80
81 static cl::opt<bool> enableOpt("opt", cl::desc("Enable optimizations"));
82
83 /// Returns a Toy AST resulting from parsing the file or a nullptr on error.
parseInputFile(llvm::StringRef filename)84 std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
85 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
86 llvm::MemoryBuffer::getFileOrSTDIN(filename);
87 if (std::error_code ec = fileOrErr.getError()) {
88 llvm::errs() << "Could not open input file: " << ec.message() << "\n";
89 return nullptr;
90 }
91 auto buffer = fileOrErr.get()->getBuffer();
92 LexerBuffer lexer(buffer.begin(), buffer.end(), std::string(filename));
93 Parser parser(lexer);
94 return parser.parseModule();
95 }
96
loadMLIR(mlir::MLIRContext & context,mlir::OwningModuleRef & module)97 int loadMLIR(mlir::MLIRContext &context, mlir::OwningModuleRef &module) {
98 // Handle '.toy' input to the compiler.
99 if (inputType != InputType::MLIR &&
100 !llvm::StringRef(inputFilename).endswith(".mlir")) {
101 auto moduleAST = parseInputFile(inputFilename);
102 if (!moduleAST)
103 return 6;
104 module = mlirGen(context, *moduleAST);
105 return !module ? 1 : 0;
106 }
107
108 // Otherwise, the input is '.mlir'.
109 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
110 llvm::MemoryBuffer::getFileOrSTDIN(inputFilename);
111 if (std::error_code EC = fileOrErr.getError()) {
112 llvm::errs() << "Could not open input file: " << EC.message() << "\n";
113 return -1;
114 }
115
116 // Parse the input mlir.
117 llvm::SourceMgr sourceMgr;
118 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc());
119 module = mlir::parseSourceFile(sourceMgr, &context);
120 if (!module) {
121 llvm::errs() << "Error can't load file " << inputFilename << "\n";
122 return 3;
123 }
124 return 0;
125 }
126
loadAndProcessMLIR(mlir::MLIRContext & context,mlir::OwningModuleRef & module)127 int loadAndProcessMLIR(mlir::MLIRContext &context,
128 mlir::OwningModuleRef &module) {
129 if (int error = loadMLIR(context, module))
130 return error;
131
132 mlir::PassManager pm(&context);
133 // Apply any generic pass manager command line options and run the pipeline.
134 applyPassManagerCLOptions(pm);
135
136 // Check to see what granularity of MLIR we are compiling to.
137 bool isLoweringToAffine = emitAction >= Action::DumpMLIRAffine;
138 bool isLoweringToLLVM = emitAction >= Action::DumpMLIRLLVM;
139
140 if (enableOpt || isLoweringToAffine) {
141 // Inline all functions into main and then delete them.
142 pm.addPass(mlir::createInlinerPass());
143
144 // Now that there is only one function, we can infer the shapes of each of
145 // the operations.
146 mlir::OpPassManager &optPM = pm.nest<mlir::FuncOp>();
147 optPM.addPass(mlir::toy::createShapeInferencePass());
148 optPM.addPass(mlir::createCanonicalizerPass());
149 optPM.addPass(mlir::createCSEPass());
150 }
151
152 if (isLoweringToAffine) {
153 mlir::OpPassManager &optPM = pm.nest<mlir::FuncOp>();
154
155 // Partially lower the toy dialect with a few cleanups afterwards.
156 optPM.addPass(mlir::toy::createLowerToAffinePass());
157 optPM.addPass(mlir::createCanonicalizerPass());
158 optPM.addPass(mlir::createCSEPass());
159
160 // Add optimizations if enabled.
161 if (enableOpt) {
162 optPM.addPass(mlir::createLoopFusionPass());
163 optPM.addPass(mlir::createMemRefDataFlowOptPass());
164 }
165 }
166
167 if (isLoweringToLLVM) {
168 // Finish lowering the toy IR to the LLVM dialect.
169 pm.addPass(mlir::toy::createLowerToLLVMPass());
170 }
171
172 if (mlir::failed(pm.run(*module)))
173 return 4;
174 return 0;
175 }
176
dumpAST()177 int dumpAST() {
178 if (inputType == InputType::MLIR) {
179 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
180 return 5;
181 }
182
183 auto moduleAST = parseInputFile(inputFilename);
184 if (!moduleAST)
185 return 1;
186
187 dump(*moduleAST);
188 return 0;
189 }
190
dumpLLVMIR(mlir::ModuleOp module)191 int dumpLLVMIR(mlir::ModuleOp module) {
192 // Convert the module to LLVM IR in a new LLVM IR context.
193 llvm::LLVMContext llvmContext;
194 auto llvmModule = mlir::translateModuleToLLVMIR(module, llvmContext);
195 if (!llvmModule) {
196 llvm::errs() << "Failed to emit LLVM IR\n";
197 return -1;
198 }
199
200 // Initialize LLVM targets.
201 llvm::InitializeNativeTarget();
202 llvm::InitializeNativeTargetAsmPrinter();
203 mlir::ExecutionEngine::setupTargetTriple(llvmModule.get());
204
205 /// Optionally run an optimization pipeline over the llvm module.
206 auto optPipeline = mlir::makeOptimizingTransformer(
207 /*optLevel=*/enableOpt ? 3 : 0, /*sizeLevel=*/0,
208 /*targetMachine=*/nullptr);
209 if (auto err = optPipeline(llvmModule.get())) {
210 llvm::errs() << "Failed to optimize LLVM IR " << err << "\n";
211 return -1;
212 }
213 llvm::errs() << *llvmModule << "\n";
214 return 0;
215 }
216
runJit(mlir::ModuleOp module)217 int runJit(mlir::ModuleOp module) {
218 // Initialize LLVM targets.
219 llvm::InitializeNativeTarget();
220 llvm::InitializeNativeTargetAsmPrinter();
221
222 // An optimization pipeline to use within the execution engine.
223 auto optPipeline = mlir::makeOptimizingTransformer(
224 /*optLevel=*/enableOpt ? 3 : 0, /*sizeLevel=*/0,
225 /*targetMachine=*/nullptr);
226
227 // Create an MLIR execution engine. The execution engine eagerly JIT-compiles
228 // the module.
229 auto maybeEngine = mlir::ExecutionEngine::create(
230 module, /*llvmModuleBuilder=*/nullptr, optPipeline);
231 assert(maybeEngine && "failed to construct an execution engine");
232 auto &engine = maybeEngine.get();
233
234 // Invoke the JIT-compiled function.
235 auto invocationResult = engine->invoke("main");
236 if (invocationResult) {
237 llvm::errs() << "JIT invocation failed\n";
238 return -1;
239 }
240
241 return 0;
242 }
243
main(int argc,char ** argv)244 int main(int argc, char **argv) {
245 // Register any command line options.
246 mlir::registerAsmPrinterCLOptions();
247 mlir::registerMLIRContextCLOptions();
248 mlir::registerPassManagerCLOptions();
249
250 cl::ParseCommandLineOptions(argc, argv, "toy compiler\n");
251
252 if (emitAction == Action::DumpAST)
253 return dumpAST();
254
255 // If we aren't dumping the AST, then we are compiling with/to MLIR.
256
257 mlir::MLIRContext context;
258 // Load our Dialect in this MLIR Context.
259 context.getOrLoadDialect<mlir::toy::ToyDialect>();
260
261 mlir::OwningModuleRef module;
262 if (int error = loadAndProcessMLIR(context, module))
263 return error;
264
265 // If we aren't exporting to non-mlir, then we are done.
266 bool isOutputingMLIR = emitAction <= Action::DumpMLIRLLVM;
267 if (isOutputingMLIR) {
268 module->dump();
269 return 0;
270 }
271
272 // Check to see if we are compiling to LLVM IR.
273 if (emitAction == Action::DumpLLVMIR)
274 return dumpLLVMIR(*module);
275
276 // Otherwise, we must be running the jit.
277 if (emitAction == Action::RunJIT)
278 return runJit(*module);
279
280 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";
281 return -1;
282 }
283