[intermediate representation] fixed some bugs

This commit is contained in:
Philippe Tillet
2019-01-04 01:43:02 -05:00
parent 88504ca172
commit f131ebb0bc
12 changed files with 55 additions and 31 deletions

View File

@@ -15,11 +15,14 @@ const char src[] =
"\
void test(fp32 *A, fp32 *B, fp32 *C, int32 i){\
int32 j = 1;\
int32 test[16, 16] = 0;\
int32 test2[16, 16];\
int32 test3[16, 16];\
int32 k;\
test = test2 + test3;\
i = i + j;\
for(k = 0; k < 10; k = k+1){\
int32 u = 1;\
u = u + i;\
if(k == 0)\
u = u + 2;\
}\
}\
";

View File

@@ -1,6 +1,7 @@
#ifndef TDL_INCLUDE_IR_BUILDER_H
#define TDL_INCLUDE_IR_BUILDER_H
#include <iostream>
#include <vector>
#include <string>
#include "instructions.h"
@@ -37,9 +38,12 @@ public:
// Insert
template<typename InstTy>
InstTy* insert(InstTy *inst, const std::string &name = ""){
if(block_)
assert(block_);
block_->get_inst_list().insert(insert_point_, inst);
inst->set_parent(block_);
inst->set_name(name);
insert_point_ = block_->end();
return inst;
}
// terminator instructions
value* create_br(basic_block *dest);

View File

@@ -39,8 +39,6 @@ public:
arg_iterator arg_end() { return args_.end(); }
const_arg_iterator arg_begin() const { return args_.begin(); }
const_arg_iterator arg_end() const { return args_.end(); }
// Accessors
function_type* get_function_ty() const;
// Factory methods
static function *create(function_type *ty, linkage_types_t linkage,
const std::string &name, module *mod);
@@ -49,6 +47,7 @@ private:
module *parent_;
args_t args_;
bool init_;
function_type *fn_ty_;
};
}

View File

@@ -23,7 +23,8 @@ protected:
public:
// parent
const basic_block *get_parent() const { return parent_;}
void set_parent(basic_block *block) { parent_ = block; }
const basic_block *get_parent() const { return parent_; }
basic_block *get_parent() { return parent_; }
private:
@@ -148,7 +149,7 @@ public:
typedef llvm::CastInst::CastOps op_t;
private:
bool is_valid(op_t op, value *arg, type *ty);
static bool is_valid(op_t op, value *arg, type *ty);
public:
// Factory methods

View File

