<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Mar 26, 2016 at 2:02 PM, Rob Clark <span dir="ltr"><<a href="mailto:robdclark@gmail.com" target="_blank">robdclark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
<br>
A pass to lower complex (struct/array/mat) inputs/outputs to primitive<br>
types. This allows, for example, linking that removes unused components<br>
of a larger type which is not indirectly accessed.<br>
<br>
In the near term, it is needed for gallium (mesa/st) support for NIR,<br>
since only used components of a type are assigned VBO slots, and we<br>
otherwise have no way to represent that to the driver backend. But it<br>
should be useful for doing shader linking in NIR.<br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
---<br>
src/compiler/Makefile.sources | 1 +<br>
src/compiler/nir/nir.h | 1 +<br>
src/compiler/nir/nir_lower_io_types.c | 178 ++++++++++++++++++++++++++++++++++<br>
3 files changed, 180 insertions(+)<br>
create mode 100644 src/compiler/nir/nir_lower_io_types.c<br>
<br>
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources<br>
index ae7efbf..8f1ffdc 100644<br>
--- a/src/compiler/Makefile.sources<br>
+++ b/src/compiler/Makefile.sources<br>
@@ -196,6 +196,7 @@ NIR_FILES = \<br>
nir/nir_lower_idiv.c \<br>
nir/nir_lower_indirect_derefs.c \<br>
nir/nir_lower_io.c \<br>
+ nir/nir_lower_io_types.c \<br>
nir/nir_lower_outputs_to_temporaries.c \<br>
nir/nir_lower_passthrough_edgeflags.c \<br>
nir/nir_lower_phis_to_scalar.c \<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 65f75bd..7f7c459 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2172,6 +2172,7 @@ void nir_lower_io(nir_shader *shader,<br>
nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);<br>
nir_src *nir_get_io_vertex_index_src(nir_intrinsic_instr *instr);<br>
<br>
+void nir_lower_io_types(nir_shader *shader, int (*type_size)(const struct glsl_type *));<br>
void nir_lower_vars_to_ssa(nir_shader *shader);<br>
<br>
bool nir_remove_dead_variables(nir_shader *shader);<br>
diff --git a/src/compiler/nir/nir_lower_io_types.c b/src/compiler/nir/nir_lower_io_types.c<br>
new file mode 100644<br>
index 0000000..b7d9ebe<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_lower_io_types.c<br>
@@ -0,0 +1,178 @@<br>
+/*<br>
+ * Copyright © 2016 Red Hat<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 FROM,<br>
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>
+ * SOFTWARE.<br>
+ */<br>
+<br>
+#include "nir.h"<br>
+#include "nir_builder.h"<br>
+<br>
+/* Lower complex (struct/array/mat) input and output vars to primitive types<br>
+ * (vec4) for linking. All indirect input/output access should already be<br>
+ * lowered (ie. nir_lower_io_to_temporaries).<br>
+ */<br>
+<br>
+struct lower_io_types_state {<br>
+ nir_shader *shader;<br>
+ struct exec_list new_ins;<br>
+ struct exec_list new_outs;<br>
+ int (*type_size)(const struct glsl_type *);<br>
+};<br>
+<br>
+static nir_variable *<br>
+get_new_var(struct lower_io_types_state *state, nir_variable *var, unsigned off)<br>
+{<br>
+ struct exec_list *list;<br>
+<br>
+ if (var->data.mode == nir_var_shader_in) {<br>
+ list = &state->new_ins;<br>
+ } else {<br>
+ assert(var->data.mode == nir_var_shader_out);<br>
+ list = &state->new_outs;<br>
+ }<br>
+<br>
+ nir_foreach_variable(nvar, list) {<br>
+ if (nvar->data.location == (var->data.location + off))<br>
+ return nvar;<br>
+ }<br></blockquote><div><br></div><div>Doing a linear search here could get expensive. Why not just have an array that maps locations to variables? I think you have a maximum of 64 possible locations (if you disregard tess) so it's not that memory-intensive.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ /* doesn't already exist, so we need to create a new one: */<br>
+ /* TODO figure out if scalar vs vec, and if float/int/uint/(double?)<br>
+ * do we need to fixup interpolation mode for int vs float components<br>
+ * of a struct, etc..<br>
+ */<br>
+ const struct glsl_type *ntype = glsl_vec4_type();<br>
+ nir_variable *nvar = nir_variable_create(state->shader, var->data.mode,<br>
+ ntype, NULL);<br></blockquote><div><br></div><div>Do you want to create a new one or clone? Cloning seems better because that would ensure that interp qualifiers etc. get copied over.<br><br></div><div>Also, I seem to recall it being possible for outputs to have constant initializers coming out of GLSL IR. This pass doesn't handle that.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ nvar->name = ralloc_asprintf(nvar, "%s@%u", var->name, off);<br>
+ nvar->data = var->data;<br>
+ nvar->data.location += off;<br>
+<br>
+ /* nir_variable_create is too clever for it's own good: */<br>
+ exec_node_remove(&nvar->node);<br>
+ exec_node_self_link(&nvar->node); /* no delinit() :-( */<br></blockquote><div><br></div><div>This isn't needed. Re-insertion just stomps whatever was there before.<br><br>Incidentally, if you had an array, you could just let nir_variable_create be clever.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ exec_list_push_tail(list, &nvar->node);<br>
+<br>
+ /* remove existing var from input/output list: */<br>
+ exec_node_remove(&var->node);<br>
+ exec_node_self_link(&var->node);<br></blockquote><div><br></div><div>This isn't needed either.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ return nvar;<br>
+}<br>
+<br>
+static unsigned<br>
+get_deref_offset(struct lower_io_types_state *state, nir_deref *tail)<br>
+{<br>
+ unsigned offset = 0;<br>
+<br>
+ while (tail->child != NULL) {<br>
+ const struct glsl_type *parent_type = tail->type;<br>
+ tail = tail->child;<br>
+<br>
+ if (tail->deref_type == nir_deref_type_array) {<br>
+ nir_deref_array *deref_array = nir_deref_as_array(tail);<br>
+<br>
+ /* indirect inputs/outputs should already be lowered! */<br>
+ assert(deref_array->deref_array_type == nir_deref_array_type_direct);<br>
+<br>
+ unsigned size = state->type_size(tail->type);<br>
+<br>
+ offset += size * deref_array->base_offset;<br>
+ } else if (tail->deref_type == nir_deref_type_struct) {<br>
+ nir_deref_struct *deref_struct = nir_deref_as_struct(tail);<br>
+<br>
+ for (unsigned i = 0; i < deref_struct->index; i++) {<br>
+ offset += state->type_size(glsl_get_struct_field(parent_type, i));<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ return offset;<br>
+}<br>
+<br>
+static bool<br>
+lower_io_types_block(nir_block *block, void *void_state)<br>
+{<br>
+ struct lower_io_types_state *state = void_state;<br>
+<br>
+ nir_foreach_instr(block, instr) {<br>
+ if (instr->type != nir_instr_type_intrinsic)<br>
+ continue;<br>
+<br>
+ nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);<br>
+<br>
+ if ((intr->intrinsic != nir_intrinsic_load_var) &&<br>
+ (intr->intrinsic != nir_intrinsic_store_var))<br>
+ continue;<br></blockquote><div><br></div><div>You should say somewhere in the top comment that copies need to also be lowered prior to this pass.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ nir_variable *var = intr->variables[0]->var;<br>
+<br>
+ if ((var->data.mode != nir_var_shader_in) &&<br>
+ (var->data.mode != nir_var_shader_out))<br>
+ continue;<br>
+<br>
+ /* no need to split up if already primitive */<br>
+ if (state->type_size(var->type) == 1)<br>
+ continue;<br>
+<br>
+ unsigned off = get_deref_offset(state, &intr->variables[0]->deref);<br>
+ nir_variable *nvar = get_new_var(state, var, off);<br>
+<br>
+ /* and then re-write the load/store_var deref: */<br>
+ intr->variables[0] = nir_deref_var_create(intr, nvar);<br>
+ }<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static void<br>
+lower_io_types_impl(nir_function_impl *impl, struct lower_io_types_state *state)<br>
+{<br>
+ nir_foreach_block(impl, lower_io_types_block, state);<br>
+<br>
+ nir_metadata_preserve(impl, nir_metadata_block_index |<br>
+ nir_metadata_dominance);<br>
+}<br>
+<br>
+<br>
+void<br>
+nir_lower_io_types(nir_shader *shader, int (*type_size)(const struct glsl_type *))<br></blockquote><div><br></div><div>Just a side-note (not a request) but we really should have a "glsl_type_size_func" typedef somewhere.<br><br></div><div>Also, what happened to just using nir_type_size_vec4? Incidentally, I think you could also use the newly exposed component_slots function from my series.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+ struct lower_io_types_state state;<br>
+<br>
+ /* NOTE: only operates on units of vec4 slots: */<br>
+ assert(type_size(glsl_vec4_type()) == 1);<br>
+<br>
+ state.shader = shader;<br>
+ exec_list_make_empty(&state.new_ins);<br>
+ exec_list_make_empty(&state.new_outs);<br>
+ state.type_size = type_size;<br>
+<br>
+ nir_foreach_function(shader, function) {<br>
+ if (function->impl)<br>
+ lower_io_types_impl(function->impl, &state);<br>
+ }<br>
+<br>
+ /* move new in/out vars to shader's lists: */<br>
+ exec_list_append(&shader->inputs, &state.new_ins);<br>
+ exec_list_append(&shader->outputs, &state.new_outs);<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.5<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>