eliminate_identity_test.cpp 2.18 KB
Newer Older
1
2
3
4
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/instruction.hpp>
#include <basic_ops.hpp>
5
#include <migraphx/op/identity.hpp>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <test.hpp>

struct eliminate_identity_target
{
    std::string name() const { return "eliminate_identity"; }
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
    {
        return {migraphx::eliminate_identity{}};
    }
    migraphx::context get_context() const { return {}; }
};

TEST_CASE(simple_test)
{
    migraphx::program p;

Khalique's avatar
Khalique committed
22
    auto one          = p.add_literal(1);
23
    auto one_identity = p.add_instruction(migraphx::op::identity{}, one);
Khalique's avatar
Khalique committed
24
    auto two          = p.add_literal(2);
25
26
27
    auto two_identity = p.add_instruction(migraphx::op::identity{}, two);
    p.add_instruction(sum_op{}, one_identity, two_identity);
    p.compile(eliminate_identity_target{});
Khalique's avatar
Khalique committed
28
29
30
    EXPECT(std::none_of(p.begin(), p.end(), [](const migraphx::instruction& ins) {
        return ins.name() == "identity";
    }));
31
32
33
34
35
36
37
38
39
40
41
42
43
    auto result = p.eval({});
    EXPECT(result == migraphx::literal{3});
}

TEST_CASE(simple_test_end)
{
    migraphx::program p;

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto ans = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(migraphx::op::identity{}, ans);
    p.compile(eliminate_identity_target{});
Khalique's avatar
Khalique committed
44
45
46
    EXPECT(std::none_of(p.begin(), p.end(), [](const migraphx::instruction& ins) {
        return ins.name() == "identity";
    }));
47
48
49
50
51
52
53
54
    auto result = p.eval({});
    EXPECT(result == migraphx::literal{3});
}

TEST_CASE(simple_test_end_dependency)
{
    migraphx::program p;

Khalique's avatar
Khalique committed
55
56
    auto one   = p.add_literal(1.0);
    auto two   = p.add_literal(2.0);
57
    auto three = p.add_literal(3.0);
Khalique's avatar
Khalique committed
58
    auto ans   = p.add_instruction(sum_op{}, one, two);
59
60
61
    p.add_instruction(sum_op{}, ans, three);
    p.add_instruction(migraphx::op::identity{}, ans);
    p.compile(eliminate_identity_target{});
62
    EXPECT(std::any_of(p.begin(), p.end(), [](const migraphx::instruction& ins) {
Khalique's avatar
Khalique committed
63
64
        return ins.name() == "identity";
    }));
65
66
67
68
69
    auto result = p.eval({});
    EXPECT(result == migraphx::literal{3.0});
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }