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
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-07 21:15:54 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 16:17:36 -04:00
|
|
|
}
|