lima/ppir: duplicate consts in nir
authorErico Nunes <nunes.erico@gmail.com>
Mon, 13 Apr 2020 13:32:01 +0000 (15:32 +0200)
committerMarge Bot <eric+marge@anholt.net>
Sat, 9 May 2020 11:30:07 +0000 (11:30 +0000)
Move the duplicate consts step to a nir pass.
This makes the nir representation closer to what ppir will have in the
result.
Additionally, it handles the case where a const is used multiple times
by a single node (which can happen in instructions like fcsel). The new
implementation will only emit a single load const for that case.

Signed-off-by: Erico Nunes <nunes.erico@gmail.com>
Reviewed-by: Vasily Khoruzhick <anarsoul@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4535>

src/gallium/drivers/lima/Android.mk
src/gallium/drivers/lima/ir/lima_ir.h
src/gallium/drivers/lima/ir/lima_nir_duplicate_consts.c [new file with mode: 0644]
src/gallium/drivers/lima/ir/pp/nir.c
src/gallium/drivers/lima/lima_program.c
src/gallium/drivers/lima/meson.build

index e0a4eda56eb5bfaf935c1544686f1dbf9558ae9b..9973862d640190642ef9a1f3cc4237a9b3e43f00 100644 (file)
@@ -36,6 +36,7 @@ LOCAL_SRC_FILES := \
        ir/gp/reduce_scheduler.c \
        ir/gp/scheduler.c \
        ir/lima_ir.h \
        ir/gp/reduce_scheduler.c \
        ir/gp/scheduler.c \
        ir/lima_ir.h \
+       ir/lima_nir_duplicate_consts.c \
        ir/lima_nir_duplicate_intrinsic.c \
        ir/lima_nir_lower_uniform_to_scalar.c \
        ir/lima_nir_split_load_input.c \
        ir/lima_nir_duplicate_intrinsic.c \
        ir/lima_nir_lower_uniform_to_scalar.c \
        ir/lima_nir_split_load_input.c \
index d84bf96927677d74272040057a1e18eb714d4d2e..2817e7d563951d2341475f2fd5bb043709231fd0 100644 (file)
@@ -68,6 +68,7 @@ bool lima_nir_scale_trig(nir_shader *shader);
 bool lima_nir_lower_ftrunc(nir_shader *shader);
 bool lima_nir_split_load_input(nir_shader *shader);
 
 bool lima_nir_lower_ftrunc(nir_shader *shader);
 bool lima_nir_split_load_input(nir_shader *shader);
 
+void lima_nir_duplicate_load_consts(nir_shader *shader);
 void lima_nir_duplicate_load_inputs(nir_shader *shader);
 void lima_nir_duplicate_load_uniforms(nir_shader *shader);
 
 void lima_nir_duplicate_load_inputs(nir_shader *shader);
 void lima_nir_duplicate_load_uniforms(nir_shader *shader);
 
