[Mesa-dev] [PATCH] i965/nir: Add hooks for testing nir_shader_clone

Jason Ekstrand jason at jlekstrand.net
Mon Nov 16 14:39:10 PST 2015


This commit adds code for testing nir_shader_clone by running it after each
and every optimization pass and throwing away the old shader.  Testing
nir_shader_clone is hidden behind a new INTEL_CLONE_NIR environment
variable.
---
 src/mesa/drivers/dri/i965/brw_fs.cpp              | 10 +++--
 src/mesa/drivers/dri/i965/brw_nir.c               | 55 ++++++++++++++++-------
 src/mesa/drivers/dri/i965/brw_nir.h               | 28 ++++++------
 src/mesa/drivers/dri/i965/brw_vec4.cpp            |  6 +--
 src/mesa/drivers/dri/i965/brw_vec4_gs_visitor.cpp |  6 +--
 5 files changed, 65 insertions(+), 40 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index e094131..9d5be95 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -5458,8 +5458,9 @@ brw_compile_fs(const struct brw_compiler *compiler, void *log_data,
                char **error_str)
 {
    nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
-   brw_nir_apply_sampler_key(shader, compiler->devinfo, &key->tex, true);
-   brw_postprocess_nir(shader, compiler->devinfo, true);
+   shader = brw_nir_apply_sampler_key(shader, compiler->devinfo,
+                                      &key->tex, true);
+   shader = brw_postprocess_nir(shader, compiler->devinfo, true);
 
    /* key->alpha_test_func means simulating alpha testing via discards,
     * so the shader definitely kills pixels.
@@ -5619,8 +5620,9 @@ brw_compile_cs(const struct brw_compiler *compiler, void *log_data,
                char **error_str)
 {
    nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
-   brw_nir_apply_sampler_key(shader, compiler->devinfo, &key->tex, true);
-   brw_postprocess_nir(shader, compiler->devinfo, true);
+   shader = brw_nir_apply_sampler_key(shader, compiler->devinfo,
+                                      &key->tex, true);
+   shader = brw_postprocess_nir(shader, compiler->devinfo, true);
 
    prog_data->local_size[0] = shader->info.cs.local_size[0];
    prog_data->local_size[1] = shader->info.cs.local_size[1];
diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c
index a897f27..452dbb7 100644
--- a/src/mesa/drivers/dri/i965/brw_nir.c
+++ b/src/mesa/drivers/dri/i965/brw_nir.c
@@ -171,11 +171,26 @@ brw_nir_lower_outputs(nir_shader *nir, bool is_scalar)
    }
 }
 
-#define _OPT(do_pass) (({     \
-   bool this_progress = true; \
-   do_pass                    \
-   nir_validate_shader(nir);  \
-   this_progress;             \
+static bool
+should_clone_nir()
+{
+   static int should_clone = -1;
+   if (should_clone < 1)
+      should_clone = brw_env_var_as_boolean("INTEL_CLONE_NIR", false);
+
+   return should_clone;
+}
+
+#define _OPT(do_pass) (({                                            \
+   bool this_progress = true;                                        \
+   do_pass                                                           \
+   nir_validate_shader(nir);                                         \
+   if (should_clone_nir()) {                                         \
+      nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
+      ralloc_free(nir);                                              \
+      nir = clone;                                                   \
+   }                                                                 \
+   this_progress;                                                    \
 }))
 
 #define OPT(pass, ...) _OPT(                   \
@@ -191,7 +206,7 @@ brw_nir_lower_outputs(nir_shader *nir, bool is_scalar)
    pass(nir, ##__VA_ARGS__);   \
 )
 
-static void
+static nir_shader *
 nir_optimize(nir_shader *nir, bool is_scalar)
 {
    bool progress;
@@ -219,6 +234,8 @@ nir_optimize(nir_shader *nir, bool is_scalar)
       OPT(nir_opt_remove_phis);
       OPT(nir_opt_undef);
    } while (progress);
+
+   return nir;
 }
 
 /* Does some simple lowering and runs the standard suite of optimizations
@@ -230,7 +247,7 @@ nir_optimize(nir_shader *nir, bool is_scalar)
  * intended for the FS backend as long as nir_optimize is called again with
  * is_scalar = true to scalarize everything prior to code gen.
  */
-void
+nir_shader *
 brw_preprocess_nir(nir_shader *nir, bool is_scalar)
 {
    bool progress; /* Written by OPT and OPT_V */
@@ -250,15 +267,17 @@ brw_preprocess_nir(nir_shader *nir, bool is_scalar)
 
    OPT(nir_split_var_copies);
 
-   nir_optimize(nir, is_scalar);
+   nir = nir_optimize(nir, is_scalar);
 
    /* Lower a bunch of stuff */
    OPT_V(nir_lower_var_copies);
 
    /* Get rid of split copies */
-   nir_optimize(nir, is_scalar);
+   nir = nir_optimize(nir, is_scalar);
 
    OPT(nir_remove_dead_variables);
+
+   return nir;
 }
 
 /* Lowers inputs, outputs, uniforms, and samplers for i965
@@ -268,7 +287,7 @@ brw_preprocess_nir(nir_shader *nir, bool is_scalar)
  * shader_prog parameter is optional and is used only for lowering sampler
  * derefs and atomics for GLSL shaders.
  */
-void
+nir_shader *
 brw_lower_nir(nir_shader *nir,
               const struct brw_device_info *devinfo,
               const struct gl_shader_program *shader_prog,
@@ -294,7 +313,7 @@ brw_lower_nir(nir_shader *nir,
       OPT_V(nir_lower_atomics, shader_prog);
    }
 
-   nir_optimize(nir, is_scalar);
+   return nir_optimize(nir, is_scalar);
 }
 
 /* Prepare the given shader for codegen
@@ -304,7 +323,7 @@ brw_lower_nir(nir_shader *nir,
  * called on a shader, it will no longer be in SSA form so most optimizations
  * will not work.
  */
-void
+nir_shader *
 brw_postprocess_nir(nir_shader *nir,
                     const struct brw_device_info *devinfo,
                     bool is_scalar)
@@ -362,6 +381,8 @@ brw_postprocess_nir(nir_shader *nir,
               _mesa_shader_stage_to_string(nir->stage));
       nir_print_shader(nir, stderr);
    }
