<p dir="ltr"><br>
On Aug 25, 2015 12:24 PM, "Kenneth Graunke" <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>> wrote:<br>
><br>
> This patch implements a general nir_instr_insert() function that takes a<br>
> nir_cursor for the insertion point.  It then reworks the existing API to<br>
> simply be a wrapper around that for compatibility.<br>
><br>
> This largely involves moving the existing code into a new function.<br>
><br>
> Suggested by Connor Abbott.<br>
><br>
> Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
> ---<br>
>  src/glsl/nir/nir.c | 115 ++++++++++++++++++++++++++---------------------------<br>
>  src/glsl/nir/nir.h |   7 ++++<br>
>  2 files changed, 63 insertions(+), 59 deletions(-)<br>
><br>
> diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c<br>
> index ff758f4..a3e7966 100644<br>
> --- a/src/glsl/nir/nir.c<br>
> +++ b/src/glsl/nir/nir.c<br>
> @@ -664,102 +664,99 @@ add_defs_uses(nir_instr *instr)<br>
>  }<br>
><br>
>  void<br>
> +nir_instr_insert(nir_cursor cursor, nir_instr *instr)<br>
> +{<br>
> +   switch (cursor.option) {<br>
> +   case nir_cursor_before_block:<br>
> +      /* Only allow inserting jumps into empty blocks. */<br>
> +      if (instr->type == nir_instr_type_jump)<br>
> +         assert(exec_list_is_empty(&cursor.block->instr_list));<br>
> +<br>
> +      instr->block = cursor.block;<br>
> +      add_defs_uses(instr);<br>
> +      exec_list_push_head(&cursor.block->instr_list, &instr->node);<br>
> +      break;<br>
> +   case nir_cursor_after_block: {<br>
> +      /* Inserting instructions after a jump is illegal. */<br>
> +      nir_instr *last = nir_block_last_instr(cursor.block);<br>
> +      assert(last == NULL || last->type != nir_instr_type_jump);<br>
> +      (void) last;<br>
> +<br>
> +      instr->block = cursor.block;<br>
> +      add_defs_uses(instr);<br>
> +      exec_list_push_tail(&cursor.block->instr_list, &instr->node);<br>
> +      break;<br>
> +   }<br>
> +   case nir_cursor_before_instr:<br>
> +      assert(instr->type != nir_instr_type_jump);<br>
> +      instr->block = cursor.instr->block;<br>
> +      add_defs_uses(instr);<br>
> +      exec_node_insert_node_before(&cursor.instr->node, &instr->node);<br>
> +      break;<br>
> +   case nir_cursor_after_instr:<br>
> +      /* Inserting instructions after a jump is illegal. */<br>
> +      assert(cursor.instr->type != nir_instr_type_jump);<br>
> +<br>
> +      /* Only allow inserting jumps at the end of the block. */<br>
> +      if (instr->type == nir_instr_type_jump)<br>
> +         assert(cursor.instr == nir_block_last_instr(cursor.instr->block));<br>
> +<br>
> +      instr->block = cursor.instr->block;<br>
> +      add_defs_uses(instr);<br>
> +      exec_node_insert_after(&cursor.instr->node, &instr->node);<br>
> +      break;<br>
> +   }<br>
> +<br>
> +   if (instr->type == nir_instr_type_jump)<br>
> +      nir_handle_add_jump(instr->block);<br>
> +}<br>
> +<br>
> +void<br>
>  nir_instr_insert_before(nir_instr *instr, nir_instr *before)<br>
>  {<br>
> -   assert(before->type != nir_instr_type_jump);<br>
> -   before->block = instr->block;<br>
> -   add_defs_uses(before);<br>
> -   exec_node_insert_node_before(&instr->node, &before->node);<br>
> +   nir_instr_insert(nir_before_instr(instr), before);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_after(nir_instr *instr, nir_instr *after)<br>
>  {<br>
> -   assert(instr->type != nir_instr_type_jump);<br>
> -<br>
> -   if (after->type == nir_instr_type_jump) {<br>
> -      assert(instr == nir_block_last_instr(instr->block));<br>
> -   }<br>
> -<br>
> -   after->block = instr->block;<br>
> -   add_defs_uses(after);<br>
> -   exec_node_insert_after(&instr->node, &after->node);<br>
> -<br>
> -   if (after->type == nir_instr_type_jump)<br>
> -      nir_handle_add_jump(after->block);<br>
> +   nir_instr_insert(nir_after_instr(instr), after);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_before_block(nir_block *block, nir_instr *before)<br>
>  {<br>
> -   if (before->type == nir_instr_type_jump)<br>
> -      assert(exec_list_is_empty(&block->instr_list));<br>
> -<br>
> -   before->block = block;<br>
> -   add_defs_uses(before);<br>
> -   exec_list_push_head(&block->instr_list, &before->node);<br>
> -<br>
> -   if (before->type == nir_instr_type_jump)<br>
> -      nir_handle_add_jump(block);<br>
> +   nir_instr_insert(nir_before_block(block), before);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_after_block(nir_block *block, nir_instr *after)<br>
>  {<br>
> -   nir_instr *last = nir_block_last_instr(block);<br>
> -   assert(last == NULL || last->type != nir_instr_type_jump);<br>
> -   (void) last;<br>
> -<br>
> -   after->block = block;<br>
> -   add_defs_uses(after);<br>
> -   exec_list_push_tail(&block->instr_list, &after->node);<br>
> -<br>
> -   if (after->type == nir_instr_type_jump)<br>
> -      nir_handle_add_jump(block);<br>
> +   nir_instr_insert(nir_after_block(block), after);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)<br>
>  {<br>
> -   if (node->type == nir_cf_node_block) {<br>
> -      nir_instr_insert_before_block(nir_cf_node_as_block(node), before);<br>
> -   } else {<br>
> -      nir_cf_node *prev = nir_cf_node_prev(node);<br>
> -      assert(prev->type == nir_cf_node_block);<br>
> -      nir_block *prev_block = nir_cf_node_as_block(prev);<br>
> -<br>
> -      nir_instr_insert_before_block(prev_block, before);<br>
> -   }<br>
> +   nir_instr_insert(nir_before_cf_node(node), before);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)<br>
>  {<br>
> -   if (node->type == nir_cf_node_block) {<br>
> -      nir_instr_insert_after_block(nir_cf_node_as_block(node), after);<br>
> -   } else {<br>
> -      nir_cf_node *next = nir_cf_node_next(node);<br>
> -      assert(next->type == nir_cf_node_block);<br>
> -      nir_block *next_block = nir_cf_node_as_block(next);<br>
> -<br>
> -      nir_instr_insert_before_block(next_block, after);<br>
> -   }<br>
> +   nir_instr_insert(nir_after_cf_node(node), after);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)<br>
>  {<br>
> -   nir_cf_node *first_node = exec_node_data(nir_cf_node,<br>
> -                                            exec_list_get_head(list), node);<br>
> -   nir_instr_insert_before_cf(first_node, before);<br>
> +   nir_instr_insert(nir_before_cf_list(list), before);<br>
>  }<br>
><br>
>  void<br>
>  nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)<br>
>  {<br>
> -   nir_cf_node *last_node = exec_node_data(nir_cf_node,<br>
> -                                           exec_list_get_tail(list), node);<br>
> -   nir_instr_insert_after_cf(last_node, after);<br>
> +   nir_instr_insert(nir_after_cf_list(list), after);<br>
>  }</p>
<p dir="ltr">Since these are now just trivial wrappers, can you make them inline and put them in nir.h? Otherwise, LGTM. Sorry for not pushing my control flow patches - I'll do it when I get back this weekend, remind me if I forget! </p>
<p dir="ltr">(Responding on phone so sorry for any horrible line-wrapping...)</p>
<p dir="ltr">><br>
>  static bool<br>
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> index 65e4daf..ac01c86 100644<br>
> --- a/src/glsl/nir/nir.h<br>
> +++ b/src/glsl/nir/nir.h<br>
> @@ -1641,6 +1641,13 @@ nir_after_cf_list(struct exec_list *cf_list)<br>
>     return nir_after_cf_node(last_node);<br>
>  }<br>
><br>
> +/**<br>
> + * Insert a NIR instruction at the given cursor.<br>
> + *<br>
> + * Note: This does not update the cursor.<br>
> + */<br>
> +void nir_instr_insert(nir_cursor cursor, nir_instr *instr);<br>
> +<br>
>  void nir_instr_insert_before(nir_instr *instr, nir_instr *before);<br>
>  void nir_instr_insert_after(nir_instr *instr, nir_instr *after);<br>
><br>
> --<br>
> 2.5.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>