diff --git a/src/gallium/drivers/lima/ir/lima_nir_duplicate_consts.c b/src/gallium/drivers/lima/ir/lima_nir_duplicate_consts.c
new file mode 100644 (file)
index 0000000..0eeab6b
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2020 Lima Project
+ *
+ * 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 "nir.h"
+#include "nir_builder.h"
+#include "lima_ir.h"
+
+static bool
+lima_nir_duplicate_load_const(nir_builder *b, nir_load_const_instr *load)
+{
+   nir_load_const_instr *last_dupl = NULL;
+   nir_instr *last_parent_instr = NULL;
+
+   nir_foreach_use_safe(use_src, &load->def) {
+      nir_load_const_instr *dupl;
+
+      if (last_parent_instr != use_src->parent_instr) {
+         /* if ssa use, clone for the target block */
+         b->cursor = nir_before_instr(use_src->parent_instr);
+
+         dupl = nir_load_const_instr_create(b->shader, load->def.num_components,
+                                            load->def.bit_size);
+         memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);
+
+         dupl->instr.pass_flags = 1;
+         nir_builder_instr_insert(b, &dupl->instr);
+      }
+      else {
+         dupl = last_dupl;
+      }
+
+      nir_instr_rewrite_src(use_src->parent_instr, use_src, nir_src_for_ssa(&dupl->def));
+      last_parent_instr = use_src->parent_instr;
+      last_dupl = dupl;
+   }
+
+   last_dupl = NULL;
+   last_parent_instr = NULL;
+
+   nir_foreach_if_use_safe(use_src, &load->def) {
+      nir_load_const_instr *dupl;
+
+      if (last_parent_instr != use_src->parent_instr) {
+         /* if 'if use', clone where it is */
+         b->cursor = nir_before_instr(&load->instr);
+
+         dupl = nir_load_const_instr_create(b->shader, load->def.num_components,
+                                            load->def.bit_size);
+         memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);
+
+         dupl->instr.pass_flags = 1;
+         nir_builder_instr_insert(b, &dupl->instr);
+      }
+      else {
+         dupl = last_dupl;
+      }
+
+      nir_if_rewrite_condition(use_src->parent_if, nir_src_for_ssa(&dupl->def));
+      last_parent_instr = use_src->parent_instr;
+      last_dupl = dupl;
+   }
+
+   nir_instr_remove(&load->instr);
+   return true;
+}
+
+static void
+lima_nir_duplicate_load_consts_impl(nir_shader *shader, nir_function_impl *impl)
+{
+   nir_builder builder;
+   nir_builder_init(&builder, impl);
+
+   nir_foreach_block(block, impl) {
+      nir_foreach_instr(instr, block) {
+         instr->pass_flags = 0;
+      }
+
+      nir_foreach_instr_safe(instr, block) {
+         if (instr->type != nir_instr_type_load_const)
+            continue;
+
+         nir_load_const_instr *load = nir_instr_as_load_const(instr);
+
+         if (load->instr.pass_flags)
+            continue;
+
+         lima_nir_duplicate_load_const(&builder, load);
+      }
+   }
+
+   nir_metadata_preserve(impl, nir_metadata_block_index |
+                               nir_metadata_dominance);
+}
+
+/* Duplicate load consts for every user.
+ * Helps by utilizing the load const instruction slots that would
+ * otherwise stay empty, and reduces register pressure. */
+void
+lima_nir_duplicate_load_consts(nir_shader *shader)
+{
+   nir_foreach_function(function, shader) {
+      if (function->impl) {
+         lima_nir_duplicate_load_consts_impl(shader, function->impl);
+      }
+   }
+}
index 91f4be151b3c31f99fd39bff61b3f4de63dd73b0..5175888d2473908d4acdf56075a089a88f6f5a2f 100644 (file)
@@ -99,11 +99,7 @@ static void ppir_node_add_src(ppir_compiler *comp, ppir_node *node,
 
    if (ns->is_ssa) {
       child = comp->var_nodes[ns->ssa->index];
 
    if (ns->is_ssa) {
       child = comp->var_nodes[ns->ssa->index];
-      /* Clone consts for each successor */
       switch (child->op) {
       switch (child->op) {
-      case ppir_op_const:
-         child = ppir_node_clone(node->block, child);
-         break;
       case ppir_op_load_varying:
          /* If at least one successor is load_texture, promote it to
           * load_coords to ensure that is has exactly one successor */
       case ppir_op_load_varying:
          /* If at least one successor is load_texture, promote it to
           * load_coords to ensure that is has exactly one successor */
index bd7c40f7f6e59afba2fdc520281694fba853988b..75e74e23bc36c986aa147df8dfa8530e6c3d9cc8 100644 (file)
@@ -250,6 +250,7 @@ lima_program_optimize_fs_nir(struct nir_shader *s,
 
    NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
    NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
 
    NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
    NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
+   NIR_PASS_V(s, lima_nir_duplicate_load_consts);
 
    nir_sweep(s);
 }
 
    nir_sweep(s);
 }
index aae30917bad40194ec2f0767d1fddf0990ab37b0..1b1affb0b24a985ceb5e26e04b560191d60a94ea 100644 (file)
@@ -46,6 +46,7 @@ files_lima = files(
   'ir/pp/node_to_instr.c',
   'ir/pp/disasm.c',
 
   'ir/pp/node_to_instr.c',
   'ir/pp/disasm.c',
 
+  'ir/lima_nir_duplicate_consts.c',
   'ir/lima_nir_duplicate_intrinsic.c',
   'ir/lima_nir_lower_uniform_to_scalar.c',
   'ir/lima_nir_split_load_input.c',
   'ir/lima_nir_duplicate_intrinsic.c',
   'ir/lima_nir_lower_uniform_to_scalar.c',
   'ir/lima_nir_split_load_input.c',