<html><head></head><body><div>On Fri, 2016-09-16 at 15:25 -0700, Jason Ekstrand wrote:</div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Sep 15, 2016 at 12:03 AM, Timothy Arceri <span dir="ltr"><<a href="mailto:timothy.arceri@collabora.com" target="_blank">timothy.arceri@collabora.com</a>></span> wrote:<br><blockquote type="cite">From: Thomas Helland <<a href="mailto:thomashelland90@gmail.com" target="_blank">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<wbr> | 2 +<br>
src/compiler/nir/nir.h | 36 +-<br>
src/compiler/nir/nir_loop_ana<wbr>lyze.c | 1012 ++++++++++++++++++++++++++++++<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_anal<wbr>yze.c<br>
<br>
diff --git a/src/compiler/Makefile.source<wbr>s b/src/compiler/Makefile.source<wbr>s<br>
index f5b4f9c..7ed26a9 100644<br>
--- a/src/compiler/Makefile.source<wbr>s<br>
+++ b/src/compiler/Makefile.source<wbr>s<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></blockquote><div><br></div><div>We could use 0 or -1 to indicate "I don't know trip count" instead of an extra boolean. Not sure that it matters much.<br></div><div> </div><blockquote type="cite">
+<br>
+ /* Unroll the loop regardless of its size */<br>
+ bool force_unroll;<br></blockquote><div><br></div><div>It seems a bit odd to have this decide to force-unroll. This is an analysis pass, not a "make decisions" pass.</div></div></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+<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_rese<wbr>t = 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_intrins<wbr>ics;<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_funct<wbr>ion_impl *impl, nir_metadata required);<br>
+void nir_metadata_require(nir_funct<wbr>ion_impl *impl, nir_metadata required, ...);<br>
/** dirties all but the preserved metadata */<br>
void nir_metadata_preserve(nir_func<wbr>tion_impl *impl, nir_metadata preserved);<br>
<br>
@@ -2559,6 +2589,10 @@ void nir_lower_double_pack(nir_shad<wbr>er *shader);<br>
bool nir_normalize_cubemap_coords(n<wbr>ir_shader *shader);<br>
<br>
void nir_live_ssa_defs_impl(nir_fun<wbr>ction_impl *impl);<br>
+<br>
+void nir_loop_analyze_impl(nir_func<wbr>tion_impl *impl,<br>
+ nir_variable_mode indirect_mask);<br>
+<br>
bool nir_ssa_defs_interfere(nir_ssa<wbr>_def *a, nir_ssa_def *b);<br>
<br>
void nir_convert_to_ssa_impl(nir_fu<wbr>nction_impl *impl);<br>
diff --git a/src/compiler/nir/nir_loop_an<wbr>alyze.c b/src/compiler/nir/nir_loop_an<wbr>alyze.c<br>
new file mode 100644<br>
index 0000000..6bea9e5<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_loop_an<wbr>alyze.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 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>
+<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>
+ nir_loop_variable *alu_def; /* The def of the alu-operation */<br>
+ nir_loop_variable *invariant; /* The invariant alu-operand */<br>
+ nir_loop_variable *phi; /* The other alu-operand */<br>
+ nir_loop_variable *def_outside_loop; /* The phi-src outside the 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></blockquote><div><br></div><div>Why are these two separate things? You have two arrays both indexed by nir_ssa_def::index and both called loop_vars and one has a pointer to the other. Can we just make 1 array?<br></div></div></div></div></blockquote><div><br></div><div>No really sure. There is other stuff I've removed from this pass Thomas may have had other ideas for this or just never cleaned it up. Will see if I can combine them.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+<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></blockquote><div><br></div><div>Also, this seems like an unneeded level of indirection. Just put a pointer in loop_variable that points to the induction var struct.<br></div></div></div></div></blockquote><div><br></div><div>Sure.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+} 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->inde<wbr>x]);<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 the<br>
+ * defs in the loop instead of the end when walking the list. This 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_co<wbr>nditional ||<br>
+ loop_init_state->mark_nested)<wbr>)<br>
+ LIST_ADDTAIL(&(var->process_li<wbr>nk),<br>
+ &(loop_init_state->state->pro<wbr>cess_list));<br></blockquote><div><br></div><div>I have a mild preference for the lower-case variants. As I recall, they are more for legacy.<br></div></div></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+<br>
+ if (loop_init_state->mark_in_cond<wbr>itional)<br>
+ var->nir_loop_var->in_conditio<wbr>nal_block = true;<br>
+<br>
+ if (loop_init_state->mark_nested)<br>
+ var->nir_loop_var->in_nested_l<wbr>oop = 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></blockquote><div><br></div><div>Can we make this function take the actual parameters and limit init_loop_state to being the thing we use for init_loop_def. Passing piles of pointers through made sense when Thomas originally wrote it but now that we have better loop iteration macros, we can just pass arguments.<br></div></div></div></div></blockquote><div><br></div><div>Sure.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+{<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->paren<wbr>t_instr->type == 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->paren<wbr>t_instr->type == 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_i<wbr>nstr);<br>
+<br>
+ for (unsigned i = 0; i < nir_op_infos[alu->op].num_inpu<wbr>ts; i++) {<br>
+ if (!is_ssa_def_invariant(alu->sr<wbr>c[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 the<br>
+ * other is the phi itself. These should be removed by 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_informatio<wbr>n(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_conditi<wbr>onal_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->nir<wbr>_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(lo<wbr>op_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 variables<br>
+ * have a phi node<br>
+ */<br>
+ if (!is_var_phi(var))<br>
+ return false;<br>
+<br>
+ nir_phi_instr *phi = nir_instr_as_phi(var->nir_loop<wbr>_var->def->parent_instr);<br>
+<br>
+ nir_basic_induction_var *biv = rzalloc(state, 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 panic.<br>
+ */<br>
+ if (src_var->nir_loop_var->in_con<wbr>ditional_block ||<br>
+ src_var->nir_loop_var->in_nest<wbr>ed_loop)<br>
+ break;<br>
+<br>
+ if (!src_var->in_loop)<br>
+ biv->def_outside_loop = src_var->nir_loop_var;<br></blockquote><div><br></div><div>Could we assert biv->def_outside_loop == NULL<br></div><div> </div><blockquote type="cite">
+<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 nir_op_uadd_carry:<br>
+ case nir_op_fsub: case nir_op_isub: case 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></blockquote><div><br></div><div>Is there a reason why this is an explicit list and not simply "nir_op_infos[alu->op].num_inputs == 2"?<br></div></div></div></div></blockquote><div><br></div><div>No sure. This is just they way Thomas did it. If that makes sense also I'll make the change.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+<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>
+ if (is_ssa_def_invariant(alu->src<wbr>[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].s<wbr>rc.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 && 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, 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>
+ ralloc_free(biv);<br>
+ return false;<br>
+}<br>
+<br>
+static bool<br>
+compute_induction_information<wbr>(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 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_conditi<wbr>onal_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(va<wbr>r, state)) {<br>
+ /* If a phi is marked basic_ind we also mark the alu_def basic_ind<br>
+ * at the same time. It will then be detected as basic_ind 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_de<wbr>f *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_c<wbr>f_node *node, void *state)<br></blockquote><div><br></div><div>There's no good reason for this to be a void *; it just leads to extra casting. Also, this function could use a better name. Maybe cf_node_find_loop_jumps?<br></div><div> </div><blockquote type="cite">
+{<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></blockquote><div><br></div><div>Jumps are always the last instruction in a block. There's no sense in walking all of them just to find the jump. Use nir_block_last_instr() instead.<br></div><div> </div><blockquote type="cite">
+ if (instr->type == nir_instr_type_jump) {<br>
+ if (nir_instr_as_jump(instr)->typ<wbr>e == nir_jump_break) {<br>
+ ((loop_jumps *) state)->contains_break = true;<br>
+ } else if (nir_instr_as_jump(instr)->typ<wbr>e == 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_cf<wbr>_node, node, node, &if_stmt->then_list)<br>
+ if (!foreach_cf_node_ex_loop(node<wbr>, state))<br>
+ return false;<br>
+<br>
+ foreach_list_typed_safe(nir_cf<wbr>_node, node, node, &if_stmt->else_list)<br>
+ if (!foreach_cf_node_ex_loop(node<wbr>, state))<br>
+ return false;<br>
+<br>
+ break;<br>
+ }<br></blockquote><div><br></div><div>Maybe add an explicit case for nir_cf_node_loop with a comment that we don't care about inner loops.<br></div><div> </div><blockquote type="cite">
+<br>
+ default:<br>
+ break;<br>
+ }<br>
+<br>
+ return false;<br>
+}<br>
+<br>
+static bool<br>
+is_trivial_loop_terminator(ni<wbr>r_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_c<wbr>f_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></blockquote><div><br></div><div>How about "if (!exec_list_empty(&nif->else_l<wbr>ist)) return false;"? That seems far simpler.<br></div><div> </div><blockquote type="cite">
+<br>
+ nir_cf_node *first_then = nir_if_first_then_node(nif);<br>
+ nir_block *first_then_block = nir_cf_node_as_block(first_the<wbr>n);<br>
+ nir_instr *first_instr = nir_block_first_instr(first_th<wbr>en_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_in<wbr>fo_state *state)<br>
+{<br>
+ bool success = false;<br>
+ foreach_list_typed_safe(nir_c<wbr>f_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 can<br>
+ * not find a loop terminator, but there is a break-statement then<br>
+ * we should return false so that we do not try to find trip-count<br>
+ */<br>
+ if (!is_trivial_loop_terminator(n<wbr>if))<br>
+ return false;<br>
+<br>
+ if (nif->condition.ssa->parent_in<wbr>str->type == 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_te<wbr>rminator_link,<br>
+ &state->info->loop_terminator_<wbr>list);<br>
+<br>
+ terminator->nif = nif;<br>
+ terminator->conditional_instr = nif->condition.ssa->parent_ins<wbr>tr;<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_va<wbr>r(loop_variable *var, loop_info_state *state)<br>
+{<br>
+ assert(var->nir_loop_var->typ<wbr>e == basic_induction);<br>
+<br>
+ struct hash_entry *entry =<br>
+ _mesa_hash_table_search(state-<wbr>>var_to_basic_ind, 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 *step,<br>
+ nir_const_value *limit, nir_alu_instr *alu, int32_t *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 <= limit->u32[0];<br>
+ break;<br>
+ case nir_op_ult:<br>
+ valid_loop = limit_rhs ? uadd < limit->u32[0] : uadd > 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 <= limit->i32[0];<br>
+ break;<br>
+ case nir_op_ilt:<br>
+ valid_loop = limit_rhs ? iadd < limit->i32[0] : iadd > 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_cons<wbr>t_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></blockquote><div><br></div><div>You only allow iadd and isub except that we lower away isub so you'll never see it. Probably best to remove the dead code. Also...<br></div></div></div></div></blockquote><div><br></div><div>ok thats interesting, out of interest why do we do that? And in what pass? Also I think I should probably add support for other ops I believe I've seen shaders that use</div><div>either / or * possibly both.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div> 1) You don't handle swizzles at all<br></div></div></div></div></blockquote><div><br></div><div>I recall there being a TODO for this originally. Probably a dumb question but can you give a small shader example? I wasn't sure what I need to handle exactly. </div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> 2) You don't handle things other than 64 or 16-bit. Those are coming soon; they need to be supported.<br></div></div></div></div></blockquote><div><br></div><div>Do we need this for the first implementation?</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+ return -1;<br>
+<br>
+ /* do-while loops can increment the starting value before the 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 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></blockquote><div><br></div><div>Is there a reason why this can't be handled as "trip_count + 1"? This seems way overcomplicated.<br></div></div></div></div></blockquote><div><br></div><div>Yes there is. We don't know that we will increment by 1 it could be by 10, also if we support more opts we may have to do a mul etc.</div><div>We could set the initial value here but I decided to keep it all in get_iteration() I guess I could move this logic there also.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+ }<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 conditional is<br>
+ * unsigned with a huge iteration count so don't bother going any 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 exit<br>
+ * condition. This is needed to catch off-by-one errors and some types of<br>
+ * ill-formed loops. For example, we need to detect that the 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, 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></blockquote><div><br></div><div>I think it would be clearer if we combined these two functions.<br></div></div></div></div></blockquote><div><br></div><div>I started out trying to do that but ended up at this. I think my concern was that the initial value could be negative so we need to</div><div>handle that somehow. This seemed slightly nicer than doing "int32_t iadd = mul + initial_val;" in each struct case. </div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+ }<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 possible<br>
+ * trip-count. We need to check them all, and set the lowest trip-count 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_sta<wbr>te *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_instr<wbr>->type != nir_instr_type_alu) {<br>
+ /* If we get here the loop is likely not really a loop and will get<br>
+ * cleaned up elsewhere.<br>
+ */<br></blockquote><div><br></div><div>The if statement (and its contents) seem fine but the comment here seems sketchy.<br></div></div></div></div></blockquote><div><br></div><div>I think I was seeing things like this get cleaned up by the dead cf pass.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+ trip_count_known = false;<br>
+ continue;<br>
+ }<br>
+<br>
+ nir_alu_instr *alu = nir_instr_as_alu(terminator->c<wbr>onditional_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.s<wbr>sa, state);<br>
+ limit = get_loop_var(alu->src[1].src.s<wbr>sa, 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.s<wbr>sa, state);<br>
+ limit = get_loop_var(alu->src[0].src.s<wbr>sa, 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_cons<wbr>tant) {<br>
+ trip_count_known = false;<br>
+ continue;<br>
+ }<br>
+<br>
+ nir_basic_induction_var *ind =<br>
+ get_basic_ind_var_for_loop_va<wbr>r(basic_ind, state);<br>
+<br>
+ if (!ind->def_outside_loop->is_co<wbr>nstant ||<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->p<wbr>arent_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_i<wbr>nstr);<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->par<wbr>ent_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, 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 limiting<br>
+ * terminator) set the trip count and limiting terminator.<br>
+ */<br>
+ if (min_trip_count == -2 || iterations < min_trip_count) {<br></blockquote><div><br></div><div>Can we have a #define for -2 so that it has a name.<br></div></div></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote type="cite">
+ 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_kn<wbr>own = trip_count_known;<br>
+ if (min_trip_count > -2)<br>
+ state->info->trip_count = min_trip_count;<br>
+ state->info->limiting_termina<wbr>tor = limiting_terminator;<br>
+}<br>
+<br>
+/* Checks if we should force the loop to be unrolled regardless of size */<br>
+static bool<br>
+force_unroll(loop_info_state *state, nir_shader *ns, nir_deref_var *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 != 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></blockquote><div><br></div><div>Thinking a bit about analysis vs. lowering...<br><br></div><div>I wonder if it wouldn't be better to make the loop anlaysis pass be a bit more informational and less decision making. For instance, it could record what all variable modes it has seen be indexed by an induction variable and let the pass using the analysis decide whether or not to force-unroll. </div></div></div></div></blockquote><div><br></div><div>Seems reasonable.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> For that matter, it could just produce a hash map from induction variables to the loop for which they are an induction variable. </div></div></div></div></blockquote><div><br></div><div>I guess so but the above suggestion seems simpler.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> You could also just record the trip count per-loop. </div></div></div></div></blockquote><div><br></div><div>No sure what you mean as we do this already.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> That all seems a bit more like things an analysis pass would do than just setting a force_unroll bit.<br><br></div><div>Please don't feel like you need to make any changes in this direction yet. I'm mostly trying to open the discussion up a bit and feel out exactly how we want things to be structured.<br><br></div><div>I think I've written you a long enough book for now. I'll try to look at the others as I get time.<br></div></div></div></div></blockquote><div><br></div><div>Thanks for taking a look.</div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>--Jason<br></div><div> </div><blockquote type="cite">
+ }<br>
+ }<br>
+<br>
+ return false;<br>
+}<br>
+<br>
+static bool<br>
+count_instructions(loop_info_<wbr>state *state, nir_shader *ns, nir_block *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 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->in<wbr>trinsic].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 list<br>
+ * Mark the entries in conditionals or in nested loops accordingly<br>
+ */<br>
+ foreach_list_typed_safe(nir_c<wbr>f_node, node, node, &state->loop->body) {<br>
+ switch (node->type) {<br>
+<br>
+ case nir_cf_node_block:<br>
+ init_state.mark_in_conditiona<wbr>l = false;<br>
+ init_state.mark_nested = false;<br>
+ init_loop_block(nir_cf_node_a<wbr>s_block(node), &init_state);<br>
+ break;<br>
+<br>
+ case nir_cf_node_if:<br>
+ init_state.mark_in_conditiona<wbr>l = 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_conditiona<wbr>l = 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>
+ compute_invariance_informatio<wbr>n(state);<br>
+<br>
+ /* We may now have filled the process_list with instructions from inside<br>
+ * the nested blocks in the loop. Remove all instructions from the 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 list.<br>
+ * Don't include defs inn nested loops or in conditionals.<br>
+ */<br>
+ init_state.mark_in_conditiona<wbr>l = false;<br>
+ init_state.mark_nested = false;<br>
+<br>
+ foreach_list_typed_safe(nir_c<wbr>f_node, node, node, &state->loop->body)<br>
+ if (node->type == nir_cf_node_block)<br>
+ init_loop_block(nir_cf_node_a<wbr>s_block(node), &init_state);<br>
+<br>
+ /* We have invariance information so try to find induction variables */<br>
+ if (!compute_induction_informatio<wbr>n(state))<br>
+ return;<br>
+<br>
+ /* Try to find all simple terminators of the loop. If we can't find 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>
+ find_trip_count(state);<br>
+<br>
+ nir_shader *ns = impl->function->shader;<br>
+ foreach_list_typed_safe(nir_c<wbr>f_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(ni<wbr>r_loop *loop, void *mem_ctx, 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, 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->l<wbr>oop_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_node<wbr>, nested_node, node, &if_stmt->then_list)<br>
+ process_loops(nested_node, indirect_mask);<br>
+ foreach_list_typed(nir_cf_node<wbr>, nested_node, node, &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_node<wbr>, 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_no<wbr>de);<br>
+ void *mem_ctx = ralloc_context(NULL);<br>
+<br>
+ loop_info_state *state = initialize_loop_info_state(loo<wbr>p, mem_ctx, 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_fun<wbr>ction_impl *impl,<br>
+ nir_variable_mode indirect_mask)<br>
+{<br>
+ if (impl->function->shader->optio<wbr>ns->max_unroll_iterations == 0)<br>
+ return;<br>
+<br>
+ nir_index_ssa_defs(impl);<br>
+ foreach_list_typed(nir_cf_nod<wbr>e, node, node, &impl->body)<br>
+ process_loops(node, indirect_mask);<br>
+}<br>
diff --git a/src/compiler/nir/nir_metadat<wbr>a.c b/src/compiler/nir/nir_metadat<wbr>a.c<br>
index 9e1cff5..f71cf43 100644<br>
--- a/src/compiler/nir/nir_metadat<wbr>a.c<br>
+++ b/src/compiler/nir/nir_metadat<wbr>a.c<br>
@@ -31,7 +31,7 @@<br>
*/<br>
<br>
void<br>
-nir_metadata_require(nir_func<wbr>tion_impl *impl, nir_metadata required)<br>
+nir_metadata_require(nir_func<wbr>tion_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_funct<wbr>ion_impl *impl, nir_metadata required)<br>
nir_calc_dominance_impl(impl)<wbr>;<br>
if (NEEDS_UPDATE(nir_metadata_liv<wbr>e_ssa_defs))<br>
nir_live_ssa_defs_impl(impl);<br>
+ if (NEEDS_UPDATE(nir_metadata_loo<wbr>p_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>
<span><font color="#888888"><br>
--<br>
2.7.4<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">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>
</font></span><br></blockquote></div><br></div></div>
<pre>_______________________________________________
mesa-dev mailing list
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a>
</pre></blockquote></body></html>