<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">I made a bunch more comments but they're all cosmetic. The one non-cosmetic thing I'd like to see changed before we merge is that we fix the case where the break is in the else. Feel free to grab the tip of my jenkins_vulkan branch and squash it in if you like the approach I took. Or you can do something slightly different so long as it has the same effect.<br><br></div><div class="gmail_quote">Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">--Jason<br></div><div class="gmail_quote"><br>On Sun, Dec 18, 2016 at 9:47 PM, Timothy Arceri <span dir="ltr"><<a href="mailto:timothy.arceri@collabora.com" target="_blank">timothy.arceri@collabora.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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: (Timothy Arceri)<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>
V5: (Timothy Arceri)<br>
- fix metadata mask value 0x10 vs 0x16<br>
<br>
V6: (Timothy Arceri)<br>
- merge loop_variable and nir_loop_variable structs and lists suggested by Jason<br>
- remove induction var hash table and store pointer to induction information in<br>
the loop_variable suggested by Jason.<br>
- use lowercase list_addtail() suggested by Jason.<br>
- tidy up init_loop_block() as per Jasons suggestions.<br>
- replace switch with nir_op_infos[alu->op].num_inpu<wbr>ts == 2 in<br>
is_var_basic_induction_var() as suggested by Jason.<br>
- use nir_block_last_instr() in and rename foreach_cf_node_ex_loop() as suggested<br>
by Jason.<br>
- fix else check for is_trivial_loop_terminator() as per Connors suggetions.<br>
- simplify offset for induction valiables incremented before the exit conditions is<br>
checked.<br>
- replace nir_op_isub check with assert() as it should have been lowered away.<br>
<br>
V7: (Timothy Arceri)<br>
- use rzalloc() on nir_loop struct creation. Worked previously because ralloc()<br>
was broken and always zeroed the struct.<br>
- fix cf_node_find_loop_jumps() to find jumps when loops contain<br>
nested if statements. Code is tidier as a result.<br>
<br>
V8: (Timothy Arceri)<br>
- move is_trivial_loop_terminator() to nir.h so we can use it to assert is<br>
the loop unroll pass<br>
- fix analysis to not bail when looking for terminator when the break is in the else<br>
rather then the if<br>
- added new loop terminator fields: break_block, continue_from_block and<br>
continue_from_then so we don't have to gather these when doing unrolling.<br>
- get correct array length when forcing unrolling of variables<br>
indexed arrays that are the same size as the iteration count<br>
- add support for induction variables of type float<br>
- update trival loop terminator check to allow an if containing<br>
instructions as long as both branches contain only a single<br>
block.<br>
<br>
V9:<br>
- bunch of tidy ups and simplifications suggested by Jason.<br>
- rewrote trivial terminator detection, now the only restriction is there<br>
must be no nested jumps, anything else goes.<br>
- rewrote the iteration test to use nir_eval_const_opcode().<br>
- count instruction properly even when forcing an unroll.<br>
- bunch of other tidy ups and simplifications.<br>
---<br>
src/compiler/Makefile.<wbr>sources | 2 +<br>
src/compiler/nir/nir.c | 2 +-<br>
src/compiler/nir/nir.h | 41 +-<br>
src/compiler/nir/nir_loop_ana<wbr>lyze.c | 852 ++++++++++++++++++++++++++++++<wbr>++++++<br>
src/compiler/nir/nir_loop_ana<wbr>lyze.h | 92 ++++<br>
src/compiler/nir/nir_<wbr>metadata.c | 8 +-<br>
6 files changed, 994 insertions(+), 3 deletions(-)<br>
create mode 100644 src/compiler/nir/nir_loop_anal<wbr>yze.c<br>
create mode 100644 src/compiler/nir/nir_loop_anal<wbr>yze.h<br>
<br>
diff --git a/src/compiler/Makefile.source<wbr>s b/src/compiler/Makefile.source<wbr>s<br>
index 17b15de..ca8a056 100644<br>
--- a/src/compiler/Makefile.source<wbr>s<br>
+++ b/src/compiler/Makefile.source<wbr>s<br>
@@ -193,6 +193,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.c b/src/compiler/nir/nir.c<br>
index 2d882f7..2c3531c 100644<br>
--- a/src/compiler/nir/nir.c<br>
+++ b/src/compiler/nir/nir.c<br>
@@ -393,7 +393,7 @@ nir_if_create(nir_shader *shader)<br>
nir_loop *<br>
nir_loop_create(nir_shader *shader)<br>
{<br>
- nir_loop *loop = ralloc(shader, nir_loop);<br>
+ nir_loop *loop = rzalloc(shader, nir_loop);<br>
<br>
cf_init(&loop->cf_node, nir_cf_node_loop);<br>
<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index ba88a24..28010aa 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -1506,9 +1506,41 @@ typedef struct nir_if {<br>
} nir_if;<br>
<br>
typedef struct {<br>
+ nir_if *nif;<br>
+<br>
+ nir_instr *conditional_instr;<br>
+<br>
+ nir_block *break_block;<br>
+ nir_block *continue_from_block;<br>
+<br>
+ bool continue_from_then;<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>
+ /* Unroll the loop regardless of its size */<br>
+ bool force_unroll;<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>
/**<br>
@@ -1521,6 +1553,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 = 0x10,<br>
} nir_metadata;<br>
<br>
typedef struct {<br>
@@ -1749,6 +1782,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 {<br>
@@ -1859,7 +1894,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>
@@ -2479,6 +2514,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..5a7cac8<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_loop_an<wbr>alyze.c<br>
@@ -0,0 +1,852 @@<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>
+#include "nir_constant_expressions.h"<br>
+#include "nir_loop_analyze.h"<br>
+<br>
+typedef enum {<br>
+ undefined,<br>
+ invariant,<br>
+ not_invariant,<br>
+ basic_induction<br>
+} nir_loop_variable_type;<br>
+<br>
+struct 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>
+ /* 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 this is of type basic_induction */<br>
+ struct nir_basic_induction_var *ind;<br>
+<br>
+ /* True if variable is in an if branch or a nested loop */<br>
+ bool in_control_flow;<br>
+<br>
+} nir_loop_variable;<br>
+<br>
+typedef struct nir_basic_induction_var {<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 *def_outside_loop; /* The phi-src outside the loop */<br>
+} nir_basic_induction_var;<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>
+ nir_loop_variable *loop_vars;<br>
+<br>
+ /* A list of the loop_vars to analyze */<br>
+ struct list_head process_list;<br>
+<br>
+ nir_variable_mode indirect_mask;<br>
+<br>
+} loop_info_state;<br>
+<br>
+static nir_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>
+typedef struct {<br>
+ loop_info_state *state;<br>
+ bool mark_in_control_flow;<br></blockquote><div><br></div><div>Mind renaming this to just in_control_flow. We're using it for more than just marking.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+} 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>
+ nir_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_con<wbr>trol_flow) {<br>
+ list_addtail(&(var->process_li<wbr>nk),<br>
+ &(loop_init_state->state->pro<wbr>cess_list));<br></blockquote><div><br></div><div>We don't need the extra parens.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<br>
+<br>
+ if (loop_init_state->mark_in_cont<wbr>rol_flow)<br>
+ var->in_control_flow = true;<br></blockquote><div><br></div><div>if (in_control_flow) {<br></div><div> var->in_control_flow = true;<br></div><div>} else {<br></div><div> list_addtail(...)<br></div><div>}<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ var->in_loop = true;<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static bool<br>
+init_loop_block(nir_block *block, loop_info_state *state,<br>
+ bool in_control_flow)<br>
+{<br>
+ init_loop_state init_state = {.mark_in_control_flow = in_control_flow,<br>
+ .state = state };<br>
+<br>
+ nir_foreach_instr(instr, block) {<br>
+ if (instr->type == nir_instr_type_intrinsic ||<br>
+ instr->type == nir_instr_type_alu ||<br>
+ instr->type == nir_instr_type_tex) {<br>
+ state->loop->info->num_instru<wbr>ctions++;<br>
+ }<br>
+<br>
+ nir_foreach_ssa_def(instr, init_loop_def, &init_state);<br>
+ }<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static inline bool<br>
+is_var_alu(nir_loop_variable *var)<br>
+{<br>
+ return var->def->parent_instr->type == nir_instr_type_alu;<br>
+}<br>
+<br>
+static inline bool<br>
+is_var_constant(nir_loop_vari<wbr>able *var)<br>
+{<br>
+ return var->def->parent_instr->type == nir_instr_type_load_const;<br>
+}<br>
+<br>
+static inline bool<br>
+is_var_phi(nir_loop_variable *var)<br>
+{<br>
+ return var->def->parent_instr->type == nir_instr_type_phi;<br>
+}<br>
+<br>
+static inline bool<br>
+mark_invariant(nir_ssa_def *def, loop_info_state *state)<br>
+{<br>
+ nir_loop_variable *var = get_loop_var(def, state);<br>
+<br>
+ if (var->type == invariant)<br>
+ return true;<br>
+<br>
+ if (!var->in_loop) {<br>
+ var->type = invariant;<br>
+ return true;<br>
+ }<br>
+<br>
+ if (var->type == not_invariant)<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 (!mark_invariant(alu->src[i].s<wbr>rc.ssa, state))<br>
+ return false;<br></blockquote><div><br></div><div>Should you set var->type = not_invariant here too?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<br>
+ 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->type = not_invariant;<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>
+ list_for_each_entry_safe(nir_<wbr>loop_variable, var, &state->process_list,<br>
+ process_link) {<br>
+ if (var->in_control_flow) {<br>
+ list_del(&var->process_link);<br>
+ continue;<br>
+ }<br></blockquote><div><br></div><div>I don't think this is needed. Above, we only add it to process_list if it's *not* in control flow.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ if (mark_invariant(var->def, state))<br>
+ list_del(&var->process_link);<br>
+ }<br>
+}<br>
+<br>
+static bool<br>
+compute_induction_information<wbr>(loop_info_state *state)<br>
+{<br>
+ bool found_induction_var = false;<br>
+ list_for_each_entry_safe(nir_<wbr>loop_variable, var, &state->process_list,<br>
+ process_link) {<br>
+<br>
+ /* It can't be an induction variable if it is invariant. Invariants and<br>
+ * things in nested loops or conditionals should have been removed from<br>
+ * the list by compute_invariance_information<wbr>().<br>
+ */<br>
+ assert(!var->in_control_flow && var->type != invariant);<br></blockquote><div><br></div><div>Extra space after &&<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<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>
+ continue;<br>
+<br>
+ nir_phi_instr *phi = nir_instr_as_phi(var->def->par<wbr>ent_instr);<br>
+ nir_basic_induction_var *biv = rzalloc(state, nir_basic_induction_var);<br>
+<br>
+ nir_foreach_phi_src(src, phi) {<br>
+ nir_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->in_control_flow)<br>
+ break;<br>
+<br>
+ if (!src_var->in_loop)<br>
+ biv->def_outside_loop = src_var;<br>
+<br>
+ if (src_var->in_loop && is_var_alu(src_var)) {<br></blockquote><div><br></div><div>Maybe just "else if is_var_alu(src_var)"?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ nir_alu_instr *alu = nir_instr_as_alu(src_var->def-<wbr>>parent_instr);<br>
+<br>
+ if (nir_op_infos[alu->op].num_inp<wbr>uts == 2) {<br>
+ biv->alu_def = src_var;<br>
+ biv->alu_op = alu->op;<br>
+<br>
+ for (unsigned i = 0; i < 2; i++) {<br>
+ /* Is one of the operands const, and the other the phi */<br>
+ if (alu->src[i].src.ssa->parent_i<wbr>nstr->type == nir_instr_type_load_const &&<br>
+ alu->src[1-i].src.ssa == &phi->dest.ssa)<br>
+ biv->invariant = get_loop_var(alu->src[i].src.s<wbr>sa, state);<br>
+ }<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ if (biv->alu_def && biv->def_outside_loop && biv->invariant &&<br>
+ is_var_constant(biv->def_outsi<wbr>de_loop)) {<br>
+ assert(is_var_constant(biv->i<wbr>nvariant));<br>
+ biv->alu_def->type = basic_induction;<br>
+ biv->alu_def->ind = biv;<br>
+ var->type = basic_induction;<br>
+ var->ind = biv;<br>
+<br>
+ found_induction_var = true;<br>
+ } else {<br>
+ ralloc_free(biv);<br>
+ }<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>
+ nir_loop_variable *var = get_loop_var(def, state);<br>
+<br>
+ var->in_loop = false;<br>
+ var->def = def;<br>
+<br>
+ if (def->parent_instr->type == nir_instr_type_load_const) {<br>
+ var->type = invariant;<br>
+ } else {<br>
+ var->type = undefined;<br>
+ }<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static inline bool<br>
+ends_in_break(nir_block *block)<br>
+{<br>
+ if (exec_list_is_empty(&block->in<wbr>str_list))<br>
+ return false;<br>
+<br>
+ nir_instr *instr = nir_block_last_instr(block);<br>
+ return instr->type == nir_instr_type_jump &&<br>
+ nir_instr_as_jump(instr)->type == nir_jump_break;<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>
+ nir_block *break_blk = NULL;<br>
+ nir_block *continue_from_blk = NULL;<br>
+ bool continue_from_then = true;<br>
+<br>
+ nir_block *last_then = nir_if_last_then_block(nif);<br>
+ nir_block *last_else = nir_if_last_else_block(nif);<br>
+ if (ends_in_break(last_then)) {<br>
+ break_blk = last_then;<br>
+ continue_from_blk = last_else;<br>
+ continue_from_then = false;<br>
+ } else if (ends_in_break(last_else)) {<br>
+ break_blk = last_else;<br>
+ continue_from_blk = last_then;<br></blockquote><div><br></div><div>This is tabbed over one too far.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<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 (!nir_is_trivial_loop_if(nif, break_blk))<br>
+ return false;<br>
+<br>
+ /* Continue if the if contained no jumps at all */<br>
+ if (!break_blk)<br>
+ continue;<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->loop->info, nir_loop_terminator);<br>
+<br>
+ list_add(&terminator->loop_te<wbr>rminator_link,<br>
+ &state->loop->info->loop_termi<wbr>nator_list);<br>
+<br>
+ terminator->nif = nif;<br>
+ terminator->break_block = break_blk;<br>
+ terminator->continue_from_blo<wbr>ck = continue_from_blk;<br>
+ terminator->continue_from_<wbr>then = continue_from_then;<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 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)<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>
+ int32_t span = limit->i32[0] - initial_val;<br>
+ iter = span / step->i32[0];<br>
+ break;<br>
+ }<br>
+ case nir_op_uge:<br>
+ case nir_op_ult: {<br>
+ uint32_t initial_val = initial->u32[0];<br>
+ uint32_t span = limit->u32[0] - initial_val;<br>
+ iter = span / step->u32[0];<br>
+ break;<br>
+ }<br>
+ case nir_op_fge:<br>
+ case nir_op_flt:<br>
+ case nir_op_feq:<br>
+ case nir_op_fne: {<br>
+ int32_t initial_val = initial->f32[0];<br>
+ int32_t span = limit->f32[0] - initial_val;<br>
+ iter = span / step->f32[0];<br>
+ break;<br>
+ }<br>
+ default:<br>
+ return -1;<br>
+ }<br>
+<br>
+ return iter;<br>
+}<br>
+<br>
+static bool<br>
+test_iterations(int32_t iter_int, nir_const_value *step,<br>
+ nir_const_value *limit, nir_op cond_op, unsigned bit_size,<br>
+ nir_alu_type induction_base_type,<br>
+ nir_const_value *initial, bool limit_rhs)<br>
+{<br>
+ assert(nir_op_infos[cond_op].<wbr>num_inputs == 2);<br>
+<br>
+ nir_const_value iter_src = { {0, } };<br>
+ nir_op mul_op;<br>
+ nir_op add_op;<br>
+ switch (induction_base_type) {<br>
+ case nir_type_float:<br>
+ iter_src.f32[0] = (float) iter_int;<br>
+ mul_op = nir_op_fmul;<br>
+ add_op = nir_op_fadd;<br>
+ break;<br>
+ case nir_type_int:<br>
+ case nir_type_uint:<br>
+ iter_src.i32[0] = iter_int;<br>
+ mul_op = nir_op_imul;<br>
+ add_op = nir_op_iadd;<br>
+ break;<br>
+ default:<br>
+ unreachable("Unhandled induction variable base type!");<br>
+ }<br>
+<br>
+ /* Multiple the iteration count we are testing by the number of times we<br>
+ * step the induction variable each iteration.<br>
+ */<br>
+ nir_const_value mul_src[2] = { { {0, } }, { {0, } } };<br></blockquote><div><br></div><div>How about just<br><br></div><div>nir_const_value mul_src[2] = {<br></div><div> iter_src,<br></div><div> *step,<br>};<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ mul_src[0] = iter_src;<br>
+ mul_src[1] = *step;<br>
+ nir_const_value mul_result =<br>
+ nir_eval_const_opcode(mul_op, 1, bit_size, mul_src);<br>
+<br>
+ /* Add the initial value to the accumulated induction variable total */<br>
+ nir_const_value add_src[2] = { { {0, } }, { {0, } } };<br>
+ add_src[0] = mul_result;<br>
+ add_src[1] = *initial;<br></blockquote><div><br></div><div>Same here<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ nir_const_value add_result =<br>
+ nir_eval_const_opcode(add_op, 1, bit_size, add_src);<br>
+<br>
+ nir_const_value src[2] = { { {0, } }, { {0, } } };<br>
+ src[limit_rhs ? 0 : 1] = add_result;<br>
+ src[limit_rhs ? 1 : 0] = *limit;<br>
+<br>
+ /* Evaluate the loop exit condition */<br>
+ nir_const_value result = nir_eval_const_opcode(cond_op, 1, bit_size, src);<br>
+<br>
+ return result.u32[0] != 0;<br>
+}<br>
+<br>
+static int<br>
+calculate_iterations(nir_cons<wbr>t_value *initial, nir_const_value *step,<br>
+ nir_const_value *limit, nir_loop_variable *alu_def,<br>
+ nir_alu_instr *cond_alu, 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>
+ /* nir_op_isub should have been lowered away by this point */<br>
+ assert(alu->op != nir_op_isub);<br>
+<br>
+ /* Make sure the alu type for our induction variable is compatible with the<br>
+ * conditional alus input type. If its not something has gone really wrong.<br>
+ */<br>
+ nir_alu_type induction_base_type =<br>
+ nir_alu_type_get_base_type(nir<wbr>_op_infos[alu->op].output_type<wbr>);<br>
+ if (induction_base_type == nir_type_int || induction_base_type == nir_type_uint) {<br>
+ assert(nir_alu_type_get_base_t<wbr>ype(nir_op_infos[cond_alu->op]<wbr>.input_types[1]) == nir_type_int ||<br>
+ nir_alu_type_get_base_type(ni<wbr>r_op_infos[cond_alu->op].input<wbr>_types[1]) == nir_type_uint);<br>
+ } else {<br>
+ assert(nir_alu_type_get_base_t<wbr>ype(nir_op_infos[cond_alu->op]<wbr>.input_types[0]) ==<br>
+ induction_base_type);<br>
+ }<br>
+<br>
+ /* Check for nsupported alu operations */<br>
+ if (alu->op != nir_op_iadd && alu->op != nir_op_fadd)<br>
+ 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>
+ unsigned trip_offset = 0;<br>
+ if (cond_alu->src[0].src.ssa == alu_def->def ||<br>
+ cond_alu->src[1].src.ssa == alu_def->def) {<br>
+ trip_offset = 1;<br>
+ }<br>
+<br>
+ int iter_int = get_iteration(cond_alu->op, initial, step, limit, alu);<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>
+ unsigned bit_size_src0 = nir_src_bit_size(alu->src[0].s<wbr>rc);<br>
+ unsigned bit_size_src1 = nir_src_bit_size(alu->src[1].s<wbr>rc);<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>
+ unsigned bit_size =<br>
+ bit_size_src0 > bit_size_src1 ? bit_size_src0 : bit_size_src1;<br></blockquote><div><br></div><div>You can just assert bit_size_src1 == bit_size_src0. NIR does no autoconversion of bit sizes.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ for (int bias = -1; bias <= 1; bias++) {<br>
+ const int iter_bias = iter_int + bias;<br>
+<br>
+ if (test_iterations(iter_bias, step, limit, cond_alu->op, bit_size,<br>
+ induction_base_type, initial, limit_rhs)) {<br>
+ return iter_bias > 0 ? iter_bias - trip_offset : iter_bias;<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 = -1;<br>
+<br>
+ list_for_each_entry(nir_loop_<wbr>terminator, terminator,<br>
+ &state->loop->info->loop_term<wbr>inator_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 dead and will get cleaned up by the<br>
+ * nir_opt_dead_cf pass.<br>
+ */<br>
+ trip_count_known = false;<br>
+ continue;<br>
+ }<br>
+<br>
+ nir_alu_instr *alu = nir_instr_as_alu(terminator->c<wbr>onditional_instr);<br>
+ nir_loop_variable *basic_ind = NULL;<br>
+ nir_loop_variable *limit = NULL;<br>
+ bool limit_rhs = true;<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>
+<br>
+ if (basic_ind->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->type != basic_induction || !is_var_constant(limit)) {<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_const_value initial_val =<br>
+ nir_instr_as_load_const(basic_<wbr>ind->ind->def_outside_loop-><br>
+ def->parent_instr)->value;<br>
+<br>
+ nir_const_value step_val =<br>
+ nir_instr_as_load_const(basic_<wbr>ind->ind->invariant->def-><br>
+ parent_instr)->value;<br>
+<br>
+ nir_const_value limit_val =<br>
+ nir_instr_as_load_const(limit-<wbr>>def->parent_instr)->value;<br>
+<br>
+ int iterations = calculate_iterations(&initial_<wbr>val, &step_val,<br>
+ &limit_val,<br>
+ basic_ind->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 limiting<br>
+ * terminator) set the trip count and limiting terminator.<br>
+ */<br>
+ if (min_trip_count == -1 || iterations < min_trip_count) {<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->loop->info->is_trip_co<wbr>unt_known = trip_count_known;<br>
+ if (min_trip_count > -1)<br>
+ state->loop->info->trip_count = min_trip_count;<br>
+ state->loop->info->limiting_t<wbr>erminator = limiting_terminator;<br>
+}<br>
+<br>
+/* Checks if we should force the loop to be unrolled regardless of size<br>
+ * due to array access heuristics.<br>
+ */<br>
+static bool<br>
+force_unroll_array_access(loo<wbr>p_info_state *state, nir_shader *ns,<br>
+ 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_loop_var(deref_array->indi<wbr>rect.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(variable->der<wbr>ef.type) ==<br>
+ state->loop->info->trip_<wbr>count) {<br>
+ state->loop->info->force_unrol<wbr>l = true;<br>
+ return state->loop->info->force_unrol<wbr>l;<br>
+ }<br>
+<br>
+ if (variable->var->data.mode & state->indirect_mask) {<br>
+ state->loop->info->force_unrol<wbr>l = true;<br>
+ return state->loop->info->force_unrol<wbr>l;<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ return false;<br>
+}<br>
+<br>
+static bool<br>
+force_unroll_heuristics(loop_<wbr>info_state *state, nir_shader *ns,<br>
+ nir_block *block)<br>
+{<br>
+ nir_foreach_instr(instr, block) {<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_array_access(sta<wbr>te, 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>
+ /* 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_loop_block(nir_cf_node_a<wbr>s_block(node), state, false);<br>
+ break;<br>
+<br>
+ case nir_cf_node_if:<br>
+ nir_foreach_block_in_cf_node(<wbr>block, node)<br>
+ init_loop_block(block, state, true);<br>
+ break;<br>
+<br>
+ case nir_cf_node_loop:<br>
+ nir_foreach_block_in_cf_node(<wbr>block, node) {<br>
+ init_loop_block(block, state, true);<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 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>
+ list_for_each_entry_safe(nir_l<wbr>oop_terminator, terminator,<br>
+ &state->loop->info->loop_term<wbr>inator_list,<br>
+ loop_terminator_link) {<br>
+ list_del(&terminator->loop_te<wbr>rminator_link);<br>
+ ralloc_free(terminator);<br>
+ }<br>
+ return;<br>
+ }<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 (force_unroll_heuristics(state<wbr>, ns, nir_cf_node_as_block(node)))<br>
+ break;<br>
+ } else {<br>
+ nir_foreach_block_in_cf_node(<wbr>block, node) {<br>
+ if (force_unroll_heuristics(state<wbr>, 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,<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, nir_loop_variable,<br>
+ impl->ssa_alloc);<br>
+ state->loop = loop;<br>
+<br>
+ list_inithead(&state-><wbr>process_list);<br>
+<br>
+ if (loop->info)<br>
+ ralloc_free(loop->info);<br>
+<br>
+ loop->info = rzalloc(loop, nir_loop_info);<br>
+<br>
+ list_inithead(&loop->info->lo<wbr>op_terminator_list);<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>
+ 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>
+ 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_loop_an<wbr>alyze.h b/src/compiler/nir/nir_loop_an<wbr>alyze.h<br>
new file mode 100644<br>
index 0000000..b7f862e<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_loop_an<wbr>alyze.h<br>
@@ -0,0 +1,92 @@<br>
+/*<br>
+ * Copyright © 2016 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#pragma once<br>
+<br>
+#include "nir.h"<br>
+<br>
+/* Returns true if nir_cf_node contains a jump other than the expected_jump<br>
+ * parameter.<br>
+ */<br>
+static inline bool<br>
+contains_other_jump(nir_cf_no<wbr>de *node, nir_instr *expected_jump)<br>
+{<br>
+ switch (node->type) {<br>
+ case nir_cf_node_block: {<br>
+ nir_instr *lst_instr = nir_block_last_instr(nir_cf_no<wbr>de_as_block(node));<br>
+<br>
+ /* dead_cf should have eliminated any instruction after the first break<br>
+ */<br>
+ nir_foreach_instr(instr, nir_cf_node_as_block(node))<br>
+ assert(instr->type != nir_instr_type_jump || instr == lst_instr);<br>
+<br>
+ if (lst_instr && lst_instr->type == nir_instr_type_jump &&<br>
+ lst_instr != expected_jump)<br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ return true;<br>
+ else<br>
+ return false;<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 (contains_other_jump(node, expected_jump))<br>
+ return true;<br>
+ }<br>
+<br>
+ foreach_list_typed_safe(nir_cf<wbr>_node, node, node, &if_stmt->else_list) {<br>
+ if (contains_other_jump(node, expected_jump))<br>
+ return true;<br>
+ }<br>
+<br>
+ return false;<br>
+ }<br>
+ case nir_cf_node_loop:<br>
+ return true;<br></blockquote><div><br></div><div>I think one could make a decent argument for returning false here as well. Yes, a loop will almost certainly contain a jump of some form (it had better contain at least a break!) but that jump can't escape the loop so it doesn't reallly matter as far as is_trivial_loop_if is concerned. Then again, false is the safe option and this seems like something that's likely not tested. :-)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ default:<br>
+ unreachable("Unhandled cf node type");<br>
+ }<br>
+}<br>
+<br>
+/* Here we define a trivial if as containing only a single break that must be<br>
+ * located at the end of either the then or else branch of the top level if,<br>
+ * there must be no other breaks or any other type of jump. Or we pass NULL<br>
+ * to break_block the if must contains no jumps at all.<br>
+ */<br>
+static inline bool<br>
+nir_is_trivial_loop_if(nir_if *nif, nir_block *break_block)<br>
+{<br>
+ nir_instr *last_instr = NULL;<br>
+<br>
+ if (break_block) {<br>
+ last_instr = nir_block_last_instr(break_blo<wbr>ck);<br>
+ assert(last_instr && last_instr->type == nir_instr_type_jump &&<br>
+ nir_instr_as_jump(last_instr)<wbr>->type == nir_jump_break);<br>
+ }<br>
+<br>
+ if (contains_other_jump(&nif->cf_<wbr>node, last_instr))<br>
+ return false;<br>
+<br>
+ return true;<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 class="m_7038286457565317162HOEnZb"><font color="#888888"><br>
--<br>
2.9.3<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></blockquote></div><br></div></div>