<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 1, 2016 at 4:20 PM, Jason Ekstrand <span dir="ltr"><<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Reviewed-by: Rob Clark <<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>><br>
Cc: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
<br>
v2: Pull get_io_offset into nir_gather_info and add an assert that our<br>
    shader is for one of the supported stages. (Ken)<br>
---<br>
 src/compiler/Makefile.sources      |   1 +<br>
 src/compiler/nir/Makefile.sources  |   1 +<br>
 src/compiler/nir/nir.h             |   1 +<br>
 src/compiler/nir/nir_gather_info.c | 160 +++++++++++++++++++++++++++++++++++++<br>
 4 files changed, 163 insertions(+)<br>
 create mode 100644 src/compiler/nir/nir_gather_info.c<br>
<br>
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources<br>
index 0aee200..b243328 100644<br>
--- a/src/compiler/Makefile.sources<br>
+++ b/src/compiler/Makefile.sources<br>
@@ -176,6 +176,7 @@ NIR_FILES = \<br>
        nir/nir_control_flow_private.h \<br>
        nir/nir_dominance.c \<br>
        nir/nir_from_ssa.c \<br>
+       nir/nir_gather_info.c \<br>
        nir/nir_gs_count_vertices.c \<br>
        nir/nir_inline_functions.c \<br>
        nir/nir_instr_set.c \<br>
diff --git a/src/compiler/nir/Makefile.sources b/src/compiler/nir/Makefile.sources<br>
index 3474302..c920d2c 100644<br>
--- a/src/compiler/nir/Makefile.sources<br>
+++ b/src/compiler/nir/Makefile.sources<br>
@@ -19,6 +19,7 @@ NIR_FILES = \<br>
        nir_control_flow_private.h \<br>
        nir_dominance.c \<br>
        nir_from_ssa.c \<br>
+       nir_gather_info.c \<br>
        nir_gs_count_vertices.c \<br>
        nir_inline_functions.c \<br>
        nir_instr_set.c \<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 484598a..b657939 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2211,6 +2211,7 @@ bool nir_lower_locals_to_regs(nir_shader *shader);<br>
<br>
 void nir_lower_outputs_to_temporaries(nir_shader *shader,<br>
                                       nir_function *entrypoint);<br>
