[codegen/alignment_info] better alignment information
This commit is contained in:
@@ -9,6 +9,18 @@ namespace triton {
|
||||
namespace codegen{
|
||||
|
||||
|
||||
inline int gcd(int a, int b) {
|
||||
if (a == 0)
|
||||
return b;
|
||||
if (b == 0)
|
||||
return a;
|
||||
if (a == b)
|
||||
return a;
|
||||
if (a > b)
|
||||
return gcd(a-b, b);
|
||||
return gcd(a, b-a);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T add_to_cache(ir::value *i, T value, std::map<ir::value*, T> &map) {
|
||||
return map[i] = value;
|
||||
@@ -22,50 +34,69 @@ bool alignment_info::is_first_axis_unit(ir::value *x){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool alignment_info::populate_is_constant(ir::value *v) {
|
||||
alignment_info::cst_info alignment_info::populate_is_constant(ir::value *v) {
|
||||
if(is_constant_.find(v) != is_constant_.end())
|
||||
return is_constant_.at(v);
|
||||
// helper for the cache
|
||||
auto cache = [this,v](bool value){ return add_to_cache(v, value, is_constant_); };
|
||||
auto cache = [this,v](cst_info value){
|
||||
return add_to_cache(v, value, is_constant_); }
|
||||
;
|
||||
// populate
|
||||
if(auto *x = dynamic_cast<ir::retile_inst*>(v)){
|
||||
ir::value *op = x->get_operand(0);
|
||||
populate_is_constant(op);
|
||||
if(is_first_axis_unit(op))
|
||||
return cache(true);
|
||||
auto op_cst = populate_is_constant(op);
|
||||
if(is_first_axis_unit(op)){
|
||||
unsigned num_cst = x->get_type()->get_tile_shapes()[0]->get_value();
|
||||
return cache({num_cst, op_cst.value});
|
||||
}
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::constant_int*>(v))
|
||||
return cache(true);
|
||||
return cache({true, (unsigned)x->get_value()});
|
||||
if(auto *x = dynamic_cast<ir::binary_operator*>(v)){
|
||||
bool lhs = populate_is_constant(x->get_operand(0));
|
||||
bool rhs = populate_is_constant(x->get_operand(1));
|
||||
return cache(lhs && rhs);
|
||||
ir::value* lhs_op = x->get_operand(0);
|
||||
ir::value* rhs_op = x->get_operand(1);
|
||||
cst_info lhs = populate_is_constant(lhs_op);
|
||||
cst_info rhs = populate_is_constant(rhs_op);
|
||||
if(lhs.num_cst==0 && rhs.value && x->is_int_div()){
|
||||
unsigned max_contiguous = populate_max_contiguous(lhs_op);
|
||||
unsigned starting_multiple = populate_starting_multiple(lhs_op);
|
||||
return cache({gcd(max_contiguous, rhs.value) - (starting_multiple % rhs.value), 0});
|
||||
}
|
||||
return cache({std::min(lhs.num_cst, rhs.num_cst), 0});
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::getelementptr_inst*>(v)){
|
||||
ir::value* lhs_op = x->get_operand(0);
|
||||
ir::value* rhs_op = x->get_operand(1);
|
||||
cst_info lhs = populate_is_constant(lhs_op);
|
||||
cst_info rhs = populate_is_constant(rhs_op);
|
||||
return cache({std::min(lhs.num_cst, rhs.num_cst), 0});
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::psi_inst*>(v)){
|
||||
bool value_true = populate_is_constant(x->get_value_true());
|
||||
bool value_false = populate_is_constant(x->get_value_false());
|
||||
return cache(value_true && value_false);
|
||||
cst_info value_true = populate_is_constant(x->get_value_true());
|
||||
cst_info value_false = populate_is_constant(x->get_value_false());
|
||||
return cache({std::min(value_true.num_cst, value_false.num_cst), 0});
|
||||
}
|
||||
if(v->get_type()->is_tile_ty())
|
||||
return cache(false);
|
||||
return cache({0, 0});
|
||||
if(auto *x = dynamic_cast<ir::phi_node*>(v)){
|
||||
// put a conservative initial value in phi node to avoid infinite recursion
|
||||
bool result = true;
|
||||
unsigned result = 1;
|
||||
for(unsigned n = 0; n < x->get_num_incoming(); n++){
|
||||
ir::value* inc = x->get_incoming_value(n);
|
||||
if(is_constant_.find(inc) != is_constant_.end())
|
||||
result = is_constant_.at(inc);
|
||||
result = is_constant_.at(inc).num_cst;
|
||||
}
|
||||
cache(result);
|
||||
cache({result, 0});
|
||||
// recurse
|
||||
for(unsigned n = 0; n < x->get_num_incoming(); n++){
|
||||
ir::value* inc = x->get_incoming_value(n);
|
||||
result = result && populate_is_constant(inc);
|
||||
result = std::min(result, populate_is_constant(inc).num_cst);
|
||||
}
|
||||
return cache(result);
|
||||
return cache({result, 0});
|
||||
}
|
||||
// scalars are always constant in the contiguous dimension
|
||||
return cache(true);
|
||||
// but value is not known at compile-time
|
||||
return cache({1, 0});
|
||||
}
|
||||
|
||||
unsigned alignment_info::populate_max_contiguous(ir::value *v){
|
||||
@@ -95,13 +126,21 @@ unsigned alignment_info::populate_max_contiguous(ir::value *v){
|
||||
ir::value* rhs = x->get_operand(1);
|
||||
unsigned lhs_max_contiguous = populate_max_contiguous(lhs);
|
||||
unsigned rhs_max_contiguous = populate_max_contiguous(rhs);
|
||||
bool lhs_has_cst = populate_is_constant(lhs);
|
||||
bool rhs_has_cst = populate_is_constant(rhs);
|
||||
if(x->is_int_add_sub()){
|
||||
if(lhs_has_cst)
|
||||
return cache(rhs_max_contiguous);
|
||||
if(rhs_has_cst)
|
||||
cst_info lhs_cst_info = populate_is_constant(lhs);
|
||||
cst_info rhs_cst_info = populate_is_constant(rhs);
|
||||
if(x->is_int_rem() && rhs_cst_info.value > 0)
|
||||
return cache(std::min(lhs_max_contiguous, rhs_cst_info.value));
|
||||
if(x->is_int_mult()){
|
||||
if(rhs_cst_info.value == 1)
|
||||
return cache(lhs_max_contiguous);
|
||||
if(lhs_cst_info.value == 1)
|
||||
return cache(rhs_max_contiguous);
|
||||
}
|
||||
if(x->is_int_add_sub()){
|
||||
if(lhs_cst_info.num_cst)
|
||||
return cache(gcd(rhs_max_contiguous, lhs_cst_info.num_cst));
|
||||
if(rhs_cst_info.num_cst)
|
||||
return cache(gcd(lhs_max_contiguous, rhs_cst_info.num_cst));
|
||||
}
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::psi_inst*>(v)){
|
||||
@@ -114,11 +153,11 @@ unsigned alignment_info::populate_max_contiguous(ir::value *v){
|
||||
ir::value* rhs = x->get_operand(1);
|
||||
unsigned lhs_max_contiguous = populate_max_contiguous(lhs);
|
||||
unsigned rhs_max_contiguous = populate_max_contiguous(rhs);
|
||||
bool lhs_has_cst = populate_is_constant(lhs);
|
||||
bool rhs_has_cst = populate_is_constant(rhs);
|
||||
if(lhs_has_cst)
|
||||
auto lhs_cst_info = populate_is_constant(lhs);
|
||||
auto rhs_cst_info = populate_is_constant(rhs);
|
||||
if(lhs_cst_info.num_cst)
|
||||
return cache(rhs_max_contiguous);
|
||||
if(rhs_has_cst)
|
||||
if(rhs_cst_info.num_cst)
|
||||
return cache(lhs_max_contiguous);
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::phi_node*>(v)){
|
||||
@@ -140,22 +179,12 @@ unsigned alignment_info::populate_max_contiguous(ir::value *v){
|
||||
return cache(1);
|
||||
}
|
||||
|
||||
inline int gcd(int a, int b) {
|
||||
if (a == 0)
|
||||
return b;
|
||||
if (b == 0)
|
||||
return a;
|
||||
if (a == b)
|
||||
return a;
|
||||
if (a > b)
|
||||
return gcd(a-b, b);
|
||||
return gcd(a, b-a);
|
||||
}
|
||||
|
||||
unsigned alignment_info::populate_starting_multiple(ir::value *v){
|
||||
if(starting_multiple_.find(v) != starting_multiple_.end())
|
||||
return starting_multiple_.at(v);
|
||||
auto cache = [this,v](unsigned value){ return add_to_cache(v, value, starting_multiple_); };
|
||||
auto cache = [this,v](unsigned value){
|
||||
return add_to_cache(v, value, starting_multiple_);
|
||||
};
|
||||
// has metadata
|
||||
if(auto *x = dynamic_cast<ir::instruction*>(v)){
|
||||
unsigned multiple_of = x->get_metadata(ir::metadata::multiple_of);
|
||||
@@ -185,15 +214,16 @@ unsigned alignment_info::populate_starting_multiple(ir::value *v){
|
||||
return cache(gcd(lhs, rhs));
|
||||
if(x->is_int_div())
|
||||
return cache(std::max(lhs / rhs, 1));
|
||||
if(x->is_int_rem())
|
||||
return cache(std::max(lhs % rhs, 1));
|
||||
if(x->is_int_rem() && rhs > 1)
|
||||
return cache(gcd(lhs, rhs));
|
||||
if(x->is_shl())
|
||||
return cache(lhs << rhs);
|
||||
if(x->is_shr())
|
||||
return cache(std::max(lhs >> rhs, 1));
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::constant_int*>(v))
|
||||
if(auto *x = dynamic_cast<ir::constant_int*>(v)){
|
||||
return cache(x->get_value());
|
||||
}
|
||||
if(auto *x = dynamic_cast<ir::constant_range*>(v)){
|
||||
return cache(x->get_first()->get_value());
|
||||
}
|
||||
@@ -270,7 +300,6 @@ void alignment_info::run(ir::module &mod) {
|
||||
for(ir::basic_block *block: fn->blocks())
|
||||
for(ir::instruction *i: block->get_inst_list()){
|
||||
populate_max_contiguous(i);
|
||||
// std::cout << i->get_name() << " " << max_contiguous_.at(i) << " " << starting_multiple_.at(i) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user