eliminate_allocation.cpp 1.21 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);
Paul's avatar
Paul committed
16
    if(!enabled(MIGRAPHX_DISABLE_MEMORY_COLORING{}))
mei-ye's avatar
mei-ye committed
17
18
        return;

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

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