[Mesa-dev] [PATCH 7/7] nir: add helper macros for running NIR passes (v2)

Rob Clark robdclark at gmail.com
Mon Oct 26 08:27:45 PDT 2015


From: Rob Clark <robclark at freedesktop.org>

Convenient place to put in some extra sanity checking, without making
things messy for the drivers running the passes.

v2: don't use GNU C ({}) extension

Signed-off-by: Rob Clark <robclark at freedesktop.org>
---
 src/glsl/nir/nir.h                  |  38 +++++++++++
 src/mesa/drivers/dri/i965/brw_nir.c | 126 ++++++++++++------------------------
 2 files changed, 81 insertions(+), 83 deletions(-)

diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index a0585f1..047c0ea 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1945,10 +1945,48 @@ nir_shader_mutable(nir_shader *shader)
 
 #ifdef DEBUG
 void nir_validate_shader(nir_shader *shader);
+static inline bool
+__nir_test_clone(void)
+{
+   static int enable = -1;
+   if (enable == -1) {
+      enable = (getenv("NIR_TEST_CLONE") == NULL) ? 0 : 1;
+   }
+   return enable == 1;
+}
 #else
 static inline void nir_validate_shader(nir_shader *shader) { (void) shader; }
+static inline bool __nir_test_clone(void) { return false; }
 #endif /* DEBUG */
 
+
+#define NIR_PASS(pass, nir, ...) do {                           \
+      assert(nir_shader_is_mutable(nir));                       \
+      pass((nir), ##__VA_ARGS__);                               \
+      nir_validate_shader(nir);                                 \
+      if (__nir_test_clone()) {                                 \
+         void *mem_ctx = ralloc_parent(nir);                    \
+         nir_shader *ns = nir_shader_clone(mem_ctx, (nir));     \
+         nir_shader_unref(nir);                                 \
+         nir_validate_shader(ns);                               \
+         (nir) = ns;                                            \
+      }                                                         \
+   } while (0)
+
+#define NIR_PASS_PROGRESS(progress, pass, nir, ...) do {        \
+      assert(nir_shader_is_mutable(nir));                       \
+      (progress) |= pass((nir), ##__VA_ARGS__);                 \
+      nir_validate_shader(nir);                                 \
+      if (__nir_test_clone()) {                                 \
+         void *mem_ctx = ralloc_parent(nir);                    \
+         nir_shader *ns = nir_shader_clone(mem_ctx, (nir));     \
+         nir_shader_unref(nir);                                 \
+         nir_validate_shader(ns);                               \
+         (nir) = ns;                                            \
+      }                                                         \
+   } while (0)
+
+
 void nir_calc_dominance_impl(nir_function_impl *impl);
 void nir_calc_dominance(nir_shader *shader);
 
diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c
index 8f09165..17f2244 100644
--- a/src/mesa/drivers/dri/i965/brw_nir.c
+++ b/src/mesa/drivers/dri/i965/brw_nir.c
@@ -136,47 +136,34 @@ brw_nir_lower_outputs(nir_shader *nir, bool is_scalar)
    }
 }
 
