Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / examples / Kaleidoscope / Chapter4 / toy.cpp
blob19ec70efd5e155371422d391b210304fd57c7804
1 #include "../include/KaleidoscopeJIT.h"
2 #include "llvm/ADT/APFloat.h"
3 #include "llvm/ADT/STLExtras.h"
4 #include "llvm/Analysis/AssumptionCache.h"
5 #include "llvm/Analysis/BasicAliasAnalysis.h"
6 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
7 #include "llvm/Analysis/MemorySSA.h"
8 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
9 #include "llvm/Analysis/ProfileSummaryInfo.h"
10 #include "llvm/Analysis/TargetTransformInfo.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/DerivedTypes.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Passes/PassBuilder.h"
22 #include "llvm/Passes/StandardInstrumentations.h"
23 #include "llvm/Support/TargetSelect.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Transforms/InstCombine/InstCombine.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Scalar/GVN.h"
28 #include "llvm/Transforms/Scalar/Reassociate.h"
29 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
30 #include <algorithm>
31 #include <cassert>
32 #include <cctype>
33 #include <cstdint>
34 #include <cstdio>
35 #include <cstdlib>
36 #include <map>
37 #include <memory>
38 #include <string>
39 #include <vector>
41 using namespace llvm;
42 using namespace llvm::orc;
44 //===----------------------------------------------------------------------===//
45 // Lexer
46 //===----------------------------------------------------------------------===//
48 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
49 // of these for known things.
50 enum Token {
51 tok_eof = -1,
53 // commands
54 tok_def = -2,
55 tok_extern = -3,
57 // primary
58 tok_identifier = -4,
59 tok_number = -5
62 static std::string IdentifierStr; // Filled in if tok_identifier
63 static double NumVal; // Filled in if tok_number
65 /// gettok - Return the next token from standard input.
66 static int gettok() {
67 static int LastChar = ' ';
69 // Skip any whitespace.
70 while (isspace(LastChar))
71 LastChar = getchar();
73 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
74 IdentifierStr = LastChar;
75 while (isalnum((LastChar = getchar())))
76 IdentifierStr += LastChar;
78 if (IdentifierStr == "def")
79 return tok_def;
80 if (IdentifierStr == "extern")
81 return tok_extern;
82 return tok_identifier;
85 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
86 std::string NumStr;
87 do {
88 NumStr += LastChar;
89 LastChar = getchar();
90 } while (isdigit(LastChar) || LastChar == '.');
92 NumVal = strtod(NumStr.c_str(), nullptr);
93 return tok_number;
96 if (LastChar == '#') {
97 // Comment until end of line.
99 LastChar = getchar();
100 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
102 if (LastChar != EOF)
103 return gettok();
106 // Check for end of file. Don't eat the EOF.
107 if (LastChar == EOF)
108 return tok_eof;
110 // Otherwise, just return the character as its ascii value.
111 int ThisChar = LastChar;
112 LastChar = getchar();
113 return ThisChar;
116 //===----------------------------------------------------------------------===//
117 // Abstract Syntax Tree (aka Parse Tree)
118 //===----------------------------------------------------------------------===//
120 namespace {
122 /// ExprAST - Base class for all expression nodes.
123 class ExprAST {
124 public:
125 virtual ~ExprAST() = default;
127 virtual Value *codegen() = 0;
130 /// NumberExprAST - Expression class for numeric literals like "1.0".
131 class NumberExprAST : public ExprAST {
132 double Val;
134 public:
135 NumberExprAST(double Val) : Val(Val) {}
137 Value *codegen() override;
140 /// VariableExprAST - Expression class for referencing a variable, like "a".
141 class VariableExprAST : public ExprAST {
142 std::string Name;
144 public:
145 VariableExprAST(const std::string &Name) : Name(Name) {}
147 Value *codegen() override;
150 /// BinaryExprAST - Expression class for a binary operator.
151 class BinaryExprAST : public ExprAST {
152 char Op;
153 std::unique_ptr<ExprAST> LHS, RHS;
155 public:
156 BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
157 std::unique_ptr<ExprAST> RHS)
158 : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
160 Value *codegen() override;
163 /// CallExprAST - Expression class for function calls.
164 class CallExprAST : public ExprAST {
165 std::string Callee;
166 std::vector<std::unique_ptr<ExprAST>> Args;
168 public:
169 CallExprAST(const std::string &Callee,
170 std::vector<std::unique_ptr<ExprAST>> Args)
171 : Callee(Callee), Args(std::move(Args)) {}
173 Value *codegen() override;
176 /// PrototypeAST - This class represents the "prototype" for a function,
177 /// which captures its name, and its argument names (thus implicitly the number
178 /// of arguments the function takes).
179 class PrototypeAST {
180 std::string Name;
181 std::vector<std::string> Args;
183 public:
184 PrototypeAST(const std::string &Name, std::vector<std::string> Args)
185 : Name(Name), Args(std::move(Args)) {}
187 Function *codegen();
188 const std::string &getName() const { return Name; }
191 /// FunctionAST - This class represents a function definition itself.
192 class FunctionAST {
193 std::unique_ptr<PrototypeAST> Proto;
194 std::unique_ptr<ExprAST> Body;
196 public:
197 FunctionAST(std::unique_ptr<PrototypeAST> Proto,
198 std::unique_ptr<ExprAST> Body)
199 : Proto(std::move(Proto)), Body(std::move(Body)) {}
201 Function *codegen();
204 } // end anonymous namespace
206 //===----------------------------------------------------------------------===//
207 // Parser
208 //===----------------------------------------------------------------------===//
210 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
211 /// token the parser is looking at. getNextToken reads another token from the
212 /// lexer and updates CurTok with its results.
213 static int CurTok;
214 static int getNextToken() { return CurTok = gettok(); }
216 /// BinopPrecedence - This holds the precedence for each binary operator that is
217 /// defined.
218 static std::map<char, int> BinopPrecedence;
220 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
221 static int GetTokPrecedence() {
222 if (!isascii(CurTok))
223 return -1;
225 // Make sure it's a declared binop.
226 int TokPrec = BinopPrecedence[CurTok];
227 if (TokPrec <= 0)
228 return -1;
229 return TokPrec;
232 /// LogError* - These are little helper functions for error handling.
233 std::unique_ptr<ExprAST> LogError(const char *Str) {
234 fprintf(stderr, "Error: %s\n", Str);
235 return nullptr;
238 std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) {
239 LogError(Str);
240 return nullptr;
243 static std::unique_ptr<ExprAST> ParseExpression();
245 /// numberexpr ::= number
246 static std::unique_ptr<ExprAST> ParseNumberExpr() {
247 auto Result = std::make_unique<NumberExprAST>(NumVal);
248 getNextToken(); // consume the number
249 return std::move(Result);
252 /// parenexpr ::= '(' expression ')'
253 static std::unique_ptr<ExprAST> ParseParenExpr() {
254 getNextToken(); // eat (.
255 auto V = ParseExpression();
256 if (!V)
257 return nullptr;
259 if (CurTok != ')')
260 return LogError("expected ')'");
261 getNextToken(); // eat ).
262 return V;
265 /// identifierexpr
266 /// ::= identifier
267 /// ::= identifier '(' expression* ')'
268 static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
269 std::string IdName = IdentifierStr;
271 getNextToken(); // eat identifier.
273 if (CurTok != '(') // Simple variable ref.
274 return std::make_unique<VariableExprAST>(IdName);
276 // Call.
277 getNextToken(); // eat (
278 std::vector<std::unique_ptr<ExprAST>> Args;
279 if (CurTok != ')') {
280 while (true) {
281 if (auto Arg = ParseExpression())
282 Args.push_back(std::move(Arg));
283 else
284 return nullptr;
286 if (CurTok == ')')
287 break;
289 if (CurTok != ',')
290 return LogError("Expected ')' or ',' in argument list");
291 getNextToken();
295 // Eat the ')'.
296 getNextToken();
298 return std::make_unique<CallExprAST>(IdName, std::move(Args));
301 /// primary
302 /// ::= identifierexpr
303 /// ::= numberexpr
304 /// ::= parenexpr
305 static std::unique_ptr<ExprAST> ParsePrimary() {
306 switch (CurTok) {
307 default:
308 return LogError("unknown token when expecting an expression");
309 case tok_identifier:
310 return ParseIdentifierExpr();
311 case tok_number:
312 return ParseNumberExpr();
313 case '(':
314 return ParseParenExpr();
318 /// binoprhs
319 /// ::= ('+' primary)*
320 static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
321 std::unique_ptr<ExprAST> LHS) {
322 // If this is a binop, find its precedence.
323 while (true) {
324 int TokPrec = GetTokPrecedence();
326 // If this is a binop that binds at least as tightly as the current binop,
327 // consume it, otherwise we are done.
328 if (TokPrec < ExprPrec)
329 return LHS;
331 // Okay, we know this is a binop.
332 int BinOp = CurTok;
333 getNextToken(); // eat binop
335 // Parse the primary expression after the binary operator.
336 auto RHS = ParsePrimary();
337 if (!RHS)
338 return nullptr;
340 // If BinOp binds less tightly with RHS than the operator after RHS, let
341 // the pending operator take RHS as its LHS.
342 int NextPrec = GetTokPrecedence();
343 if (TokPrec < NextPrec) {
344 RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
345 if (!RHS)
346 return nullptr;
349 // Merge LHS/RHS.
350 LHS =
351 std::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
355 /// expression
356 /// ::= primary binoprhs
358 static std::unique_ptr<ExprAST> ParseExpression() {
359 auto LHS = ParsePrimary();
360 if (!LHS)
361 return nullptr;
363 return ParseBinOpRHS(0, std::move(LHS));
366 /// prototype
367 /// ::= id '(' id* ')'
368 static std::unique_ptr<PrototypeAST> ParsePrototype() {
369 if (CurTok != tok_identifier)
370 return LogErrorP("Expected function name in prototype");
372 std::string FnName = IdentifierStr;
373 getNextToken();
375 if (CurTok != '(')
376 return LogErrorP("Expected '(' in prototype");
378 std::vector<std::string> ArgNames;
379 while (getNextToken() == tok_identifier)
380 ArgNames.push_back(IdentifierStr);
381 if (CurTok != ')')
382 return LogErrorP("Expected ')' in prototype");
384 // success.
385 getNextToken(); // eat ')'.
387 return std::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
390 /// definition ::= 'def' prototype expression
391 static std::unique_ptr<FunctionAST> ParseDefinition() {
392 getNextToken(); // eat def.
393 auto Proto = ParsePrototype();
394 if (!Proto)
395 return nullptr;
397 if (auto E = ParseExpression())
398 return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
399 return nullptr;
402 /// toplevelexpr ::= expression
403 static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
404 if (auto E = ParseExpression()) {
405 // Make an anonymous proto.
406 auto Proto = std::make_unique<PrototypeAST>("__anon_expr",
407 std::vector<std::string>());
408 return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
410 return nullptr;
413 /// external ::= 'extern' prototype
414 static std::unique_ptr<PrototypeAST> ParseExtern() {
415 getNextToken(); // eat extern.
416 return ParsePrototype();
419 //===----------------------------------------------------------------------===//
420 // Code Generation
421 //===----------------------------------------------------------------------===//
423 static std::unique_ptr<LLVMContext> TheContext;
424 static std::unique_ptr<Module> TheModule;
425 static std::unique_ptr<IRBuilder<>> Builder;
426 static std::map<std::string, Value *> NamedValues;
427 static std::unique_ptr<KaleidoscopeJIT> TheJIT;
428 static std::unique_ptr<FunctionPassManager> TheFPM;
429 static std::unique_ptr<FunctionAnalysisManager> TheFAM;
430 static std::unique_ptr<ModuleAnalysisManager> TheMAM;
431 static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
432 static std::unique_ptr<StandardInstrumentations> TheSI;
433 static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
434 static ExitOnError ExitOnErr;
436 Value *LogErrorV(const char *Str) {
437 LogError(Str);
438 return nullptr;
441 Function *getFunction(std::string Name) {
442 // First, see if the function has already been added to the current module.
443 if (auto *F = TheModule->getFunction(Name))
444 return F;
446 // If not, check whether we can codegen the declaration from some existing
447 // prototype.
448 auto FI = FunctionProtos.find(Name);
449 if (FI != FunctionProtos.end())
450 return FI->second->codegen();
452 // If no existing prototype exists, return null.
453 return nullptr;
456 Value *NumberExprAST::codegen() {
457 return ConstantFP::get(*TheContext, APFloat(Val));
460 Value *VariableExprAST::codegen() {
461 // Look this variable up in the function.
462 Value *V = NamedValues[Name];
463 if (!V)
464 return LogErrorV("Unknown variable name");
465 return V;
468 Value *BinaryExprAST::codegen() {
469 Value *L = LHS->codegen();
470 Value *R = RHS->codegen();
471 if (!L || !R)
472 return nullptr;
474 switch (Op) {
475 case '+':
476 return Builder->CreateFAdd(L, R, "addtmp");
477 case '-':
478 return Builder->CreateFSub(L, R, "subtmp");
479 case '*':
480 return Builder->CreateFMul(L, R, "multmp");
481 case '<':
482 L = Builder->CreateFCmpULT(L, R, "cmptmp");
483 // Convert bool 0/1 to double 0.0 or 1.0
484 return Builder->CreateUIToFP(L, Type::getDoubleTy(*TheContext), "booltmp");
485 default:
486 return LogErrorV("invalid binary operator");
490 Value *CallExprAST::codegen() {
491 // Look up the name in the global module table.
492 Function *CalleeF = getFunction(Callee);
493 if (!CalleeF)
494 return LogErrorV("Unknown function referenced");
496 // If argument mismatch error.
497 if (CalleeF->arg_size() != Args.size())
498 return LogErrorV("Incorrect # arguments passed");
500 std::vector<Value *> ArgsV;
501 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
502 ArgsV.push_back(Args[i]->codegen());
503 if (!ArgsV.back())
504 return nullptr;
507 return Builder->CreateCall(CalleeF, ArgsV, "calltmp");
510 Function *PrototypeAST::codegen() {
511 // Make the function type: double(double,double) etc.
512 std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(*TheContext));
513 FunctionType *FT =
514 FunctionType::get(Type::getDoubleTy(*TheContext), Doubles, false);
516 Function *F =
517 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
519 // Set names for all arguments.
520 unsigned Idx = 0;
521 for (auto &Arg : F->args())
522 Arg.setName(Args[Idx++]);
524 return F;
527 Function *FunctionAST::codegen() {
528 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
529 // reference to it for use below.
530 auto &P = *Proto;
531 FunctionProtos[Proto->getName()] = std::move(Proto);
532 Function *TheFunction = getFunction(P.getName());
533 if (!TheFunction)
534 return nullptr;
536 // Create a new basic block to start insertion into.
537 BasicBlock *BB = BasicBlock::Create(*TheContext, "entry", TheFunction);
538 Builder->SetInsertPoint(BB);
540 // Record the function arguments in the NamedValues map.
541 NamedValues.clear();
542 for (auto &Arg : TheFunction->args())
543 NamedValues[std::string(Arg.getName())] = &Arg;
545 if (Value *RetVal = Body->codegen()) {
546 // Finish off the function.
547 Builder->CreateRet(RetVal);
549 // Validate the generated code, checking for consistency.
550 verifyFunction(*TheFunction);
552 // Run the optimizer on the function.
553 TheFPM->run(*TheFunction, *TheFAM);
555 return TheFunction;
558 // Error reading body, remove function.
559 TheFunction->eraseFromParent();
560 return nullptr;
563 //===----------------------------------------------------------------------===//
564 // Top-Level parsing and JIT Driver
565 //===----------------------------------------------------------------------===//
567 static void InitializeModuleAndManagers() {
568 // Open a new context and module.
569 TheContext = std::make_unique<LLVMContext>();
570 TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
571 TheModule->setDataLayout(TheJIT->getDataLayout());
573 // Create a new builder for the module.
574 Builder = std::make_unique<IRBuilder<>>(*TheContext);
576 // Create new pass and analysis managers.
577 TheFPM = std::make_unique<FunctionPassManager>();
578 TheFAM = std::make_unique<FunctionAnalysisManager>();
579 TheMAM = std::make_unique<ModuleAnalysisManager>();
580 ThePIC = std::make_unique<PassInstrumentationCallbacks>();
581 TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
582 /*DebugLogging*/ true);
583 TheSI->registerCallbacks(*ThePIC, TheMAM.get());
585 // Add transform passes.
586 // Do simple "peephole" optimizations and bit-twiddling optzns.
587 TheFPM->addPass(InstCombinePass());
588 // Reassociate expressions.
589 TheFPM->addPass(ReassociatePass());
590 // Eliminate Common SubExpressions.
591 TheFPM->addPass(GVNPass());
592 // Simplify the control flow graph (deleting unreachable blocks, etc).
593 TheFPM->addPass(SimplifyCFGPass());
595 // Register analysis passes used in these transform passes.
596 TheFAM->registerPass([&] { return AAManager(); });
597 TheFAM->registerPass([&] { return AssumptionAnalysis(); });
598 TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
599 TheFAM->registerPass([&] { return LoopAnalysis(); });
600 TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
601 TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
602 TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
603 TheFAM->registerPass([&] {
604 return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
606 TheFAM->registerPass(
607 [&] { return PassInstrumentationAnalysis(ThePIC.get()); });
608 TheFAM->registerPass([&] { return TargetIRAnalysis(); });
609 TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
611 TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
614 static void HandleDefinition() {
615 if (auto FnAST = ParseDefinition()) {
616 if (auto *FnIR = FnAST->codegen()) {
617 fprintf(stderr, "Read function definition:");
618 FnIR->print(errs());
619 fprintf(stderr, "\n");
620 ExitOnErr(TheJIT->addModule(
621 ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
622 InitializeModuleAndManagers();
624 } else {
625 // Skip token for error recovery.
626 getNextToken();
630 static void HandleExtern() {
631 if (auto ProtoAST = ParseExtern()) {
632 if (auto *FnIR = ProtoAST->codegen()) {
633 fprintf(stderr, "Read extern: ");
634 FnIR->print(errs());
635 fprintf(stderr, "\n");
636 FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
638 } else {
639 // Skip token for error recovery.
640 getNextToken();
644 static void HandleTopLevelExpression() {
645 // Evaluate a top-level expression into an anonymous function.
646 if (auto FnAST = ParseTopLevelExpr()) {
647 if (FnAST->codegen()) {
648 // Create a ResourceTracker to track JIT'd memory allocated to our
649 // anonymous expression -- that way we can free it after executing.
650 auto RT = TheJIT->getMainJITDylib().createResourceTracker();
652 auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
653 ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
654 InitializeModuleAndManagers();
656 // Search the JIT for the __anon_expr symbol.
657 auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
659 // Get the symbol's address and cast it to the right type (takes no
660 // arguments, returns a double) so we can call it as a native function.
661 double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
662 fprintf(stderr, "Evaluated to %f\n", FP());
664 // Delete the anonymous expression module from the JIT.
665 ExitOnErr(RT->remove());
667 } else {
668 // Skip token for error recovery.
669 getNextToken();
673 /// top ::= definition | external | expression | ';'
674 static void MainLoop() {
675 while (true) {
676 fprintf(stderr, "ready> ");
677 switch (CurTok) {
678 case tok_eof:
679 return;
680 case ';': // ignore top-level semicolons.
681 getNextToken();
682 break;
683 case tok_def:
684 HandleDefinition();
685 break;
686 case tok_extern:
687 HandleExtern();
688 break;
689 default:
690 HandleTopLevelExpression();
691 break;
696 //===----------------------------------------------------------------------===//
697 // "Library" functions that can be "extern'd" from user code.
698 //===----------------------------------------------------------------------===//
700 #ifdef _WIN32
701 #define DLLEXPORT __declspec(dllexport)
702 #else
703 #define DLLEXPORT
704 #endif
706 /// putchard - putchar that takes a double and returns 0.
707 extern "C" DLLEXPORT double putchard(double X) {
708 fputc((char)X, stderr);
709 return 0;
712 /// printd - printf that takes a double prints it as "%f\n", returning 0.
713 extern "C" DLLEXPORT double printd(double X) {
714 fprintf(stderr, "%f\n", X);
715 return 0;
718 //===----------------------------------------------------------------------===//
719 // Main driver code.
720 //===----------------------------------------------------------------------===//
722 int main() {
723 InitializeNativeTarget();
724 InitializeNativeTargetAsmPrinter();
725 InitializeNativeTargetAsmParser();
727 // Install standard binary operators.
728 // 1 is lowest precedence.
729 BinopPrecedence['<'] = 10;
730 BinopPrecedence['+'] = 20;
731 BinopPrecedence['-'] = 20;
732 BinopPrecedence['*'] = 40; // highest.
734 // Prime the first token.
735 fprintf(stderr, "ready> ");
736 getNextToken();
738 TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
740 InitializeModuleAndManagers();
742 // Run the main "interpreter loop" now.
743 MainLoop();
745 return 0;