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

Paul's avatar
Paul committed
9
namespace migraphx {
Paul's avatar
Paul committed
10
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
11

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

32
void dead_code_elimination::apply(module& p) const
Paul's avatar
Paul committed
33
{
34
35
    auto last = std::prev(p.end());
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
36
    {
Paul's avatar
Paul committed
37
38
39
40
        // Skip the first instruction, since we always process the previous
        // instruction
        if(ins == p.begin())
            continue;
41
        const auto i = std::prev(ins);
Paul's avatar
Paul committed
42
        // Skip the last instruction
43
        if(i == last)
Paul's avatar
Paul committed
44
            break;
Paul's avatar
Paul committed
45
46
47
        // Skip instruction with empty shape as output unless its a builtin or undefined or identity
        if(i->get_shape().elements() == 0 and i->name().front() != '@' and
           i->name() != "undefined" and i->name() != "identity")
Paul's avatar
Paul committed
48
49
            continue;
        assert(bidistance(p, i, last) > 0);
50
        fix([&](auto self, auto leaf) {
Shucai Xiao's avatar
Shucai Xiao committed
51
52
53
            if(not p.has_instruction(leaf))
                return;

Paul's avatar
Paul committed
54
            if(leaf->outputs().empty())
Paul's avatar
Paul committed
55
            {
Paul's avatar
Paul committed
56
57
                std::unordered_set<instruction_ref> args(leaf->inputs().begin(),
                                                         leaf->inputs().end());
Paul's avatar
Paul committed
58
                leaf->clear_arguments();
Paul's avatar
Paul committed
59
60
                assert(bidistance(p, last, leaf) < 0);
                assert(leaf != ins);
61
                p.move_instruction(leaf, p.end());
Paul's avatar
Paul committed
62
                for(auto arg : args)
Paul's avatar
Paul committed
63
64
                    self(arg);
            }
65
        })(i);
Paul's avatar
Paul committed
66
    }
67
    p.remove_instructions(std::next(last), p.end());
Paul's avatar
Paul committed
68
69
}

Paul's avatar
Paul committed
70
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
71
} // namespace migraphx