Commit 20d2b5d9 authored by Ted Themistokleous's avatar Ted Themistokleous
Browse files

Simplify algebra for 0*x, x*0 and 0/x operations

Simplify addition zero multiplication and divide operations. Added approrpiate test cases with returns and replacing the instruction and operand to just return zero.
parent 93f733c3
...@@ -898,6 +898,29 @@ struct find_neg_unit_ops ...@@ -898,6 +898,29 @@ struct find_neg_unit_ops
} }
}; };
struct find_zero_ops
{
auto matcher() const
{
auto mul_zero = match::name("mul")(
match::either_arg(0, 1)(match::has_value(0.0f), match::any().bind("x")));
auto div_zero =
match::name("div")(match::args(match::has_value(0.0f), match::any().bind("x")));
return match::any_of(mul_zero, div_zero);
}
void apply(module& m, const match::matcher_result& r) const
{
auto ins = r.result;
auto x_ins = r.instructions["x"];
auto zero = m.add_literal(0);
auto ret = m.add_return({zero});
m.remove_instruction((x_ins));
m.replace_instruction(ins, ret);
}
};
struct find_sub_const struct find_sub_const
{ {
auto matcher() const auto matcher() const
...@@ -1097,6 +1120,7 @@ void simplify_algebra::apply(module& m) const ...@@ -1097,6 +1120,7 @@ void simplify_algebra::apply(module& m) const
find_mul_add{}, find_mul_add{},
find_unit_ops{}, find_unit_ops{},
find_neg_unit_ops{}, find_neg_unit_ops{},
find_zero_ops{},
find_div_const{}, find_div_const{},
find_sub_const{}, find_sub_const{},
find_rsqrt{}, find_rsqrt{},
......
...@@ -910,6 +910,52 @@ TEST_CASE(simplify_sub_const) ...@@ -910,6 +910,52 @@ TEST_CASE(simplify_sub_const)
EXPECT(m1 == m2); EXPECT(m1 == m2);
} }
TEST_CASE(simplify_zero_mult_const)
{
migraphx::module m1;
{
auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
auto zero = m1.add_literal(0);
m1.add_instruction(migraphx::make_op("mul"), x, zero);
}
run_pass(m1);
migraphx::module m2;
{
auto zero = m2.add_literal(0);
m2.add_return({zero});
}
migraphx::module m3;
{
auto zero = m3.add_literal(0);
auto x = m3.add_parameter("x", {migraphx::shape::int32_type, {1}});
m3.add_instruction(migraphx::make_op("mul"), zero, x);
}
run_pass(m3);
EXPECT((m1 == m3) && (m1 == m2));
}
TEST_CASE(simplify_zero_div_const)
{
migraphx::module m1;
{
auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
auto zero = m1.add_literal(0);
m1.add_instruction(migraphx::make_op("div"), zero, x);
}
run_pass(m1);
migraphx::module m2;
{
auto zero = m2.add_literal(0);
m2.add_return({zero});
}
EXPECT(m1 == m2);
}
TEST_CASE(simplify_rsqrt) TEST_CASE(simplify_rsqrt)
{ {
migraphx::module m1; migraphx::module m1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment