zink: remove discard_if
authorErik Faye-Lund <erik.faye-lund@collabora.com>
Thu, 1 Nov 2018 20:44:09 +0000 (21:44 +0100)
committerErik Faye-Lund <erik.faye-lund@collabora.com>
Mon, 28 Oct 2019 08:51:43 +0000 (08:51 +0000)
Acked-by: Jordan Justen <jordan.l.justen@intel.com>
src/gallium/drivers/zink/zink_compiler.c

index 520ac30ee93b58fd1e00a35141f02b8e9f6cffbc..e243166e23d5963aa37b9727ab2f4c40cfddf52b 100644 (file)
@@ -167,6 +167,51 @@ position_to_vulkan(nir_shader *s)
    }
 }
 
+static bool
+lower_discard_if_instr(nir_intrinsic_instr *instr, nir_builder *b)
+{
+   if (instr->intrinsic == nir_intrinsic_discard_if) {
+      b->cursor = nir_before_instr(&instr->instr);
+
+      nir_if *if_stmt = nir_push_if(b, nir_ssa_for_src(b, instr->src[0], 1));
+      nir_intrinsic_instr *discard =
+         nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
+      nir_builder_instr_insert(b, &discard->instr);
+      nir_pop_if(b, if_stmt);
+      nir_instr_remove(&instr->instr);
+      return true;
+   }
+   assert(instr->intrinsic != nir_intrinsic_discard ||
+          nir_block_last_instr(instr->instr.block) == &instr->instr);
+
+   return false;
+}
+
+static bool
+lower_discard_if(nir_shader *shader)
+{
+   bool progress = false;
+
+   nir_foreach_function(function, shader) {
+      if (function->impl) {
+         nir_builder builder;
+         nir_builder_init(&builder, function->impl);
+         nir_foreach_block(block, function->impl) {
+            nir_foreach_instr_safe(instr, block) {
+               if (instr->type == nir_instr_type_intrinsic)
+                  progress |= lower_discard_if_instr(
+                                                  nir_instr_as_intrinsic(instr),
+                                                  &builder);
+            }
+         }
+
+         nir_metadata_preserve(function->impl, nir_metadata_dominance);
+      }
+   }
+
+   return progress;
+}
+
 static const struct nir_shader_compiler_options nir_options = {
    .lower_all_io_to_temps = true,
    .lower_ffma = true,
@@ -257,6 +302,7 @@ zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
    NIR_PASS_V(nir, nir_lower_bool_to_float);
    optimize_nir(nir);
    NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
+   NIR_PASS_V(nir, lower_discard_if);
 
    if (zink_debug & ZINK_DEBUG_NIR) {
       fprintf(stderr, "NIR shader:\n---8<---\n");