<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 24, 2016 at 10:25 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Sat, Feb 13, 2016 at 8:48 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> This new pass lowers load/store_var intrinsics that act on indirect derefs<br>
> to if-ladder of direct load/store_var intrinsics.  The if-ladders perform a<br>
> simple binary search on the indirect.<br>
> ---<br>
>  src/compiler/Makefile.sources                |   1 +<br>
>  src/compiler/nir/Makefile.sources            |   1 +<br>
>  src/compiler/nir/nir.h                       |   2 +<br>
>  src/compiler/nir/nir_lower_indirect_derefs.c | 239 +++++++++++++++++++++++++++<br>
>  4 files changed, 243 insertions(+)<br>
>  create mode 100644 src/compiler/nir/nir_lower_indirect_derefs.c<br>
><br>
> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources<br>
> index c9780d6..9f3bcf0 100644<br>
> --- a/src/compiler/Makefile.sources<br>
> +++ b/src/compiler/Makefile.sources<br>
> @@ -189,6 +189,7 @@ NIR_FILES = \<br>
>         nir/nir_lower_load_const_to_scalar.c \<br>
>         nir/nir_lower_locals_to_regs.c \<br>
>         nir/nir_lower_idiv.c \<br>
> +       nir/nir_lower_indirect_derefs.c \<br>
>         nir/nir_lower_io.c \<br>
>         nir/nir_lower_outputs_to_temporaries.c \<br>
>         nir/nir_lower_phis_to_scalar.c \<br>
> diff --git a/src/compiler/nir/Makefile.sources b/src/compiler/nir/Makefile.sources<br>
> index 0755a10..f31547b 100644<br>
> --- a/src/compiler/nir/Makefile.sources<br>
> +++ b/src/compiler/nir/Makefile.sources<br>
> @@ -33,6 +33,7 @@ NIR_FILES = \<br>
>         nir_lower_load_const_to_scalar.c \<br>
>         nir_lower_locals_to_regs.c \<br>
>         nir_lower_idiv.c \<br>
> +       nir_lower_indirect_derefs.c \<br>
>         nir_lower_io.c \<br>
>         nir_lower_outputs_to_temporaries.c \<br>
>         nir_lower_phis_to_scalar.c \<br>
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> index cccb3a4..cfbac81 100644<br>
> --- a/src/compiler/nir/nir.h<br>
> +++ b/src/compiler/nir/nir.h<br>
> @@ -2055,6 +2055,8 @@ void nir_lower_var_copies(nir_shader *shader);<br>
><br>
>  bool nir_lower_global_vars_to_local(nir_shader *shader);<br>
><br>
> +bool nir_lower_indirect_derefs(nir_shader *shader, uint32_t mode_mask);<br>
> +<br>
>  bool nir_lower_locals_to_regs(nir_shader *shader);<br>
><br>
>  void nir_lower_outputs_to_temporaries(nir_shader *shader);<br>
> diff --git a/src/compiler/nir/nir_lower_indirect_derefs.c b/src/compiler/nir/nir_lower_indirect_derefs.c<br>
> new file mode 100644<br>
> index 0000000..69f2df4<br>
> --- /dev/null<br>
> +++ b/src/compiler/nir/nir_lower_indirect_derefs.c<br>
> @@ -0,0 +1,239 @@<br>
> +/*<br>
> + * Copyright © 2016 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +#include "nir_builder.h"<br>
> +<br>
> +static void<br>
> +emit_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,<br>
> +                nir_deref_var *deref, nir_deref *tail,<br>
> +                nir_ssa_def **dest, nir_ssa_def *src);<br>
> +<br>
> +static void<br>
> +emit_indirect_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,<br>
> +                         nir_deref_var *deref, nir_deref *arr_parent,<br>
> +                         int start, int end,<br>
> +                         nir_ssa_def **dest, nir_ssa_def *src)<br>
> +{<br>
> +   assert(arr_parent->child &&<br>
> +          arr_parent->child->deref_type == nir_deref_type_array);<br>
> +   nir_deref_array *arr = nir_deref_as_array(arr_parent->child);<br>
> +   assert(arr->deref_array_type == nir_deref_array_type_indirect);<br>
> +   assert(arr->indirect.is_ssa);<br>
> +<br>
> +   assert(start < end);<br>
> +   if (start == end - 1) {<br>
> +      /* Base case.  Just emit the load/store op */<br>
> +      nir_deref_array direct = *arr;<br>
> +      direct.deref_array_type = nir_deref_array_type_direct;<br>
> +      direct.base_offset += start;<br>
> +      direct.indirect = NIR_SRC_INIT;<br>
> +<br>
> +      arr_parent->child = &direct.deref;<br>
> +      emit_load_store(b, orig_instr, deref, &arr->deref, dest, src);<br>
> +      arr_parent->child = &arr->deref;<br>
> +   } else {<br>
> +      int mid = start + (end - start) / 2;<br>
> +<br>
> +      nir_ssa_def *then_dest, *else_dest;<br>
> +<br>
> +      nir_if *if_stmt = nir_if_create(b->shader);<br>
> +      if_stmt->condition = nir_src_for_ssa(nir_ilt(b, arr->indirect.ssa,<br>
> +                                                      nir_imm_int(b, mid)));<br>
> +      nir_cf_node_insert(b->cursor, &if_stmt->cf_node);<br>
> +<br>
> +      b->cursor = nir_after_cf_list(&if_stmt->then_list);<br>
> +      emit_indirect_load_store(b, orig_instr, deref, arr_parent,<br>
> +                               start, mid, &then_dest, src);<br>
> +<br>
> +      b->cursor = nir_after_cf_list(&if_stmt->else_list);<br>
> +      emit_indirect_load_store(b, orig_instr, deref, arr_parent,<br>
> +                               mid, end, &else_dest, src);<br>
> +<br>
> +      b->cursor = nir_after_cf_node(&if_stmt->cf_node);<br>
> +<br>
> +      if (src == NULL) {<br>
> +         /* We're a load.  We need to insert a phi node */<br>
> +         nir_phi_instr *phi = nir_phi_instr_create(b->shader);<br>
> +         nir_ssa_dest_init(&phi->instr, &phi->dest,<br>
> +                           then_dest->num_components, NULL);<br>
> +<br>
> +         nir_phi_src *src0 = ralloc(phi, nir_phi_src);<br>
> +         src0->pred = nir_cf_node_as_block(nir_if_last_then_node(if_stmt));<br>
> +         src0->src = nir_src_for_ssa(then_dest);<br>
> +         exec_list_push_tail(&phi->srcs, &src0->node);<br>
> +<br>
> +         nir_phi_src *src1 = ralloc(phi, nir_phi_src);<br>
> +         src1->pred = nir_cf_node_as_block(nir_if_last_else_node(if_stmt));<br>
> +         src1->src = nir_src_for_ssa(else_dest);<br>
> +         exec_list_push_tail(&phi->srcs, &src1->node);<br>
> +<br>
> +         nir_builder_instr_insert(b, &phi->instr);<br>
> +         *dest = &phi->dest.ssa;<br>
> +      }<br>
> +   }<br>
> +}<br>
> +<br>
> +static void<br>
> +emit_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,<br>
> +                nir_deref_var *deref, nir_deref *tail,<br>
> +                nir_ssa_def **dest, nir_ssa_def *src)<br>
> +{<br>
> +   for (; tail->child; tail = tail->child) {<br>
> +      if (tail->child->deref_type != nir_deref_type_array)<br>
> +         continue;<br>
> +<br>
> +      nir_deref_array *arr = nir_deref_as_array(tail->child);<br>
> +      if (arr->deref_array_type != nir_deref_array_type_indirect)<br>
> +         continue;<br>
> +<br>
> +      int length = glsl_get_length(tail->type);<br>
> +<br>
> +      emit_indirect_load_store(b, orig_instr, deref, tail, -arr->base_offset,<br>
> +                               length - arr->base_offset, dest, src);<br>
> +      return;<br>
> +   }<br>
> +<br>
> +   assert(tail && tail->child == NULL);<br>
> +<br>
> +   /* We reached the end of the deref chain.  Emit the instruction */<br>
> +<br>
> +   if (src == NULL) {<br>
> +      /* This is a load instruction */<br>
> +      nir_intrinsic_instr *load =<br>
> +         nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);<br>
> +      load->num_components = orig_instr->num_components;<br>
> +      load->variables[0] =<br>
> +         nir_deref_as_var(nir_copy_deref(load, &deref->deref));<br>
> +      nir_ssa_dest_init(&load->instr, &load->dest,<br>
> +                        load->num_components, NULL);<br>
> +      nir_builder_instr_insert(b, &load->instr);<br>
> +      *dest = &load->dest.ssa;<br>
> +   } else {<br>
> +      /* This is a store instruction */<br>
> +      nir_intrinsic_instr *store =<br>
> +         nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);<br>
> +      store->num_components = orig_instr->num_components;<br>
> +      store->const_index[0] = orig_instr->const_index[0]; /* writemask */<br>
<br>
</div></div>This should probably use Rob's new setters/getters to make it more<br>
readable/robust. Other than that, this patch is<br></blockquote><div><br></div><div>Done<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br></blockquote><div><br></div><div>Thanks!<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
> +      store->variables[0] =<br>
> +         nir_deref_as_var(nir_copy_deref(store, &deref->deref));<br>
> +      store->src[0] = nir_src_for_ssa(src);<br>
> +      nir_builder_instr_insert(b, &store->instr);<br>
> +   }<br>
> +}<br>
> +<br>
> +static bool<br>
> +deref_has_indirect(nir_deref_var *deref)<br>
> +{<br>
> +   for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {<br>
> +      if (tail->deref_type != nir_deref_type_array)<br>
> +         continue;<br>
> +<br>
> +      nir_deref_array *arr = nir_deref_as_array(tail);<br>
> +      if (arr->deref_array_type == nir_deref_array_type_indirect)<br>
> +         return true;<br>
> +   }<br>
> +<br>
> +   return false;<br>
> +}<br>
> +<br>
> +struct lower_indirect_state {<br>
> +   nir_builder builder;<br>
> +   uint32_t mode_mask;<br>
> +   bool progress;<br>
> +};<br>
> +<br>
> +static bool<br>
> +lower_indirect_block(nir_block *block, void *void_state)<br>
> +{<br>
> +   struct lower_indirect_state *state = void_state;<br>
> +<br>
> +   nir_foreach_instr_safe(block, instr) {<br>
> +      if (instr->type != nir_instr_type_intrinsic)<br>
> +         continue;<br>
> +<br>
> +      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
> +      if (intrin->intrinsic != nir_intrinsic_load_var &&<br>
> +          intrin->intrinsic != nir_intrinsic_store_var)<br>
> +         continue;<br>
> +<br>
> +      if (!deref_has_indirect(intrin->variables[0]))<br>
> +         continue;<br>
> +<br>
> +      /* Only lower variables whose mode is in the mask */<br>
> +      if (!(state->mode_mask & (1 << intrin->variables[0]->var->data.mode)))<br>
> +         continue;<br>
> +<br>
> +      state->builder.cursor = nir_before_instr(&intrin->instr);<br>
> +<br>
> +      if (intrin->intrinsic == nir_intrinsic_load_var) {<br>
> +         nir_ssa_def *result;<br>
> +         emit_load_store(&state->builder, intrin, intrin->variables[0],<br>
> +                         &intrin->variables[0]->deref, &result, NULL);<br>
> +         nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(result));<br>
> +      } else {<br>
> +         assert(intrin->src[0].is_ssa);<br>
> +         emit_load_store(&state->builder, intrin, intrin->variables[0],<br>
> +                         &intrin->variables[0]->deref, NULL, intrin->src[0].ssa);<br>
> +      }<br>
> +      nir_instr_remove(&intrin->instr);<br>
> +      state->progress = true;<br>
> +   }<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +lower_indirects_impl(nir_function_impl *impl, uint32_t mode_mask)<br>
> +{<br>
> +   struct lower_indirect_state state;<br>
> +<br>
> +   state.progress = false;<br>
> +   state.mode_mask = mode_mask;<br>
> +   nir_builder_init(&state.builder, impl);<br>
> +<br>
> +   nir_foreach_block(impl, lower_indirect_block, &state);<br>
> +<br>
> +   if (state.progress)<br>
> +      nir_metadata_preserve(impl, nir_metadata_none);<br>
> +<br>
> +   return state.progress;<br>
> +}<br>
> +<br>
> +/** Lowers indirect variable loads/stores to direct loads/stores.<br>
> + *<br>
> + * The pass works by replacing any indirect load or store with an if-ladder<br>
> + * that does a binary search on the array index.<br>
> + */<br>
> +bool<br>
> +nir_lower_indirect_derefs(nir_shader *shader, uint32_t mode_mask)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_foreach_function(shader, function) {<br>
> +      if (function->impl)<br>
> +         progress = lower_indirects_impl(function->impl, mode_mask) || progress;<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
> --<br>
> 2.5.0.400.gff86faf<br>
><br>
</div></div>> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div>