Commit 9874be6d authored by charlie's avatar charlie
Browse files

Simplify fix

parent 6b6a7e80
......@@ -42,7 +42,7 @@ struct instruction
friend bool operator==(const instruction& i, instruction_ref ref);
bool valid(const module& m, bool check_order = false) const;
bool valid(instruction_ref start, bool check_order = false) const;
bool valid() const;
......
......@@ -74,7 +74,7 @@ bool operator==(const instruction& i, instruction_ref ref)
return std::addressof(i) == std::addressof(*ref);
}
bool instruction::valid(const module& m, bool check_order) const
bool instruction::valid(instruction_ref start, bool check_order) const
{
return valid() && std::all_of(arguments.begin(), arguments.end(), [&](instruction_ref i) {
auto self = std::find(i->outputs().begin(), i->outputs().end(), *this);
......@@ -82,7 +82,7 @@ bool instruction::valid(const module& m, bool check_order) const
if(check_order)
{
// check arguments for this instruction before this instruction
ret = ret and (std::distance(m.begin(), i) < std::distance(m.begin(), *self));
ret = ret and (std::distance(start, i) < std::distance(start, *self));
}
return ret;
});
......
......@@ -184,7 +184,7 @@ instruction_ref module::insert_instruction(instruction_ref ins,
shape r = compute_shape(op, args);
auto result = impl->insert(ins, {op, r, std::move(args)});
instruction::backreference(result);
assert(result->valid(*this));
assert(result->valid(begin()));
return result;
}
......@@ -206,7 +206,7 @@ instruction_ref module::insert_instruction(instruction_ref ins,
auto out_shape = compute_shape(op, args, module_args);
auto result = impl->insert(ins, {op, out_shape, std::move(args), std::move(module_args)});
instruction::backreference(result);
assert(result->valid(*this));
assert(result->valid(begin()));
return result;
}
......@@ -219,7 +219,7 @@ instruction_ref module::replace_instruction(instruction_ref ins,
shape r = compute_shape(op, args);
instruction::replace(ins, op, r, std::move(args));
assert(ins->valid(*this));
assert(ins->valid(begin()));
return ins;
}
......@@ -232,7 +232,7 @@ instruction_ref module::replace_instruction(instruction_ref ins,
assert(not starts_with(op.name(), "@"));
auto out_shape = compute_shape(op, args, module_args);
instruction::replace(ins, op, out_shape, std::move(args), std::move(module_args));
assert(ins->valid(*this));
assert(ins->valid(begin()));
return ins;
}
......@@ -261,7 +261,7 @@ instruction_ref module::replace_instruction(instruction_ref ins, instruction_ref
{
instruction::replace_argument(out, ins, rep);
}
assert(out->valid(*this));
assert(out->valid(begin()));
}
// Replacement should not be dead code unless its the last instruction
assert(!rep->outputs().empty() or rep == std::prev(end()));
......@@ -269,8 +269,8 @@ instruction_ref module::replace_instruction(instruction_ref ins, instruction_ref
assert(ins->outputs().empty() or std::all_of(ins->outputs().begin(),
ins->outputs().end(),
[&](auto i) { return i == rep; }));
assert(ins->valid(*this));
assert(rep->valid(*this));
assert(ins->valid(begin()));
assert(rep->valid(begin()));
return rep;
}
......@@ -383,7 +383,7 @@ instruction_ref module::add_return(std::vector<instruction_ref> args)
impl->push_back({builtin::returns{}, {}, std::move(args)});
auto result = std::prev(impl->instructions.end());
instruction::backreference(result);
assert(result->valid(*this));
assert(result->valid(begin()));
return result;
}
......@@ -397,7 +397,7 @@ instruction_ref module::replace_return(std::vector<instruction_ref> args)
shape r = compute_shape(last->get_operator(), args);
instruction::replace(last, last->get_operator(), r, std::move(args));
assert(last->valid(*this));
assert(last->valid(begin()));
return last;
}
......@@ -505,22 +505,13 @@ std::vector<shape> module::get_output_shapes() const
instruction_ref module::validate() const
{
auto check_invalid = [&](instruction_ref i) {
auto inputs = (*i).inputs();
bool check_order = std::all_of(
inputs.begin(), inputs.end(), [&](instruction_ref in) { return has_instruction(in); });
return not(*i).valid(*this, check_order);
};
for(instruction_ref i = impl->instructions.begin(); i != impl->instructions.end(); ++i)
{
if(check_invalid(i))
{
return i;
}
}
return impl->instructions.end();
return std::find_if(
impl->instructions.begin(), impl->instructions.end(), [&](const instruction& i) {
auto inputs = i.inputs();
bool check_order = std::all_of(
inputs.begin(), inputs.end(), [&](auto in) { return has_instruction(in); });
return !i.valid(impl->instructions.begin(), check_order);
});
}
bool is_borrowed(instruction_ref ins)
......
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