[Mesa-dev] [PATCH] i965: Delete most of the linked GLSL IR when using NIR.

Kenneth Graunke kenneth at whitecape.org
Tue May 5 18:57:28 PDT 2015


Vertex shader attribute and fragment shader output queries rely on being
able to inspect top-level ir_variable objects.  So, we have to keep
those.  However, functions and global temporary variables can be deleted
with impunity.

Saves 58MB of memory when replaying a Dota 2 trace on Broadwell.

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
---
 src/mesa/drivers/dri/i965/brw_shader.cpp | 43 +++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/src/mesa/drivers/dri/i965/brw_shader.cpp b/src/mesa/drivers/dri/i965/brw_shader.cpp
index c1fd859..6e4abb2 100644
--- a/src/mesa/drivers/dri/i965/brw_shader.cpp
+++ b/src/mesa/drivers/dri/i965/brw_shader.cpp
@@ -138,6 +138,45 @@ brw_lower_packing_builtins(struct brw_context *brw,
    lower_packing_builtins(ir, ops);
 }
 
+static bool
+is_vert_input_or_frag_output(gl_shader_stage stage, ir_variable *var)
+{
+   if (var) {
+      if (stage == MESA_SHADER_VERTEX) {
+         return var->data.mode == ir_var_shader_in ||
+                var->data.mode == ir_var_system_value;
+      }
+
+      if (stage == MESA_SHADER_FRAGMENT) {
+         return var->data.mode == ir_var_shader_out;
+      }
+   }
+   return false;
+}
+
+/**
+ * Delete GLSL IR except for VS inputs and FS outputs.
+ *
+ * Once we've translated to NIR, we don't need most of the linked GLSL IR anymore.
+ * However, GL API calls for introspecting certain shader inputs/outputs
+ * (shader_query.cpp) require us to keep some top-level ir_variables.
+ */
+static void
+delete_most_glsl_ir(struct gl_shader *shader)
+{
+   void *mem_ctx = ralloc_context(NULL);
+   ralloc_adopt(mem_ctx, shader->ir);
+
+   foreach_in_list_safe(ir_instruction, ir, shader->ir) {
+      if (!is_vert_input_or_frag_output(shader->Stage, ir->as_variable()))
+         ir->remove();
+   }
+
+   reparent_ir(shader->ir, shader->ir);
+   ralloc_free(mem_ctx);
+}
+
+
 static void
 process_glsl_ir(struct brw_context *brw,
                 struct gl_shader_program *shader_prog,
@@ -297,8 +336,10 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
 
       brw_add_texrect_params(prog);
 
-      if (options->NirOptions)
+      if (options->NirOptions) {
          prog->nir = brw_create_nir(brw, shProg, prog, (gl_shader_stage) stage);
+         delete_most_glsl_ir(shader);
+      }
 
       _mesa_reference_program(ctx, &prog, NULL);
    }
-- 
2.3.7



More information about the mesa-dev mailing list