Module: Mesa
Branch: staging/23.3
Commit: 0104e87a083473c9772d0678bda606f10f5b6b3f
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=0104e87a083473c9772d0678bda606f10f5b6b3f

Author: Konstantin Seurer <[email protected]>
Date:   Sat Nov 18 18:18:47 2023 +0100

nir/lower_vars_to_scratch: Remove all unused derefs

If the shader passed to nir_lower_vars_to_scratch contains some unused
derefs to a variable that will be lowered, validation will fail because
the variable is not part of the shader after the pass.

cc: mesa-stable

Reviewed-by: Rhys Perry <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26271>
(cherry picked from commit 827b0fa1efd734f76364ac5cd72ae1537062eb71)

---

 .pick_status.json                    |  2 +-
 src/compiler/nir/nir_lower_scratch.c | 11 +++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/.pick_status.json b/.pick_status.json
index 0e5dc68ac6f..2f716d8513a 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -1404,7 +1404,7 @@
         "description": "nir/lower_vars_to_scratch: Remove all unused derefs",
         "nominated": true,
         "nomination_type": 0,
-        "resolution": 0,
+        "resolution": 1,
         "main_sha": null,
         "because_sha": null,
         "notes": null
diff --git a/src/compiler/nir/nir_lower_scratch.c 
b/src/compiler/nir/nir_lower_scratch.c
index 55e58688171..98bb30ee8cb 100644
--- a/src/compiler/nir/nir_lower_scratch.c
+++ b/src/compiler/nir/nir_lower_scratch.c
@@ -145,13 +145,21 @@ nir_lower_vars_to_scratch(nir_shader *shader,
       return false;
    }
 
+   bool progress = false;
+
    nir_foreach_function_impl(impl, shader) {
       nir_foreach_block(block, impl) {
-         nir_foreach_instr(instr, block) {
+         nir_foreach_instr_safe(instr, block) {
             if (instr->type != nir_instr_type_deref)
                continue;
 
             nir_deref_instr *deref = nir_instr_as_deref(instr);
+
+            if (nir_deref_instr_remove_if_unused(deref)) {
+               progress = true;
+               continue;
+            }
+
             if (deref->deref_type != nir_deref_type_var)
                continue;
 
@@ -178,7 +186,6 @@ nir_lower_vars_to_scratch(nir_shader *shader,
       var->data.location = INT_MAX;
    }
 
-   bool progress = false;
    nir_foreach_function_impl(impl, shader) {
       nir_builder build = nir_builder_create(impl);
 

Reply via email to