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"
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"
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"
37 #include <system_error>
41 namespace cl
= llvm::cl
;
43 static cl::opt
<std::string
> inputFilename(cl::Positional
,
44 cl::desc("<input toy file>"),
46 cl::value_desc("filename"));
49 enum InputType
{ Toy
, MLIR
};
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")));
58 enum Action
{ None
, DumpAST
, DumpMLIR
};
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")));
65 static cl::opt
<bool> enableOpt("opt", cl::desc("Enable optimizations"));
67 /// Returns a Toy AST resulting from parsing the file or a nullptr on error.
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";
75 auto buffer
= fileOrErr
.get()->getBuffer();
76 LexerBuffer
lexer(buffer
.begin(), buffer
.end(), std::string(filename
));
78 return parser
.parseModule();
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
).endswith(".mlir")) {
86 auto moduleAST
= parseInputFile(inputFilename
);
89 module
= mlirGen(context
, *moduleAST
);
90 return !module
? 1 : 0;
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";
101 // Parse the input mlir.
102 sourceMgr
.AddNewSourceBuffer(std::move(*fileOrErr
), llvm::SMLoc());
103 module
= mlir::parseSourceFile
<mlir::ModuleOp
>(sourceMgr
, &context
);
105 llvm::errs() << "Error can't load file " << inputFilename
<< "\n";
112 mlir::MLIRContext context
;
113 // Load our Dialect in this MLIR Context.
114 context
.getOrLoadDialect
<mlir::toy::ToyDialect
>();
116 mlir::OwningOpRef
<mlir::ModuleOp
> module
;
117 llvm::SourceMgr sourceMgr
;
118 mlir::SourceMgrDiagnosticHandler
sourceMgrHandler(sourceMgr
, &context
);
119 if (int error
= loadMLIR(sourceMgr
, context
, module
))
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
)))
128 // Add a run of the canonicalizer to optimize the mlir module.
129 pm
.addNestedPass
<mlir::toy::FuncOp
>(mlir::createCanonicalizerPass());
130 if (mlir::failed(pm
.run(*module
)))
139 if (inputType
== InputType::MLIR
) {
140 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
144 auto moduleAST
= parseInputFile(inputFilename
);
152 int main(int argc
, char **argv
) {
153 // Register any command line options.
154 mlir::registerAsmPrinterCLOptions();
155 mlir::registerMLIRContextCLOptions();
156 mlir::registerPassManagerCLOptions();
158 cl::ParseCommandLineOptions(argc
, argv
, "toy compiler\n");
160 switch (emitAction
) {
161 case Action::DumpAST
:
163 case Action::DumpMLIR
:
166 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";