109 changes: 42 additions & 67 deletions llvm/examples/Kaleidoscope/Chapter3/toy.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include <cctype>
#include <cstdio>
#include <map>
Expand Down Expand Up @@ -91,7 +90,7 @@ namespace {
class ExprAST {
public:
virtual ~ExprAST() {}
virtual Value *Codegen() = 0;
virtual Value *codegen() = 0;
};

/// NumberExprAST - Expression class for numeric literals like "1.0".
Expand All @@ -100,7 +99,7 @@ class NumberExprAST : public ExprAST {

public:
NumberExprAST(double Val) : Val(Val) {}
Value *Codegen() override;
Value *codegen() override;
};

/// VariableExprAST - Expression class for referencing a variable, like "a".
Expand All @@ -109,7 +108,7 @@ class VariableExprAST : public ExprAST {

public:
VariableExprAST(const std::string &Name) : Name(Name) {}
Value *Codegen() override;
Value *codegen() override;
};

/// BinaryExprAST - Expression class for a binary operator.
Expand All @@ -121,7 +120,7 @@ class BinaryExprAST : public ExprAST {
BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
std::unique_ptr<ExprAST> RHS)
: Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
Value *Codegen() override;
Value *codegen() override;
};

/// CallExprAST - Expression class for function calls.
Expand All @@ -133,7 +132,7 @@ class CallExprAST : public ExprAST {
CallExprAST(const std::string &Callee,
std::vector<std::unique_ptr<ExprAST>> Args)
: Callee(Callee), Args(std::move(Args)) {}
Value *Codegen() override;
Value *codegen() override;
};

/// PrototypeAST - This class represents the "prototype" for a function,
Expand All @@ -146,7 +145,8 @@ class PrototypeAST {
public:
PrototypeAST(const std::string &Name, std::vector<std::string> Args)
: Name(Name), Args(std::move(Args)) {}
Function *Codegen();
Function *codegen();
const std::string &getName() const { return Name; }
};

/// FunctionAST - This class represents a function definition itself.
Expand All @@ -158,7 +158,7 @@ class FunctionAST {
FunctionAST(std::unique_ptr<PrototypeAST> Proto,
std::unique_ptr<ExprAST> Body)
: Proto(std::move(Proto)), Body(std::move(Body)) {}
Function *Codegen();
Function *codegen();
};
} // end anonymous namespace

Expand Down Expand Up @@ -197,10 +197,6 @@ std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
Error(Str);
return nullptr;
}
std::unique_ptr<FunctionAST> ErrorF(const char *Str) {
Error(Str);
return nullptr;
}

static std::unique_ptr<ExprAST> ParseExpression();

