nir: Properly preserve metadata in more cases
authorJason Ekstrand <jason@jlekstrand.net>
Fri, 22 May 2020 03:34:37 +0000 (22:34 -0500)
committerMarge Bot <eric+marge@anholt.net>
Thu, 11 Jun 2020 05:08:12 +0000 (05:08 +0000)
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5171>

src/compiler/glsl/gl_nir_lower_images.c
src/compiler/glsl/gl_nir_lower_samplers_as_deref.c
src/compiler/nir/nir_lower_clip_cull_distance_arrays.c
src/compiler/nir/nir_lower_samplers.c
src/compiler/nir/nir_opt_conditional_discard.c
src/compiler/nir/nir_opt_large_constants.c
src/compiler/nir/nir_opt_loop_unroll.c
src/compiler/nir/nir_remove_dead_variables.c
src/compiler/nir/nir_search.c
src/compiler/nir/nir_split_per_member_structs.c
src/compiler/nir/nir_split_vars.c

index 7dbefc36d0fa834f3d2102451a4493872fecb34c..5b51c3aa03cc6fdbf5f00678ab8022692c6fa5a4 100644 (file)
@@ -107,9 +107,19 @@ gl_nir_lower_images(nir_shader *shader, bool bindless_only)
          nir_builder b;
          nir_builder_init(&b, function->impl);
 
+         bool impl_progress = false;
          nir_foreach_block(block, function->impl)
             nir_foreach_instr(instr, block)
-               progress |= lower_impl(&b, instr, bindless_only);
+               impl_progress |= lower_impl(&b, instr, bindless_only);
+
+         if (impl_progress) {
+            nir_metadata_preserve(function->impl,
+                                  nir_metadata_block_index |
+                                  nir_metadata_dominance);
+            progress = true;
+         } else {
+            nir_metadata_preserve(function->impl, nir_metadata_all);
+         }
       }
    }
 
index 49a06cfabb615f7238d48e1ce6ee055bc0ebc365..384f4a4d28456d76c8e60b822946fb4ad2256c9b 100644 (file)
@@ -331,6 +331,13 @@ lower_impl(nir_function_impl *impl, struct lower_samplers_as_deref_state *state)
       }
    }
 
+   if (progress) {
+      nir_metadata_preserve(impl, nir_metadata_block_index |
+                                  nir_metadata_dominance);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
+   }
+
    return progress;
 }
 
index 7587bb26e094c52371fe0071b1700b6876ada511..390eded7b01a4a5610f734f2709203baddab5477 100644 (file)
@@ -116,16 +116,6 @@ combine_clip_cull(nir_shader *nir,
       cull->data.location_frac = clip_array_size % 4;
    }
 
-   nir_foreach_function(function, nir) {
-      if (function->impl) {
-         nir_metadata_preserve(function->impl,
-                               nir_metadata_block_index |
-                               nir_metadata_dominance |
-                               nir_metadata_live_ssa_defs |
-                               nir_metadata_loop_analysis);
-      }
-   }
-
    return true;
 }
 
@@ -140,5 +130,20 @@ nir_lower_clip_cull_distance_arrays(nir_shader *nir)
    if (nir->info.stage > MESA_SHADER_VERTEX)
       progress |= combine_clip_cull(nir, &nir->inputs, false);
 
+   nir_foreach_function(function, nir) {
+      if (!function->impl)
+         continue;
+
+      if (progress) {
+         nir_metadata_preserve(function->impl,
+                               nir_metadata_block_index |
+                               nir_metadata_dominance |
+                               nir_metadata_live_ssa_defs |
+                               nir_metadata_loop_analysis);
+      } else {
+         nir_metadata_preserve(function->impl, nir_metadata_all);
+      }
+   }
+
    return progress;
 }
index b60e6aa63b7537a774b11877e4f03e30079c7e21..3eb69bcd5de0ee27310d6bdb87e01ddb3516386b 100644 (file)
@@ -133,6 +133,13 @@ lower_impl(nir_function_impl *impl)
       }
    }
 
+   if (progress) {
+      nir_metadata_preserve(impl, nir_metadata_block_index |
+                                  nir_metadata_dominance);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
+   }
+
    return progress;
 }
 
index 781438df06ea63945097bd540b58d1b82a44de82..3d5c255ee73a3bbf2651fa3673700cb63ee80343 100644 (file)
@@ -124,8 +124,18 @@ nir_opt_conditional_discard(nir_shader *shader)
    nir_foreach_function(function, shader) {
       if (function->impl) {
          nir_builder_init(&builder, function->impl);
+
+         bool impl_progress = false;
          nir_foreach_block_safe(block, function->impl) {
-            progress |= nir_opt_conditional_discard_block(&builder, block);
+            if (nir_opt_conditional_discard_block(&builder, block))
+               impl_progress = true;
+         }
+
+         if (impl_progress) {
+            nir_metadata_preserve(function->impl, nir_metadata_none);
+            progress = true;
+         } else {
+            nir_metadata_preserve(function->impl, nir_metadata_all);
          }
       }
    }
