dead_code_elimination.cpp 2.13 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 <migraphx/stringutils.hpp>
8
#include <unordered_set>
Paul's avatar
Paul committed
9

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

13
14
15
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
16
{
17
18
    auto last = std::prev(m.end());
    for(auto ins : iterator_for(m))
Paul's avatar
Paul committed
19
    {
Paul's avatar
Paul committed
20
21
        // Skip the first instruction, since we always process the previous
        // instruction
22
        if(ins == m.begin())
Paul's avatar
Paul committed
23
            continue;
24
        const auto i = std::prev(ins);
Paul's avatar
Paul committed
25
        // Skip the last instruction
26
        if(i == last)
Paul's avatar
Paul committed
27
            break;
28
29
        // Skip instruction with empty shape as output unless its a builtin, undefined, identity, or
        // allocate
Paul's avatar
Paul committed
30
        if(i->get_shape().elements() == 0 and i->name().front() != '@' and
31
           not contains({"undefined", "identity", "allocate"}, i->name()))
Paul's avatar
Paul committed
32
            continue;
33
34
        assert(std::distance(m.begin(), i) <= std::distance(m.begin(), last));
        std::unordered_set<instruction_ref> visited;
35
        fix([&](auto self, auto leaf) {
36
            if(not m.has_instruction(leaf))
Shucai Xiao's avatar
Shucai Xiao committed
37
38
                return;

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

Paul's avatar
Paul committed
59
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
60
} // namespace migraphx