2019-08-16 15:56:58 -07:00
|
|
|
#include <algorithm>
|
2019-09-20 16:01:12 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include "triton/codegen/transform/cts.h"
|
|
|
|
#include "triton/codegen/instructions.h"
|
2019-04-25 16:17:36 -04:00
|
|
|
#include "triton/ir/module.h"
|
|
|
|
#include "triton/ir/function.h"
|
|
|
|
#include "triton/ir/basic_block.h"
|
|
|
|
#include "triton/ir/instructions.h"
|
|
|
|
#include "triton/ir/type.h"
|
|
|
|
|
|
|
|
namespace triton {
|
|
|
|
|
|
|
|
namespace codegen{
|
2019-08-07 21:15:54 -07:00
|
|
|
namespace analysis{
|
2019-04-25 16:17:36 -04:00
|
|
|
|
|
|
|
// run pass on module
|
2019-09-20 16:01:12 -04:00
|
|
|
bool cts::is_loop_latch(ir::phi_node *phi, ir::instruction *terminator){
|
2019-04-25 16:17:36 -04:00
|
|
|
if(phi->get_parent() != terminator->get_parent())
|
|
|
|
return false;
|
|
|
|
if(auto *br = dynamic_cast<ir::cond_branch_inst*>(terminator))
|
|
|
|
return br->get_true_dest() == phi->get_parent()
|
|
|
|
|| br->get_false_dest() == phi->get_parent();
|
2019-08-18 14:08:57 -07:00
|
|
|
else if(dynamic_cast<ir::uncond_branch_inst*>(terminator))
|
2019-04-25 16:17:36 -04:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("unreachable");
|
|
|
|
}
|
|
|
|
|
2019-09-13 14:17:21 -04:00
|
|
|
|
|
|
|
|
2019-04-25 16:17:36 -04:00
|
|
|
inline bool get_is_shared(ir::value* v) {
|
2019-08-18 14:08:57 -07:00
|
|
|
if(dynamic_cast<ir::atomic_cas_inst*>(v))
|
2019-04-25 16:17:36 -04:00
|
|
|
return true;
|
2019-08-18 14:08:57 -07:00
|
|
|
if(dynamic_cast<ir::trans_inst*>(v))
|
2019-04-25 16:17:36 -04:00
|
|
|
return true;
|
2019-08-18 14:08:57 -07:00
|
|
|
if(dynamic_cast<ir::copy_to_shared_inst*>(v))
|
2019-04-25 16:17:36 -04:00
|
|
|
return true;
|
2019-08-18 14:08:57 -07:00
|
|
|
if(dynamic_cast<ir::reduce_inst*>(v))
|
2019-07-08 18:44:37 -07:00
|
|
|
return true;
|
2019-08-18 14:08:57 -07:00
|
|
|
if(auto *x = dynamic_cast<ir::phi_node*>(v)){
|
2019-04-25 16:17:36 -04:00
|
|
|
bool res = true;
|
|
|
|
for(unsigned inc = 0; inc < x->get_num_incoming(); inc++)
|
|
|
|
res = res && get_is_shared(x->get_incoming_value(inc));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-20 16:01:12 -04:00
|
|
|
void add_copy(ir::instruction *parent, ir::value *x, ir::builder &builder) {
|
|
|
|
auto *i = dynamic_cast<ir::instruction*>(x);
|
|
|
|
// not an instruction
|
|
|
|
if(!i) {
|
|
|
|
builder.set_insert_point(parent);
|
|
|
|
ir::value *cts = builder.create_copy_to_shared(x);
|
|
|
|
parent->replace_uses_of_with(x, cts);
|
|
|
|
return;
|
2019-04-25 16:17:36 -04:00
|
|
|
}
|
2019-09-20 16:01:12 -04:00
|
|
|
// phi node
|
|
|
|
if(auto* phi = dynamic_cast<ir::phi_node*>(x)) {
|
|
|
|
for(unsigned i = 0; i < phi->get_num_incoming(); ++i)
|
|
|
|
add_copy(phi, phi->get_incoming_value(i), builder);
|
|
|
|
return;
|
2019-04-25 16:17:36 -04:00
|
|
|
}
|
2019-09-20 16:01:12 -04:00
|
|
|
ir::value_id_t id = i->get_id();
|
|
|
|
// already in shared memory
|
|
|
|
if(storage_info.at(id).first == SHARED)
|
|
|
|
return;
|
|
|
|
// copy
|
|
|
|
builder.set_insert_point_after(i);
|
|
|
|
ir::value *cts = builder.create_copy_to_shared(x);
|
|
|
|
parent->replace_uses_of_with(x, cts);
|
2019-04-25 16:17:36 -04:00
|
|
|
}
|
|
|
|
|
2019-09-20 16:01:12 -04:00
|
|
|
void cts::run(ir::module &mod) {
|
2019-09-19 16:25:36 -04:00
|
|
|
shared_.clear();
|
|
|
|
refs_.clear();
|
|
|
|
double_.clear();
|
2019-09-14 02:36:11 -04:00
|
|
|
|
2019-04-25 16:17:36 -04:00
|
|
|
// Add shared copies
|
2019-09-20 16:01:12 -04:00
|
|
|
ir::builder &builder = mod.get_builder();
|
2019-04-25 16:17:36 -04:00
|
|
|
for(ir::function *fn: mod.get_function_list()){
|
|
|
|
for(ir::basic_block *block: fn->blocks())
|
|
|
|
for(ir::instruction *i: block->get_inst_list()){
|
2019-09-20 16:01:12 -04:00
|
|
|
auto storage = storage_info.at(i->get_id());
|
|
|
|
// copy to shared operands when necessary
|
|
|
|
for(size_t k = 0; k < storage.second.size(); k++)
|
|
|
|
if(storage.second[k] == SHARED)
|
|
|
|
add_copy(i, i->get_operand(k), builder);
|
2019-04-25 16:17:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find which buffers are shared
|
|
|
|
for(ir::function *fn: mod.get_function_list())
|
|
|
|
for(ir::basic_block *block: fn->blocks())
|
|
|
|
for(ir::instruction *i: block->get_inst_list())
|
|
|
|
if(get_is_shared(i))
|
|
|
|
shared_.insert(i);
|
|
|
|
|
|
|
|
// double-buffering
|
|
|
|
for(ir::function *fn: mod.get_function_list())
|
|
|
|
for(ir::basic_block *block: fn->blocks())
|
|
|
|
for(ir::instruction *i: block->get_inst_list()) {
|
|
|
|
if(!i->get_type()->is_tile_ty())
|
|
|
|
continue;
|
|
|
|
// handle phi
|
|
|
|
if(auto *phi = dynamic_cast<ir::phi_node*>(i))
|
|
|
|
if(is_shared(phi)){
|
|
|
|
// determine if the value is in shared memory
|
|
|
|
bool is_double = false;
|
|
|
|
for(unsigned n = 0; n < phi->get_num_incoming(); n++){
|
|
|
|
ir::basic_block *inc_block = phi->get_incoming_block(n);
|
|
|
|
ir::instruction *terminator = inc_block->get_inst_list().back();
|
|
|
|
is_double = is_double || is_loop_latch(phi, terminator);
|
|
|
|
}
|
|
|
|
// add to double-buffered
|
|
|
|
if(is_double)
|
|
|
|
double_.insert(phi);
|
|
|
|
// set references of input
|
|
|
|
for(unsigned n = 0; n < phi->get_num_incoming(); n++){
|
|
|
|
ir::value *inc_val = phi->get_incoming_value(n);
|
|
|
|
refs_[inc_val] = phi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query double-buffered status
|
2019-09-20 16:01:12 -04:00
|
|
|
bool cts::is_double(ir::value *x)
|
2019-04-25 16:17:36 -04:00
|
|
|
{ return double_.find(x) != double_.end(); }
|
|
|
|
|
|
|
|
// query shared status
|
2019-09-20 16:01:12 -04:00
|
|
|
bool cts::is_shared(ir::value *x)
|
2019-04-25 16:17:36 -04:00
|
|
|
{ return shared_.find(x) != shared_.end(); }
|
|
|
|
|
|
|
|
// get reference if any
|
2019-09-20 16:01:12 -04:00
|
|
|
ir::value *cts::get_reference(ir::value *x)
|
2019-04-25 16:17:36 -04:00
|
|
|
{ return refs_[x]; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-07 21:15:54 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 16:17:36 -04:00
|
|
|
}
|