eliminate_allocation.cpp 1.2 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
#include <migraphx/eliminate_allocation.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>
9

Paul's avatar
Paul committed
10
namespace migraphx {
Paul's avatar
Paul committed
11
inline namespace MIGRAPHX_INLINE_NS {
12
13
14

void eliminate_allocation::apply(program& p) const
{
Paul's avatar
Paul committed
15
    assert(alignment > 0);
mei-ye's avatar
mei-ye committed
16

17
18
19
20
    std::size_t n = 0;
    std::vector<std::pair<instruction_ref, std::size_t>> allocs;
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
21
        if(ins->name() != allocation_op)
22
23
            continue;
        allocs.emplace_back(ins, n);
Paul's avatar
Paul committed
24
        std::size_t size    = ins->get_shape().bytes();
Paul's avatar
Paul committed
25
26
        std::size_t padding = (alignment - (size % alignment)) % alignment;
        n += size + padding;
27
    }
Paul's avatar
Paul committed
28
    if(n > 0)
29
    {
Paul's avatar
Paul committed
30
31
32
33
34
35
36
37
        auto mem = p.add_parameter("memory", shape{shape::int8_type, {n}});
        for(auto&& pp : allocs)
        {
            auto ins    = pp.first;
            auto s      = ins->get_shape();
            auto offset = pp.second;
            p.replace_instruction(ins, op::load{s, offset}, mem);
        }
38
39
    }
}
40

Paul's avatar
Paul committed
41
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
42
} // namespace migraphx