Commit 50361163 authored by Paul's avatar Paul
Browse files

Use static replace_argument

parent b1ba0937
...@@ -39,7 +39,7 @@ void eliminate_contiguous::apply(program& p) const ...@@ -39,7 +39,7 @@ void eliminate_contiguous::apply(program& p) const
replace(new_args, arg, prev); replace(new_args, arg, prev);
if(try_compute_shape(ins->op, new_args)) if(try_compute_shape(ins->op, new_args))
{ {
replace_argument(ins, arg, prev); instruction::replace_argument(ins, arg, prev);
} }
} }
} }
......
...@@ -125,18 +125,17 @@ struct instruction ...@@ -125,18 +125,17 @@ struct instruction
migraph::erase(output, ins); migraph::erase(output, ins);
} }
// internal static void backreference(instruction_ref ref)
void replace(std::vector<instruction_ref> args)
{ {
clear_arguments(); for(auto&& arg : ref->inputs())
arguments = std::move(args); arg->add_output(ref);
} }
// internal static void replace_argument(instruction_ref ins, instruction_ref old, instruction_ref new_ins)
void replace_argument(instruction_ref old, instruction_ref new_ins)
{ {
std::replace(arguments.begin(), arguments.end(), old, new_ins); ins->replace_argument(old, new_ins);
old->remove_output(*this); backreference(ins);
ins->recompute_shape();
} }
// internal // internal
...@@ -147,6 +146,20 @@ struct instruction ...@@ -147,6 +146,20 @@ struct instruction
replace(std::move(args)); replace(std::move(args));
} }
// internal
void replace(std::vector<instruction_ref> args)
{
clear_arguments();
arguments = std::move(args);
}
// internal
void replace_argument(instruction_ref old, instruction_ref new_ins)
{
std::replace(arguments.begin(), arguments.end(), old, new_ins);
old->remove_output(*this);
}
operation op; operation op;
shape result; shape result;
std::vector<instruction_ref> output; std::vector<instruction_ref> output;
...@@ -154,19 +167,6 @@ struct instruction ...@@ -154,19 +167,6 @@ struct instruction
literal lit; literal lit;
}; };
inline void backreference(instruction_ref ref)
{
for(auto&& arg : ref->inputs())
arg->add_output(ref);
}
inline void replace_argument(instruction_ref ins, instruction_ref old, instruction_ref new_ins)
{
ins->replace_argument(old, new_ins);
backreference(ins);
ins->recompute_shape();
}
// TODO: Move to a cpp file // TODO: Move to a cpp file
inline shape compute_shape(const operation& op, const std::vector<instruction_ref>& args) inline shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
{ {
......
...@@ -92,7 +92,7 @@ instruction_ref program::insert_instruction(instruction_ref ins, ...@@ -92,7 +92,7 @@ instruction_ref program::insert_instruction(instruction_ref ins,
// TODO: Use move // TODO: Use move
shape r = compute_shape(op, args); shape r = compute_shape(op, args);
auto result = impl->instructions.insert(ins, {op, r, std::move(args)}); auto result = impl->instructions.insert(ins, {op, r, std::move(args)});
backreference(result); instruction::backreference(result);
// assert(result->inputs() == args); // assert(result->inputs() == args);
assert(result->valid(begin())); assert(result->valid(begin()));
return result; return result;
...@@ -109,7 +109,7 @@ instruction_ref program::replace_instruction(instruction_ref ins, ...@@ -109,7 +109,7 @@ instruction_ref program::replace_instruction(instruction_ref ins,
shape r = compute_shape(op, args); shape r = compute_shape(op, args);
ins->replace(op, r, std::move(args)); ins->replace(op, r, std::move(args));
backreference(ins); instruction::backreference(ins);
assert(ins->valid(begin())); assert(ins->valid(begin()));
return ins; return ins;
} }
...@@ -129,7 +129,7 @@ instruction_ref program::replace_instruction(instruction_ref ins, instruction_re ...@@ -129,7 +129,7 @@ instruction_ref program::replace_instruction(instruction_ref ins, instruction_re
// TODO: Check for possible cycles // TODO: Check for possible cycles
if(out != rep) if(out != rep)
{ {
replace_argument(out, ins, rep); instruction::replace_argument(out, ins, rep);
} }
assert(out->valid(begin())); assert(out->valid(begin()));
} }
......
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