Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeleobas committed May 10, 2021
1 parent e61074e commit d51ba6b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
51 changes: 35 additions & 16 deletions src/codegen/codegen_llvm.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"

#include "codegen_llvm.h"
#include "taco/util/print.h"
Expand Down Expand Up @@ -229,7 +230,7 @@ void CodeGen_LLVM::visit(const Var *op) {
auto _ = CodeGen_LLVM::IndentHelper(this, "Var", op->name);
auto *v = getSymbol(op->name);
if (v->getType()->isPointerTy()) {
value = Builder->CreateLoad(v, "load." + op->name);
value = this->Builder->CreateLoad(v, "load." + op->name);
} else {
value = v;
}
Expand Down Expand Up @@ -361,12 +362,11 @@ void CodeGen_LLVM::visit(const Switch *op) {

void CodeGen_LLVM::visit(const Load *op) {
auto _ = CodeGen_LLVM::IndentHelper(this, "Load");

auto *loc = codegen(op->loc);
auto *arr = codegen(op->arr);
PRINT(*loc);
PRINT(*arr);
// value = Builder->CreateLoad()
// throw logic_error("Not Implemented for Load.");
auto *gep = this->Builder->CreateInBoundsGEP(arr, loc);
value = this->Builder->CreateLoad(arr, gep);
}

void CodeGen_LLVM::visit(const Malloc *op) {
Expand All @@ -381,10 +381,13 @@ void CodeGen_LLVM::visit(const Sizeof *op) {

void CodeGen_LLVM::visit(const Store *op) {
auto _ = CodeGen_LLVM::IndentHelper(this, "Store");
codegen(op->data);
// codegen(op->loc);
PRINT(value);
throw logic_error("Not Implemented for Store.");

auto *loc = codegen(op->loc);
auto *arr = codegen(op->arr);
auto *gep = this->Builder->CreateInBoundsGEP(arr, loc); // arr[loc]
auto *data = codegen(op->data); // ... = data

this->Builder->CreateStore(data, gep); // arr[loc] = data
}

void CodeGen_LLVM::visit(const For *op) {
Expand Down Expand Up @@ -488,9 +491,15 @@ void CodeGen_LLVM::init_codegen() {
}

void CodeGen_LLVM::visit(const Function *func) {
auto M = std::make_unique<llvm::Module>("my compiler", this->Context);
auto _ = CodeGen_LLVM::IndentHelper(this, "Function");

/*
This method creates a function. By calling convention, the function
returns 0 on success or 1 otherwise.
*/

auto M = std::make_unique<llvm::Module>("my compiler", this->Context);

// 1. find the arguments to @func
FindVars varFinder(func->inputs, func->outputs, this);

Expand Down Expand Up @@ -528,11 +537,6 @@ void CodeGen_LLVM::visit(const Function *func) {

// set arg flags
arg.addAttr(llvm::Attribute::NoCapture);
// arg.addAttr(llvm::Attribute::ReadOnly); // only set this for input
// tensors

// Shouldn't
// all arguments here be a parameter? assert(var->is_parameter);

// 6.1 push args to symbol table
pushSymbol(var->name, &arg);
Expand All @@ -541,6 +545,16 @@ void CodeGen_LLVM::visit(const Function *func) {
// 7. visit function body
func->body.accept(this);

// 8. Create an exit basic block and exit it
llvm::BasicBlock *exit =
llvm::BasicBlock::Create(this->Context, "exit", this->F);
this->Builder->CreateBr(exit);
this->Builder->SetInsertPoint(exit); // ... -> exit
this->Builder->CreateRet(llvm::ConstantInt::get(i32, 0)); // return 0

// 9. Verify the created module
llvm::verifyModule(*M, &llvm::errs());

PRINT(*M);
}

Expand Down Expand Up @@ -640,12 +654,17 @@ void CodeGen_LLVM::visit(const GetProperty *op) {
this->Builder->CreateLoad(dim, name + ".load"), i32, name + ".dim");
break;
}
case TensorProperty::Values: {
auto *vals = this->Builder->CreateStructGEP(
tensor, (int)TensorProperty::Values, name + ".gep.vals");
value = this->Builder->CreateLoad(vals, name + ".vals");
break;
}
case TensorProperty::Order:
case TensorProperty::ComponentSize:
case TensorProperty::ModeOrdering:
case TensorProperty::ModeTypes:
case TensorProperty::Indices:
case TensorProperty::Values:
case TensorProperty::ValuesSize:
default:
throw logic_error("GetProperty not implemented for " +
Expand Down
8 changes: 3 additions & 5 deletions src/codegen/codegen_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ namespace taco
llvm::PointerType *tensorTypePtr;
llvm::Value *value; // last llvm value generated

std::unique_ptr<CodeGen_C> cprinter;
int64_t indent = 0;

public:
CodeGen_LLVM(std::ostream &stream, OutputKind kind)
: CodeGen(stream, LLVM), outputKind(kind), cprinter(std::make_unique<CodeGen_C>(stream, kind, false)){};
: CodeGen(stream, LLVM), outputKind(kind){};
void compile(Stmt stmt, bool isFirst = false) override;

protected:
Expand All @@ -53,7 +52,6 @@ namespace taco
void init_codegen();
llvm::Type *llvmTypeOf(Datatype);
static std::string tensorPropertyToString(const TensorProperty t);
void printVisiting(const std::string &type);

void codegen(const Stmt);
llvm::Value *codegen(const Expr);
Expand Down Expand Up @@ -116,10 +114,10 @@ namespace taco
//
for (int64_t i = 0; i < parent_->indent-1; ++i)
{
std::cout << "| ";
std::cout << "| ";
}
if (parent_->indent > 0) {
std::cout << "├─[" << parent_->indent << "]";
std::cout << "├─[" << parent_->indent << "]";
}

std::cout << "LLVM CodeGen Visiting " << type;
Expand Down

0 comments on commit d51ba6b

Please sign in to comment.