index 7ed26f5112785b4eced2093b7a16dd58e462bf04..ef51c72b7b67149ce756f0b9e56a963df6314c6c 100644 (file)
@@ -179,8 +179,10 @@ nir_opt_large_constants(nir_shader *shader,
    unsigned num_locals = exec_list_length(&impl->locals);
    nir_index_vars(shader, impl, nir_var_function_temp);
 
-   if (num_locals == 0)
+   if (num_locals == 0) {
+      nir_shader_preserve_all_metadata(shader);
       return false;
+   }
 
    struct var_info *var_infos = ralloc_array(NULL, struct var_info, num_locals);
    nir_foreach_variable(var, &impl->locals) {
@@ -302,6 +304,7 @@ nir_opt_large_constants(nir_shader *shader,
    }
 
    if (shader->constant_data_size == 0) {
+      nir_shader_preserve_all_metadata(shader);
       ralloc_free(var_infos);
       return false;
    }
index f8741ea6c50646dfced53ec03eb6e4a10fbd760e..c5e7b8c9b5169e5c2962371a14166477bff661bd 100644 (file)
@@ -963,8 +963,12 @@ nir_opt_loop_unroll_impl(nir_function_impl *impl,
    progress |= process_loops_in_block(impl->function->shader, &impl->body,
                                       &has_nested_loop);
 
-   if (progress)
+   if (progress) {
+      nir_metadata_preserve(impl, nir_metadata_none);
       nir_lower_regs_to_ssa_impl(impl);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
+   }
 
    return progress;
 }
index 4fbe035c0241ea21b362c58ce99e90bbf52b6f5f..6baa66a57670d127b02cd0e3f62f40e55a735a3e 100644 (file)
@@ -213,14 +213,16 @@ nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
       }
    }
 
-   if (progress) {
-      remove_dead_var_writes(shader, live);
+   nir_foreach_function(function, shader) {
+      if (!function->impl)
+         continue;
 
-      nir_foreach_function(function, shader) {
-         if (function->impl) {
-            nir_metadata_preserve(function->impl, nir_metadata_block_index |
-                                                  nir_metadata_dominance);
-         }
+      if (progress) {
+         remove_dead_var_writes(shader, live);
+         nir_metadata_preserve(function->impl, nir_metadata_block_index |
+                                               nir_metadata_dominance);
+      } else {
+         nir_metadata_preserve(function->impl, nir_metadata_all);
       }
    }
 
index 09dd4c341e424eb14de12bfa504b4c3b890d0915..577f0be0b9278fbff69be9ada4b69ab75f541653 100644 (file)
@@ -913,8 +913,10 @@ nir_algebraic_impl(nir_function_impl *impl,
     * anything other than constants and ALU instructions.
     */
    struct util_dynarray states = {0};
-   if (!util_dynarray_resize(&states, uint16_t, impl->ssa_alloc))
+   if (!util_dynarray_resize(&states, uint16_t, impl->ssa_alloc)) {
+      nir_metadata_preserve(impl, nir_metadata_all);
       return false;
+   }
    memset(states.data, 0, states.size);
 
    struct hash_table *range_ht = _mesa_pointer_hash_table_create(NULL);
index 9148902b27ca197b65f0ed090369dea9126f44d5..478d5fe72c35d9c36b45f2a2be3b3fb2e1fd6c89 100644 (file)
@@ -202,6 +202,10 @@ nir_split_per_member_structs(nir_shader *shader)
             }
          }
       }
+
+      nir_metadata_preserve(function->impl,
+                            nir_metadata_block_index |
+                            nir_metadata_dominance);
    }
 
    ralloc_free(dead_ctx);
index 93b5e15e8df11dae51c7ea151ef19d1a8d64fd2c..9cf4fba3d6b946db4b823b4aafbd16689323547f 100644 (file)
@@ -887,6 +887,7 @@ nir_split_array_vars(nir_shader *shader, nir_variable_mode modes)
    /* If we failed to find any arrays of arrays, bail early. */
    if (!has_any_array) {
       ralloc_free(mem_ctx);
+      nir_shader_preserve_all_metadata(shader);
       return false;
    }
 
@@ -1634,6 +1635,7 @@ nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes)
    }
    if (!has_vars_to_shrink) {
       ralloc_free(mem_ctx);
+      nir_shader_preserve_all_metadata(shader);
       return false;
    }