[intermediate representation] bugfix in getelementptr_inst

This commit is contained in:
Philippe Tillet
2019-01-07 04:08:55 -05:00
parent ce1c0a62c0
commit c48b7fb676
7 changed files with 14 additions and 20 deletions

View File

@@ -3,7 +3,7 @@
#include "ast/ast.h" #include "ast/ast.h"
#include "ir/context.h" #include "ir/context.h"
#include "ir/module.h" #include "ir/module.h"
#include "codegen/lowering.h" #include "codegen/selection.h"
#include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/Module.h" #include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h" #include "llvm/IR/LLVMContext.h"
@@ -20,15 +20,8 @@ extern translation_unit *ast_root;
const char src[] = const char src[] =
"\ "\
void test(fp32 *A, fp32 *B, fp32 *C, int32 i){\ void test(fp32 *A, fp32 *B, fp32 *C, int32 i){\
int32 j = 1;\ i = 1;\
int32 k;\ A = A + i;\
i = i + j;\
for(k = 0; k < 10; k = k+5){\
int32 u = 1;\
u = u + i;\
if(k == 0)\
j = u + 2;\
}\
}\ }\
"; ";

View File

@@ -1,5 +1,5 @@
#ifndef TDL_INCLUDE_IR_CODEGEN_LOWERING_H #ifndef TDL_INCLUDE_CODEGEN_SELECTION_H
#define TDL_INCLUDE_IR_CODEGEN_LOWERING_H #define TDL_INCLUDE_CODEGEN_SELECTION_H
#include "llvm/IR/Module.h" #include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h" #include "llvm/IR/IRBuilder.h"
@@ -139,9 +139,8 @@ Value* llvm_value(ir::value *v, LLVMContext &ctx,
return vmap.at(v); return vmap.at(v);
// create operands // create operands
if(auto *uu = dynamic_cast<ir::user*>(v)) if(auto *uu = dynamic_cast<ir::user*>(v))
for(ir::value* u: uu->ops()){ for(ir::value* u: uu->ops())
vmap[u] = llvm_value(u, ctx, vmap, bmap); vmap[u] = llvm_value(u, ctx, vmap, bmap);
}
if(auto *cc = dynamic_cast<ir::constant*>(v)) if(auto *cc = dynamic_cast<ir::constant*>(v))
return llvm_constant(cc, ctx); return llvm_constant(cc, ctx);
// instruction // instruction

View File

@@ -266,7 +266,7 @@ public:
op_iterator idx_end() { return op_end(); } op_iterator idx_end() { return op_end(); }
// factory methods // factory methods
static getelementptr_inst* create(type *pointee_ty, value *ptr, const std::vector<value*> &idx, static getelementptr_inst* create(value *ptr, const std::vector<value*> &idx,
const std::string &name = "", instruction *next = nullptr); const std::string &name = "", instruction *next = nullptr);
private: private:

View File

@@ -111,7 +111,8 @@ ir::value* function_definition::codegen(ir::module *mod) const{
/* Statements */ /* Statements */
ir::value* compound_statement::codegen(ir::module* mod) const{ ir::value* compound_statement::codegen(ir::module* mod) const{
decls_->codegen(mod); if(decls_)
decls_->codegen(mod);
if(statements_) if(statements_)
statements_->codegen(mod); statements_->codegen(mod);
return nullptr; return nullptr;

View File

@@ -168,7 +168,7 @@ DEFINE_UNARY_INT(not)
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
value* builder::create_gep(value *ptr, const std::vector<value*>& idx_list, const std::string &name){ value* builder::create_gep(value *ptr, const std::vector<value*>& 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);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@@ -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<value *> &idx, const std::string &name, instruction *next) getelementptr_inst::getelementptr_inst(type *pointee_ty, value *ptr, const std::vector<value *> &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), source_elt_ty(pointee_ty),
res_elt_ty(get_indexed_type(pointee_ty, idx)){ res_elt_ty(get_indexed_type(pointee_ty, idx)){
type *expected_ty = ((pointer_type*)(get_type()->get_scalar_ty()))->get_element_ty(); 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<value *>
return result; return result;
} }
getelementptr_inst *getelementptr_inst::create(type *pointee_ty, value *ptr, const std::vector<value *> &idx, const std::string &name, instruction *next) { getelementptr_inst *getelementptr_inst::create(value *ptr, const std::vector<value *> &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); return new getelementptr_inst(pointee_ty, ptr, idx, name, next);
} }