<p dir="ltr"><br>
On May 21, 2015 9:41 AM, "Connor Abbott" <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>> wrote:<br>
><br>
> The point of cleanup_defs_uses() is to make an instruction safe to<br>
> remove by removing any references that the rest of the shader may have<br>
> to it. Previously, it was handling register use/def sets and removing<br>
> the instruction from the use sets of any SSA sources it had, but if the<br>
> instruction defined an SSA value that was used by other instructions it<br>
> wasn't doing anything. This was ok, since we were always careful to make<br>
> sure that no removed instruction ever had any uses, but now we want to<br>
> start removing unreachable instruction which might still be used in</p>
<p dir="ltr">"unreachable instructions" plural</p>
<p dir="ltr">> reachable parts of the code. In that case, the value that any uses get<br>
> is undefined since the instruction never actually executes, so we can<br>
> just replace the instruction with an ssa_undef_instr.<br>
><br>
> v2: split out other fixes<br>
> Signed-off-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
> ---<br>
>  src/glsl/nir/nir.c | 38 ++++++++++++++++++++++++++++----------<br>
>  1 file changed, 28 insertions(+), 10 deletions(-)<br>
><br>
> diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c<br>
> index f03e80a..704553f 100644<br>
> --- a/src/glsl/nir/nir.c<br>
> +++ b/src/glsl/nir/nir.c<br>
> @@ -1206,26 +1206,26 @@ stitch_blocks(nir_block *before, nir_block *after)<br>
>  }<br>
><br>
>  static void<br>
> -remove_defs_uses(nir_instr *instr);<br>
> +remove_defs_uses(nir_instr *instr, nir_function_impl *impl);<br>
><br>
>  static void<br>
> -cleanup_cf_node(nir_cf_node *node)<br>
> +cleanup_cf_node(nir_cf_node *node, nir_function_impl *impl)<br>
>  {<br>
>     switch (node->type) {<br>
>     case nir_cf_node_block: {<br>
>        nir_block *block = nir_cf_node_as_block(node);<br>
>        /* We need to walk the instructions and clean up defs/uses */<br>
>        nir_foreach_instr(block, instr)<br>
> -         remove_defs_uses(instr);<br>
> +         remove_defs_uses(instr, impl);<br>
>        break;<br>
>     }<br>
><br>
>     case nir_cf_node_if: {<br>
>        nir_if *if_stmt = nir_cf_node_as_if(node);<br>
>        foreach_list_typed(nir_cf_node, child, node, &if_stmt->then_list)<br>
> -         cleanup_cf_node(child);<br>
> +         cleanup_cf_node(child, impl);<br>
>        foreach_list_typed(nir_cf_node, child, node, &if_stmt->else_list)<br>
> -         cleanup_cf_node(child);<br>
> +         cleanup_cf_node(child, impl);<br>
><br>
>        list_del(&if_stmt->condition.use_link);<br>
>        break;<br>
> @@ -1234,13 +1234,12 @@ cleanup_cf_node(nir_cf_node *node)<br>
>     case nir_cf_node_loop: {<br>
>        nir_loop *loop = nir_cf_node_as_loop(node);<br>
>        foreach_list_typed(nir_cf_node, child, node, &loop->body)<br>
> -         cleanup_cf_node(child);<br>
> +         cleanup_cf_node(child, impl);<br>
>        break;<br>
>     }<br>
>     case nir_cf_node_function: {<br>
> -      nir_function_impl *impl = nir_cf_node_as_function(node);<br>
>        foreach_list_typed(nir_cf_node, child, node, &impl->body)<br>
> -         cleanup_cf_node(child);<br>
> +         cleanup_cf_node(child, impl);<br>
>        break;<br>
>     }<br>
>     default:<br>
> @@ -1443,16 +1442,35 @@ remove_def_cb(nir_dest *dest, void *state)<br>
>     return true;<br>
>  }<br>
><br>
> +static bool<br>
> +remove_ssa_def_cb(nir_ssa_def *def, void *state)<br>
> +{<br>
> +   nir_function_impl *impl = state;<br>
> +   nir_shader *shader = impl->overload->function->shader;<br>
> +<br>
> +   if (!list_empty(&def->uses) || !list_empty(&def->if_uses)) {<br>
> +      nir_ssa_undef_instr *undef =<br>
> +         nir_ssa_undef_instr_create(shader, def->num_components);<br>
> +      nir_instr_insert_before_cf_list(&impl->body, &undef->instr);<br>
> +      nir_ssa_def_rewrite_uses(def, nir_src_for_ssa(&undef->def), shader);<br>
> +   }<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +<br>
>  static void<br>
> -remove_defs_uses(nir_instr *instr)<br>
> +remove_defs_uses(nir_instr *instr, nir_function_impl *impl)<br>
>  {<br>
>     nir_foreach_dest(instr, remove_def_cb, instr);<br>
>     nir_foreach_src(instr, remove_use_cb, instr);<br>
> +   nir_foreach_ssa_def(instr, remove_ssa_def_cb, impl);<br>
>  }<br>
><br>
>  void nir_instr_remove(nir_instr *instr)<br>
>  {<br>
> -   remove_defs_uses(instr);<br>
> +   nir_function_impl *impl = nir_cf_node_get_function(&instr->block->cf_node);</p>
<p dir="ltr">I'm not entirely happy that this is done unconditionally here given that it may be somewhat expensive and most of the time isn't needed.  However, I'm not going to quibble over it too bad.</p>
<p dir="ltr">Reviewed-by: Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com">jason.ekstrand@intel.com</a>></p>
<p dir="ltr">> +   remove_defs_uses(instr, impl);<br>
>     exec_node_remove(&instr->node);<br>
><br>
>     if (instr->type == nir_instr_type_jump) {<br>
> --<br>
> 2.1.0<br>
><br>
> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</p>