freedreno/ir3: add pass to move varying loads
authorRob Clark <robdclark@gmail.com>
Tue, 26 Mar 2019 15:28:34 +0000 (11:28 -0400)
committerRob Clark <robdclark@gmail.com>
Sat, 30 Mar 2019 16:56:01 +0000 (12:56 -0400)
Signed-off-by: Rob Clark <robdclark@gmail.com>
src/freedreno/Makefile.sources
src/freedreno/ir3/ir3_nir.h
src/freedreno/ir3/ir3_nir_move_varying_inputs.c [new file with mode: 0644]
src/freedreno/ir3/ir3_shader.c
src/freedreno/ir3/meson.build

index 265b8663e30d8c196138391a210b839b2f3c4691..aa8edec82f28c4faff86f862ec7281589da7a860 100644 (file)
@@ -38,6 +38,7 @@ ir3_SOURCES := \
        ir3/ir3_nir_analyze_ubo_ranges.c \
        ir3/ir3_nir_lower_io_offsets.c \
        ir3/ir3_nir_lower_tg4_to_tex.c \
+       ir3/ir3_nir_move_varying_inputs.c \
        ir3/ir3_print.c \
        ir3/ir3_ra.c \
        ir3/ir3_sched.c \
index f1eac75d5747d67f5c86f0c95b1a881bbbbdb313..1fe0bb0d0943e1cbf4c956c752b68000e86ce7ef 100644 (file)
@@ -38,6 +38,7 @@ void ir3_nir_scan_driver_consts(nir_shader *shader, struct ir3_driver_const_layo
 bool ir3_nir_apply_trig_workarounds(nir_shader *shader);
 bool ir3_nir_lower_tg4_to_tex(nir_shader *shader);
 bool ir3_nir_lower_io_offsets(nir_shader *shader);
+bool ir3_nir_move_varying_inputs(nir_shader *shader);
 
 const nir_shader_compiler_options * ir3_get_compiler_options(struct ir3_compiler *compiler);
 bool ir3_key_lowers_nir(const struct ir3_shader_key *key);
diff --git a/src/freedreno/ir3/ir3_nir_move_varying_inputs.c b/src/freedreno/ir3/ir3_nir_move_varying_inputs.c
new file mode 100644 (file)
index 0000000..ede8743
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright © 2019 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "ir3_nir.h"
+#include "compiler/nir/nir_builder.h"
+
+/**
+ * This pass moves varying fetches (and the instructions they depend on
+ * into the start block.
+ *
+ * We need to set the (ei) "end input" flag on the last varying fetch.
+ * And we want to ensure that all threads execute the instruction that
+ * sets (ei).  The easiest way to ensure this is to move all varying
+ * fetches into the start block.  Which is something we used to get for
+ * free by using lower_all_io_to_temps=true.
+ *
+ * This may come at the cost of additional register usage.  OTOH setting
+ * the (ei) flag earlier probably frees up more VS to run.
+ */
+
+
+typedef struct {
+       nir_shader *shader;
+       nir_block *start_block;
+       struct nir_instr *cursor;
+} state;
+
+static void move_instruction_to_start_block(state *state, nir_instr *instr);
+
+static bool
+move_src(nir_src *src, void *state)
+{
+       /* At this point we shouldn't have any non-ssa src: */
+       debug_assert(src->is_ssa);
+       move_instruction_to_start_block(state, src->ssa->parent_instr);
+       return true;
+}
+
+static void
+move_instruction_to_start_block(state *state, nir_instr *instr)
+{
+       /* first move (recursively) all src's to ensure they appear before
+        * load*_input that we are trying to move:
+        */
+       nir_foreach_src(instr, move_src, state);
+
+       /* and then move the instruction itself:
+        */
+       exec_node_remove(&instr->node);
+
+       if (state->cursor) {
+               exec_node_insert_after(&state->cursor->node, &instr->node);
+       } else {
+               exec_list_push_head(&state->start_block->instr_list, &instr->node);
+       }
+
+       state->cursor = instr;
+       instr->block = state->start_block;
+}
+
+static bool
+move_varying_inputs_block(state *state, nir_block *block)
+{
+       bool progress = false;
+
+       nir_foreach_instr_safe(instr, block) {
+               if (instr->type != nir_instr_type_intrinsic)
+                       continue;
+
+               nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+
+               switch (intr->intrinsic) {
+               case nir_intrinsic_load_interpolated_input:
+               case nir_intrinsic_load_input:
+                       /* TODO any others to handle? */
+                       break;
+               default:
+                       continue;
+               }
+
+               debug_assert(intr->dest.is_ssa);
+
+               state->cursor = NULL;
+               move_instruction_to_start_block(state, instr);
+
+               progress = true;
+       }
+
+       return progress;
+}
+
+bool
+ir3_nir_move_varying_inputs(nir_shader *shader)
+{
+       bool progress = false;
+
+       debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
+
+       nir_foreach_function (function, shader) {
+               state state;
+
+               if (!function->impl)
+                       continue;
+
+               state.shader = shader;
+               state.start_block = nir_start_block(function->impl);
+
+               bool progress = false;
+               nir_foreach_block (block, function->impl) {
+                       /* don't need to move anything that is already in the first block */
+                       if (block == state.start_block)
+                               continue;
+                       progress |= move_varying_inputs_block(&state, block);
+               }
+
+               if (progress) {
+                       nir_metadata_preserve(function->impl,
+                               nir_metadata_block_index | nir_metadata_dominance);
+               }
+       }
+
+       return progress;
+}
index 7ebc0b4c853585d9c0bf346ffa40e71caf2962b7..d40c6e0e161937c3c10ca288cfa371da9f134c1f 100644 (file)
@@ -262,6 +262,11 @@ ir3_shader_from_nir(struct ir3_compiler *compiler, nir_shader *nir)
        NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
                           (nir_lower_io_options)0);
 
+       if (nir->info.stage == MESA_SHADER_FRAGMENT)
+               NIR_PASS_V(nir, ir3_nir_move_varying_inputs);
+
+       NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
+
        /* do first pass optimization, ignoring the key: */
        shader->nir = ir3_optimize_nir(shader, nir, NULL);
        if (ir3_shader_debug & IR3_DBG_DISASM) {
index b9af2d427b246a4759cdc2d77af01416b5c19099..24917facd6d028700a409473e773dbbdea9cd8c9 100644 (file)
@@ -53,6 +53,7 @@ libfreedreno_ir3_files = files(
   'ir3_nir_analyze_ubo_ranges.c',
   'ir3_nir_lower_io_offsets.c',
   'ir3_nir_lower_tg4_to_tex.c',
+  'ir3_nir_move_varying_inputs.c',
   'ir3_print.c',
   'ir3_ra.c',
   'ir3_sched.c',