-static void
+static nir_shader *
 nir_optimize(nir_shader *nir, bool is_scalar)
 {
    bool progress;
    do {
       progress = false;
-      nir_lower_vars_to_ssa(nir);
-      nir_validate_shader(nir);
 
-      if (is_scalar) {
-         nir_lower_alu_to_scalar(nir);
-         nir_validate_shader(nir);
-      }
+      NIR_PASS(nir_lower_vars_to_ssa, nir);
 
-      progress |= nir_copy_prop(nir);
-      nir_validate_shader(nir);
+      if (is_scalar)
+         NIR_PASS(nir_lower_alu_to_scalar, nir);
 
-      if (is_scalar) {
-         nir_lower_phis_to_scalar(nir);
-         nir_validate_shader(nir);
-      }
+      NIR_PASS_PROGRESS(progress, nir_copy_prop, nir);
+
+      if (is_scalar)
+         NIR_PASS(nir_lower_phis_to_scalar, nir);
 
-      progress |= nir_copy_prop(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_dce(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_cse(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_peephole_select(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_algebraic(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_constant_folding(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_dead_cf(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_remove_phis(nir);
-      nir_validate_shader(nir);
-      progress |= nir_opt_undef(nir);
-      nir_validate_shader(nir);
+      NIR_PASS_PROGRESS(progress, nir_copy_prop, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_dce, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_cse, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_peephole_select, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_algebraic, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_constant_folding, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_dead_cf, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_remove_phis, nir);
+      NIR_PASS_PROGRESS(progress, nir_opt_undef, nir);
    } while (progress);
+   return nir;
 }
 
 nir_shader *
@@ -204,75 +191,52 @@ brw_create_nir(struct brw_context *brw,
    }
    nir_validate_shader(nir);
 
-   if (stage == MESA_SHADER_GEOMETRY) {
-      nir_lower_gs_intrinsics(nir);
-      nir_validate_shader(nir);
-   }
+   if (stage == MESA_SHADER_GEOMETRY)
+      NIR_PASS(nir_lower_gs_intrinsics, nir);
 
-   nir_lower_global_vars_to_local(nir);
-   nir_validate_shader(nir);
-
-   nir_lower_tex(nir, &tex_options);
-   nir_validate_shader(nir);
-
-   nir_normalize_cubemap_coords(nir);
-   nir_validate_shader(nir);
-
-   nir_split_var_copies(nir);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_lower_global_vars_to_local, nir);
+   NIR_PASS(nir_lower_tex, nir, &tex_options);
+   NIR_PASS(nir_normalize_cubemap_coords, nir);
+   NIR_PASS(nir_split_var_copies, nir);
 
-   nir_optimize(nir, is_scalar);
+   nir = nir_optimize(nir, is_scalar);
 
    /* Lower a bunch of stuff */
-   nir_lower_var_copies(nir);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_lower_var_copies, nir);
 
    /* Get rid of split copies */
-   nir_optimize(nir, is_scalar);
+   nir = nir_optimize(nir, is_scalar);
 
    brw_nir_lower_inputs(nir, is_scalar);
    brw_nir_lower_outputs(nir, is_scalar);
    nir_assign_var_locations(&nir->uniforms,
                             &nir->num_uniforms,
                             is_scalar ? type_size_scalar : type_size_vec4);
-   nir_lower_io(nir, nir_var_all,
+   NIR_PASS(nir_lower_io, nir, nir_var_all,
                 is_scalar ? type_size_scalar : type_size_vec4);
-   nir_validate_shader(nir);
-
-   nir_remove_dead_variables(nir);
-   nir_validate_shader(nir);
 
-   if (shader_prog) {
-      nir_lower_samplers(nir, shader_prog);
-      nir_validate_shader(nir);
-   }
+   NIR_PASS(nir_remove_dead_variables, nir);
 
-   nir_lower_system_values(nir);
-   nir_validate_shader(nir);
+   if (shader_prog)
+      NIR_PASS(nir_lower_samplers, nir, shader_prog);
 
-   nir_lower_atomics(nir);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_lower_system_values, nir);
+   NIR_PASS(nir_lower_atomics, nir);
 
-   nir_optimize(nir, is_scalar);
+   nir = nir_optimize(nir, is_scalar);
 
    if (brw->gen >= 6) {
       /* Try and fuse multiply-adds */
-      nir_opt_peephole_ffma(nir);
-      nir_validate_shader(nir);
+      NIR_PASS(nir_opt_peephole_ffma, nir);
    }
 
-   nir_opt_algebraic_late(nir);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_opt_algebraic_late, nir);
 
-   nir_lower_locals_to_regs(nir);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_lower_locals_to_regs, nir);
 
-   nir_lower_to_source_mods(nir);
-   nir_validate_shader(nir);
-   nir_copy_prop(nir);
-   nir_validate_shader(nir);
-   nir_opt_dce(nir);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_lower_to_source_mods, nir);
+   NIR_PASS(nir_copy_prop, nir);
+   NIR_PASS(nir_opt_dce, nir);
 
    if (unlikely(debug_enabled)) {
       /* Re-index SSA defs so we print more sensible numbers. */
@@ -286,15 +250,11 @@ brw_create_nir(struct brw_context *brw,
       nir_print_shader(nir, stderr);
    }
 
-   nir_convert_from_ssa(nir, true);
-   nir_validate_shader(nir);
+   NIR_PASS(nir_convert_from_ssa, nir, true);
 
    if (!is_scalar) {
-      nir_move_vec_src_uses_to_dest(nir);
-      nir_validate_shader(nir);
-
-      nir_lower_vec_to_movs(nir);
-      nir_validate_shader(nir);
+      NIR_PASS(nir_move_vec_src_uses_to_dest, nir);
+      NIR_PASS(nir_lower_vec_to_movs, nir);
    }
 
    /* This is the last pass we run before we start emitting stuff.  It
-- 
2.5.0



More information about the mesa-dev mailing list