Skip to content

Commit d51ba6b

Browse files
upd
1 parent e61074e commit d51ba6b

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

src/codegen/codegen_llvm.cpp

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "llvm/IR/Module.h"
2+
#include "llvm/IR/Verifier.h"
23

34
#include "codegen_llvm.h"
45
#include "taco/util/print.h"
@@ -229,7 +230,7 @@ void CodeGen_LLVM::visit(const Var *op) {
229230
auto _ = CodeGen_LLVM::IndentHelper(this, "Var", op->name);
230231
auto *v = getSymbol(op->name);
231232
if (v->getType()->isPointerTy()) {
232-
value = Builder->CreateLoad(v, "load." + op->name);
233+
value = this->Builder->CreateLoad(v, "load." + op->name);
233234
} else {
234235
value = v;
235236
}
@@ -361,12 +362,11 @@ void CodeGen_LLVM::visit(const Switch *op) {
361362

362363
void CodeGen_LLVM::visit(const Load *op) {
363364
auto _ = CodeGen_LLVM::IndentHelper(this, "Load");
365+
364366
auto *loc = codegen(op->loc);
365367
auto *arr = codegen(op->arr);
366-
PRINT(*loc);
367-
PRINT(*arr);
368-
// value = Builder->CreateLoad()
369-
// throw logic_error("Not Implemented for Load.");
368+
auto *gep = this->Builder->CreateInBoundsGEP(arr, loc);
369+
value = this->Builder->CreateLoad(arr, gep);
370370
}
371371

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

382382
void CodeGen_LLVM::visit(const Store *op) {
383383
auto _ = CodeGen_LLVM::IndentHelper(this, "Store");
384-
codegen(op->data);
385-
// codegen(op->loc);
386-
PRINT(value);
387-
throw logic_error("Not Implemented for Store.");
384+
385+
auto *loc = codegen(op->loc);
386+
auto *arr = codegen(op->arr);
387+
auto *gep = this->Builder->CreateInBoundsGEP(arr, loc); // arr[loc]
388+
auto *data = codegen(op->data); // ... = data
389+
390+
this->Builder->CreateStore(data, gep); // arr[loc] = data
388391
}
389392

390393
void CodeGen_LLVM::visit(const For *op) {
@@ -488,9 +491,15 @@ void CodeGen_LLVM::init_codegen() {
488491
}
489492

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

496+
/*
497+
This method creates a function. By calling convention, the function
498+
returns 0 on success or 1 otherwise.
499+
*/
500+
501+
auto M = std::make_unique<llvm::Module>("my compiler", this->Context);
502+
494503
// 1. find the arguments to @func
495504
FindVars varFinder(func->inputs, func->outputs, this);
496505

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

529538
// set arg flags
530539
arg.addAttr(llvm::Attribute::NoCapture);
531-
// arg.addAttr(llvm::Attribute::ReadOnly); // only set this for input
532-
// tensors
533-
534-
// Shouldn't
535-
// all arguments here be a parameter? assert(var->is_parameter);
536540

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

548+
// 8. Create an exit basic block and exit it
549+
llvm::BasicBlock *exit =
550+
llvm::BasicBlock::Create(this->Context, "exit", this->F);
551+
this->Builder->CreateBr(exit);
552+
this->Builder->SetInsertPoint(exit); // ... -> exit
553+
this->Builder->CreateRet(llvm::ConstantInt::get(i32, 0)); // return 0
554+
555+
// 9. Verify the created module
556+
llvm::verifyModule(*M, &llvm::errs());
557+
544558
PRINT(*M);
545559
}
546560

@@ -640,12 +654,17 @@ void CodeGen_LLVM::visit(const GetProperty *op) {
640654
this->Builder->CreateLoad(dim, name + ".load"), i32, name + ".dim");
641655
break;
642656
}
657+
case TensorProperty::Values: {
658+
auto *vals = this->Builder->CreateStructGEP(
659+
tensor, (int)TensorProperty::Values, name + ".gep.vals");
660+
value = this->Builder->CreateLoad(vals, name + ".vals");
661+
break;
662+
}
643663
case TensorProperty::Order:
644664
case TensorProperty::ComponentSize:
645665
case TensorProperty::ModeOrdering:
646666
case TensorProperty::ModeTypes:
647667
case TensorProperty::Indices:
648-
case TensorProperty::Values:
649668
case TensorProperty::ValuesSize:
650669
default:
651670
throw logic_error("GetProperty not implemented for " +

src/codegen/codegen_llvm.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ namespace taco
3131
llvm::PointerType *tensorTypePtr;
3232
llvm::Value *value; // last llvm value generated
3333

34-
std::unique_ptr<CodeGen_C> cprinter;
3534
int64_t indent = 0;
3635

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

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

5856
void codegen(const Stmt);
5957
llvm::Value *codegen(const Expr);
@@ -116,10 +114,10 @@ namespace taco
116114
//
117115
for (int64_t i = 0; i < parent_->indent-1; ++i)
118116
{
119-
std::cout << "| ";
117+
std::cout << "| ";
120118
}
121119
if (parent_->indent > 0) {
122-
std::cout << "├─[" << parent_->indent << "]";
120+
std::cout << "├─[" << parent_->indent << "]";
123121
}
124122

125123
std::cout << "LLVM CodeGen Visiting " << type;

0 commit comments

Comments
 (0)