eliminate_identity.cpp 1.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/stringutils.hpp>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

11
void eliminate_identity::apply(module& m) const
12
{
13
14
    auto last = std::prev(m.end());
    for(auto ins : iterator_for(m))
15
    {
16
17
        // Skip the first instruction, since we always process the previous
        // instruction
18
        if(ins == m.begin())
19
20
            continue;
        const auto i = std::prev(ins);
Khalique's avatar
Khalique committed
21

22
23
        if(i->name() == "identity")
        {
24
25
            m.replace_instruction(i, i->inputs().front());
            m.move_instruction(i, m.end());
26
27
28
29
30
        }
        if(ins == last)
        {
            if(ins->name() == "identity")
            {
Khalique's avatar
Khalique committed
31
                const instruction_ref& identity_input = ins->inputs().front();
32
33
                if(identity_input->outputs().size() == 1)
                {
34
                    m.move_instruction(identity_input, i);
35
36
37
                    // since this is the last instruction, removing it only
                    // requires changing "last" and calling remove below
                    last = std::prev(last);
Khalique's avatar
Khalique committed
38
                }
39
40
41
            }
            break;
        }
42
    }
43
    m.remove_instructions(std::next(last), m.end());
44
45
46
47
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx