"...python/carver/test_tilelang_carver_recommend_hints.py" did not exist on "2411fa28f053beea411f7fd595f181065008291f"
dead_code_elimination.cpp 1.21 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
#include <migraph/dead_code_elimination.hpp>
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/functional.hpp>

namespace migraph {

void dead_code_elimination::apply(program& p) const
{
11
12
    auto last = std::prev(p.end());
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
13
    {
Paul's avatar
Paul committed
14
15
16
17
        // Skip the first instruction, since we always process the previous
        // instruction
        if(ins == p.begin())
            continue;
18
        const auto i = std::prev(ins);
Paul's avatar
Paul committed
19
        // Skip instruction with empty shape as output unless its a builtin
Paul's avatar
Paul committed
20
        if(i->get_shape().elements() == 0 and not(i->name().front() == '@'))
21
            continue;
Paul's avatar
Paul committed
22
        // Skip the last instruction
23
        if(i == last)
Paul's avatar
Paul committed
24
            break;
25
26
        fix([&](auto self, auto leaf) {
            assert(p.has_instruction(leaf));
Paul's avatar
Paul committed
27
            if(leaf->outputs().empty())
Paul's avatar
Paul committed
28
            {
Paul's avatar
Paul committed
29
                auto args = leaf->inputs();
Paul's avatar
Paul committed
30
                leaf->clear_arguments();
31
                p.move_instruction(leaf, p.end());
Paul's avatar
Paul committed
32
                for(auto arg : args)
Paul's avatar
Paul committed
33
34
                    self(arg);
            }
35
        })(i);
Paul's avatar
Paul committed
36
    }
37
    p.remove_instructions(std::next(last), p.end());
Paul's avatar
Paul committed
38
39
40
}

} // namespace migraph