"src/vscode:/vscode.git/clone" did not exist on "eab47249c0573771784f8f3b6659b9e720c0ad81"
Commit 558ca0fe authored by Ted Themistokleous's avatar Ted Themistokleous
Browse files

First attempt at adding check for div zero.

I may need additional checks for this, or to somehow find the matching division
by zero, and cause a dangling reference so this gets flagged correctly at compile time.
Current attempt inserts a divzero instruction that would later get picked up at the verify stage
during compile. Not sure if this is correct incase we run into operator collisions down the road
parent c3edd50e
......@@ -182,6 +182,7 @@ struct module
instruction_ref validate() const;
instruction_ref find_dangling_reference() const;
instruction_ref find_division_by_zero() const;
void finalize(context& ctx);
......
......@@ -628,6 +628,29 @@ instruction_ref module::find_dangling_reference() const
return end();
}
bool is_div_zero(instruction_ref ins)
{
const auto& op = instruction::get_output_alias(ins)->name();
return op == "@divzero";
}
instruction_ref module::find_division_by_zero() const
{
auto last = std::prev(end());
if(last->name() == "@divzero")
{
auto div_zero = std::find_if(
last->inputs().begin(), last->inputs().end(), [](auto x) { return is_div_zero(x); });
if(div_zero != last->inputs().end())
return *div_zero;
}
else if(is_div_zero(last))
{
return last;
}
return end();
}
void module::finalize(context& ctx)
{
const bool trace = enabled(MIGRAPHX_TRACE_FINALIZE{});
......
......@@ -186,6 +186,7 @@ void program::compile(const target& t, compile_options options)
MIGRAPHX_THROW("Invalid module " + mod->name() + " from compilation at instruction " +
std::to_string(std::distance(mod->begin(), invalid)));
}
auto dangling = mod->find_dangling_reference();
if(dangling != mod->end())
{
......@@ -193,6 +194,15 @@ void program::compile(const target& t, compile_options options)
MIGRAPHX_THROW("Dangling reference in module " + mod->name() + " from instruction " +
std::to_string(index));
}
auto divide_by_zero = mod->find_division_by_zero();
if(divide_by_zero != mod->end())
{
auto index = std::distance(mod->begin(), divide_by_zero);
MIGRAPHX_THROW("Division by zero reference in module " + mod->name() +
" from instruction " + std::to_string(index));
}
mod->finalize(this->impl->ctx);
}
}
......
......@@ -856,12 +856,13 @@ struct find_zero_div_const
auto matcher() const
{
return match::name("div")(
match::arg(1)(match::skip_broadcasts_converts(match::has_value(0.0f))));
match::arg(1)(match::skip_broadcasts_converts(match::has_value(0.0f).bind("c"))));
}
[[noreturn]] void apply(module&, const match::matcher_result&) const
void apply(module& m, const match::matcher_result& r) const
{
MIGRAPHX_THROW("ERROR: Matched division by zero in pass");
auto ins = r.result;
m.replace_instruction(ins, make_op("divzero"));
}
};
......
......@@ -1105,17 +1105,14 @@ TEST_CASE(simplify_div_zero_const)
m1.add_instruction(migraphx::make_op("div"), x, unit);
}
bool result = false;
try
{
run_pass(m1);
}
catch(const std::runtime_error& e)
migraphx::module m2;
{
(void)e;
result = true;
auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
auto unit = m1.add_literal(0);
m1.add_instruction(migraphx::make_op("divzero"), x, unit);
}
EXPECT(result);
EXPECT(m1 == m2);
}
TEST_CASE(simplify_sub_const)
......
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