dead_code_elimination.cpp 2.06 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

12
13
14
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
15
{
16
17
    auto last = std::prev(m.end());
    for(auto ins : iterator_for(m))
Paul's avatar
Paul committed
18
    {
Paul's avatar
Paul committed
19
20
        // Skip the first instruction, since we always process the previous
        // instruction
21
        if(ins == m.begin())
Paul's avatar
Paul committed
22
            continue;
23
        const auto i = std::prev(ins);
Paul's avatar
Paul committed
24
        // Skip the last instruction
25
        if(i == last)
Paul's avatar
Paul committed
26
            break;
Paul's avatar
Paul committed
27
28
29
        // 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
30
            continue;
31
32
        assert(std::distance(m.begin(), i) <= std::distance(m.begin(), last));
        std::unordered_set<instruction_ref> visited;
33
        fix([&](auto self, auto leaf) {
34
            if(not m.has_instruction(leaf))
Shucai Xiao's avatar
Shucai Xiao committed
35
36
                return;

Paul's avatar
Paul committed
37
            if(leaf->outputs().empty())
Paul's avatar
Paul committed
38
            {
39
40
41
                // Dont visit inputs twice
                if(not visited.insert(leaf).second)
                    return;
Paul's avatar
Paul committed
42
43
                std::unordered_set<instruction_ref> args(leaf->inputs().begin(),
                                                         leaf->inputs().end());
Paul's avatar
Paul committed
44
                leaf->clear_arguments();
45
                assert(std::distance(m.begin(), leaf) < std::distance(m.begin(), last));
Paul's avatar
Paul committed
46
                assert(leaf != ins);
47
48
                if(leaf->name() != "@param")
                    m.move_instruction(leaf, m.end());
Paul's avatar
Paul committed
49
                for(auto arg : args)
Paul's avatar
Paul committed
50
51
                    self(arg);
            }
52
        })(i);
Paul's avatar
Paul committed
53
    }
54
    m.remove_instructions(std::next(last), m.end());
Paul's avatar
Paul committed
55
56
}

Paul's avatar
Paul committed
57
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
58
} // namespace migraphx