@@ -2,6 +2,7 @@
#define TDL_INCLUDE_IR_TYPE_H
#include <vector>
#include <iostream>
namespace tdl{
namespace ir{
@@ -34,7 +35,7 @@ public:
public:
//constructors
type(context &ctx, id_t id) : ctx_(ctx), id_(id) {}
type(context &ctx, id_t id) : ctx_(ctx), id_(id) { }
//destructor
virtual ~type(){}

View File

@@ -24,6 +24,7 @@ public:
void add_use(use *arg);
// name
void set_name(const std::string &name);
const std::string &get_name() const { return name_; }
type* get_type() const { return ty_; }
private:

View File

@@ -310,7 +310,7 @@ inline void implicit_broadcast(ir::module *mod, ir::builder &builder, ir::value
ir::value *binary_operator::llvm_op(ir::module *mod, ir::builder &builder, ir::value *lhs, ir::value *rhs, const std::string &name) const
{
bool is_float = false, is_ptr = false, is_int = false, is_signed = false;
// implicit_cast(builder, lhs, rhs, is_float, is_ptr, is_int, is_signed);
implicit_cast(builder, lhs, rhs, is_float, is_ptr, is_int, is_signed);
// implicit_broadcast(mod, builder, lhs, rhs);
if(op_==MUL && is_float)
return builder.create_fmul(lhs, rhs, name);

View File

@@ -9,18 +9,22 @@ class phi_node;
basic_block::basic_block(context &ctx, const std::string &name, function *parent):
value(type::get_label_ty(ctx), name), ctx_(ctx), parent_(parent){
}
basic_block* basic_block::create(context &ctx, const std::string &name, function *parent){
return new basic_block(ctx, name, parent);
}
void basic_block::add_predecessor(basic_block *pred) {
preds_.push_back(pred);
}
basic_block::iterator basic_block::get_first_non_phi(){
auto it = begin();
for(; it != end(); it++)
if(!dynamic_cast<phi_node*>(*it))
if(!dynamic_cast<phi_node*>(*it)){
return it;
}
return it;
}

View File

@@ -35,16 +35,25 @@ value *builder::get_int32(unsigned val) {
return constant_int::get(type::get_int32_ty(ctx_), val);
}
type *builder::get_float_ty()
{ return type::get_float_ty(ctx_); }
type *builder::get_double_ty()
{ return type::get_double_ty(ctx_); }
//===----------------------------------------------------------------------===//
// terminator instructions
//===----------------------------------------------------------------------===//
value* builder::create_br(basic_block *dest){
dest->add_predecessor(block_);
return insert(branch_inst::create(dest));
}
value* builder::create_cond_br(value *cond, basic_block *if_dest, basic_block *else_dest){
if_dest->add_predecessor(block_);
else_dest->add_predecessor(block_);
return insert(branch_inst::create(cond, if_dest, else_dest));
}

View File

@@ -18,16 +18,16 @@ argument *argument::create(type *ty, const std::string &name,
/* function */
function::function(function_type *ty, linkage_types_t linkage,
const std::string &name, module *parent)
: global_object(ty, 0, linkage, name), parent_(parent) {
: global_object(ty, 0, linkage, name), parent_(parent), fn_ty_(ty) {
unsigned num_params = fn_ty_->get_num_params();
// skip if no parameter
if(num_params == 0)
return;
// create arguments
function_type *fn_ty = get_function_ty();
unsigned num_params = fn_ty->get_num_params();
if(num_params > 0) {
args_.resize(num_params);
for(unsigned i = 0; i < num_params; i++){
type *param_ty = fn_ty->get_param_ty(i);
args_.push_back(argument::create(param_ty, "", this, i));
}
type *param_ty = fn_ty_->get_param_ty(i);
args_[i] = argument::create(param_ty, "", this, i);
}
}
@@ -38,10 +38,6 @@ function *function::create(function_type *ty, linkage_types_t linkage,
}
function_type* function::get_function_ty() const
{ return static_cast<function_type*>(get_type()); }
}
}

View File

@@ -26,7 +26,7 @@ instruction::instruction(type *ty, unsigned num_ops, const std::string &name, in
//===----------------------------------------------------------------------===//
phi_node::phi_node(type *ty, unsigned num_reserved, std::string const &name, instruction *next)
: instruction(ty, num_reserved, name, next){ }
: instruction(ty, num_reserved, name, next), blocks_(num_reserved){ }
// Set incoming value
void phi_node::set_incoming_value(unsigned i, value *v){
@@ -152,6 +152,11 @@ unary_inst::unary_inst(type *ty, value *v, const std::string &name, instruction
// cast_inst classes
//===----------------------------------------------------------------------===//
// TODO
bool cast_inst::is_valid(op_t op, value *arg, type *ty) {
return true;
}
cast_inst *cast_inst::create(op_t op, value *arg, type *ty, const std::string &name, instruction *next){
assert(is_valid(op, arg, ty) && "Invalid cast!");
// Construct and return the appropriate CastInst subclass

View File

@@ -1,5 +1,6 @@
#include "ir/basic_block.h"
#include "ir/module.h"
#include "ir/type.h"
namespace tdl{
namespace ir{
@@ -28,10 +29,10 @@ void module::set_value(const std::string& name, ir::value *value){
ir::phi_node* module::make_phi(ir::type *ty, unsigned num_values, ir::basic_block *block){
basic_block::iterator insert = block->get_first_non_phi();
if(insert == block->end())
if(*insert)
builder_.set_insert_point(insert);
ir::phi_node *res = builder_.create_phi(ty, num_values);
if(insert == block->end())
if(*insert)
builder_.set_insert_point(block);
return res;
}