1
1
#include " llvm/IR/Module.h"
2
+ #include " llvm/IR/Verifier.h"
2
3
3
4
#include " codegen_llvm.h"
4
5
#include " taco/util/print.h"
@@ -229,7 +230,7 @@ void CodeGen_LLVM::visit(const Var *op) {
229
230
auto _ = CodeGen_LLVM::IndentHelper (this , " Var" , op->name );
230
231
auto *v = getSymbol (op->name );
231
232
if (v->getType ()->isPointerTy ()) {
232
- value = Builder->CreateLoad (v, " load." + op->name );
233
+ value = this -> Builder ->CreateLoad (v, " load." + op->name );
233
234
} else {
234
235
value = v;
235
236
}
@@ -361,12 +362,11 @@ void CodeGen_LLVM::visit(const Switch *op) {
361
362
362
363
void CodeGen_LLVM::visit (const Load *op) {
363
364
auto _ = CodeGen_LLVM::IndentHelper (this , " Load" );
365
+
364
366
auto *loc = codegen (op->loc );
365
367
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);
370
370
}
371
371
372
372
void CodeGen_LLVM::visit (const Malloc *op) {
@@ -381,10 +381,13 @@ void CodeGen_LLVM::visit(const Sizeof *op) {
381
381
382
382
void CodeGen_LLVM::visit (const Store *op) {
383
383
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
388
391
}
389
392
390
393
void CodeGen_LLVM::visit (const For *op) {
@@ -488,9 +491,15 @@ void CodeGen_LLVM::init_codegen() {
488
491
}
489
492
490
493
void CodeGen_LLVM::visit (const Function *func) {
491
- auto M = std::make_unique<llvm::Module>(" my compiler" , this ->Context );
492
494
auto _ = CodeGen_LLVM::IndentHelper (this , " Function" );
493
495
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
+
494
503
// 1. find the arguments to @func
495
504
FindVars varFinder (func->inputs , func->outputs , this );
496
505
@@ -528,11 +537,6 @@ void CodeGen_LLVM::visit(const Function *func) {
528
537
529
538
// set arg flags
530
539
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);
536
540
537
541
// 6.1 push args to symbol table
538
542
pushSymbol (var->name , &arg);
@@ -541,6 +545,16 @@ void CodeGen_LLVM::visit(const Function *func) {
541
545
// 7. visit function body
542
546
func->body .accept (this );
543
547
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
+
544
558
PRINT (*M);
545
559
}
546
560
@@ -640,12 +654,17 @@ void CodeGen_LLVM::visit(const GetProperty *op) {
640
654
this ->Builder ->CreateLoad (dim, name + " .load" ), i32 , name + " .dim" );
641
655
break ;
642
656
}
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
+ }
643
663
case TensorProperty::Order:
644
664
case TensorProperty::ComponentSize:
645
665
case TensorProperty::ModeOrdering:
646
666
case TensorProperty::ModeTypes:
647
667
case TensorProperty::Indices:
648
- case TensorProperty::Values:
649
668
case TensorProperty::ValuesSize:
650
669
default :
651
670
throw logic_error (" GetProperty not implemented for " +
0 commit comments