Commit c2d4ceb0 authored by Ted Themistokleous's avatar Ted Themistokleous
Browse files

Add support for division by zero errors

Throw an exception when this occurs to indicate our simpliciation passes resulted in a singularity somewhere. Related to #1236
parent 20d2b5d9
......@@ -851,6 +851,19 @@ struct find_div_const
}
};
struct find_zero_div_const
{
auto matcher() const { return match::name("div")(match::arg(1)(match::has_value(0.0f))); }
void apply [[noreturn]] (const module& m, const match::matcher_result& r) const
{
m.debug_print();
std::cout << "ERROR:DIV_BY_ZERO: ";
m.debug_print(r.result);
MIGRAPHX_THROW("ERROR: Matched division by zero in pass");
}
};
struct find_unit_ops
{
auto matcher() const
......@@ -1121,6 +1134,7 @@ void simplify_algebra::apply(module& m) const
find_unit_ops{},
find_neg_unit_ops{},
find_zero_ops{},
find_zero_div_const{},
find_div_const{},
find_sub_const{},
find_rsqrt{},
......
......@@ -890,6 +890,28 @@ TEST_CASE(simplify_sub_neg_zero_const)
EXPECT(m1 == m2);
}
TEST_CASE(simplify_div_zero_const)
{
migraphx::module m1;
{
auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
auto unit = m1.add_literal(0);
m1.add_instruction(migraphx::make_op("div"), x, unit);
}
bool result = false;
try
{
run_pass(m1);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
result = true;
}
EXPECT(result);
}
TEST_CASE(simplify_sub_const)
{
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