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

fixup! Initial working commit of tested compile trap of detected divzero instructions

parent 938792b0
......@@ -628,14 +628,17 @@ instruction_ref module::find_dangling_reference() const
return end();
}
// bool is_div_zero(instruction_ref ins) {return
// instruction::get_output_alias(ins)->get_operator().name() == "divzero";}
bool is_div_zero(instruction ins) { return ins.name() == "divzero"; }
instruction_ref module::flag_division_by_zero() const
{
auto last = std::prev(end());
auto ins = end();
if (last->name() == "divzero") {
ins = begin();
}
return ins;
auto divzero = std::count_if(begin(), end(), [](auto x) { return is_div_zero(x); });
if(divzero > 0)
return begin();
return end();
}
void module::finalize(context& ctx)
......
......@@ -198,9 +198,7 @@ void program::compile(const target& t, compile_options options)
auto divide_by_zero = mod->flag_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));
MIGRAPHX_THROW("Division by zero reference in module " + mod->name() + "");
}
mod->finalize(this->impl->ctx);
......
......@@ -1360,6 +1360,32 @@ TEST_CASE(div_zero_compile_trap_after_no_passes)
EXPECT(result);
}
TEST_CASE(div_zero_compile_trap_long_program_no_passes)
{
migraphx::program p;
auto* mm = p.get_main_module();
auto zero = mm->add_literal(0);
auto one = mm->add_literal(1);
auto x = mm->add_parameter("x", {migraphx::shape::int32_type, {1}});
auto y = mm->add_parameter("y", {migraphx::shape::int32_type, {1}});
auto div0 = mm->add_instruction(migraphx::make_op("divzero"), x, zero);
auto mul = mm->add_instruction(migraphx::make_op("mul"), one, div0);
auto add = mm->add_instruction(migraphx::make_op("add"), y, mul);
mm->add_instruction(migraphx::make_op("sub"), y, add);
bool result = false;
try
{
p.compile(migraphx::ref::target{});
}
catch(const std::runtime_error& e)
{
(void)e;
result = true;
}
EXPECT(result);
}
TEST_CASE(div_zero_compile_trap_after_passes)
{
migraphx::program p;
......@@ -1382,6 +1408,34 @@ TEST_CASE(div_zero_compile_trap_after_passes)
EXPECT(result);
}
TEST_CASE(div_zero_compile_trap_long_program_after_passes)
{
migraphx::program p;
auto* mm = p.get_main_module();
auto zero = mm->add_literal(0);
auto two = mm->add_literal(2);
auto x = mm->add_parameter("x", {migraphx::shape::int32_type, {1}});
auto y = mm->add_parameter("y", {migraphx::shape::int32_type, {1}});
auto div0 = mm->add_instruction(migraphx::make_op("div"), x, zero);
auto mul = mm->add_instruction(migraphx::make_op("mul"), two, div0);
auto add = mm->add_instruction(migraphx::make_op("add"), y, mul);
mm->add_instruction(migraphx::make_op("sub"), y, add);
run_pass(*mm);
bool result = false;
try
{
p.compile(migraphx::ref::target{});
}
catch(const std::runtime_error& e)
{
(void)e;
result = true;
}
EXPECT(result);
}
TEST_CASE(elu_test)
{
migraphx::program p;
......
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