[Mesa-dev] [PATCH 16/20] nir: simplify the loop analysis trip count code a little
Timothy Arceri
tarceri at itsqueeze.com
Fri Dec 7 03:08:16 UTC 2018
Here we create a helper is_supported_terminator_condition()
and use that rather than embedding all the trip count code
inside a switch.
The new helper will also be used in a following patch.
---
src/compiler/nir/nir_loop_analyze.c | 172 +++++++++++++++-------------
1 file changed, 93 insertions(+), 79 deletions(-)
diff --git a/src/compiler/nir/nir_loop_analyze.c b/src/compiler/nir/nir_loop_analyze.c
index be74105594..5446e7a120 100644
--- a/src/compiler/nir/nir_loop_analyze.c
+++ b/src/compiler/nir/nir_loop_analyze.c
@@ -665,6 +665,26 @@ calculate_iterations(nir_const_value *initial, nir_const_value *step,
return -1;
}
+static bool
+is_supported_terminator_condition(nir_alu_instr *alu)
+{
+ switch (alu->op) {
+ case nir_op_fge:
+ case nir_op_ige:
+ case nir_op_uge:
+ case nir_op_flt:
+ case nir_op_ilt:
+ case nir_op_ult:
+ case nir_op_feq:
+ case nir_op_ieq:
+ case nir_op_fne:
+ case nir_op_ine:
+ return true;
+ default:
+ return false;
+ }
+}
+
/* Run through each of the terminators of the loop and try to infer a possible
* trip-count. We need to check them all, and set the lowest trip-count as the
* trip-count of our loop. If one of the terminators has an undecidable
@@ -696,97 +716,91 @@ find_trip_count(loop_info_state *state)
nir_loop_variable *limit = NULL;
bool limit_rhs = true;
- switch (alu->op) {
- case nir_op_fge: case nir_op_ige: case nir_op_uge:
- case nir_op_flt: case nir_op_ilt: case nir_op_ult:
- case nir_op_feq: case nir_op_ieq:
- case nir_op_fne: case nir_op_ine:
-
- /* We assume that the limit is the "right" operand */
- basic_ind = get_loop_var(alu->src[0].src.ssa, state);
- limit = get_loop_var(alu->src[1].src.ssa, state);
-
- if (basic_ind->type != basic_induction) {
- /* We had it the wrong way, flip things around */
- basic_ind = get_loop_var(alu->src[1].src.ssa, state);
- limit = get_loop_var(alu->src[0].src.ssa, state);
- limit_rhs = false;
- terminator->induction_rhs = true;
- }
+ if (!is_supported_terminator_condition(alu)) {
+ trip_count_known = false;
+ continue;
+ }
- /* The comparison has to have a basic induction variable for us to be
- * able to find trip counts.
- */
- if (basic_ind->type != basic_induction) {
- trip_count_known = false;
- continue;
- }
+ /* We assume that the limit is the "right" operand */
+ basic_ind = get_loop_var(alu->src[0].src.ssa, state);
+ limit = get_loop_var(alu->src[1].src.ssa, state);
- /* Attempt to find a constant limit for the loop */
- nir_const_value limit_val;
- if (is_var_constant(limit)) {
- limit_val =
- nir_instr_as_load_const(limit->def->parent_instr)->value;
- } else {
- trip_count_known = false;
-
- if (!try_find_limit_of_alu(limit, &limit_val, terminator, state)) {
- /* Guess loop limit based on array access */
- if (!guess_loop_limit(state, &limit_val, basic_ind)) {
- continue;
- }
+ if (basic_ind->type != basic_induction) {
+ /* We had it the wrong way, flip things around */
+ basic_ind = get_loop_var(alu->src[1].src.ssa, state);
+ limit = get_loop_var(alu->src[0].src.ssa, state);
+ limit_rhs = false;
+ terminator->induction_rhs = true;
+ }
- guessed_trip_count = true;
- }
- }
+ /* The comparison has to have a basic induction variable for us to be
+ * able to find trip counts.
+ */
+ if (basic_ind->type != basic_induction) {
+ trip_count_known = false;
+ continue;
+ }
- /* We have determined that we have the following constants:
- * (With the typical int i = 0; i < x; i++; as an example)
- * - Upper limit.
- * - Starting value
- * - Step / iteration size
- * Thats all thats needed to calculate the trip-count
- */
+ /* Attempt to find a constant limit for the loop */
+ nir_const_value limit_val;
+ if (is_var_constant(limit)) {
+ limit_val =
+ nir_instr_as_load_const(limit->def->parent_instr)->value;
+ } else {
+ trip_count_known = false;
- nir_const_value initial_val =
- nir_instr_as_load_const(basic_ind->ind->def_outside_loop->
- def->parent_instr)->value;
+ if (!try_find_limit_of_alu(limit, &limit_val, terminator, state)) {
+ /* Guess loop limit based on array access */
+ if (!guess_loop_limit(state, &limit_val, basic_ind)) {
+ continue;
+ }
- nir_const_value step_val =
- nir_instr_as_load_const(basic_ind->ind->invariant->def->
- parent_instr)->value;
+ guessed_trip_count = true;
+ }
+ }
- int iterations = calculate_iterations(&initial_val, &step_val,
- &limit_val,
- basic_ind->ind->alu_def, alu,
- limit_rhs,
- terminator->continue_from_then);
+ /* We have determined that we have the following constants:
+ * (With the typical int i = 0; i < x; i++; as an example)
+ * - Upper limit.
+ * - Starting value
+ * - Step / iteration size
+ * Thats all thats needed to calculate the trip-count
+ */
- /* Where we not able to calculate the iteration count */
- if (iterations == -1) {
- trip_count_known = false;
- guessed_trip_count = false;
- continue;
- }
+ nir_const_value initial_val =
+ nir_instr_as_load_const(basic_ind->ind->def_outside_loop->
+ def->parent_instr)->value;
- if (guessed_trip_count) {
- guessed_trip_count = false;
- state->loop->info->guessed_trip_count = iterations;
- continue;
- }
+ nir_const_value step_val =
+ nir_instr_as_load_const(basic_ind->ind->invariant->def->
+ parent_instr)->value;
- /* If this is the first run or we have found a smaller amount of
- * iterations than previously (we have identified a more limiting
- * terminator) set the trip count and limiting terminator.
- */
- if (max_trip_count == -1 || iterations < max_trip_count) {
- max_trip_count = iterations;
- limiting_terminator = terminator;
- }
- break;
+ int iterations = calculate_iterations(&initial_val, &step_val,
+ &limit_val,
+ basic_ind->ind->alu_def, alu,
+ limit_rhs,
+ terminator->continue_from_then);
- default:
+ /* Where we not able to calculate the iteration count */
+ if (iterations == -1) {
trip_count_known = false;
+ guessed_trip_count = false;
+ continue;
+ }
+
+ if (guessed_trip_count) {
+ guessed_trip_count = false;
+ state->loop->info->guessed_trip_count = iterations;
+ continue;
+ }
+
+ /* If this is the first run or we have found a smaller amount of
+ * iterations than previously (we have identified a more limiting
+ * terminator) set the trip count and limiting terminator.
+ */
+ if (max_trip_count == -1 || iterations < max_trip_count) {
+ max_trip_count = iterations;
+ limiting_terminator = terminator;
}
}
--
2.19.2
More information about the mesa-dev
mailing list