nir/deref: Layer rematerialization helpers
authorKonstantin Seurer <konstantin.seurer@gmail.com>
Mon, 18 Sep 2023 16:32:07 +0000 (18:32 +0200)
committerMarge Bot <emma+marge@anholt.net>
Fri, 22 Sep 2023 10:05:58 +0000 (10:05 +0000)
nir_rematerialize_derefs_in_use_blocks_impl can be implemented on top of
nir_rematerialize_deref_in_use_blocks.

Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23712>

src/compiler/nir/nir_deref.c

index 5dfb06e..902e7a2 100644 (file)
@@ -863,25 +863,13 @@ nir_rematerialize_deref_in_use_blocks(nir_deref_instr *instr)
 bool
 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
 {
-   struct rematerialize_deref_state state = { 0 };
-   state.builder = nir_builder_create(impl);
-
+   bool progress = false;
    nir_foreach_block_unstructured(block, impl) {
-      state.block = block;
-
       nir_foreach_instr_safe(instr, block) {
-         if (instr->type == nir_instr_type_deref &&
-             nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
-            continue;
-
-         /* If a deref is used in a phi, we can't rematerialize it, as the new
-          * derefs would appear before the phi, which is not valid.
-          */
-         if (instr->type == nir_instr_type_phi)
-            continue;
-
-         state.builder.cursor = nir_before_instr(instr);
-         nir_foreach_src(instr, rematerialize_deref_src, &state);
+         if (instr->type == nir_instr_type_deref) {
+            nir_deref_instr *deref = nir_instr_as_deref(instr);
+            progress |= nir_rematerialize_deref_in_use_blocks(deref);
+         }
       }
 
 #ifndef NDEBUG
@@ -891,7 +879,7 @@ nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
 #endif
    }
 
-   return state.progress;
+   return progress;
 }
 
 static void