+void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);<br>
<br>
 void nir_assign_var_locations(struct exec_list *var_list,<br>
                               unsigned *size,<br>
diff --git a/src/compiler/nir/nir_gather_info.c b/src/compiler/nir/nir_gather_info.c<br>
new file mode 100644<br>
index 0000000..046836f<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_gather_info.c<br>
@@ -0,0 +1,160 @@<br>
+/*<br>
+ * Copyright © 2015 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "nir.h"<br>
+<br>
+static void<br>
+gather_intrinsic_info(nir_intrinsic_instr *instr, nir_shader *shader)<br>
+{<br>
+   switch (instr->intrinsic) {<br>
+   case nir_intrinsic_discard:<br></blockquote><div><br></div><div>Need discard_if here too<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      assert(shader->stage == MESA_SHADER_FRAGMENT);<br>
+      shader->info.fs.uses_discard = true;<br>
+      break;<br>
+<br>
+   case nir_intrinsic_load_front_face:<br>
+   case nir_intrinsic_load_vertex_id:<br>
+   case nir_intrinsic_load_vertex_id_zero_base:<br>
+   case nir_intrinsic_load_base_vertex:<br>
+   case nir_intrinsic_load_instance_id:<br>
+   case nir_intrinsic_load_sample_id:<br>
+   case nir_intrinsic_load_sample_pos:<br>
+   case nir_intrinsic_load_sample_mask_in:<br>
+   case nir_intrinsic_load_primitive_id:<br>
+   case nir_intrinsic_load_invocation_id:<br>
+   case nir_intrinsic_load_local_invocation_id:<br>
+   case nir_intrinsic_load_work_group_id:<br>
+   case nir_intrinsic_load_num_work_groups:<br>
+      shader->info.system_values_read |=<br>
+         (1 << nir_system_value_from_intrinsic(instr->intrinsic));<br>
+      break;<br>
+<br>
+   case nir_intrinsic_end_primitive:<br>
+   case nir_intrinsic_end_primitive_with_counter:<br>
+      assert(shader->stage == MESA_SHADER_GEOMETRY);<br>
+      shader->info.gs.uses_end_primitive = 1;<br>
+      break;<br>
+<br>
+   default:<br>
+      break;<br>
+   }<br>
+}<br>
+<br>
+static void<br>
+gather_tex_info(nir_tex_instr *instr, nir_shader *shader)<br>
+{<br>
+   if (instr->op == nir_texop_tg4)<br>
+      shader->info.uses_texture_gather = true;<br>
+}<br>
+<br>
+static bool<br>
+gather_info_block(nir_block *block, void *shader)<br>
+{<br>
+   nir_foreach_instr(block, instr) {<br>
+      switch (instr->type) {<br>
+      case nir_instr_type_intrinsic:<br>
+         gather_intrinsic_info(nir_instr_as_intrinsic(instr), shader);<br>
+         break;<br>
+      case nir_instr_type_tex:<br>
+         gather_tex_info(nir_instr_as_tex(instr), shader);<br>
+         break;<br>
+      case nir_instr_type_call:<br>
+         assert(!"nir_shader_gather_info only works if functions are inlined");<br>
+         break;<br>
+      default:<br>
+         break;<br>
+      }<br>
+   }<br>
+<br>
+   return true;<br>
+}<br>
+<br>
+/**<br>
+ * Returns the bits in the inputs_read, outputs_written, or<br>
+ * system_values_read bitfield corresponding to this variable.<br>
+ */<br>
+static inline uint64_t<br>
+get_io_mask(nir_variable *var, gl_shader_stage stage)<br>
+{<br>
+   assert(var->data.mode == nir_var_shader_in ||<br>
+          var->data.mode == nir_var_shader_out ||<br>
+          var->data.mode == nir_var_system_value);<br>
+   assert(var->data.location >= 0);<br>
+<br>
+   const struct glsl_type *var_type = var->type;<br>
+   if (stage == MESA_SHADER_GEOMETRY && var->data.mode == nir_var_shader_in) {<br>
+      /* Most geometry shader inputs are per-vertex arrays */<br>
+      if (var->data.location >= VARYING_SLOT_VAR0)<br>
+         assert(glsl_type_is_array(var_type));<br>
+<br>
+      if (glsl_type_is_array(var_type))<br>
+         var_type = glsl_get_array_element(var_type);<br>
+   }<br>
+<br>
+   bool is_vertex_input = (var->data.mode == nir_var_shader_in &&<br>
+                           stage == MESA_SHADER_VERTEX);<br>
+   unsigned slots = glsl_count_attribute_slots(var_type, is_vertex_input);<br>
+   return ((1ull << slots) - 1) << var->data.location;<br>
+}<br>
+<br>
+void<br>
+nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)<br>
+{<br>
+   /* This pass does not yet support tessellation shaders */<br>
+   assert(shader->stage == MESA_SHADER_VERTEX ||<br>
+          shader->stage == MESA_SHADER_GEOMETRY ||<br>
+          shader->stage == MESA_SHADER_FRAGMENT ||<br>
+          shader->stage == MESA_SHADER_COMPUTE);<br>
+<br>
+   shader->info.inputs_read = 0;<br>
+   foreach_list_typed(nir_variable, var, node, &shader->inputs)<br>
+      shader->info.inputs_read |= get_io_mask(var, shader->stage);<br>
+<br>
+   /* TODO: Some day we may need to add stream support to NIR */<br>
+   shader->info.outputs_written = 0;<br>
+   foreach_list_typed(nir_variable, var, node, &shader->outputs)<br>
+      shader->info.outputs_written |= get_io_mask(var, shader->stage);<br>
+<br>
+   shader->info.system_values_read = 0;<br>
+   foreach_list_typed(nir_variable, var, node, &shader->system_values)<br>
+      shader->info.system_values_read |= get_io_mask(var, shader->stage);<br>
+<br>
+   shader->info.num_textures = 0;<br>
+   shader->info.num_images = 0;<br>
+   nir_foreach_variable(var, &shader->uniforms) {<br>
+      const struct glsl_type *type = var->type;<br>
+      unsigned count = 1;<br>
+      if (glsl_type_is_array(type)) {<br>
+         count = glsl_get_length(type);<br>
+         type = glsl_get_array_element(type);<br>
+      }<br>
+<br>
+      if (glsl_type_is_image(type)) {<br>
+         shader->info.num_images += count;<br>
+      } else if (glsl_type_is_sampler(type)) {<br>
+         shader->info.num_textures += count;<br>
+      }<br>
+   }<br>
+<br>
+   nir_foreach_block(entrypoint, gather_info_block, shader);<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.0.400.gff86faf<br>
<br>
</font></span></blockquote></div><br></div></div>