nir: sort variables by location in nir_lower_io_passes to work around a bug
authorMarek Olšák <marek.olsak@amd.com>
Fri, 1 Sep 2023 22:56:10 +0000 (18:56 -0400)
committerMarge Bot <emma+marge@anholt.net>
Sat, 7 Oct 2023 11:18:40 +0000 (11:18 +0000)
I don't know why this is necessary, but it unblocks the work on varying
optimizations.

Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25394>

src/compiler/nir/nir.h
src/compiler/nir/nir_linking_helpers.c
src/compiler/nir/nir_lower_io.c

index accb906..2d17343 100644 (file)
@@ -4978,6 +4978,7 @@ bool nir_lower_amul(nir_shader *shader,
 
 bool nir_lower_ubo_vec4(nir_shader *shader);
 
+void nir_sort_variables_by_location(nir_shader *shader, nir_variable_mode mode);
 void nir_assign_io_var_locations(nir_shader *shader,
                                  nir_variable_mode mode,
                                  unsigned *size,
index 0d37738..4ba7d71 100644 (file)
@@ -1474,6 +1474,15 @@ sort_varyings(nir_shader *shader, nir_variable_mode mode,
 }
 
 void
+nir_sort_variables_by_location(nir_shader *shader, nir_variable_mode mode)
+{
+   struct exec_list vars;
+
+   sort_varyings(shader, mode, &vars);
+   exec_list_append(&shader->variables, &vars);
+}
+
+void
 nir_assign_io_var_locations(nir_shader *shader, nir_variable_mode mode,
                             unsigned *size, gl_shader_stage stage)
 {
index cdf64e9..67988a1 100644 (file)
@@ -3127,6 +3127,19 @@ nir_lower_io_passes(nir_shader *nir, bool renumber_vs_inputs)
       (nir->options->support_indirect_outputs >> nir->info.stage) & 0x1 &&
       nir->xfb_info == NULL;
 
+   /* TODO: Sorting variables by location is required due to some bug
+    * in nir_lower_io_to_temporaries. If variables are not sorted,
+    * dEQP-GLES31.functional.separate_shader.random.0 fails.
+    *
+    * This isn't needed if nir_assign_io_var_locations is called because it
+    * also sorts variables. However, if IO is lowered sooner than that, we
+    * must sort explicitly here to get what nir_assign_io_var_locations does.
+    */
+   unsigned varying_var_mask =
+      (nir->info.stage != MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
+      (nir->info.stage != MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
+   nir_sort_variables_by_location(nir, varying_var_mask);
+
    if (!has_indirect_inputs || !has_indirect_outputs) {
       NIR_PASS_V(nir, nir_lower_io_to_temporaries,
                  nir_shader_get_entrypoint(nir), !has_indirect_outputs,