1 //===- toyc.cpp - The Toy Compiler ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the entry point for the Toy compiler.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/IR/Diagnostics.h"
14 #include "mlir/Support/LogicalResult.h"
16 #include "toy/Dialect.h"
17 #include "toy/Lexer.h"
18 #include "toy/MLIRGen.h"
19 #include "toy/Parser.h"
20 #include "toy/Passes.h"
22 #include "mlir/IR/AsmState.h"
23 #include "mlir/IR/BuiltinOps.h"
24 #include "mlir/IR/MLIRContext.h"
25 #include "mlir/IR/Verifier.h"
26 #include "mlir/Parser/Parser.h"
27 #include "mlir/Pass/PassManager.h"
28 #include "mlir/Transforms/Passes.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/ErrorOr.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/SourceMgr.h"
35 #include "llvm/Support/raw_ostream.h"
38 #include <system_error>
42 namespace cl
= llvm::cl
;
44 static cl::opt
<std::string
> inputFilename(cl::Positional
,
45 cl::desc("<input toy file>"),
47 cl::value_desc("filename"));
50 enum InputType
{ Toy
, MLIR
};
52 static cl::opt
<enum InputType
> inputType(
53 "x", cl::init(Toy
), cl::desc("Decided the kind of output desired"),
54 cl::values(clEnumValN(Toy
, "toy", "load the input file as a Toy source.")),
55 cl::values(clEnumValN(MLIR
, "mlir",
56 "load the input file as an MLIR file")));
59 enum Action
{ None
, DumpAST
, DumpMLIR
};
61 static cl::opt
<enum Action
> emitAction(
62 "emit", cl::desc("Select the kind of output desired"),
63 cl::values(clEnumValN(DumpAST
, "ast", "output the AST dump")),
64 cl::values(clEnumValN(DumpMLIR
, "mlir", "output the MLIR dump")));
66 static cl::opt
<bool> enableOpt("opt", cl::desc("Enable optimizations"));
68 /// Returns a Toy AST resulting from parsing the file or a nullptr on error.
69 std::unique_ptr
<toy::ModuleAST
> parseInputFile(llvm::StringRef filename
) {
70 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> fileOrErr
=
71 llvm::MemoryBuffer::getFileOrSTDIN(filename
);
72 if (std::error_code ec
= fileOrErr
.getError()) {
73 llvm::errs() << "Could not open input file: " << ec
.message() << "\n";
76 auto buffer
= fileOrErr
.get()->getBuffer();
77 LexerBuffer
lexer(buffer
.begin(), buffer
.end(), std::string(filename
));
79 return parser
.parseModule();
82 int loadMLIR(llvm::SourceMgr
&sourceMgr
, mlir::MLIRContext
&context
,
83 mlir::OwningOpRef
<mlir::ModuleOp
> &module
) {
84 // Handle '.toy' input to the compiler.
85 if (inputType
!= InputType::MLIR
&&
86 !llvm::StringRef(inputFilename
).endswith(".mlir")) {
87 auto moduleAST
= parseInputFile(inputFilename
);
90 module
= mlirGen(context
, *moduleAST
);
91 return !module
? 1 : 0;
94 // Otherwise, the input is '.mlir'.
95 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> fileOrErr
=
96 llvm::MemoryBuffer::getFileOrSTDIN(inputFilename
);
97 if (std::error_code ec
= fileOrErr
.getError()) {
98 llvm::errs() << "Could not open input file: " << ec
.message() << "\n";
102 // Parse the input mlir.
103 sourceMgr
.AddNewSourceBuffer(std::move(*fileOrErr
), llvm::SMLoc());
104 module
= mlir::parseSourceFile
<mlir::ModuleOp
>(sourceMgr
, &context
);
106 llvm::errs() << "Error can't load file " << inputFilename
<< "\n";
113 mlir::MLIRContext context
;
114 // Load our Dialect in this MLIR Context.
115 context
.getOrLoadDialect
<mlir::toy::ToyDialect
>();
117 mlir::OwningOpRef
<mlir::ModuleOp
> module
;
118 llvm::SourceMgr sourceMgr
;
119 mlir::SourceMgrDiagnosticHandler
sourceMgrHandler(sourceMgr
, &context
);
120 if (int error
= loadMLIR(sourceMgr
, context
, module
))
124 mlir::PassManager
pm(module
.get()->getName());
125 // Apply any generic pass manager command line options and run the pipeline.
126 if (mlir::failed(mlir::applyPassManagerCLOptions(pm
)))
129 // Inline all functions into main and then delete them.
130 pm
.addPass(mlir::createInlinerPass());
132 // Now that there is only one function, we can infer the shapes of each of
134 mlir::OpPassManager
&optPM
= pm
.nest
<mlir::toy::FuncOp
>();
135 optPM
.addPass(mlir::toy::createShapeInferencePass());
136 optPM
.addPass(mlir::createCanonicalizerPass());
137 optPM
.addPass(mlir::createCSEPass());
139 if (mlir::failed(pm
.run(*module
)))
148 if (inputType
== InputType::MLIR
) {
149 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
153 auto moduleAST
= parseInputFile(inputFilename
);
161 int main(int argc
, char **argv
) {
162 // Register any command line options.
163 mlir::registerAsmPrinterCLOptions();
164 mlir::registerMLIRContextCLOptions();
165 mlir::registerPassManagerCLOptions();
167 cl::ParseCommandLineOptions(argc
, argv
, "toy compiler\n");
169 switch (emitAction
) {
170 case Action::DumpAST
:
172 case Action::DumpMLIR
:
175 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";