Expand Down Expand Up @@ -365,8 +361,8 @@ static std::unique_ptr<FunctionAST> ParseDefinition() {
static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
if (auto E = ParseExpression()) {
// Make an anonymous proto.
auto Proto =
llvm::make_unique<PrototypeAST>("", std::vector<std::string>());
auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr",
std::vector<std::string>());
return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
}
return nullptr;
Expand All @@ -382,30 +378,30 @@ static std::unique_ptr<PrototypeAST> ParseExtern() {
// Code Generation
//===----------------------------------------------------------------------===//

static std::unique_ptr<Module> TheModule;
static IRBuilder<> Builder(getGlobalContext());
static std::map<std::string, Value *> NamedValues;

Value *ErrorV(const char *Str) {
Error(Str);
return nullptr;
}

static Module *TheModule;
static IRBuilder<> Builder(getGlobalContext());
static std::map<std::string, Value *> NamedValues;

Value *NumberExprAST::Codegen() {
Value *NumberExprAST::codegen() {
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}

Value *VariableExprAST::Codegen() {
Value *VariableExprAST::codegen() {
// Look this variable up in the function.
Value *V = NamedValues[Name];
if (!V)
return ErrorV("Unknown variable name");
return V;
}

Value *BinaryExprAST::Codegen() {
Value *L = LHS->Codegen();
Value *R = RHS->Codegen();
Value *BinaryExprAST::codegen() {
Value *L = LHS->codegen();
Value *R = RHS->codegen();
if (!L || !R)
return nullptr;

Expand All @@ -426,7 +422,7 @@ Value *BinaryExprAST::Codegen() {
}
}

Value *CallExprAST::Codegen() {
Value *CallExprAST::codegen() {
// Look up the name in the global module table.
Function *CalleeF = TheModule->getFunction(Callee);
if (!CalleeF)
Expand All @@ -438,69 +434,52 @@ Value *CallExprAST::Codegen() {

std::vector<Value *> ArgsV;
for (unsigned i = 0, e = Args.size(); i != e; ++i) {
ArgsV.push_back(Args[i]->Codegen());
ArgsV.push_back(Args[i]->codegen());
if (!ArgsV.back())
return nullptr;
}

return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
}

Function *PrototypeAST::Codegen() {
Function *PrototypeAST::codegen() {
// Make the function type: double(double,double) etc.
std::vector<Type *> Doubles(Args.size(),
Type::getDoubleTy(getGlobalContext()));
FunctionType *FT =
FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);

Function *F =
Function::Create(FT, Function::ExternalLinkage, Name, TheModule);

// If F conflicted, there was already something named 'Name'. If it has a
// body, don't allow redefinition or reextern.
if (F->getName() != Name) {
// Delete the one we just made and get the existing one.
F->eraseFromParent();
F = TheModule->getFunction(Name);

// If F already has a body, reject this.
if (!F->empty()) {
ErrorF("redefinition of function");
return nullptr;
}

// If F took a different number of args, reject.
if (F->arg_size() != Args.size()) {
ErrorF("redefinition of function with different # args");
return nullptr;
}
}
Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());

// Set names for all arguments.
unsigned Idx = 0;
for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
++AI, ++Idx) {
AI->setName(Args[Idx]);

// Add arguments to variable symbol table.
NamedValues[Args[Idx]] = AI;
}
for (auto &Arg : F->args())
Arg.setName(Args[Idx++]);

return F;
}

Function *FunctionAST::Codegen() {
NamedValues.clear();
Function *FunctionAST::codegen() {
// First, check for an existing function from a previous 'extern' declaration.
Function *TheFunction = TheModule->getFunction(Proto->getName());

if (!TheFunction)
TheFunction = Proto->codegen();

Function *TheFunction = Proto->Codegen();
if (!TheFunction)
return nullptr;

// Create a new basic block to start insertion into.
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
Builder.SetInsertPoint(BB);

if (Value *RetVal = Body->Codegen()) {
// Record the function arguments in the NamedValues map.
NamedValues.clear();
for (auto &Arg : TheFunction->args())
NamedValues[Arg.getName()] = &Arg;

if (Value *RetVal = Body->codegen()) {
// Finish off the function.
Builder.CreateRet(RetVal);

Expand All @@ -521,7 +500,7 @@ Function *FunctionAST::Codegen() {

static void HandleDefinition() {
if (auto FnAST = ParseDefinition()) {
if (auto *FnIR = FnAST->Codegen()) {
if (auto *FnIR = FnAST->codegen()) {
fprintf(stderr, "Read function definition:");
FnIR->dump();
}
Expand All @@ -533,7 +512,7 @@ static void HandleDefinition() {

static void HandleExtern() {
if (auto ProtoAST = ParseExtern()) {
if (auto *FnIR = ProtoAST->Codegen()) {
if (auto *FnIR = ProtoAST->codegen()) {
fprintf(stderr, "Read extern: ");
FnIR->dump();
}
Expand All @@ -546,7 +525,7 @@ static void HandleExtern() {
static void HandleTopLevelExpression() {
// Evaluate a top-level expression into an anonymous function.
if (auto FnAST = ParseTopLevelExpr()) {
if (auto *FnIR = FnAST->Codegen()) {
if (auto *FnIR = FnAST->codegen()) {
fprintf(stderr, "Read top-level expression:");
FnIR->dump();
}
Expand Down Expand Up @@ -584,8 +563,6 @@ static void MainLoop() {
//===----------------------------------------------------------------------===//

int main() {
LLVMContext &Context = getGlobalContext();

// Install standard binary operators.
// 1 is lowest precedence.
BinopPrecedence['<'] = 10;
Expand All @@ -598,9 +575,7 @@ int main() {
getNextToken();

// Make the module, which holds all the code.
std::unique_ptr<Module> Owner =
llvm::make_unique<Module>("my cool jit", Context);
TheModule = Owner.get();
TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());

// Run the main "interpreter loop" now.
MainLoop();
Expand Down
419 changes: 107 additions & 312 deletions llvm/examples/Kaleidoscope/Chapter4/toy.cpp

Large diffs are not rendered by default.

239 changes: 113 additions & 126 deletions llvm/examples/Kaleidoscope/Chapter5/toy.cpp

Large diffs are not rendered by default.

256 changes: 121 additions & 135 deletions llvm/examples/Kaleidoscope/Chapter6/toy.cpp

Large diffs are not rendered by default.

291 changes: 133 additions & 158 deletions llvm/examples/Kaleidoscope/Chapter7/toy.cpp

Large diffs are not rendered by default.

315 changes: 128 additions & 187 deletions llvm/examples/Kaleidoscope/Chapter8/toy.cpp

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//===----- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Contains a simple JIT definition for use in the kaleidoscope tutorials.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H

#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/DynamicLibrary.h"

namespace llvm {
namespace orc {

class KaleidoscopeJIT {
public:
typedef ObjectLinkingLayer<> ObjLayerT;
typedef IRCompileLayer<ObjLayerT> CompileLayerT;
typedef CompileLayerT::ModuleSetHandleT ModuleHandleT;

KaleidoscopeJIT()
: TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}

TargetMachine &getTargetMachine() { return *TM; }

ModuleHandleT addModule(std::unique_ptr<Module> M) {
// We need a memory manager to allocate memory and resolve symbols for this
// new module. Create one that resolves symbols by looking back into the
// JIT.
auto Resolver = createLambdaResolver(
[&](const std::string &Name) {
if (auto Sym = findMangledSymbol(Name))
return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
return RuntimeDyld::SymbolInfo(nullptr);
},
[](const std::string &S) { return nullptr; });
auto H = CompileLayer.addModuleSet(singletonSet(std::move(M)),
make_unique<SectionMemoryManager>(),
std::move(Resolver));

ModuleHandles.push_back(H);
return H;
}

void removeModule(ModuleHandleT H) {
ModuleHandles.erase(
std::find(ModuleHandles.begin(), ModuleHandles.end(), H));
CompileLayer.removeModuleSet(H);
}

JITSymbol findSymbol(const std::string Name) {
return findMangledSymbol(mangle(Name));
}

private:

std::string mangle(const std::string &Name) {
std::string MangledName;
{
raw_string_ostream MangledNameStream(MangledName);
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
}
return MangledName;
}

template <typename T> static std::vector<T> singletonSet(T t) {
std::vector<T> Vec;
Vec.push_back(std::move(t));
return Vec;
}

JITSymbol findMangledSymbol(const std::string &Name) {
// Search modules in reverse order: from last added to first added.
// This is the opposite of the usual search order for dlsym, but makes more
// sense in a REPL where we want to bind to the newest available definition.
for (auto H : make_range(ModuleHandles.rbegin(), ModuleHandles.rend()))
if (auto Sym = CompileLayer.findSymbolIn(H, Name, true))
return Sym;

// If we can't find the symbol in the JIT, try looking in the host process.
if (auto SymAddr = RTDyldMemoryManager::getSymbolAddressInProcess(Name))
return JITSymbol(SymAddr, JITSymbolFlags::Exported);

return nullptr;
}

std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
ObjLayerT ObjectLayer;
CompileLayerT CompileLayer;
std::vector<ModuleHandleT> ModuleHandles;
};

} // End namespace orc.
} // End namespace llvm

#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H