Module: Mesa
Branch: main
Commit: f349c8ab4b3822285ee78af01303af9bcc6b6a3d
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=f349c8ab4b3822285ee78af01303af9bcc6b6a3d

Author: Lionel Landwerlin <[email protected]>
Date:   Mon Sep 27 18:00:03 2021 +0300

spirv: don't bother initializing variables to Undef

If an OpVariable's initializer is undef, there is no need to
initialize the variable.

v2: Comment the code (Caio)

Signed-off-by: Lionel Landwerlin <[email protected]>
Reviewed-by: Caio Marcelo de Oliveira Filho <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13030>

---

 src/compiler/spirv/spirv_to_nir.c  | 3 +++
 src/compiler/spirv/vtn_private.h   | 3 +++
 src/compiler/spirv/vtn_variables.c | 6 +++++-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/compiler/spirv/spirv_to_nir.c 
b/src/compiler/spirv/spirv_to_nir.c
index d2f8c3af898..fd372cd4b95 100644
--- a/src/compiler/spirv/spirv_to_nir.c
+++ b/src/compiler/spirv/spirv_to_nir.c
@@ -1964,11 +1964,14 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
                   spirv_op_to_string(opcode), elem_count, val->type->length);
 
       nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
+      val->is_undef_constant = true;
       for (unsigned i = 0; i < elem_count; i++) {
          struct vtn_value *elem_val = vtn_untyped_value(b, w[i + 3]);
 
          if (elem_val->value_type == vtn_value_type_constant) {
             elems[i] = elem_val->constant;
+            val->is_undef_constant = val->is_undef_constant &&
+                                     elem_val->is_undef_constant;
          } else {
             vtn_fail_if(elem_val->value_type != vtn_value_type_undef,
                         "only constants or undefs allowed for "
diff --git a/src/compiler/spirv/vtn_private.h b/src/compiler/spirv/vtn_private.h
index 0b2beda92d4..0ddf34e4d8f 100644
--- a/src/compiler/spirv/vtn_private.h
+++ b/src/compiler/spirv/vtn_private.h
@@ -604,6 +604,9 @@ struct vtn_value {
    /* Valid for vtn_value_type_constant to indicate the value is 
OpConstantNull. */
    bool is_null_constant:1;
 
+   /* Valid when all the members of the value are undef. */
+   bool is_undef_constant:1;
+
    const char *name;
    struct vtn_decoration *decoration;
    struct vtn_type *type;
diff --git a/src/compiler/spirv/vtn_variables.c 
b/src/compiler/spirv/vtn_variables.c
index e67bee4d7fa..e93e05f663d 100644
--- a/src/compiler/spirv/vtn_variables.c
+++ b/src/compiler/spirv/vtn_variables.c
@@ -2021,7 +2021,10 @@ vtn_create_variable(struct vtn_builder *b, struct 
vtn_value *val,
        storage_class == SpvStorageClassWorkgroup)
       initializer = NULL;
 
-   if (initializer) {
+   /* Only initialize variable when there is an initializer and it's not
+    * undef.
+    */
+   if (initializer && !initializer->is_undef_constant) {
       switch (storage_class) {
       case SpvStorageClassWorkgroup:
          /* VK_KHR_zero_initialize_workgroup_memory. */
@@ -2334,6 +2337,7 @@ vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
    case SpvOpUndef: {
       struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
       val->type = vtn_get_type(b, w[1]);
+      val->is_undef_constant = true;
       break;
    }
 

Reply via email to