+
+   return nir;
 }
 
 nir_shader *
@@ -389,13 +410,13 @@ brw_create_nir(struct brw_context *brw,
 
    (void)progress;
 
-   brw_preprocess_nir(nir, is_scalar);
-   brw_lower_nir(nir, devinfo, shader_prog, is_scalar);
+   nir = brw_preprocess_nir(nir, is_scalar);
+   nir = brw_lower_nir(nir, devinfo, shader_prog, is_scalar);
 
    return nir;
 }
 
-void
+nir_shader *
 brw_nir_apply_sampler_key(nir_shader *nir,
                           const struct brw_device_info *devinfo,
                           const struct brw_sampler_prog_key_data *key_tex,
@@ -426,8 +447,10 @@ brw_nir_apply_sampler_key(nir_shader *nir,
 
    if (nir_lower_tex(nir, &tex_options)) {
       nir_validate_shader(nir);
-      nir_optimize(nir, is_scalar);
+      nir = nir_optimize(nir, is_scalar);
    }
+
+   return nir;
 }
 
 enum brw_reg_type
diff --git a/src/mesa/drivers/dri/i965/brw_nir.h b/src/mesa/drivers/dri/i965/brw_nir.h
index a399992..0a8a5a2 100644
--- a/src/mesa/drivers/dri/i965/brw_nir.h
+++ b/src/mesa/drivers/dri/i965/brw_nir.h
@@ -81,20 +81,20 @@ nir_shader *brw_create_nir(struct brw_context *brw,
                            gl_shader_stage stage,
                            bool is_scalar);
 
-void brw_preprocess_nir(nir_shader *nir, bool is_scalar);
-void brw_lower_nir(nir_shader *nir,
-                   const struct brw_device_info *devinfo,
-                   const struct gl_shader_program *shader_prog,
-                   bool is_scalar);
-void brw_postprocess_nir(nir_shader *nir,
-                         const struct brw_device_info *devinfo,
-                         bool is_scalar);
-
-
-void brw_nir_apply_sampler_key(nir_shader *nir,
-                               const struct brw_device_info *devinfo,
-                               const struct brw_sampler_prog_key_data *key,
-                               bool is_scalar);
+nir_shader *brw_preprocess_nir(nir_shader *nir, bool is_scalar);
+nir_shader *brw_lower_nir(nir_shader *nir,
+                          const struct brw_device_info *devinfo,
+                          const struct gl_shader_program *shader_prog,
+                          bool is_scalar);
+nir_shader *brw_postprocess_nir(nir_shader *nir,
+                                const struct brw_device_info *devinfo,
+                                bool is_scalar);
+
+
+nir_shader *brw_nir_apply_sampler_key(nir_shader *nir,
+                                      const struct brw_device_info *devinfo,
+                                      const struct brw_sampler_prog_key_data *key,
+                                      bool is_scalar);
 
 enum brw_reg_type brw_type_for_nir_type(nir_alu_type type);
 
diff --git a/src/mesa/drivers/dri/i965/brw_vec4.cpp b/src/mesa/drivers/dri/i965/brw_vec4.cpp
index 6c27ee2..243c413 100644
--- a/src/mesa/drivers/dri/i965/brw_vec4.cpp
+++ b/src/mesa/drivers/dri/i965/brw_vec4.cpp
@@ -1986,9 +1986,9 @@ brw_compile_vs(const struct brw_compiler *compiler, void *log_data,
                char **error_str)
 {
    nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
-   brw_nir_apply_sampler_key(shader, compiler->devinfo, &key->tex,
-                             compiler->scalar_vs);
-   brw_postprocess_nir(shader, compiler->devinfo, compiler->scalar_vs);
+   shader = brw_nir_apply_sampler_key(shader, compiler->devinfo,
+                                      &key->tex, compiler->scalar_vs);
+   shader = brw_postprocess_nir(shader, compiler->devinfo, compiler->scalar_vs);
 
    const unsigned *assembly = NULL;
 
diff --git a/src/mesa/drivers/dri/i965/brw_vec4_gs_visitor.cpp b/src/mesa/drivers/dri/i965/brw_vec4_gs_visitor.cpp
index 2d1ab49..5c4c6b0 100644
--- a/src/mesa/drivers/dri/i965/brw_vec4_gs_visitor.cpp
+++ b/src/mesa/drivers/dri/i965/brw_vec4_gs_visitor.cpp
@@ -616,9 +616,9 @@ brw_compile_gs(const struct brw_compiler *compiler, void *log_data,
    c.key = *key;
 
    nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
-   brw_nir_apply_sampler_key(shader, compiler->devinfo, &key->tex,
-                             compiler->scalar_gs);
-   brw_postprocess_nir(shader, compiler->devinfo, compiler->scalar_gs);
+   shader = brw_nir_apply_sampler_key(shader, compiler->devinfo,
+                                      &key->tex, compiler->scalar_gs);
+   shader = brw_postprocess_nir(shader, compiler->devinfo, compiler->scalar_gs);
 
    prog_data->include_primitive_id =
       (shader->info.inputs_read & VARYING_BIT_PRIMITIVE_ID) != 0;
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list