From c4f2297f007202ce868b7f5297c7418fe88ad9bb Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Wed, 24 Jun 2020 13:44:08 -0700 Subject: [PATCH] spirv: Recognize zero initializers in Workgroup variables This will be used to implement VK_KHR_zero_initialize_workgroup_memory. Reviewed-by: Bas Nieuwenhuizen Reviewed-by: Jason Ekstrand Part-of: --- src/compiler/shader_info.h | 2 ++ src/compiler/spirv/spirv_to_nir.c | 1 + src/compiler/spirv/vtn_private.h | 3 +++ src/compiler/spirv/vtn_variables.c | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/src/compiler/shader_info.h b/src/compiler/shader_info.h index 6bec004c743..7a73dcc8c19 100644 --- a/src/compiler/shader_info.h +++ b/src/compiler/shader_info.h @@ -368,6 +368,8 @@ typedef struct shader_info { */ enum gl_derivative_group derivative_group:2; + bool zero_initialize_shared_memory; + /** * Size of shared variables accessed by the compute shader. */ diff --git a/src/compiler/spirv/spirv_to_nir.c b/src/compiler/spirv/spirv_to_nir.c index 1b910fd1c16..2960acc7f71 100644 --- a/src/compiler/spirv/spirv_to_nir.c +++ b/src/compiler/spirv/spirv_to_nir.c @@ -2167,6 +2167,7 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode, case SpvOpConstantNull: val->constant = vtn_null_constant(b, val->type); + val->is_null_constant = true; break; default: diff --git a/src/compiler/spirv/vtn_private.h b/src/compiler/spirv/vtn_private.h index 710efd00ecb..7a7295b0363 100644 --- a/src/compiler/spirv/vtn_private.h +++ b/src/compiler/spirv/vtn_private.h @@ -603,6 +603,9 @@ struct vtn_value { * the existence of a NonUniform decoration on this value.*/ uint32_t propagated_non_uniform : 1; + /* Valid for vtn_value_type_constant to indicate the value is OpConstantNull. */ + bool is_null_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 26e715bb3f7..6a72fbdae3e 100644 --- a/src/compiler/spirv/vtn_variables.c +++ b/src/compiler/spirv/vtn_variables.c @@ -1977,6 +1977,26 @@ vtn_create_variable(struct vtn_builder *b, struct vtn_value *val, } if (initializer) { + switch (var->mode) { + case vtn_variable_mode_workgroup: + /* VK_KHR_zero_initialize_workgroup_memory. */ + vtn_fail_if(b->options->environment != NIR_SPIRV_VULKAN, + "Only Vulkan supports variable initializer " + "for Workgroup variable %u", + vtn_id_for_value(b, val)); + vtn_fail_if(initializer->value_type != vtn_value_type_constant || + !initializer->is_null_constant, + "Workgroup variable %u can only have OpConstantNull " + "as initializer, but have %u instead", + vtn_id_for_value(b, val), + vtn_id_for_value(b, initializer)); + b->shader->info.cs.zero_initialize_shared_memory = true; + break; + default: + /* Nothing to check. */ + break; + } + switch (initializer->value_type) { case vtn_value_type_constant: var->var->constant_initializer =