eliminate_workspace.cpp 1.18 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
#include <migraphx/gpu/eliminate_workspace.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/pass_config.hpp>
10

Paul's avatar
Paul committed
11
namespace migraphx {
Paul's avatar
Paul committed
12
inline namespace MIGRAPHX_INLINE_NS {
13
14
15
16
17
18
19
20
namespace gpu {

void eliminate_workspace::apply(program& p) const
{
    std::size_t n = 0;
    std::vector<instruction_ref> allocs;
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
21
        if(ins->outputs().size() != 1)
22
            continue;
Paul's avatar
Paul committed
23
        if(ins->name() != "hip::allocate")
24
            continue;
25
        auto&& a = any_cast<hip_allocate>(ins->get_operator());
Paul's avatar
Paul committed
26
        if(a.tag == "workspace")
27
28
29
30
31
        {
            n = std::max(n, ins->get_shape().bytes());
            allocs.push_back(ins);
        }
    }
Paul's avatar
Paul committed
32
    if(n > 0)
33
    {
Paul's avatar
Paul committed
34
35
36
37
38
39
        auto ws = p.add_parameter("workspace", shape{shape::int8_type, {n}});
        for(auto&& a : allocs)
        {
            p.replace_instruction(a, ws);
            p.remove_instruction(a);
        }
40
41
    }
}
42

43
} // namespace gpu
Paul's avatar
Paul committed
44
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
45
} // namespace migraphx