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/Dialect/Affine/Passes.h" 19 #include "mlir/ExecutionEngine/ExecutionEngine.h" 20 #include "mlir/ExecutionEngine/OptUtils.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/InitAllDialects.h" 26 #include "mlir/Parser/Parser.h" 27 #include "mlir/Pass/Pass.h" 28 #include "mlir/Pass/PassManager.h" 29 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" 30 #include "mlir/Target/LLVMIR/Export.h" 31 #include "mlir/Transforms/Passes.h" 32 33 #include "llvm/ADT/StringRef.h" 34 #include "llvm/IR/Module.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/ErrorOr.h" 37 #include "llvm/Support/MemoryBuffer.h" 38 #include "llvm/Support/SourceMgr.h" 39 #include "llvm/Support/TargetSelect.h" 40 #include "llvm/Support/raw_ostream.h" 41 42 using namespace toy; 43 namespace cl = llvm::cl; 44 45 static cl::opt<std::string> inputFilename(cl::Positional, 46 cl::desc("<input toy file>"), 47 cl::init("-"), 48 cl::value_desc("filename")); 49 50 namespace { 51 enum InputType { Toy, MLIR }; 52 } // namespace 53 static cl::opt<enum InputType> inputType( 54 "x", cl::init(Toy), cl::desc("Decided the kind of output desired"), 55 cl::values(clEnumValN(Toy, "toy", "load the input file as a Toy source.")), 56 cl::values(clEnumValN(MLIR, "mlir", 57 "load the input file as an MLIR file"))); 58 59 namespace { 60 enum Action { 61 None, 62 DumpAST, 63 DumpMLIR, 64 DumpMLIRAffine, 65 DumpMLIRLLVM, 66 DumpLLVMIR, 67 RunJIT 68 }; 69 } // namespace 70 static cl::opt<enum Action> emitAction( 71 "emit", cl::desc("Select the kind of output desired"), 72 cl::values(clEnumValN(DumpAST, "ast", "output the AST dump")), 73 cl::values(clEnumValN(DumpMLIR, "mlir", "output the MLIR dump")), 74 cl::values(clEnumValN(DumpMLIRAffine, "mlir-affine", 75 "output the MLIR dump after affine lowering")), 76 cl::values(clEnumValN(DumpMLIRLLVM, "mlir-llvm", 77 "output the MLIR dump after llvm lowering")), 78 cl::values(clEnumValN(DumpLLVMIR, "llvm", "output the LLVM IR dump")), 79 cl::values( 80 clEnumValN(RunJIT, "jit", 81 "JIT the code and run it by invoking the main function"))); 82 83 static cl::opt<bool> enableOpt("opt", cl::desc("Enable optimizations")); 84 85 /// Returns a Toy AST resulting from parsing the file or a nullptr on error. 86 std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) { 87 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr = 88 llvm::MemoryBuffer::getFileOrSTDIN(filename); 89 if (std::error_code ec = fileOrErr.getError()) { 90 llvm::errs() << "Could not open input file: " << ec.message() << "\n"; 91 return nullptr; 92 } 93 auto buffer = fileOrErr.get()->getBuffer(); 94 LexerBuffer lexer(buffer.begin(), buffer.end(), std::string(filename)); 95 Parser parser(lexer); 96 return parser.parseModule(); 97 } 98 99 int loadMLIR(mlir::MLIRContext &context, 100 mlir::OwningOpRef<mlir::ModuleOp> &module) { 101 // Handle '.toy' input to the compiler. 102 if (inputType != InputType::MLIR && 103 !llvm::StringRef(inputFilename).endswith(".mlir")) { 104 auto moduleAST = parseInputFile(inputFilename); 105 if (!moduleAST) 106 return 6; 107 module = mlirGen(context, *moduleAST); 108 return !module ? 1 : 0; 109 } 110 111 // Otherwise, the input is '.mlir'. 112 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr = 113 llvm::MemoryBuffer::getFileOrSTDIN(inputFilename); 114 if (std::error_code ec = fileOrErr.getError()) { 115 llvm::errs() << "Could not open input file: " << ec.message() << "\n"; 116 return -1; 117 } 118 119 // Parse the input mlir. 120 llvm::SourceMgr sourceMgr; 121 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc()); 122 module = mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context); 123 if (!module) { 124 llvm::errs() << "Error can't load file " << inputFilename << "\n"; 125 return 3; 126 } 127 return 0; 128 } 129 130 int loadAndProcessMLIR(mlir::MLIRContext &context, 131 mlir::OwningOpRef<mlir::ModuleOp> &module) { 132 if (int error = loadMLIR(context, module)) 133 return error; 134 135 mlir::PassManager pm(&context); 136 // Apply any generic pass manager command line options and run the pipeline. 137 applyPassManagerCLOptions(pm); 138 139 // Check to see what granularity of MLIR we are compiling to. 140 bool isLoweringToAffine = emitAction >= Action::DumpMLIRAffine; 141 bool isLoweringToLLVM = emitAction >= Action::DumpMLIRLLVM; 142 143 if (enableOpt || isLoweringToAffine) { 144 // Inline all functions into main and then delete them. 145 pm.addPass(mlir::createInlinerPass()); 146 147 // Now that there is only one function, we can infer the shapes of each of 148 // the operations. 149 mlir::OpPassManager &optPM = pm.nest<mlir::toy::FuncOp>(); 150 optPM.addPass(mlir::createCanonicalizerPass()); 151 optPM.addPass(mlir::toy::createShapeInferencePass()); 152 optPM.addPass(mlir::createCanonicalizerPass()); 153 optPM.addPass(mlir::createCSEPass()); 154 } 155 156 if (isLoweringToAffine) { 157 // Partially lower the toy dialect. 158 pm.addPass(mlir::toy::createLowerToAffinePass()); 159 160 // Add a few cleanups post lowering. 161 mlir::OpPassManager &optPM = pm.nest<mlir::func::FuncOp>(); 162 optPM.addPass(mlir::createCanonicalizerPass()); 163 optPM.addPass(mlir::createCSEPass()); 164 165 // Add optimizations if enabled. 166 if (enableOpt) { 167 optPM.addPass(mlir::createLoopFusionPass()); 168 optPM.addPass(mlir::createAffineScalarReplacementPass()); 169 } 170 } 171 172 if (isLoweringToLLVM) { 173 // Finish lowering the toy IR to the LLVM dialect. 174 pm.addPass(mlir::toy::createLowerToLLVMPass()); 175 } 176 177 if (mlir::failed(pm.run(*module))) 178 return 4; 179 return 0; 180 } 181 182 int dumpAST() { 183 if (inputType == InputType::MLIR) { 184 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n"; 185 return 5; 186 } 187 188 auto moduleAST = parseInputFile(inputFilename); 189 if (!moduleAST) 190 return 1; 191 192 dump(*moduleAST); 193 return 0; 194 } 195 196 int dumpLLVMIR(mlir::ModuleOp module) { 197 // Register the translation to LLVM IR with the MLIR context. 198 mlir::registerLLVMDialectTranslation(*module->getContext()); 199 200 // Convert the module to LLVM IR in a new LLVM IR context. 201 llvm::LLVMContext llvmContext; 202 auto llvmModule = mlir::translateModuleToLLVMIR(module, llvmContext); 203 if (!llvmModule) { 204 llvm::errs() << "Failed to emit LLVM IR\n"; 205 return -1; 206 } 207 208 // Initialize LLVM targets. 209 llvm::InitializeNativeTarget(); 210 llvm::InitializeNativeTargetAsmPrinter(); 211 mlir::ExecutionEngine::setupTargetTriple(llvmModule.get()); 212 213 /// Optionally run an optimization pipeline over the llvm module. 214 auto optPipeline = mlir::makeOptimizingTransformer( 215 /*optLevel=*/enableOpt ? 3 : 0, /*sizeLevel=*/0, 216 /*targetMachine=*/nullptr); 217 if (auto err = optPipeline(llvmModule.get())) { 218 llvm::errs() << "Failed to optimize LLVM IR " << err << "\n"; 219 return -1; 220 } 221 llvm::errs() << *llvmModule << "\n"; 222 return 0; 223 } 224 225 int runJit(mlir::ModuleOp module) { 226 // Initialize LLVM targets. 227 llvm::InitializeNativeTarget(); 228 llvm::InitializeNativeTargetAsmPrinter(); 229 230 // Register the translation from MLIR to LLVM IR, which must happen before we 231 // can JIT-compile. 232 mlir::registerLLVMDialectTranslation(*module->getContext()); 233 234 // An optimization pipeline to use within the execution engine. 235 auto optPipeline = mlir::makeOptimizingTransformer( 236 /*optLevel=*/enableOpt ? 3 : 0, /*sizeLevel=*/0, 237 /*targetMachine=*/nullptr); 238 239 // Create an MLIR execution engine. The execution engine eagerly JIT-compiles 240 // the module. 241 mlir::ExecutionEngineOptions engineOptions; 242 engineOptions.transformer = optPipeline; 243 auto maybeEngine = mlir::ExecutionEngine::create(module, engineOptions); 244 assert(maybeEngine && "failed to construct an execution engine"); 245 auto &engine = maybeEngine.get(); 246 247 // Invoke the JIT-compiled function. 248 auto invocationResult = engine->invokePacked("main"); 249 if (invocationResult) { 250 llvm::errs() << "JIT invocation failed\n"; 251 return -1; 252 } 253 254 return 0; 255 } 256 257 int main(int argc, char **argv) { 258 // Register any command line options. 259 mlir::registerAsmPrinterCLOptions(); 260 mlir::registerMLIRContextCLOptions(); 261 mlir::registerPassManagerCLOptions(); 262 263 cl::ParseCommandLineOptions(argc, argv, "toy compiler\n"); 264 265 if (emitAction == Action::DumpAST) 266 return dumpAST(); 267 268 // If we aren't dumping the AST, then we are compiling with/to MLIR. 269 270 mlir::MLIRContext context; 271 // Load our Dialect in this MLIR Context. 272 context.getOrLoadDialect<mlir::toy::ToyDialect>(); 273 274 mlir::OwningOpRef<mlir::ModuleOp> module; 275 if (int error = loadAndProcessMLIR(context, module)) 276 return error; 277 278 // If we aren't exporting to non-mlir, then we are done. 279 bool isOutputingMLIR = emitAction <= Action::DumpMLIRLLVM; 280 if (isOutputingMLIR) { 281 module->dump(); 282 return 0; 283 } 284 285 // Check to see if we are compiling to LLVM IR. 286 if (emitAction == Action::DumpLLVMIR) 287 return dumpLLVMIR(*module); 288 289 // Otherwise, we must be running the jit. 290 if (emitAction == Action::RunJIT) 291 return runJit(*module); 292 293 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n"; 294 return -1; 295 } 296