eliminate_allocation.cpp 1.27 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
#include <migraphx/eliminate_allocation.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
7
8
9
10
#include <migraphx/serialize.hpp>

#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
11
#include <migraphx/pass_config.hpp>
12

Paul's avatar
Paul committed
13
namespace migraphx {
Paul's avatar
Paul committed
14
inline namespace MIGRAPHX_INLINE_NS {
15

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

20
21
    int n = 0;
    std::vector<std::pair<instruction_ref, int>> allocs;
22
23
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
24
        if(ins->name() != allocation_op)
25
26
            continue;
        allocs.emplace_back(ins, n);
Paul's avatar
Paul committed
27
        std::size_t size    = ins->get_shape().bytes();
Paul's avatar
Paul committed
28
29
        std::size_t padding = (alignment - (size % alignment)) % alignment;
        n += size + padding;
30
    }
Paul's avatar
Paul committed
31
    if(n > 0)
32
    {
Paul's avatar
Paul committed
33
34
35
36
37
38
        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;
39
40
            p.replace_instruction(
                ins, make_op("load", {{"shape", to_value(s)}, {"offset", offset}}), mem);
Paul's avatar
Paul committed
41
        }
42
43
    }
}
44

Paul's avatar
Paul committed
45
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
46
} // namespace migraphx