dead_code_elimination.cpp 2.36 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
33
34
void dead_code_elimination::apply(program& p) const { p.remove_unused_modules(); }

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

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

Paul's avatar
Paul committed
73
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
74
} // namespace migraphx