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/IR/Diagnostics.h"
14 #include "toy/AST.h"
15 #include "toy/Dialect.h"
16 #include "toy/Lexer.h"
17 #include "toy/MLIRGen.h"
18 #include "toy/Parser.h"
19 #include "toy/Passes.h"
20
21 #include "mlir/IR/AsmState.h"
22 #include "mlir/IR/BuiltinOps.h"
23 #include "mlir/IR/MLIRContext.h"
24 #include "mlir/IR/Verifier.h"
25 #include "mlir/Parser/Parser.h"
26 #include "mlir/Pass/PassManager.h"
27 #include "mlir/Transforms/Passes.h"
28
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/ErrorOr.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/SourceMgr.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <memory>
36 #include <string>
37 #include <system_error>
38 #include <utility>
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 } // namespace
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 { None, DumpAST, DumpMLIR };
59 } // namespace
60 static cl::opt<enum Action> emitAction(
61 "emit", cl::desc("Select the kind of output desired"),
62 cl::values(clEnumValN(DumpAST, "ast", "output the AST dump")),
63 cl::values(clEnumValN(DumpMLIR, "mlir", "output the MLIR dump")));
64
65 static cl::opt<bool> enableOpt("opt", cl::desc("Enable optimizations"));
66
67 /// Returns a Toy AST resulting from parsing the file or a nullptr on error.
parseInputFile(llvm::StringRef filename)68 std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
69 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
70 llvm::MemoryBuffer::getFileOrSTDIN(filename);
71 if (std::error_code ec = fileOrErr.getError()) {
72 llvm::errs() << "Could not open input file: " << ec.message() << "\n";
73 return nullptr;
74 }
75 auto buffer = fileOrErr.get()->getBuffer();
76 LexerBuffer lexer(buffer.begin(), buffer.end(), std::string(filename));
77 Parser parser(lexer);
78 return parser.parseModule();
79 }
80
loadMLIR(llvm::SourceMgr & sourceMgr,mlir::MLIRContext & context,mlir::OwningOpRef<mlir::ModuleOp> & module)81 int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
82 mlir::OwningOpRef<mlir::ModuleOp> &module) {
83 // Handle '.toy' input to the compiler.
84 if (inputType != InputType::MLIR &&
85 !llvm::StringRef(inputFilename).ends_with(".mlir")) {
86 auto moduleAST = parseInputFile(inputFilename);
87 if (!moduleAST)
88 return 6;
89 module = mlirGen(context, *moduleAST);
90 return !module ? 1 : 0;
91 }
92
93 // Otherwise, the input is '.mlir'.
94 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
95 llvm::MemoryBuffer::getFileOrSTDIN(inputFilename);
96 if (std::error_code ec = fileOrErr.getError()) {
97 llvm::errs() << "Could not open input file: " << ec.message() << "\n";
98 return -1;
99 }
100
101 // Parse the input mlir.
102 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc());
103 module = mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
104 if (!module) {
105 llvm::errs() << "Error can't load file " << inputFilename << "\n";
106 return 3;
107 }
108 return 0;
109 }
110
dumpMLIR()111 int dumpMLIR() {
112 mlir::MLIRContext context;
113 // Load our Dialect in this MLIR Context.
114 context.getOrLoadDialect<mlir::toy::ToyDialect>();
115
116 mlir::OwningOpRef<mlir::ModuleOp> module;
117 llvm::SourceMgr sourceMgr;
118 mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
119 if (int error = loadMLIR(sourceMgr, context, module))
120 return error;
121
122 if (enableOpt) {
123 mlir::PassManager pm(module.get()->getName());
124 // Apply any generic pass manager command line options and run the pipeline.
125 if (mlir::failed(mlir::applyPassManagerCLOptions(pm)))
126 return 4;
127
128 // Inline all functions into main and then delete them.
129 pm.addPass(mlir::createInlinerPass());
130
131 // Now that there is only one function, we can infer the shapes of each of
132 // the operations.
133 mlir::OpPassManager &optPM = pm.nest<mlir::toy::FuncOp>();
134 optPM.addPass(mlir::toy::createShapeInferencePass());
135 optPM.addPass(mlir::createCanonicalizerPass());
136 optPM.addPass(mlir::createCSEPass());
137
138 if (mlir::failed(pm.run(*module)))
139 return 4;
140 }
141
142 module->dump();
143 return 0;
144 }
145
dumpAST()146 int dumpAST() {
147 if (inputType == InputType::MLIR) {
148 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
149 return 5;
150 }
151
152 auto moduleAST = parseInputFile(inputFilename);
153 if (!moduleAST)
154 return 1;
155
156 dump(*moduleAST);
157 return 0;
158 }
159
main(int argc,char ** argv)160 int main(int argc, char **argv) {
161 // Register any command line options.
162 mlir::registerAsmPrinterCLOptions();
163 mlir::registerMLIRContextCLOptions();
164 mlir::registerPassManagerCLOptions();
165
166 cl::ParseCommandLineOptions(argc, argv, "toy compiler\n");
167
168 switch (emitAction) {
169 case Action::DumpAST:
170 return dumpAST();
171 case Action::DumpMLIR:
172 return dumpMLIR();
173 default:
174 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";
175 }
176
177 return 0;
178 }
179