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