From c48b7fb676cab6b0319d0bfdbb34853012a2790d Mon Sep 17 00:00:00 2001 From: Philippe Tillet Date: Mon, 7 Jan 2019 04:08:55 -0500 Subject: [PATCH] [intermediate representation] bugfix in getelementptr_inst --- examples/matrix.cpp | 13 +++---------- include/codegen/{storage_alloc.h => allocation.h} | 0 include/codegen/{lowering.h => selection.h} | 9 ++++----- include/ir/instructions.h | 2 +- lib/ast/lowering.cpp | 3 ++- lib/ir/builder.cpp | 2 +- lib/ir/instructions.cpp | 5 +++-- 7 files changed, 14 insertions(+), 20 deletions(-) rename include/codegen/{storage_alloc.h => allocation.h} (100%) rename include/codegen/{lowering.h => selection.h} (97%) diff --git a/examples/matrix.cpp b/examples/matrix.cpp index 925c9edd6..89d4aa9ec 100644 --- a/examples/matrix.cpp +++ b/examples/matrix.cpp @@ -3,7 +3,7 @@ #include "ast/ast.h" #include "ir/context.h" #include "ir/module.h" -#include "codegen/lowering.h" +#include "codegen/selection.h" #include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/Module.h" #include "llvm/IR/LLVMContext.h" @@ -20,15 +20,8 @@ extern translation_unit *ast_root; const char src[] = "\ void test(fp32 *A, fp32 *B, fp32 *C, int32 i){\ - int32 j = 1;\ - int32 k;\ - i = i + j;\ - for(k = 0; k < 10; k = k+5){\ - int32 u = 1;\ - u = u + i;\ - if(k == 0)\ - j = u + 2;\ - }\ + i = 1;\ + A = A + i;\ }\ "; diff --git a/include/codegen/storage_alloc.h b/include/codegen/allocation.h similarity index 100% rename from include/codegen/storage_alloc.h rename to include/codegen/allocation.h diff --git a/include/codegen/lowering.h b/include/codegen/selection.h similarity index 97% rename from include/codegen/lowering.h rename to include/codegen/selection.h index b3c62c685..5c2bff41e 100644 --- a/include/codegen/lowering.h +++ b/include/codegen/selection.h @@ -1,5 +1,5 @@ -#ifndef TDL_INCLUDE_IR_CODEGEN_LOWERING_H -#define TDL_INCLUDE_IR_CODEGEN_LOWERING_H +#ifndef TDL_INCLUDE_CODEGEN_SELECTION_H +#define TDL_INCLUDE_CODEGEN_SELECTION_H #include "llvm/IR/Module.h" #include "llvm/IR/IRBuilder.h" @@ -139,9 +139,8 @@ Value* llvm_value(ir::value *v, LLVMContext &ctx, return vmap.at(v); // create operands if(auto *uu = dynamic_cast(v)) - for(ir::value* u: uu->ops()){ - vmap[u] = llvm_value(u, ctx, vmap, bmap); - } + for(ir::value* u: uu->ops()) + vmap[u] = llvm_value(u, ctx, vmap, bmap); if(auto *cc = dynamic_cast(v)) return llvm_constant(cc, ctx); // instruction diff --git a/include/ir/instructions.h b/include/ir/instructions.h index 920c47c85..875b2b0a0 100644 --- a/include/ir/instructions.h +++ b/include/ir/instructions.h @@ -266,7 +266,7 @@ public: op_iterator idx_end() { return op_end(); } // factory methods - static getelementptr_inst* create(type *pointee_ty, value *ptr, const std::vector &idx, + static getelementptr_inst* create(value *ptr, const std::vector &idx, const std::string &name = "", instruction *next = nullptr); private: diff --git a/lib/ast/lowering.cpp b/lib/ast/lowering.cpp index 79a589776..9dcbb2826 100644 --- a/lib/ast/lowering.cpp +++ b/lib/ast/lowering.cpp @@ -111,7 +111,8 @@ ir::value* function_definition::codegen(ir::module *mod) const{ /* Statements */ ir::value* compound_statement::codegen(ir::module* mod) const{ - decls_->codegen(mod); + if(decls_) + decls_->codegen(mod); if(statements_) statements_->codegen(mod); return nullptr; diff --git a/lib/ir/builder.cpp b/lib/ir/builder.cpp index a2c7ef809..ccdf49141 100644 --- a/lib/ir/builder.cpp +++ b/lib/ir/builder.cpp @@ -168,7 +168,7 @@ DEFINE_UNARY_INT(not) //===----------------------------------------------------------------------===// value* builder::create_gep(value *ptr, const std::vector& idx_list, const std::string &name){ - return insert(getelementptr_inst::create(nullptr, ptr, idx_list), name); + return insert(getelementptr_inst::create(ptr, idx_list), name); } //===----------------------------------------------------------------------===// diff --git a/lib/ir/instructions.cpp b/lib/ir/instructions.cpp index 7f8f9d1c5..a46c4f036 100644 --- a/lib/ir/instructions.cpp +++ b/lib/ir/instructions.cpp @@ -236,7 +236,7 @@ cond_branch_inst::cond_branch_inst(basic_block *if_dst, basic_block *else_dst, v //===----------------------------------------------------------------------===// getelementptr_inst::getelementptr_inst(type *pointee_ty, value *ptr, const std::vector &idx, const std::string &name, instruction *next) - : instruction(get_return_type(pointee_ty, ptr, idx), idx.size(), name, next), + : instruction(get_return_type(pointee_ty, ptr, idx), 1 + idx.size(), name, next), source_elt_ty(pointee_ty), res_elt_ty(get_indexed_type(pointee_ty, idx)){ type *expected_ty = ((pointer_type*)(get_type()->get_scalar_ty()))->get_element_ty(); @@ -283,7 +283,8 @@ type *getelementptr_inst::get_indexed_type(type *ty, const std::vector return result; } -getelementptr_inst *getelementptr_inst::create(type *pointee_ty, value *ptr, const std::vector &idx, const std::string &name, instruction *next) { +getelementptr_inst *getelementptr_inst::create(value *ptr, const std::vector &idx, const std::string &name, instruction *next) { + type *pointee_ty = ((pointer_type*)(ptr->get_type()->get_scalar_ty()))->get_element_ty(); return new getelementptr_inst(pointee_ty, ptr, idx, name, next); }