constant_propagate.cpp 869 Bytes
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

Paul's avatar
Paul committed
6
namespace migraphx {
Paul's avatar
Paul committed
7
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
8
9
10
11
12
13
14
15
16
17

struct match_const_add
{
    auto matcher() const
    {
        return match::name("add")(match::args(match::name("@literal"), match::name("@literal")));
    }

    void apply(program& p, match::matcher_result r) const
    {
Paul's avatar
Paul committed
18
        auto ins  = r.result;
Paul's avatar
Paul committed
19
20
21
22
23
24
25
26
        auto arg1 = ins->inputs().at(0)->get_literal();
        auto arg2 = ins->inputs().at(1)->get_literal();

        auto sum = p.add_literal(transform(arg1, arg2, [](auto x, auto y) { return x + y; }));
        p.replace_instruction(ins, sum);
    }
};

Paul's avatar
Paul committed
27
void constant_propagate::apply(program& p) const { match::find_matches(p, match_const_add{}); }
Paul's avatar
Paul committed
28

Paul's avatar
Paul committed
29
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
30
} // namespace migraphx