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);
+         }
       }
    }
 
 
       }
    }
 
+   if (progress) {
+      nir_metadata_preserve(impl, nir_metadata_block_index |
+                                  nir_metadata_dominance);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
+   }
+
    return progress;
 }
 
 
       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;
 }
 
    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;
 }
 
       }
    }
 
+   if (progress) {
+      nir_metadata_preserve(impl, nir_metadata_block_index |
+                                  nir_metadata_dominance);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
+   }
+
    return progress;
 }
 
 
    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);
          }
       }
    }
 
    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) {
    }
 
    if (shader->constant_data_size == 0) {
+      nir_shader_preserve_all_metadata(shader);
       ralloc_free(var_infos);
       return false;
    }
 
    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;
 }
 
       }
    }
 
-   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);
       }
    }
 
 
     * 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);
 
             }
          }
       }
+
+      nir_metadata_preserve(function->impl,
+                            nir_metadata_block_index |
+                            nir_metadata_dominance);
    }
 
    ralloc_free(dead_ctx);
 
    /* 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;
    }
 
    }
    if (!has_vars_to_shrink) {
       ralloc_free(mem_ctx);
+      nir_shader_preserve_all_metadata(shader);
       return false;
    }