nir: Remove handling of dead writes from copy_prop_vars
[mesa.git] / src / compiler / nir / nir_lower_io_to_temporaries.c
index 3153a49b7bed284b13f63fbae4fa78e043c69d20..d93e20e8d75a39e14ac1d50ff0c7cb373a1e1a3a 100644 (file)
  */
 
 #include "nir.h"
+#include "nir_builder.h"
 
 struct lower_io_state {
    nir_shader *shader;
-   nir_function *entrypoint;
+   nir_function_impl *entrypoint;
    struct exec_list old_outputs;
    struct exec_list old_inputs;
 };
 
 static void
-emit_copies(nir_cursor cursor, nir_shader *shader, struct exec_list *new_vars,
-          struct exec_list *old_vars)
+emit_copies(nir_builder *b, struct exec_list *dest_vars,
+            struct exec_list *src_vars)
 {
-   assert(exec_list_length(new_vars) == exec_list_length(old_vars));
+   assert(exec_list_length(dest_vars) == exec_list_length(src_vars));
 
-   foreach_two_lists(new_node, new_vars, old_node, old_vars) {
-      nir_variable *newv = exec_node_data(nir_variable, new_node, node);
-      nir_variable *temp = exec_node_data(nir_variable, old_node, node);
+   foreach_two_lists(dest_node, dest_vars, src_node, src_vars) {
+      nir_variable *dest = exec_node_data(nir_variable, dest_node, node);
+      nir_variable *src = exec_node_data(nir_variable, src_node, node);
 
-      nir_intrinsic_instr *copy =
-         nir_intrinsic_instr_create(shader, nir_intrinsic_copy_var);
-      copy->variables[0] = nir_deref_var_create(copy, newv);
-      copy->variables[1] = nir_deref_var_create(copy, temp);
+      /* No need to copy the contents of a non-fb_fetch_output output variable
+       * to the temporary allocated for it, since its initial value is
+       * undefined.
+       */
+      if (src->data.mode == nir_var_shader_out &&
+          !src->data.fb_fetch_output)
+         continue;
+
+      /* Can't copy the contents of the temporary back to a read-only
+       * interface variable.  The value of the temporary won't have been
+       * modified by the shader anyway.
+       */
+      if (dest->data.read_only)
+         continue;
 
-      nir_instr_insert(cursor, &copy->instr);
+      nir_copy_var(b, dest, src);
    }
 }
 
 static void
 emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
 {
-   if (state->shader->stage == MESA_SHADER_GEOMETRY) {
+   nir_builder b;
+   nir_builder_init(&b, impl);
+
+   if (state->shader->info.stage == MESA_SHADER_GEOMETRY) {
       /* For geometry shaders, we have to emit the output copies right
        * before each EmitVertex call.
        */
@@ -72,22 +86,23 @@ emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
 
             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
             if (intrin->intrinsic == nir_intrinsic_emit_vertex) {
-               nir_cursor cursor = nir_before_instr(&intrin->instr);
-               emit_copies(cursor, state->shader, &state->shader->outputs,
-                           &state->old_outputs);
+               b.cursor = nir_before_instr(&intrin->instr);
+               emit_copies(&b, &state->shader->outputs, &state->old_outputs);
             }
          }
       }
-   } else if (impl->function == state->entrypoint) {
+   } else if (impl == state->entrypoint) {
+      b.cursor = nir_before_block(nir_start_block(impl));
+      emit_copies(&b, &state->old_outputs, &state->shader->outputs);
+
       /* For all other shader types, we need to do the copies right before
        * the jumps to the end block.
        */
       struct set_entry *block_entry;
       set_foreach(impl->end_block->predecessors, block_entry) {
          struct nir_block *block = (void *)block_entry->key;
-         nir_cursor cursor = nir_after_block_before_jump(block);
-         emit_copies(cursor, state->shader, &state->shader->outputs,
-                     &state->old_outputs);
+         b.cursor = nir_after_block_before_jump(block);
+         emit_copies(&b, &state->shader->outputs, &state->old_outputs);
       }
    }
 }
@@ -95,10 +110,11 @@ emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
 static void
 emit_input_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
 {
-   if (impl->function == state->entrypoint) {
-      nir_cursor cursor = nir_before_block(nir_start_block(impl));
-      emit_copies(cursor, state->shader, &state->old_inputs,
-                  &state->shader->inputs);
+   if (impl == state->entrypoint) {
+      nir_builder b;
+      nir_builder_init(&b, impl);
+      b.cursor = nir_before_block(nir_start_block(impl));
+      emit_copies(&b, &state->old_inputs, &state->shader->inputs);
    }
 }
 
@@ -114,25 +130,26 @@ create_shadow_temp(struct lower_io_state *state, nir_variable *var)
    /* Reparent the name to the new variable */
    ralloc_steal(nvar, nvar->name);
 
-   /* Reparent the constant initializer (if any) */
-   ralloc_steal(nvar, nvar->constant_initializer);
+   assert(nvar->constant_initializer == NULL);
 
    /* Give the original a new name with @<mode>-temp appended */
    const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";
    temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
    temp->data.mode = nir_var_global;
-   temp->constant_initializer = NULL;
+   temp->data.read_only = false;
+   temp->data.fb_fetch_output = false;
+   temp->data.compact = false;
 
    return nvar;
 }
 
 void
-nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,
+nir_lower_io_to_temporaries(nir_shader *shader, nir_function_impl *entrypoint,
                             bool outputs, bool inputs)
 {
    struct lower_io_state state;
 
-   if (shader->stage == MESA_SHADER_TESS_CTRL)
+   if (shader->info.stage == MESA_SHADER_TESS_CTRL)
       return;
 
    state.shader = shader;
@@ -178,4 +195,6 @@ nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,
 
    exec_list_append(&shader->globals, &state.old_inputs);
    exec_list_append(&shader->globals, &state.old_outputs);
+
+   nir_fixup_deref_modes(shader);
 }