"...targets/git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "651ea160b2c2445250dbc03bb9ad5030d91d59f5"
eliminate_allocation.cpp 1011 Bytes
Newer Older
Paul's avatar
Paul committed
1
#include <migraph/eliminate_allocation.hpp>
2
3
4
5
6
7
8
9
10
11
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>

namespace migraph {

void eliminate_allocation::apply(program& p) const
{
Paul's avatar
Paul committed
12
    assert(alignment > 0);
13
14
15
16
    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
17
        if(ins->op.name() != allocation_op)
18
19
            continue;
        allocs.emplace_back(ins, n);
Paul's avatar
Paul committed
20
        std::size_t size    = ins->get_shape().bytes();
Paul's avatar
Paul committed
21
22
        std::size_t padding = (alignment - (size % alignment)) % alignment;
        n += size + padding;
23
24
25
26
    }
    auto mem = p.add_parameter("memory", shape{shape::int8_type, {n}});
    for(auto&& pp : allocs)
    {
Paul's avatar
Paul committed
27
28
        auto ins    = pp.first;
        auto s      = ins->get_shape();
29
        auto offset = pp.second;
Paul's avatar
Paul committed
30
        p.replace_instruction(ins, load{s, offset}, mem);
31
32
33
    }
}
} // namespace migraph