<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 16, 2016 at 5:36 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 Fri, Sep 16, 2016 at 6:25 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> On Thu, Sep 15, 2016 at 12:03 AM, Timothy Arceri<br>
> <<a href="mailto:timothy.arceri@collabora.com">timothy.arceri@collabora.com</a>> wrote:<br>
>><br>
>> From: Thomas Helland <<a href="mailto:thomashelland90@gmail.com">thomashelland90@gmail.com</a>><br>
>><br>
>> This pass detects induction variables and calculates the<br>
>> trip count of loops to be used for loop unrolling.<br>
>><br>
>> I've removed support for float induction values for now, for the<br>
>> simple reason that they don't appear in my shader-db collection,<br>
>> and so I don't see it as common enough that we want to pollute the<br>
>> pass with this in the initial version.<br>
>><br>
>> V2: Rebase, adapt to removal of function overloads<br>
>><br>
>> V3: (Timothy Arceri)<br>
>>  - don't try to find trip count if loop terminator conditional is a phi<br>
>>  - fix trip count for do-while loops<br>
>>  - replace conditional type != alu assert with return<br>
>>  - disable unrolling of loops with continues<br>
>>  - multiple fixes to memory allocation, stop leaking and don't destroy<br>
>>    structs we want to use for unrolling.<br>
>>  - fix iteration count bugs when induction var not on RHS of condition<br>
>>  - add FIXME for && conditions<br>
>>  - calculate trip count for unsigned induction/limit vars<br>
>><br>
>> V4:<br>
>> - count instructions in a loop<br>
>> - set the limiting_terminator even if we can't find the trip count for<br>
>>  all terminators. This is needed for complex unrolling where we handle<br>
>>  2 terminators and the trip count is unknown for one of them.<br>
>> - restruct structs so we don't keep information not required after<br>
>>  analysis and remove dead fields.<br>
>> - force unrolling in some cases as per the rules in the GLSL IR pass<br>
>> ---<br>
>>  src/compiler/Makefile.sources       |    2 +<br>
>>  src/compiler/nir/nir.h              |   36 +-<br>
>>  src/compiler/nir/nir_loop_<wbr>analyze.c | 1012<br>
>> ++++++++++++++++++++++++++++++<wbr>+++++<br>
>>  src/compiler/nir/nir_metadata.<wbr>c     |    8 +-<br>
>>  4 files changed, 1056 insertions(+), 2 deletions(-)<br>
>>  create mode 100644 src/compiler/nir/nir_loop_<wbr>analyze.c<br>
>><br>
>> diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
>> index f5b4f9c..7ed26a9 100644<br>
>> --- a/src/compiler/Makefile.<wbr>sources<br>
>> +++ b/src/compiler/Makefile.<wbr>sources<br>
>> @@ -190,6 +190,8 @@ NIR_FILES = \<br>
>>         nir/nir_intrinsics.c \<br>
>>         nir/nir_intrinsics.h \<br>
>>         nir/nir_liveness.c \<br>
>> +       nir/nir_loop_analyze.c \<br>
>> +       nir/nir_loop_analyze.h \<br>
>>         nir/nir_lower_alu_to_scalar.c \<br>
>>         nir/nir_lower_atomics.c \<br>
>>         nir/nir_lower_bitmap.c \<br>
>> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
>> index ff7c422..49e8cd8 100644<br>
>> --- a/src/compiler/nir/nir.h<br>
>> +++ b/src/compiler/nir/nir.h<br>
>> @@ -1549,9 +1549,36 @@ nir_if_last_else_node(nir_if *if_stmt)<br>
>>  }<br>
>><br>
>>  typedef struct {<br>
>> +   nir_if *nif;<br>
>> +<br>
>> +   nir_instr *conditional_instr;<br>
>> +<br>
>> +   struct list_head loop_terminator_link;<br>
>> +} nir_loop_terminator;<br>
>> +<br>
>> +typedef struct {<br>
>> +   /* Number of instructions in the loop */<br>
>> +   unsigned num_instructions;<br>
>> +<br>
>> +   /* How many times the loop is run (if known) */<br>
>> +   unsigned trip_count;<br>
>> +   bool is_trip_count_known;<br>
><br>
><br>
> We could use 0 or -1 to indicate "I don't know trip count" instead of an<br>
> extra boolean.  Not sure that it matters much.<br>
><br>
>><br>
>> +<br>
>> +   /* Unroll the loop regardless of its size */<br>
>> +   bool force_unroll;<br>
><br>
><br>
> It seems a bit odd to have this decide to force-unroll.  This is an analysis<br>
> pass, not a "make decisions" pass.<br>
><br>
>><br>
>> +<br>
>> +   nir_loop_terminator *limiting_terminator;<br>
>> +<br>
>> +   /* A list of loop_terminators terminating this loop. */<br>
>> +   struct list_head loop_terminator_list;<br>
>> +} nir_loop_info;<br>
>> +<br>
>> +typedef struct {<br>
>>     nir_cf_node cf_node;<br>
>><br>
>>     struct exec_list body; /** < list of nir_cf_node */<br>
>> +<br>
>> +   nir_loop_info *info;<br>
>>  } nir_loop;<br>
>><br>
>>  static inline nir_cf_node *<br>
>> @@ -1576,6 +1603,7 @@ typedef enum {<br>
>>     nir_metadata_dominance = 0x2,<br>
>>     nir_metadata_live_ssa_defs = 0x4,<br>
>>     nir_metadata_not_properly_<wbr>reset = 0x8,<br>
>> +   nir_metadata_loop_analysis = 0x16,<br>
>>  } nir_metadata;<br>
>><br>
>>  typedef struct {<br>
>> @@ -1758,6 +1786,8 @@ typedef struct nir_shader_compiler_options {<br>
>>      * information must be inferred from the list of input nir_variables.<br>
>>      */<br>
>>     bool use_interpolated_input_<wbr>intrinsics;<br>
>> +<br>
>> +   unsigned max_unroll_iterations;<br>
>>  } nir_shader_compiler_options;<br>
>><br>
>>  typedef struct nir_shader_info {<br>
>> @@ -1962,7 +1992,7 @@ nir_loop *nir_loop_create(nir_shader *shader);<br>
>>  nir_function_impl *nir_cf_node_get_function(nir_<wbr>cf_node *node);<br>
>><br>
>>  /** requests that the given pieces of metadata be generated */<br>
>> -void nir_metadata_require(nir_<wbr>function_impl *impl, nir_metadata<br>
>> required);<br>
>> +void nir_metadata_require(nir_<wbr>function_impl *impl, nir_metadata required,<br>
>> ...);<br>
>>  /** dirties all but the preserved metadata */<br>
>>  void nir_metadata_preserve(nir_<wbr>function_impl *impl, nir_metadata<br>
>> preserved);<br>
>><br>
>> @@ -2559,6 +2589,10 @@ void nir_lower_double_pack(nir_<wbr>shader *shader);<br>
>>  bool nir_normalize_cubemap_coords(<wbr>nir_shader *shader);<br>
>><br>
>>  void nir_live_ssa_defs_impl(nir_<wbr>function_impl *impl);<br>
>> +<br>
>> +void nir_loop_analyze_impl(nir_<wbr>function_impl *impl,<br>
>> +                           nir_variable_mode indirect_mask);<br>
>> +<br>
>>  bool nir_ssa_defs_interfere(nir_<wbr>ssa_def *a, nir_ssa_def *b);<br>
>><br>
>>  void nir_convert_to_ssa_impl(nir_<wbr>function_impl *impl);<br>
>> diff --git a/src/compiler/nir/nir_loop_<wbr>analyze.c<br>
>> b/src/compiler/nir/nir_loop_<wbr>analyze.c<br>
>> new file mode 100644<br>
>> index 0000000..6bea9e5<br>
>> --- /dev/null<br>
>> +++ b/src/compiler/nir/nir_loop_<wbr>analyze.c<br>
>> @@ -0,0 +1,1012 @@<br>
>> +/*<br>
>> + * Copyright © 2015 Thomas Helland<br>
>> + *<br>
>> + * Permission is hereby granted, free of charge, to any person obtaining<br>
>> a<br>
>> + * copy of this software and associated documentation files (the<br>
>> "Software"),<br>
>> + * to deal in the Software without restriction, including without<br>
>> limitation<br>
>> + * the rights to use, copy, modify, merge, publish, distribute,<br>
>> 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<br>
>> next<br>
>> + * paragraph) shall be included in all copies or substantial portions of<br>
>> the<br>
>> + * Software.<br>
>> + *<br>
>> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,<br>
>> EXPRESS OR<br>
>> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF<br>
>> MERCHANTABILITY,<br>
>> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT<br>
>> SHALL<br>
>> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
>> OTHER<br>
>> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
>> ARISING<br>
>> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER<br>
>> DEALINGS<br>
>> + * IN THE SOFTWARE.<br>
>> + */<br>
>> +<br>
>> +#include "nir.h"<br>
>> +<br>
>> +typedef enum {<br>
>> +   undefined,<br>
>> +   invariant,<br>
>> +   basic_induction<br>
>> +} nir_loop_variable_type;<br>
>> +<br>
>> +typedef struct {<br>
>> +   /* The ssa_def associated with this info */<br>
>> +   nir_ssa_def *def;<br>
>> +<br>
>> +   /* The type of this ssa_def */<br>
>> +   nir_loop_variable_type type;<br>
>> +<br>
>> +   /* If the ssa-def is constant */<br>
>> +   bool is_constant;<br>
>> +<br>
>> +   bool in_conditional_block;<br>
>> +<br>
>> +   bool in_nested_loop;<br>
>> +} nir_loop_variable;<br>
>> +<br>
>> +typedef struct {<br>
>> +   nir_op alu_op;                           /* The type of alu-operation<br>
>> */<br>
>> +   nir_loop_variable *alu_def;              /* The def of the<br>
>> alu-operation */<br>
>> +   nir_loop_variable *invariant;            /* The invariant alu-operand<br>
>> */<br>
>> +   nir_loop_variable *phi;                  /* The other alu-operand<br>
>> */<br>
>> +   nir_loop_variable *def_outside_loop;     /* The phi-src outside the<br>
>> loop */<br>
>> +} nir_basic_induction_var;<br>
>> +<br>
>> +typedef struct {<br>
>> +   /* A link for the work list */<br>
>> +   struct list_head process_link;<br>
>> +<br>
>> +   bool in_loop;<br>
>> +<br>
>> +   nir_loop_variable *nir_loop_var;<br>
>> +} loop_variable;<br>
>> +<br>
>> +typedef struct {<br>
>> +   bool contains_break;<br>
>> +   bool contains_continue;<br>
>> +} loop_jumps;<br>
>> +<br>
>> +typedef struct {<br>
>> +   /* The loop we store information for */<br>
>> +   nir_loop *loop;<br>
>> +<br>
>> +   /* Loop_variable for all ssa_defs in function */<br>
>> +   loop_variable *loop_vars;<br>
>> +<br>
>> +   /* Loop_variable for all ssa_defs in function */<br>
>> +   nir_loop_variable *nir_loop_vars;<br>
><br>
><br>
> Why are these two separate things?  You have two arrays both indexed by<br>
> nir_ssa_def::index and both called loop_vars and one has a pointer to the<br>
> other.  Can we just make 1 array?<br>
><br>
>><br>
>> +<br>
>> +   /* A list of the loop_vars to analyze */<br>
>> +   struct list_head process_list;<br>
>> +<br>
>> +   nir_loop_info *info;<br>
>> +<br>
>> +   nir_variable_mode indirect_mask;<br>
>> +<br>
>> +   struct hash_table *var_to_basic_ind;<br>
><br>
><br>
> Also, this seems like an unneeded level of indirection.  Just put a pointer<br>
> in loop_variable that points to the induction var struct.<br>
><br>
>><br>
>> +} loop_info_state;<br>
>> +<br>
>> +static loop_variable *<br>
>> +get_loop_var(nir_ssa_def *value, loop_info_state *state)<br>
>> +{<br>
>> +   return &(state->loop_vars[value-><wbr>index]);<br>
>> +}<br>
>> +<br>
>> +static nir_loop_variable *<br>
>> +get_nir_loop_var(nir_ssa_def *value, loop_info_state *state)<br>
>> +{<br>
>> +   return &(state->nir_loop_vars[value-><wbr>index]);<br>
>> +}<br>
>> +<br>
>> +typedef struct {<br>
>> +   loop_info_state *state;<br>
>> +   bool mark_nested;<br>
>> +   bool mark_in_conditional;<br>
>> +} init_loop_state;<br>
>> +<br>
>> +static bool<br>
>> +init_loop_def(nir_ssa_def *def, void *void_init_loop_state)<br>
>> +{<br>
>> +   init_loop_state *loop_init_state = void_init_loop_state;<br>
>> +   loop_variable *var = get_loop_var(def, loop_init_state->state);<br>
>> +<br>
>> +   /* Add to the tail of the list. That way we start at the beginning of<br>
>> the<br>
>> +    * defs in the loop instead of the end when walking the list. This<br>
>> means<br>
>> +    * less recursive calls. Only add defs that are not in nested loops or<br>
>> +    * conditional blocks.<br>
>> +    */<br>
>> +   if (!(loop_init_state->mark_in_<wbr>conditional ||<br>
>> +         loop_init_state->mark_nested))<br>
>> +      LIST_ADDTAIL(&(var->process_<wbr>link),<br>
>> +                   &(loop_init_state->state-><wbr>process_list));<br>
><br>
><br>
> I have a mild preference for the lower-case variants.  As I recall, they are<br>
> more for legacy.<br>
><br>
>><br>
>> +<br>
>> +   if (loop_init_state->mark_in_<wbr>conditional)<br>
>> +      var->nir_loop_var->in_<wbr>conditional_block = true;<br>
>> +<br>
>> +   if (loop_init_state->mark_nested)<br>
>> +      var->nir_loop_var->in_nested_<wbr>loop = true;<br>
>> +<br>
>> +   var->in_loop = true;<br>
>> +<br>
>> +   return true;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +init_loop_block(nir_block *block, void *void_init_loop_state)<br>
><br>
><br>
> Can we make this function take the actual parameters and limit<br>
> init_loop_state to being the thing we use for init_loop_def.  Passing piles<br>
> of pointers through made sense when Thomas originally wrote it but now that<br>
> we have better loop iteration macros, we can just pass arguments.<br>
><br>
>><br>
>> +{<br>
>> +   init_loop_state *loop_init_state = void_init_loop_state;<br>
>> +<br>
>> +   nir_foreach_instr(instr, block)<br>
>> +      nir_foreach_ssa_def(instr, init_loop_def, loop_init_state);<br>
>> +<br>
>> +   return true;<br>
>> +}<br>
>> +<br>
>> +static inline bool<br>
>> +is_var_alu(loop_variable *var)<br>
>> +{<br>
>> +   return (var->nir_loop_var->def-><wbr>parent_instr->type ==<br>
>> nir_instr_type_alu);<br>
>> +}<br>
>> +<br>
>> +static inline bool<br>
>> +is_var_phi(loop_variable *var)<br>
>> +{<br>
>> +   return (var->nir_loop_var->def-><wbr>parent_instr->type ==<br>
>> nir_instr_type_phi);<br>
>> +}<br>
>> +<br>
>> +static inline bool<br>
>> +is_ssa_def_invariant(nir_ssa_<wbr>def *def, loop_info_state *state)<br>
>> +{<br>
>> +   loop_variable *var = get_loop_var(def, state);<br>
>> +<br>
>> +   if (var->nir_loop_var->type == invariant)<br>
>> +      return true;<br>
>> +<br>
>> +   if (!var->in_loop) {<br>
>> +      var->nir_loop_var->type = invariant;<br>
>> +      return true;<br>
>> +   }<br>
>> +<br>
>> +   if (var->nir_loop_var->type == basic_induction)<br>
>> +      return false;<br>
>> +<br>
>> +   if (is_var_alu(var)) {<br>
>> +      nir_alu_instr *alu = nir_instr_as_alu(def->parent_<wbr>instr);<br>
>> +<br>
>> +      for (unsigned i = 0; i < nir_op_infos[alu->op].num_<wbr>inputs; i++) {<br>
>> +         if (!is_ssa_def_invariant(alu-><wbr>src[i].src.ssa, state))<br>
>> +            return false;<br>
>> +      }<br>
>> +      var->nir_loop_var->type = invariant;<br>
>> +      return true;<br>
>> +   }<br>
>> +<br>
>> +   /* Phis shouldn't be invariant except if one operand is invariant, and<br>
>> the<br>
>> +    * other is the phi itself. These should be removed by<br>
>> opt_remove_phis.<br>
>> +    * load_consts are already set to invariant and constant during init,<br>
>> +    * and so should return earlier. Remaining op_codes are set undefined.<br>
>> +    */<br>
>> +   var->nir_loop_var->type = undefined;<br>
>> +   return false;<br>
>> +}<br>
>> +<br>
>> +static void<br>
>> +compute_invariance_<wbr>information(loop_info_state *state)<br>
>> +{<br>
>> +   /* An expression is invariant in a loop L if:<br>
>> +    *  (base cases)<br>
>> +    *    – it’s a constant<br>
>> +    *    – it’s a variable use, all of whose single defs are outside of L<br>
>> +    *  (inductive cases)<br>
>> +    *    – it’s a pure computation all of whose args are loop invariant<br>
>> +    *    – it’s a variable use whose single reaching def, and the<br>
>> +    *      rhs of that def is loop-invariant<br>
>> +    */<br>
>> +   bool changes;<br>
>> +<br>
>> +   do {<br>
>> +      changes = false;<br>
>> +      list_for_each_entry_safe(loop_<wbr>variable, var,<br>
>> +                               &state->process_list, process_link) {<br>
>> +<br>
>> +         if (var->nir_loop_var->in_<wbr>conditional_block ||<br>
>> +             var->nir_loop_var->in_nested_<wbr>loop) {<br>
>> +            LIST_DEL(&var->process_link);<br>
>> +            continue;<br>
>> +         }<br>
>> +<br>
>> +         if (is_ssa_def_invariant(var-><wbr>nir_loop_var->def, state)) {<br>
>> +            LIST_DEL(&var->process_link);<br>
>> +            changes = true;<br>
>> +         }<br>
>> +      }<br>
>> +   } while (changes);<br>
>> +}<br>
>> +<br>
>> +static inline bool<br>
>> +is_var_basic_induction_var(<wbr>loop_variable *var, loop_info_state *state)<br>
>> +{<br>
>> +   if (var->nir_loop_var->type == basic_induction)<br>
>> +      return true;<br>
>> +<br>
>> +   /* We are only interested in checking phi's for the basic induction<br>
>> +    * variable case as its simple to detect. All basic induction<br>
>> variables<br>
>> +    * have a phi node<br>
>> +    */<br>
>> +   if (!is_var_phi(var))<br>
>> +      return false;<br>
>> +<br>
>> +   nir_phi_instr *phi =<br>
>> nir_instr_as_phi(var->nir_<wbr>loop_var->def->parent_instr);<br>
>> +<br>
>> +   nir_basic_induction_var *biv = rzalloc(state,<br>
>> nir_basic_induction_var);<br>
>> +   biv->phi = var->nir_loop_var;<br>
>> +<br>
>> +   nir_foreach_phi_src(src, phi) {<br>
>> +      loop_variable *src_var = get_loop_var(src->src.ssa, state);<br>
>> +<br>
>> +      /* If one of the sources is in a conditional or nested block then<br>
>> panic.<br>
>> +       */<br>
>> +      if (src_var->nir_loop_var->in_<wbr>conditional_block ||<br>
>> +          src_var->nir_loop_var->in_<wbr>nested_loop)<br>
>> +         break;<br>
>> +<br>
>> +      if (!src_var->in_loop)<br>
>> +         biv->def_outside_loop = src_var->nir_loop_var;<br>
><br>
><br>
> Could we assert biv->def_outside_loop == NULL<br>
><br>
>><br>
>> +<br>
>> +      if (src_var->in_loop && is_var_alu(src_var)) {<br>
>> +          nir_alu_instr *alu =<br>
>> +             nir_instr_as_alu(src_var->nir_<wbr>loop_var->def->parent_instr);<br>
>> +<br>
>> +         switch (alu->op) {<br>
>> +         case nir_op_fadd:    case nir_op_iadd:    case<br>
>> nir_op_uadd_carry:<br>
>> +         case nir_op_fsub:    case nir_op_isub:    case<br>
>> nir_op_usub_borrow:<br>
>> +         case nir_op_fmul:    case nir_op_imul:    case nir_op_umul_high:<br>
>> +         case nir_op_fdiv:    case nir_op_idiv:    case nir_op_udiv:<br>
><br>
><br>
> Is there a reason why this is an explicit list and not simply<br>
> "nir_op_infos[alu->op].num_<wbr>inputs == 2"?<br>
><br>
>><br>
>> +<br>
>> +            biv->alu_def = src_var->nir_loop_var;<br>
>> +<br>
>> +            for (unsigned i = 0; i < 2; i++) {<br>
>> +               /* Is one of the operands invariant, and the other the phi<br>
>> */<br>
>> +               if (is_ssa_def_invariant(alu-><wbr>src[i].src.ssa, state) &&<br>
>> +                   alu->src[1-i].src.ssa->index == phi->dest.ssa.index)<br>
>> +                  biv->invariant = get_nir_loop_var(alu->src[i].<wbr>src.ssa,<br>
>> +                                                    state);<br>
>> +            }<br>
>> +<br>
>> +            biv->alu_op = alu->op;<br>
>> +            break;<br>
>> +         default:<br>
>> +            break;<br>
>> +         }<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   if (biv->alu_def && biv->def_outside_loop && biv->invariant &&<br>
>> biv->phi) {<br>
>> +      biv->alu_def->type = basic_induction;<br>
>> +      biv->phi->type = basic_induction;<br>
>> +      _mesa_hash_table_insert(state-<wbr>>var_to_basic_ind, biv->alu_def,<br>
>> biv);<br>
>> +      _mesa_hash_table_insert(state-<wbr>>var_to_basic_ind, biv->phi, biv);<br>
>> +      return true;<br>
>> +   }<br>
>> +<br>
>> +   /* The requirements for a basic induction variable are not fulfilled<br>
>> */<br>
>> +   ralloc_free(biv);<br>
>> +   return false;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +compute_induction_<wbr>information(loop_info_state *state)<br>
>> +{<br>
>> +   bool changes;<br>
>> +   bool found_induction_var = false;<br>
>> +<br>
>> +   do {<br>
>> +      changes = false;<br>
>> +      list_for_each_entry_safe(loop_<wbr>variable, var,<br>
>> +                               &state->process_list, process_link) {<br>
>> +<br>
>> +         /* It can't be an induction variable if it is invariant. We<br>
>> don't<br>
>> +          * want to deal with things in nested loops or conditionals.<br>
>> +          */<br>
>> +         if (var->nir_loop_var->type == invariant ||<br>
>> +             var->nir_loop_var->in_<wbr>conditional_block ||<br>
>> +             var->nir_loop_var->in_nested_<wbr>loop) {<br>
>> +            LIST_DEL(&(var->process_link))<wbr>;<br>
>> +            continue;<br>
>> +         }<br>
>> +<br>
>> +         if (is_var_basic_induction_var(<wbr>var, state)) {<br>
>> +            /* If a phi is marked basic_ind we also mark the alu_def<br>
>> basic_ind<br>
>> +             * at the same time. It will then be detected as basic_ind<br>
>> here,<br>
>> +             * on the second passing, and be removed from the list.<br>
>> +             */<br>
>> +            LIST_DEL(&(var->process_link))<wbr>;<br>
>> +            changes = true;<br>
>> +            found_induction_var = true;<br>
>> +         }<br>
>> +      }<br>
>> +   } while (changes);<br>
>> +<br>
>> +   return found_induction_var;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +initialize_ssa_def(nir_ssa_<wbr>def *def, void *void_state)<br>
>> +{<br>
>> +   loop_info_state *state = void_state;<br>
>> +   loop_variable *var = get_loop_var(def, state);<br>
>> +<br>
>> +   var->nir_loop_var = get_nir_loop_var(def, state);<br>
>> +<br>
>> +   var->in_loop = false;<br>
>> +   var->nir_loop_var->def = def;<br>
>> +<br>
>> +   if (def->parent_instr->type == nir_instr_type_load_const) {<br>
>> +      var->nir_loop_var->type = invariant;<br>
>> +      var->nir_loop_var->is_constant = true;<br>
>> +   } else {<br>
>> +      var->nir_loop_var->type = undefined;<br>
>> +   }<br>
>> +<br>
>> +   return true;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +foreach_cf_node_ex_loop(nir_<wbr>cf_node *node, void *state)<br>
><br>
><br>
> There's no good reason for this to be a void *; it just leads to extra<br>
> casting.  Also, this function could use a better name.  Maybe<br>
> cf_node_find_loop_jumps?<br>
><br>
>><br>
>> +{<br>
>> +   nir_block *block;<br>
>> +<br>
>> +   switch (node->type) {<br>
>> +   case nir_cf_node_block:<br>
>> +      block = nir_cf_node_as_block(node);<br>
>> +      nir_foreach_instr(instr, block) {<br>
><br>
><br>
> Jumps are always the last instruction in a block.  There's no sense in<br>
> walking all of them just to find the jump.  Use nir_block_last_instr()<br>
> instead.<br>
><br>
>><br>
>> +         if (instr->type == nir_instr_type_jump) {<br>
>> +            if (nir_instr_as_jump(instr)-><wbr>type == nir_jump_break) {<br>
>> +               ((loop_jumps *) state)->contains_break = true;<br>
>> +            } else if (nir_instr_as_jump(instr)-><wbr>type ==<br>
>> nir_jump_continue) {<br>
>> +               ((loop_jumps *) state)->contains_continue = true;<br>
>> +            }<br>
>> +         }<br>
>> +      }<br>
>> +      return true;<br>
>> +<br>
>> +   case nir_cf_node_if: {<br>
>> +      nir_if *if_stmt = nir_cf_node_as_if(node);<br>
>> +<br>
>> +      foreach_list_typed_safe(nir_<wbr>cf_node, node, node,<br>
>> &if_stmt->then_list)<br>
>> +         if (!foreach_cf_node_ex_loop(<wbr>node, state))<br>
>> +            return false;<br>
>> +<br>
>> +      foreach_list_typed_safe(nir_<wbr>cf_node, node, node,<br>
>> &if_stmt->else_list)<br>
>> +         if (!foreach_cf_node_ex_loop(<wbr>node, state))<br>
>> +            return false;<br>
>> +<br>
>> +      break;<br>
>> +   }<br>
><br>
><br>
> Maybe add an explicit case for nir_cf_node_loop with a comment that we don't<br>
> care about inner loops.<br>
><br>
>><br>
>> +<br>
>> +   default:<br>
>> +      break;<br>
>> +   }<br>
>> +<br>
>> +   return false;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +is_trivial_loop_terminator(<wbr>nir_if *nif)<br>
>> +{<br>
>> +   /* If there is stuff in the else-block that means that this is not a<br>
>> +    * simple break on true if-statement and so we bail<br>
>> +    */<br>
>> +   foreach_list_typed_safe(nir_<wbr>cf_node, node, node, &nif->else_list)<br>
>> +      if (node->type == nir_cf_node_block)<br>
>> +         nir_foreach_instr(instr, nir_cf_node_as_block(node))<br>
>> +            return false;<br>
><br>
><br>
> How about "if (!exec_list_empty(&nif->else_<wbr>list)) return false;"?  That<br>
> seems far simpler.<br>
<br>
</div></div>That won't work, since cf lists are never empty. I think the intent<br>
was to check that the else branch consists of one empty basic block,<br>
in which case it's broken -- it could have just a loop, for example,<br>
and we wouldn't return false. I think what you want to do is get the<br>
first node, cast it to a basic block, bail if it has any instructions,<br>
and then bail if it doesn't equal the last node.<br></blockquote><div><br></div><div>You are correct, sir.  That is what I meant.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">
><br>
>><br>
>> +<br>
>> +   nir_cf_node *first_then = nir_if_first_then_node(nif);<br>
>> +   nir_block *first_then_block = nir_cf_node_as_block(first_<wbr>then);<br>
>> +   nir_instr *first_instr = nir_block_first_instr(first_<wbr>then_block);<br>
>> +<br>
>> +   if (first_instr && first_instr->type == nir_instr_type_jump &&<br>
>> +       nir_instr_as_jump(first_instr)<wbr>->type == nir_jump_break) {<br>
>> +      return true;<br>
>> +   }<br>
>> +<br>
>> +   return false;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +find_loop_terminators(loop_<wbr>info_state *state)<br>
>> +{<br>
>> +   bool success = false;<br>
>> +   foreach_list_typed_safe(nir_<wbr>cf_node, node, node, &state->loop->body) {<br>
>> +      if (node->type == nir_cf_node_if) {<br>
>> +         nir_if *nif = nir_cf_node_as_if(node);<br>
>> +<br>
>> +         /* Don't check the nested loops if there are breaks */<br>
>> +         loop_jumps lj;<br>
>> +         lj.contains_break = false;<br>
>> +         lj.contains_continue = false;<br>
>> +<br>
>> +         foreach_cf_node_ex_loop(&nif-><wbr>cf_node, &lj);<br>
>> +<br>
>> +         if (lj.contains_continue)<br>
>> +            return false;<br>
>> +<br>
>> +         if (!lj.contains_break)<br>
>> +            continue;<br>
>> +<br>
>> +         /* If there is a break then we should find a terminator. If we<br>
>> can<br>
>> +          * not find a loop terminator, but there is a break-statement<br>
>> then<br>
>> +          * we should return false so that we do not try to find<br>
>> trip-count<br>
>> +          */<br>
>> +         if (!is_trivial_loop_terminator(<wbr>nif))<br>
>> +            return false;<br>
>> +<br>
>> +         if (nif->condition.ssa->parent_<wbr>instr->type ==<br>
>> nir_instr_type_phi)<br>
>> +            return false;<br>
>> +<br>
>> +         nir_loop_terminator *terminator =<br>
>> +            rzalloc(state->info, nir_loop_terminator);<br>
>> +<br>
>> +         list_add(&terminator->loop_<wbr>terminator_link,<br>
>> +                  &state->info->loop_terminator_<wbr>list);<br>
>> +<br>
>> +         terminator->nif = nif;<br>
>> +         terminator->conditional_instr =<br>
>> nif->condition.ssa->parent_<wbr>instr;<br>
>> +<br>
>> +         success = true;<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   return success;<br>
>> +}<br>
>> +<br>
>> +static nir_basic_induction_var *<br>
>> +get_basic_ind_var_for_loop_<wbr>var(loop_variable *var, loop_info_state<br>
>> *state)<br>
>> +{<br>
>> +   assert(var->nir_loop_var->type == basic_induction);<br>
>> +<br>
>> +   struct hash_entry *entry =<br>
>> +      _mesa_hash_table_search(state-<wbr>>var_to_basic_ind,<br>
>> var->nir_loop_var);<br>
>> +<br>
>> +   return entry->data;<br>
>> +}<br>
>> +<br>
>> +static int32_t<br>
>> +get_iteration(nir_op cond_op, nir_const_value *initial, nir_const_value<br>
>> *step,<br>
>> +              nir_const_value *limit, nir_alu_instr *alu, int32_t<br>
>> *init_val,<br>
>> +              bool increment_before)<br>
>> +{<br>
>> +   int32_t iter;<br>
>> +<br>
>> +   switch (cond_op) {<br>
>> +   case nir_op_ige:<br>
>> +   case nir_op_ilt:<br>
>> +   case nir_op_ieq:<br>
>> +   case nir_op_ine: {<br>
>> +      int32_t initial_val = initial->i32[0];<br>
>> +      if (increment_before) {<br>
>> +         initial_val = alu->op == nir_op_iadd ?<br>
>> +            initial_val + step->i32[0] : initial_val - step->i32[0];<br>
>> +      }<br>
>> +<br>
>> +      int32_t span = limit->i32[0] - initial_val;<br>
>> +      iter = span / step->i32[0];<br>
>> +      *init_val = initial_val;<br>
>> +      break;<br>
>> +   }<br>
>> +   case nir_op_uge:<br>
>> +   case nir_op_ult: {<br>
>> +      uint32_t initial_val = initial->u32[0];<br>
>> +      if (increment_before) {<br>
>> +         initial_val = alu->op == nir_op_iadd ?<br>
>> +            initial_val + step->u32[0] : initial_val - step->u32[0];<br>
>> +      }<br>
>> +<br>
>> +      uint32_t span = limit->u32[0] - initial_val;<br>
>> +      iter = span / step->u32[0];<br>
>> +      *init_val = initial_val;<br>
>> +      break;<br>
>> +   }<br>
>> +   default:<br>
>> +      return -1;<br>
>> +   }<br>
>> +<br>
>> +   return iter;<br>
>> +}<br>
>> +<br>
>> +static uint32_t<br>
>> +utest_interations(int32_t iter_int, nir_const_value *step,<br>
>> +                  nir_const_value *limit, nir_op cond_op,<br>
>> +                  uint32_t initial_val, bool limit_rhs)<br>
>> +{<br>
>> +   bool valid_loop = false;<br>
>> +   uint32_t mul = iter_int * step->u32[0];<br>
>> +   uint32_t uadd = mul + initial_val;<br>
>> +<br>
>> +   switch (cond_op) {<br>
>> +   case nir_op_uge:<br>
>> +      valid_loop = limit_rhs ? uadd >= limit->u32[0] : uadd <=<br>
>> limit->u32[0];<br>
>> +      break;<br>
>> +   case nir_op_ult:<br>
>> +      valid_loop = limit_rhs ? uadd < limit->u32[0] : uadd ><br>
>> limit->u32[0];<br>
>> +      break;<br>
>> +   default:<br>
>> +      unreachable("Unhandled loop condition!");<br>
>> +   }<br>
>> +<br>
>> +   return valid_loop;<br>
>> +}<br>
>> +<br>
>> +static int32_t<br>
>> +itest_interations(int32_t iter_int, nir_const_value *step,<br>
>> +                  nir_const_value *limit, nir_op cond_op,<br>
>> +                  int32_t initial_val, bool limit_rhs)<br>
>> +{<br>
>> +   bool valid_loop = false;<br>
>> +   int32_t mul = iter_int * step->i32[0];<br>
>> +   int32_t iadd = mul + initial_val;<br>
>> +<br>
>> +   switch (cond_op) {<br>
>> +   case nir_op_ige:<br>
>> +      valid_loop = limit_rhs ? iadd >= limit->i32[0] : iadd <=<br>
>> limit->i32[0];<br>
>> +      break;<br>
>> +   case nir_op_ilt:<br>
>> +      valid_loop = limit_rhs ? iadd < limit->i32[0] : iadd ><br>
>> limit->i32[0];<br>
>> +      break;<br>
>> +   case nir_op_ieq:<br>
>> +      valid_loop = iadd == limit->i32[0];<br>
>> +      break;<br>
>> +   case nir_op_ine:<br>
>> +      valid_loop = iadd != limit->i32[0];<br>
>> +      break;<br>
>> +   default:<br>
>> +      unreachable("Unhandled loop condition!");<br>
>> +   }<br>
>> +<br>
>> +   return valid_loop;<br>
>> +}<br>
>> +<br>
>> +static int<br>
>> +calculate_iterations(nir_<wbr>const_value *initial, nir_const_value *step,<br>
>> +                     nir_const_value *limit, nir_op cond_op,<br>
>> +                     nir_loop_variable *alu_def, nir_alu_instr *cond_alu,<br>
>> +                     bool limit_rhs)<br>
>> +{<br>
>> +   assert(initial != NULL && step != NULL && limit != NULL);<br>
>> +<br>
>> +   nir_alu_instr *alu = nir_instr_as_alu(alu_def->def-<wbr>>parent_instr);<br>
>> +<br>
>> +   /* Unsupported alu operation */<br>
>> +   if (!(alu->op == nir_op_iadd || alu->op == nir_op_isub))<br>
><br>
><br>
> You only allow iadd and isub except that we lower away isub so you'll never<br>
> see it.  Probably best to remove the dead code.  Also...<br>
><br>
>  1) You don't handle swizzles at all<br>
>  2) You don't handle things other than 64 or 16-bit.  Those are coming soon;<br>
> they need to be supported.<br>
><br>
>><br>
>> +      return -1;<br>
>> +<br>
>> +   /* do-while loops can increment the starting value before the<br>
>> condition is<br>
>> +    * checked. e.g.<br>
>> +    *<br>
>> +    *    do {<br>
>> +    *        ndx++;<br>
>> +    *     } while (ndx < 3);<br>
>> +    *<br>
>> +    * Here we check if the induction variable is used directly by the<br>
>> loop<br>
>> +    * condition and if so we assume we need to step the initial value.<br>
>> +    */<br>
>> +   bool increment_before = false;<br>
>> +   if (cond_alu->src[0].src.ssa == alu_def->def ||<br>
>> +       cond_alu->src[1].src.ssa == alu_def->def) {<br>
>> +      increment_before = true;<br>
><br>
><br>
> Is there a reason why this can't be handled as "trip_count + 1"?  This seems<br>
> way overcomplicated.<br>
><br>
>><br>
>> +   }<br>
>> +<br>
>> +   int32_t initial_val;<br>
>> +   int iter_int = get_iteration(cond_op, initial, step, limit, alu,<br>
>> +                                &initial_val, increment_before);<br>
>> +<br>
>> +   /* If iter_int is negative the loop is ill-formed or is the<br>
>> conditional is<br>
>> +    * unsigned with a huge iteration count so don't bother going any<br>
>> further.<br>
>> +    */<br>
>> +   if (iter_int < 0)<br>
>> +      return -1;<br>
>> +<br>
>> +   /* An explanation from the GLSL unrolling pass:<br>
>> +    *<br>
>> +    * Make sure that the calculated number of iterations satisfies the<br>
>> exit<br>
>> +    * condition.  This is needed to catch off-by-one errors and some<br>
>> types of<br>
>> +    * ill-formed loops.  For example, we need to detect that the<br>
>> following<br>
>> +    * loop does not have a maximum iteration count.<br>
>> +    *<br>
>> +    *    for (float x = 0.0; x != 0.9; x += 0.2);<br>
>> +    */<br>
>> +   const int bias[] = { -1, 1, 1 };<br>
>> +<br>
>> +   for (unsigned i = 0; i < ARRAY_SIZE(bias); i++) {<br>
>> +      iter_int = iter_int + bias[i];<br>
>> +<br>
>> +      switch (cond_op) {<br>
>> +      case nir_op_ige:<br>
>> +      case nir_op_ilt:<br>
>> +      case nir_op_ieq:<br>
>> +      case nir_op_ine:<br>
>> +         if (itest_interations(iter_int, step, limit, cond_op,<br>
>> initial_val,<br>
>> +                               limit_rhs)) {<br>
>> +            return iter_int;<br>
>> +         }<br>
>> +         break;<br>
>> +      case nir_op_uge:<br>
>> +      case nir_op_ult:<br>
>> +         if (utest_interations(iter_int, step, limit, cond_op,<br>
>> +                               (uint32_t) initial_val, limit_rhs)) {<br>
>> +            return iter_int;<br>
><br>
><br>
> I think it would be clearer if we combined these two functions.<br>
><br>
>><br>
>> +         }<br>
>> +         break;<br>
>> +      default:<br>
>> +         return -1;<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   return -1;<br>
>> +}<br>
>> +<br>
>> +/* Run through each of the terminators of the loop and try to infer a<br>
>> possible<br>
>> + * trip-count. We need to check them all, and set the lowest trip-count<br>
>> as the<br>
>> + * trip-count of our loop. If one of the terminators has an undecidable<br>
>> + * trip-count we can not safely assume anything about the duration of the<br>
>> + * loop.<br>
>> + */<br>
>> +static void<br>
>> +find_trip_count(loop_info_<wbr>state *state)<br>
>> +{<br>
>> +   bool trip_count_known = true;<br>
>> +   nir_loop_terminator *limiting_terminator = NULL;<br>
>> +   int min_trip_count = -2;<br>
>> +<br>
>> +   list_for_each_entry(nir_loop_<wbr>terminator, terminator,<br>
>> +                       &state->info->loop_terminator_<wbr>list,<br>
>> +                       loop_terminator_link) {<br>
>> +<br>
>> +      if (terminator->conditional_<wbr>instr->type != nir_instr_type_alu) {<br>
>> +         /* If we get here the loop is likely not really a loop and will<br>
>> get<br>
>> +          * cleaned up elsewhere.<br>
>> +          */<br>
><br>
><br>
> The if statement (and its contents) seem fine but the comment here seems<br>
> sketchy.<br>
><br>
>><br>
>> +         trip_count_known = false;<br>
>> +         continue;<br>
>> +      }<br>
>> +<br>
>> +      nir_alu_instr *alu =<br>
>> nir_instr_as_alu(terminator-><wbr>conditional_instr);<br>
>> +      loop_variable *basic_ind = NULL;<br>
>> +      loop_variable *limit = NULL;<br>
>> +      bool limit_rhs = true;<br>
>> +      nir_op cond_op;<br>
>> +<br>
>> +      switch (alu->op) {<br>
>> +      case nir_op_fge:      case nir_op_ige:      case nir_op_uge:<br>
>> +      case nir_op_flt:      case nir_op_ilt:      case nir_op_ult:<br>
>> +      case nir_op_feq:      case nir_op_ieq:<br>
>> +      case nir_op_fne:      case nir_op_ine:<br>
>> +<br>
>> +         /* We assume that the limit is the "right" operand */<br>
>> +         basic_ind = get_loop_var(alu->src[0].src.<wbr>ssa, state);<br>
>> +         limit = get_loop_var(alu->src[1].src.<wbr>ssa, state);<br>
>> +         cond_op = alu->op;<br>
>> +<br>
>> +         if (basic_ind->nir_loop_var->type != basic_induction) {<br>
>> +            /* We had it the wrong way, flip things around */<br>
>> +            basic_ind = get_loop_var(alu->src[1].src.<wbr>ssa, state);<br>
>> +            limit = get_loop_var(alu->src[0].src.<wbr>ssa, state);<br>
>> +            limit_rhs = false;<br>
>> +         }<br>
>> +<br>
>> +         /* The comparison has to have a basic induction variable<br>
>> +          * and a constant for us to be able to find trip counts<br>
>> +          */<br>
>> +         if (basic_ind->nir_loop_var->type != basic_induction ||<br>
>> +             !limit->nir_loop_var->is_<wbr>constant) {<br>
>> +            trip_count_known = false;<br>
>> +            continue;<br>
>> +         }<br>
>> +<br>
>> +         nir_basic_induction_var *ind =<br>
>> +               get_basic_ind_var_for_loop_<wbr>var(basic_ind, state);<br>
>> +<br>
>> +         if (!ind->def_outside_loop->is_<wbr>constant ||<br>
>> +             !ind->invariant->is_constant) {<br>
>> +            trip_count_known = false;<br>
>> +            continue;<br>
>> +         }<br>
>> +<br>
>> +         /* We have determined that we have the following constants:<br>
>> +          * (With the typical int i = 0; i < x; i++; as an example)<br>
>> +          *    - Upper limit.<br>
>> +          *    - Starting value<br>
>> +          *    - Step / iteration size<br>
>> +          * Thats all thats needed to calculate the trip-count<br>
>> +          */<br>
>> +<br>
>> +         nir_load_const_instr *initial_instr =<br>
>> +               nir_instr_as_load_const(<br>
>> +                     ind->def_outside_loop->def-><wbr>parent_instr);<br>
>> +<br>
>> +         nir_const_value initial_val = initial_instr->value;<br>
>> +<br>
>> +         nir_load_const_instr *step_instr =<br>
>> +               nir_instr_as_load_const(<br>
>> +                     ind->invariant->def->parent_<wbr>instr);<br>
>> +<br>
>> +         nir_const_value step_val = step_instr->value;<br>
>> +<br>
>> +         nir_load_const_instr *limit_instr =<br>
>> +               nir_instr_as_load_const(<br>
>> +                     limit->nir_loop_var->def-><wbr>parent_instr);<br>
>> +<br>
>> +         nir_const_value limit_val = limit_instr->value;<br>
>> +<br>
>> +         int iterations = calculate_iterations(&initial_<wbr>val, &step_val,<br>
>> +                                               &limit_val, cond_op,<br>
>> +                                               ind->alu_def, alu,<br>
>> limit_rhs);<br>
>> +<br>
>> +         /* Where we not able to calculate the iteration count */<br>
>> +         if (iterations == -1) {<br>
>> +            trip_count_known = false;<br>
>> +            continue;<br>
>> +         }<br>
>> +<br>
>> +         /* If this is the first run or we have found a smaller amount of<br>
>> +          * iterations than previously (we have identified a more<br>
>> limiting<br>
>> +          * terminator) set the trip count and limiting terminator.<br>
>> +          */<br>
>> +         if (min_trip_count == -2 || iterations < min_trip_count) {<br>
><br>
><br>
> Can we have a #define for -2 so that it has a name.<br>
><br>
>><br>
>> +            min_trip_count = iterations;<br>
>> +            limiting_terminator = terminator;<br>
>> +         }<br>
>> +         break;<br>
>> +<br>
>> +      default:<br>
>> +         trip_count_known = false;<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   state->info->is_trip_count_<wbr>known = trip_count_known;<br>
>> +   if (min_trip_count > -2)<br>
>> +      state->info->trip_count = min_trip_count;<br>
>> +   state->info->limiting_<wbr>terminator = limiting_terminator;<br>
>> +}<br>
>> +<br>
>> +/* Checks if we should force the loop to be unrolled regardless of size<br>
>> */<br>
>> +static bool<br>
>> +force_unroll(loop_info_state *state, nir_shader *ns, nir_deref_var<br>
>> *variable)<br>
>> +{<br>
>> +   nir_deref *tail = &variable->deref;<br>
>> +<br>
>> +   while (tail->child != NULL) {<br>
>> +      tail = tail->child;<br>
>> +<br>
>> +      if (tail->deref_type == nir_deref_type_array) {<br>
>> +<br>
>> +         nir_deref_array *deref_array = nir_deref_as_array(tail);<br>
>> +         if (deref_array->deref_array_type !=<br>
>> nir_deref_array_type_indirect)<br>
>> +            continue;<br>
>> +<br>
>> +         nir_loop_variable *array_index =<br>
>> +            get_nir_loop_var(deref_array-><wbr>indirect.ssa, state);<br>
>> +<br>
>> +         if (array_index->type != basic_induction)<br>
>> +            continue;<br>
>> +<br>
>> +         /* If an array is indexed by a loop induction variable, and the<br>
>> +          * array size is exactly the number of loop iterations, this is<br>
>> +          * probably a simple for-loop trying to access each element in<br>
>> +          * turn; the application may expect it to be unrolled.<br>
>> +          */<br>
>> +         if (glsl_get_length(tail->type) == state->info->trip_count) {<br>
>> +            state->info->force_unroll = true;<br>
>> +            return state->info->force_unroll;<br>
>> +         }<br>
>> +<br>
>> +         if (variable->var->data.mode & state->indirect_mask) {<br>
>> +            state->info->force_unroll = true;<br>
>> +            return state->info->force_unroll;<br>
>> +         }<br>
><br>
><br>
> Thinking a bit about analysis vs. lowering...<br>
><br>
> I wonder if it wouldn't be better to make the loop anlaysis pass be a bit<br>
> more informational and less decision making.  For instance, it could record<br>
> what all variable modes it has seen be indexed by an induction variable and<br>
> let the pass using the analysis decide whether or not to force-unroll.  For<br>
> that matter, it could just produce a hash map from induction variables to<br>
> the loop for which they are an induction variable.  You could also just<br>
> record the trip count per-loop.  That all seems a bit more like things an<br>
> analysis pass would do than just setting a force_unroll bit.<br>
><br>
> Please don't feel like you need to make any changes in this direction yet.<br>
> I'm mostly trying to open the discussion up a bit and feel out exactly how<br>
> we want things to be structured.<br>
><br>
> I think I've written you a long enough book for now.  I'll try to look at<br>
> the others as I get time.<br>
><br>
> --Jason<br>
><br>
>><br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   return false;<br>
>> +}<br>
>> +<br>
>> +static bool<br>
>> +count_instructions(loop_info_<wbr>state *state, nir_shader *ns, nir_block<br>
>> *block)<br>
>> +{<br>
>> +   nir_foreach_instr(instr, block) {<br>
>> +      if (instr->type == nir_instr_type_intrinsic ||<br>
>> +          instr->type == nir_instr_type_alu) {<br>
>> +         state->info->num_instructions+<wbr>+;<br>
>> +      }<br>
>> +<br>
>> +      if (instr->type != nir_instr_type_intrinsic)<br>
>> +         continue;<br>
>> +<br>
>> +      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
>> +<br>
>> +      /* Check for arrays variably-indexed by a loop induction variable.<br>
>> +       * Unrolling the loop may convert that access into<br>
>> constant-indexing.<br>
>> +       */<br>
>> +      if (intrin->intrinsic == nir_intrinsic_load_var ||<br>
>> +          intrin->intrinsic == nir_intrinsic_store_var ||<br>
>> +          intrin->intrinsic == nir_intrinsic_copy_var) {<br>
>> +         unsigned num_vars =<br>
>> +            nir_intrinsic_infos[intrin-><wbr>intrinsic].num_variables;<br>
>> +         for (unsigned i = 0; i < num_vars; i++) {<br>
>> +            if (force_unroll(state, ns, intrin->variables[i]))<br>
>> +               return true;<br>
>> +         }<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   return false;<br>
>> +}<br>
>> +<br>
>> +static void<br>
>> +get_loop_info(loop_info_state *state, nir_function_impl *impl)<br>
>> +{<br>
>> +   /* Initialize all variables to "outside_loop". This also marks defs<br>
>> +    * invariant and constant if they are nir_instr_type_load_const's<br>
>> +    */<br>
>> +   nir_foreach_block(block, impl) {<br>
>> +      nir_foreach_instr(instr, block)<br>
>> +         nir_foreach_ssa_def(instr, initialize_ssa_def, state);<br>
>> +   }<br>
>> +<br>
>> +   init_loop_state init_state = {.mark_in_conditional = false,<br>
>> +                                 .mark_nested = false, .state = state };<br>
>> +<br>
>> +   /* Add all entries in the outermost part of the loop to the processing<br>
>> list<br>
>> +    * Mark the entries in conditionals or in nested loops accordingly<br>
>> +    */<br>
>> +   foreach_list_typed_safe(nir_<wbr>cf_node, node, node, &state->loop->body) {<br>
>> +      switch (node->type) {<br>
>> +<br>
>> +      case nir_cf_node_block:<br>
>> +         init_state.mark_in_conditional = false;<br>
>> +         init_state.mark_nested = false;<br>
>> +         init_loop_block(nir_cf_node_<wbr>as_block(node), &init_state);<br>
>> +         break;<br>
>> +<br>
>> +      case nir_cf_node_if:<br>
>> +         init_state.mark_in_conditional = true;<br>
>> +         init_state.mark_nested = false;<br>
>> +         nir_foreach_block_in_cf_node(<wbr>block, node)<br>
>> +            init_loop_block(block, &init_state);<br>
>> +         break;<br>
>> +<br>
>> +      case nir_cf_node_loop:<br>
>> +         init_state.mark_in_conditional = false;<br>
>> +         init_state.mark_nested = true;<br>
>> +         nir_foreach_block_in_cf_node(<wbr>block, node) {<br>
>> +            init_loop_block(block, &init_state);<br>
>> +         }<br>
>> +         break;<br>
>> +<br>
>> +      case nir_cf_node_function:<br>
>> +         break;<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   /* Induction analysis needs invariance information so get that first<br>
>> */<br>
>> +   compute_invariance_<wbr>information(state);<br>
>> +<br>
>> +   /* We may now have filled the process_list with instructions from<br>
>> inside<br>
>> +    * the nested blocks in the loop. Remove all instructions from the<br>
>> list<br>
>> +    * nir_foreach_block_in_cf_node before we start computing induction<br>
>> +    * information.<br>
>> +    */<br>
>> +   list_inithead(&state->process_<wbr>list);<br>
>> +<br>
>> +   /* Add all entries in the outermost part of the loop to the processing<br>
>> list.<br>
>> +    * Don't include defs inn nested loops or in conditionals.<br>
>> +    */<br>
>> +   init_state.mark_in_conditional = false;<br>
>> +   init_state.mark_nested = false;<br>
>> +<br>
>> +   foreach_list_typed_safe(nir_<wbr>cf_node, node, node, &state->loop->body)<br>
>> +      if (node->type == nir_cf_node_block)<br>
>> +         init_loop_block(nir_cf_node_<wbr>as_block(node), &init_state);<br>
>> +<br>
>> +   /* We have invariance information so try to find induction variables<br>
>> */<br>
>> +   if (!compute_induction_<wbr>information(state))<br>
>> +      return;<br>
>> +<br>
>> +   /* Try to find all simple terminators of the loop. If we can't find<br>
>> any,<br>
>> +    * or we find possible terminators that have side effects then bail.<br>
>> +    */<br>
>> +   if (!find_loop_terminators(state)<wbr>)<br>
>> +      return;<br>
>> +<br>
>> +   /* Run through each of the terminators and try to compute a trip-count<br>
>> */<br>
>> +   find_trip_count(state);<br>
>> +<br>
>> +   nir_shader *ns = impl->function->shader;<br>
>> +   foreach_list_typed_safe(nir_<wbr>cf_node, node, node, &state->loop->body) {<br>
>> +      if (node->type == nir_cf_node_block) {<br>
>> +         if (count_instructions(state, ns, nir_cf_node_as_block(node)))<br>
>> +            break;<br>
>> +      } else {<br>
>> +         nir_foreach_block_in_cf_node(<wbr>block, node) {<br>
>> +            if (count_instructions(state, ns, block))<br>
>> +               break;<br>
>> +         }<br>
>> +      }<br>
>> +   }<br>
>> +}<br>
>> +<br>
>> +static loop_info_state *<br>
>> +initialize_loop_info_state(<wbr>nir_loop *loop, void *mem_ctx,<br>
>> nir_function_impl *impl)<br>
>> +{<br>
>> +   loop_info_state *state = rzalloc(mem_ctx, loop_info_state);<br>
>> +   state->loop_vars = rzalloc_array(mem_ctx, loop_variable,<br>
>> impl->ssa_alloc);<br>
>> +   state->loop = loop;<br>
>> +   state->nir_loop_vars = rzalloc_array(mem_ctx, nir_loop_variable,<br>
>> +                                        impl->ssa_alloc);<br>
>> +<br>
>> +   LIST_INITHEAD(&state->process_<wbr>list);<br>
>> +<br>
>> +   if (loop->info)<br>
>> +     ralloc_free(loop->info);<br>
>> +<br>
>> +   state->info = rzalloc(loop, nir_loop_info);<br>
>> +<br>
>> +   LIST_INITHEAD(&state->info-><wbr>loop_terminator_list);<br>
>> +<br>
>> +   state->var_to_basic_ind =<br>
>> +      _mesa_hash_table_create(state-<wbr>>info, _mesa_hash_pointer,<br>
>> +                              _mesa_key_pointer_equal);<br>
>> +<br>
>> +   return state;<br>
>> +}<br>
>> +<br>
>> +static void<br>
>> +process_loops(nir_cf_node *cf_node, nir_variable_mode indirect_mask)<br>
>> +{<br>
>> +   switch (cf_node->type) {<br>
>> +   case nir_cf_node_block:<br>
>> +      return;<br>
>> +   case nir_cf_node_if: {<br>
>> +      nir_if *if_stmt = nir_cf_node_as_if(cf_node);<br>
>> +      foreach_list_typed(nir_cf_<wbr>node, nested_node, node,<br>
>> &if_stmt->then_list)<br>
>> +         process_loops(nested_node, indirect_mask);<br>
>> +      foreach_list_typed(nir_cf_<wbr>node, nested_node, node,<br>
>> &if_stmt->else_list)<br>
>> +         process_loops(nested_node, indirect_mask);<br>
>> +      return;<br>
>> +   }<br>
>> +   case nir_cf_node_loop: {<br>
>> +      nir_loop *loop = nir_cf_node_as_loop(cf_node);<br>
>> +      foreach_list_typed(nir_cf_<wbr>node, nested_node, node, &loop->body)<br>
>> +         process_loops(nested_node, indirect_mask);<br>
>> +      break;<br>
>> +   }<br>
>> +   default:<br>
>> +      unreachable("unknown cf node type");<br>
>> +   }<br>
>> +<br>
>> +   nir_loop *loop = nir_cf_node_as_loop(cf_node);<br>
>> +   nir_function_impl *impl = nir_cf_node_get_function(cf_<wbr>node);<br>
>> +   void *mem_ctx = ralloc_context(NULL);<br>
>> +<br>
>> +   loop_info_state *state = initialize_loop_info_state(<wbr>loop, mem_ctx,<br>
>> impl);<br>
>> +   state->indirect_mask = indirect_mask;<br>
>> +<br>
>> +   get_loop_info(state, impl);<br>
>> +<br>
>> +   loop->info = state->info;<br>
>> +<br>
>> +   ralloc_free(mem_ctx);<br>
>> +}<br>
>> +<br>
>> +void<br>
>> +nir_loop_analyze_impl(nir_<wbr>function_impl *impl,<br>
>> +                      nir_variable_mode indirect_mask)<br>
>> +{<br>
>> +   if (impl->function->shader-><wbr>options->max_unroll_iterations == 0)<br>
>> +      return;<br>
>> +<br>
>> +   nir_index_ssa_defs(impl);<br>
>> +   foreach_list_typed(nir_cf_<wbr>node, node, node, &impl->body)<br>
>> +      process_loops(node, indirect_mask);<br>
>> +}<br>
>> diff --git a/src/compiler/nir/nir_<wbr>metadata.c<br>
>> b/src/compiler/nir/nir_<wbr>metadata.c<br>
>> index 9e1cff5..f71cf43 100644<br>
>> --- a/src/compiler/nir/nir_<wbr>metadata.c<br>
>> +++ b/src/compiler/nir/nir_<wbr>metadata.c<br>
>> @@ -31,7 +31,7 @@<br>
>>   */<br>
>><br>
>>  void<br>
>> -nir_metadata_require(nir_<wbr>function_impl *impl, nir_metadata required)<br>
>> +nir_metadata_require(nir_<wbr>function_impl *impl, nir_metadata required, ...)<br>
>>  {<br>
>>  #define NEEDS_UPDATE(X) ((required & ~impl->valid_metadata) & (X))<br>
>><br>
>> @@ -41,6 +41,12 @@ nir_metadata_require(nir_<wbr>function_impl *impl,<br>
>> nir_metadata required)<br>
>>        nir_calc_dominance_impl(impl);<br>
>>     if (NEEDS_UPDATE(nir_metadata_<wbr>live_ssa_defs))<br>
>>        nir_live_ssa_defs_impl(impl);<br>
>> +   if (NEEDS_UPDATE(nir_metadata_<wbr>loop_analysis)) {<br>
>> +      va_list ap;<br>
>> +      va_start(ap, required);<br>
>> +      nir_loop_analyze_impl(impl, va_arg(ap, nir_variable_mode));<br>
>> +      va_end(ap);<br>
>> +   }<br>
>><br>
>>  #undef NEEDS_UPDATE<br>
>><br>
>> --<br>
>> 2.7.4<br>
>><br>
>> ______________________________<wbr>_________________<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/<wbr>mailman/listinfo/mesa-dev</a><br>
><br>
><br>
><br>
> ______________________________<wbr>_________________<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/<wbr>mailman/listinfo/mesa-dev</a><br>
><br>
</div></div></blockquote></div><br></div></div>