"docs/source/index.rst" did not exist on "37a984a9a0bf758dcd6b7905f2870be0706f72a9"
eliminate_allocation.cpp 1.12 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraph/eliminate_allocation.hpp>
2
3
4
5
6
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>
mei-ye's avatar
mei-ye committed
7
8
#include <migraph/stringutils.hpp>
#include <migraph/pass_config.hpp>
9
10
11
12
13

namespace migraph {

void eliminate_allocation::apply(program& p) const
{
Paul's avatar
Paul committed
14
    assert(alignment > 0);
mei-ye's avatar
mei-ye committed
15
    if(!enabled(MIGRAPH_DISABLE_MEMORY_COLORING{}))
mei-ye's avatar
mei-ye committed
16
17
        return;

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