dead_code_elimination.cpp 1.95 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#include <migraph/dead_code_elimination.hpp>
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/functional.hpp>
Paul's avatar
Paul committed
6
#include <migraph/ranges.hpp>
Paul's avatar
Paul committed
7
8

namespace migraph {
9
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
template <class Range, class Iterator>
Paul's avatar
Paul committed
12
13
std::ptrdiff_t bidistance(const Range& r, Iterator start, Iterator last)
{
Paul's avatar
Paul committed
14
    auto start_forward   = start;
Paul's avatar
Paul committed
15
    auto start_backwards = start;
Paul's avatar
Paul committed
16
    std::size_t n        = 0;
Paul's avatar
Paul committed
17
18
19
    while(start_forward != last and start_backwards != last)
    {
        n++;
Paul's avatar
Paul committed
20
21
22
23
        if(start_forward != r.end())
            start_forward++;
        if(start_backwards != r.begin())
            start_backwards--;
Paul's avatar
Paul committed
24
25
26
27
28
29
30
    }
    if(start_forward == last)
        return n;
    else
        return -n;
}

Paul's avatar
Paul committed
31
32
void dead_code_elimination::apply(program& p) const
{
33
34
    auto last = std::prev(p.end());
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
35
    {
Paul's avatar
Paul committed
36
37
38
39
        // Skip the first instruction, since we always process the previous
        // instruction
        if(ins == p.begin())
            continue;
40
        const auto i = std::prev(ins);
Paul's avatar
Paul committed
41
        // Skip the last instruction
42
        if(i == last)
Paul's avatar
Paul committed
43
            break;
Paul's avatar
Paul committed
44
45
46
47
        // Skip instruction with empty shape as output unless its a builtin
        if(i->get_shape().elements() == 0 and not(i->name().front() == '@'))
            continue;
        assert(bidistance(p, i, last) > 0);
48
49
        fix([&](auto self, auto leaf) {
            assert(p.has_instruction(leaf));
Paul's avatar
Paul committed
50
            if(leaf->outputs().empty())
Paul's avatar
Paul committed
51
            {
Paul's avatar
Paul committed
52
                auto args = leaf->inputs();
Paul's avatar
Paul committed
53
                leaf->clear_arguments();
Paul's avatar
Paul committed
54
55
                assert(bidistance(p, last, leaf) < 0);
                assert(leaf != ins);
56
                p.move_instruction(leaf, p.end());
Paul's avatar
Paul committed
57
                for(auto arg : args)
Paul's avatar
Paul committed
58
59
                    self(arg);
            }
60
        })(i);
Paul's avatar
Paul committed
61
    }
62
    p.remove_instructions(std::next(last), p.end());
Paul's avatar
Paul committed
63
64
}

65
} // inline namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
66
} // namespace migraph