[Mesa-dev] [PATCH 1/2] glsl: Add a way to skip passes when they're ineffective

Thomas Helland thomashelland90 at gmail.com
Sun Nov 13 01:29:09 UTC 2016


This is based on the assumption that some optimization passes
will snowball on each other, allowing for further optimizations,
while optimizations that does not give any benefit initially, likely
will not show any benefit at all and therefore can be skipped.
So instead of looping over all passes again and again, just
drop optimization passes from being rerun when they show no benefit.

These passes will still get rerun though, as the function
do_common_optimizations is run in a loop until there's no progress.
---
 src/compiler/glsl/glsl_parser_extras.cpp | 146 +++++++++++++++++--------------
 1 file changed, 81 insertions(+), 65 deletions(-)

diff --git a/src/compiler/glsl/glsl_parser_extras.cpp b/src/compiler/glsl/glsl_parser_extras.cpp
index 0cb3c12..a5a0837 100644
--- a/src/compiler/glsl/glsl_parser_extras.cpp
+++ b/src/compiler/glsl/glsl_parser_extras.cpp
@@ -2049,74 +2049,90 @@ do_common_optimization(exec_list *ir, bool linked,
    const bool debug = false;
    GLboolean progress = GL_FALSE;
 
-#define OPT(PASS, ...) do {                                             \
-      if (debug) {                                                      \
-         fprintf(stderr, "START GLSL optimization %s\n", #PASS);        \
-         const bool opt_progress = PASS(__VA_ARGS__);                   \
-         progress = opt_progress || progress;                           \
-         if (opt_progress)                                              \
-            _mesa_print_ir(stderr, ir, NULL);                           \
-         fprintf(stderr, "GLSL optimization %s: %s progress\n",         \
-                 #PASS, opt_progress ? "made" : "no");                  \
-      } else {                                                          \
-         progress = PASS(__VA_ARGS__) || progress;                      \
-      }                                                                 \
-   } while (false)
-
-   OPT(lower_instructions, ir, SUB_TO_ADD_NEG);
-
-   if (linked) {
-      OPT(do_function_inlining, ir);
-      OPT(do_dead_functions, ir);
-      OPT(do_structure_splitting, ir);
-   }
-   propagate_invariance(ir);
-   OPT(do_if_simplification, ir);
-   OPT(opt_flatten_nested_if_blocks, ir);
-   OPT(opt_conditional_discard, ir);
-   OPT(do_copy_propagation, ir);
-   OPT(do_copy_propagation_elements, ir);
-
-   if (options->OptimizeForAOS && !linked)
-      OPT(opt_flip_matrices, ir);
-
-   if (linked && options->OptimizeForAOS) {
-      OPT(do_vectorize, ir);
-   }
+   bool skip_opt_pass[31] = {false};
+   bool iteration_progress = false;
+
+   do {
+      iteration_progress = false;
+#define OPT(PASS, index, ...) do {                                         \
+         if (skip_opt_pass[index]) {                                       \
+            if (debug)                                                     \
+               fprintf(stderr, "Skipping optimization %d\n", index);       \
+            continue;                                                      \
+         }                                                                 \
+         if (debug) {                                                      \
+            fprintf(stderr, "START GLSL optimization %s\n", #PASS);        \
+            const bool opt_progress = PASS(__VA_ARGS__);                   \
+            skip_opt_pass[index] = !opt_progress;                          \
+            progress = opt_progress || progress;                           \
+            iteration_progress = iteration_progress || opt_progress;       \
+            if (opt_progress)                                              \
+               _mesa_print_ir(stderr, ir, NULL);                           \
+            fprintf(stderr, "GLSL optimization %s: %s progress\n",         \
+                    #PASS, opt_progress ? "made" : "no");                  \
+         } else {                                                          \
+            const bool opt_progress = PASS(__VA_ARGS__);                   \
+            skip_opt_pass[index] = !opt_progress;                          \
+            iteration_progress = iteration_progress || opt_progress;       \
+            progress = opt_progress || progress;                           \
+         }                                                                 \
+      } while (false)
+
+      OPT(lower_instructions, 0, ir, SUB_TO_ADD_NEG);
+
+      if (linked) {
+         OPT(do_function_inlining, 1, ir);
+         OPT(do_dead_functions, 2, ir);
+         OPT(do_structure_splitting, 3, ir);
+      }
+      propagate_invariance(ir);
+      OPT(do_if_simplification, 4, ir);
+      OPT(opt_flatten_nested_if_blocks, 5, ir);
+      OPT(opt_conditional_discard, 6, ir);
+      OPT(do_copy_propagation, 7, ir);
+      OPT(do_copy_propagation_elements, 8, ir);
+
+      if (options->OptimizeForAOS && !linked)
+         OPT(opt_flip_matrices, 9, ir);
+
+      if (linked && options->OptimizeForAOS) {
+         OPT(do_vectorize, 10, ir);
+      }
 
-   if (linked)
-      OPT(do_dead_code, ir, uniform_locations_assigned);
-   else
-      OPT(do_dead_code_unlinked, ir);
-   OPT(do_dead_code_local, ir);
-   OPT(do_tree_grafting, ir);
-   OPT(do_constant_propagation, ir);
-   if (linked)
-      OPT(do_constant_variable, ir);
-   else
-      OPT(do_constant_variable_unlinked, ir);
-   OPT(do_constant_folding, ir);
-   OPT(do_minmax_prune, ir);
-   OPT(do_rebalance_tree, ir);
-   OPT(do_algebraic, ir, native_integers, options);
-   OPT(do_lower_jumps, ir);
-   OPT(do_vec_index_to_swizzle, ir);
-   OPT(lower_vector_insert, ir, false);
-   OPT(do_swizzle_swizzle, ir);
-   OPT(do_noop_swizzle, ir);
-
-   OPT(optimize_split_arrays, ir, linked);
-   OPT(optimize_redundant_jumps, ir);
-
-   if (options->MaxUnrollIterations) {
-      loop_state *ls = analyze_loop_variables(ir);
-      if (ls->loop_found) {
-         OPT(set_loop_controls, ir, ls);
-         OPT(unroll_loops, ir, ls, options);
+      if (linked)
+         OPT(do_dead_code, 11, ir, uniform_locations_assigned);
+      else
+         OPT(do_dead_code_unlinked, 12, ir);
+      OPT(do_dead_code_local, 13, ir);
+      OPT(do_tree_grafting, 14, ir);
+      OPT(do_constant_propagation, 15, ir);
+      if (linked)
+         OPT(do_constant_variable, 16, ir);
+      else
+         OPT(do_constant_variable_unlinked, 17, ir);
+      OPT(do_constant_folding, 18, ir);
+      OPT(do_minmax_prune, 19, ir);
+      OPT(do_rebalance_tree, 20, ir);
+      OPT(do_algebraic, 21, ir, native_integers, options);
+      OPT(do_lower_jumps, 22, ir);
+      OPT(do_vec_index_to_swizzle, 23, ir);
+      OPT(lower_vector_insert, 24, ir, false);
+      OPT(do_swizzle_swizzle, 25, ir);
+      OPT(do_noop_swizzle, 26, ir);
+
+      OPT(optimize_split_arrays, 27, ir, linked);
+      OPT(optimize_redundant_jumps, 28, ir);
+
+      if (options->MaxUnrollIterations) {
+         loop_state *ls = analyze_loop_variables(ir);
+         if (ls->loop_found) {
+            OPT(set_loop_controls, 29, ir, ls);
+            OPT(unroll_loops, 30, ir, ls, options);
+         }
+         delete ls;
       }
-      delete ls;
-   }
 
+   } while(iteration_progress);
 #undef OPT
 
    return progress;
-- 
2.9.3



More information about the mesa-dev mailing list