"vscode:/vscode.git/clone" did not exist on "8421734fb8cb0cf3e04ca657a846ae50b3ce2eb3"
constant_propagate.cpp 1.13 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
#include <migraphx/constant_propagate.hpp>
#include <migraphx/program.hpp>
#include <migraphx/matcher.hpp>
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/functional.hpp>
Paul's avatar
Paul committed
6

Paul's avatar
Paul committed
7
namespace migraphx {
Paul's avatar
Paul committed
8
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
bool skip_propogate(instruction_ref ins)
Paul's avatar
Paul committed
11
{
Paul's avatar
Paul committed
12
    if(ins->name() == "@literal")
Paul's avatar
Paul committed
13
        return true;
Paul's avatar
Paul committed
14
    if(ins->get_shape().broadcasted() and not ins->get_shape().scalar())
Paul's avatar
Paul committed
15
        return true;
Paul's avatar
Paul committed
16
    if(ins->get_shape().scalar() and ins->get_shape().elements() != 1)
Paul's avatar
Paul committed
17
18
19
        return true;
    return false;
}
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
void constant_propagate::apply(program& p) const
Paul's avatar
Paul committed
22
23
{
    fix([&](auto self, auto ins) {
Paul's avatar
Paul committed
24
        if(not skip_propogate(ins))
Paul's avatar
Paul committed
25
26
        {
            auto r = ins->eval();
Paul's avatar
Paul committed
27
            if(not r.empty())
Paul's avatar
Paul committed
28
            {
Paul's avatar
Paul committed
29
                assert(r.get_shape() == ins->get_shape());
Paul's avatar
Paul committed
30
31
32
33
34
35
                auto l = p.add_literal(r.get_shape(), r.data());
                p.replace_instruction(ins, l);
                return;
            }
        }
        auto children = ins->inputs();
Paul's avatar
Paul committed
36
        for(auto child : children)
Paul's avatar
Paul committed
37
38
39
            self(child);
    })(std::prev(p.end()));
}
Paul's avatar
Paul committed
40

Paul's avatar
Paul committed
41
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
42
} // namespace migraphx