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