From ea02a0c03a143d5b93cbe01123ece3ab97082a32 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Fri, 22 Jan 2021 13:48:16 +0100 Subject: [PATCH] Check entry point variables in is_hidden_variables. Need to be careful not to emit globals we're not supposed to. --- ...-in-entry-point.noeliminate.spv14.asm.comp | 11 ++++ ...-in-entry-point.noeliminate.spv14.asm.comp | 23 ++++++++ ...-in-entry-point.noeliminate.spv14.asm.comp | 7 +++ ...-in-entry-point.noeliminate.spv14.asm.comp | 59 +++++++++++++++++++ ...-in-entry-point.noeliminate.spv14.asm.comp | 59 +++++++++++++++++++ ...-in-entry-point.noeliminate.spv14.asm.comp | 59 +++++++++++++++++++ spirv_cross.cpp | 14 +++-- spirv_glsl.cpp | 5 +- spirv_hlsl.cpp | 6 +- test_shaders.py | 5 +- 10 files changed, 240 insertions(+), 8 deletions(-) create mode 100644 reference/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp create mode 100644 reference/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp create mode 100644 reference/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp create mode 100644 shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp create mode 100644 shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp create mode 100644 shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp diff --git a/reference/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp b/reference/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp new file mode 100644 index 00000000..a53efc4f --- /dev/null +++ b/reference/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp @@ -0,0 +1,11 @@ +static const uint3 gl_WorkGroupSize = uint3(64u, 1u, 1u); + +void comp_main() +{ +} + +[numthreads(64, 1, 1)] +void main() +{ + comp_main(); +} diff --git a/reference/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp b/reference/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp new file mode 100644 index 00000000..e265f1bd --- /dev/null +++ b/reference/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp @@ -0,0 +1,23 @@ +#include +#include + +using namespace metal; + +constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(64u, 1u, 1u); + +struct UBO +{ + float v; +}; + +struct SSBO +{ + float v; +}; + +kernel void main0() +{ + threadgroup float w; + float v; +} + diff --git a/reference/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp b/reference/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp new file mode 100644 index 00000000..77ea0349 --- /dev/null +++ b/reference/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp @@ -0,0 +1,7 @@ +#version 450 +layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; + +void main() +{ +} + diff --git a/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp b/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp new file mode 100644 index 00000000..73f3ceee --- /dev/null +++ b/shaders-hlsl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp @@ -0,0 +1,59 @@ +; SPIR-V +; Version: 1.5 +; Generator: Khronos Glslang Reference Front End; 10 +; Bound: 26 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + ;OpEntryPoint GLCompute %main "main" %Samp %ubo %ssbo %v %w + OpEntryPoint GLCompute %main "main" + OpExecutionMode %main LocalSize 64 1 1 + OpSource GLSL 450 + OpName %main "main" + OpName %Samp "Samp" + OpName %UBO "UBO" + OpMemberName %UBO 0 "v" + OpName %ubo "ubo" + OpName %SSBO "SSBO" + OpMemberName %SSBO 0 "v" + OpName %ssbo "ssbo" + OpName %v "v" + OpName %w "w" + OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize + OpDecorate %Samp DescriptorSet 0 + OpDecorate %Samp Binding 0 + OpMemberDecorate %UBO 0 Offset 0 + OpDecorate %UBO Block + OpDecorate %ubo DescriptorSet 0 + OpDecorate %ubo Binding 1 + OpMemberDecorate %SSBO 0 Offset 0 + OpDecorate %SSBO Block + OpDecorate %ssbo DescriptorSet 0 + OpDecorate %ssbo Binding 2 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %uint = OpTypeInt 32 0 + %v3uint = OpTypeVector %uint 3 + %uint_64 = OpConstant %uint 64 + %uint_1 = OpConstant %uint 1 +%gl_WorkGroupSize = OpConstantComposite %v3uint %uint_64 %uint_1 %uint_1 + %float = OpTypeFloat 32 + %12 = OpTypeImage %float 2D 0 0 0 1 Unknown + %13 = OpTypeSampledImage %12 +%_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13 + %Samp = OpVariable %_ptr_UniformConstant_13 UniformConstant + %UBO = OpTypeStruct %float +%_ptr_Uniform_UBO = OpTypePointer Uniform %UBO + %ubo = OpVariable %_ptr_Uniform_UBO Uniform + %SSBO = OpTypeStruct %float +%_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO + %ssbo = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer +%_ptr_Private_float = OpTypePointer Private %float + %v = OpVariable %_ptr_Private_float Private +%_ptr_Workgroup_float = OpTypePointer Workgroup %float + %w = OpVariable %_ptr_Workgroup_float Workgroup + %main = OpFunction %void None %3 + %5 = OpLabel + OpReturn + OpFunctionEnd diff --git a/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp b/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp new file mode 100644 index 00000000..73f3ceee --- /dev/null +++ b/shaders-msl-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp @@ -0,0 +1,59 @@ +; SPIR-V +; Version: 1.5 +; Generator: Khronos Glslang Reference Front End; 10 +; Bound: 26 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + ;OpEntryPoint GLCompute %main "main" %Samp %ubo %ssbo %v %w + OpEntryPoint GLCompute %main "main" + OpExecutionMode %main LocalSize 64 1 1 + OpSource GLSL 450 + OpName %main "main" + OpName %Samp "Samp" + OpName %UBO "UBO" + OpMemberName %UBO 0 "v" + OpName %ubo "ubo" + OpName %SSBO "SSBO" + OpMemberName %SSBO 0 "v" + OpName %ssbo "ssbo" + OpName %v "v" + OpName %w "w" + OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize + OpDecorate %Samp DescriptorSet 0 + OpDecorate %Samp Binding 0 + OpMemberDecorate %UBO 0 Offset 0 + OpDecorate %UBO Block + OpDecorate %ubo DescriptorSet 0 + OpDecorate %ubo Binding 1 + OpMemberDecorate %SSBO 0 Offset 0 + OpDecorate %SSBO Block + OpDecorate %ssbo DescriptorSet 0 + OpDecorate %ssbo Binding 2 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %uint = OpTypeInt 32 0 + %v3uint = OpTypeVector %uint 3 + %uint_64 = OpConstant %uint 64 + %uint_1 = OpConstant %uint 1 +%gl_WorkGroupSize = OpConstantComposite %v3uint %uint_64 %uint_1 %uint_1 + %float = OpTypeFloat 32 + %12 = OpTypeImage %float 2D 0 0 0 1 Unknown + %13 = OpTypeSampledImage %12 +%_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13 + %Samp = OpVariable %_ptr_UniformConstant_13 UniformConstant + %UBO = OpTypeStruct %float +%_ptr_Uniform_UBO = OpTypePointer Uniform %UBO + %ubo = OpVariable %_ptr_Uniform_UBO Uniform + %SSBO = OpTypeStruct %float +%_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO + %ssbo = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer +%_ptr_Private_float = OpTypePointer Private %float + %v = OpVariable %_ptr_Private_float Private +%_ptr_Workgroup_float = OpTypePointer Workgroup %float + %w = OpVariable %_ptr_Workgroup_float Workgroup + %main = OpFunction %void None %3 + %5 = OpLabel + OpReturn + OpFunctionEnd diff --git a/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp b/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp new file mode 100644 index 00000000..73f3ceee --- /dev/null +++ b/shaders-no-opt/asm/comp/eliminate-globals-not-in-entry-point.noeliminate.spv14.asm.comp @@ -0,0 +1,59 @@ +; SPIR-V +; Version: 1.5 +; Generator: Khronos Glslang Reference Front End; 10 +; Bound: 26 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + ;OpEntryPoint GLCompute %main "main" %Samp %ubo %ssbo %v %w + OpEntryPoint GLCompute %main "main" + OpExecutionMode %main LocalSize 64 1 1 + OpSource GLSL 450 + OpName %main "main" + OpName %Samp "Samp" + OpName %UBO "UBO" + OpMemberName %UBO 0 "v" + OpName %ubo "ubo" + OpName %SSBO "SSBO" + OpMemberName %SSBO 0 "v" + OpName %ssbo "ssbo" + OpName %v "v" + OpName %w "w" + OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize + OpDecorate %Samp DescriptorSet 0 + OpDecorate %Samp Binding 0 + OpMemberDecorate %UBO 0 Offset 0 + OpDecorate %UBO Block + OpDecorate %ubo DescriptorSet 0 + OpDecorate %ubo Binding 1 + OpMemberDecorate %SSBO 0 Offset 0 + OpDecorate %SSBO Block + OpDecorate %ssbo DescriptorSet 0 + OpDecorate %ssbo Binding 2 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %uint = OpTypeInt 32 0 + %v3uint = OpTypeVector %uint 3 + %uint_64 = OpConstant %uint 64 + %uint_1 = OpConstant %uint 1 +%gl_WorkGroupSize = OpConstantComposite %v3uint %uint_64 %uint_1 %uint_1 + %float = OpTypeFloat 32 + %12 = OpTypeImage %float 2D 0 0 0 1 Unknown + %13 = OpTypeSampledImage %12 +%_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13 + %Samp = OpVariable %_ptr_UniformConstant_13 UniformConstant + %UBO = OpTypeStruct %float +%_ptr_Uniform_UBO = OpTypePointer Uniform %UBO + %ubo = OpVariable %_ptr_Uniform_UBO Uniform + %SSBO = OpTypeStruct %float +%_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO + %ssbo = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer +%_ptr_Private_float = OpTypePointer Private %float + %v = OpVariable %_ptr_Private_float Private +%_ptr_Workgroup_float = OpTypePointer Workgroup %float + %w = OpVariable %_ptr_Workgroup_float Workgroup + %main = OpFunction %void None %3 + %5 = OpLabel + OpReturn + OpFunctionEnd diff --git a/spirv_cross.cpp b/spirv_cross.cpp index 199d0fd2..f024fa78 100644 --- a/spirv_cross.cpp +++ b/spirv_cross.cpp @@ -546,10 +546,16 @@ bool Compiler::is_hidden_variable(const SPIRVariable &var, bool include_builtins return false; } - bool hidden = false; - if (check_active_interface_variables && storage_class_is_interface(var.storage)) - hidden = active_interface_variables.find(var.self) == end(active_interface_variables); - return hidden; + // In SPIR-V 1.4 and up we must also use the active variable interface to disable global variables + // which are not part of the entry point. + if (ir.get_spirv_version() >= 0x10400 && var.storage != spv::StorageClassGeneric && + var.storage != spv::StorageClassFunction && !interface_variable_exists_in_entry_point(var.self)) + { + return true; + } + + return check_active_interface_variables && storage_class_is_interface(var.storage) && + active_interface_variables.find(var.self) == end(active_interface_variables); } bool Compiler::is_builtin_type(const SPIRType &type) const diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index f86790c8..ca8267ec 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -542,7 +542,7 @@ void CompilerGLSL::ray_tracing_khr_fixup_locations() if (var.storage != StorageClassRayPayloadKHR && var.storage != StorageClassCallableDataKHR && var.storage != StorageClassIncomingRayPayloadKHR && var.storage != StorageClassIncomingCallableDataKHR) return; - if (!interface_variable_exists_in_entry_point(var.self)) + if (is_hidden_variable(var)) return; set_decoration(var.self, DecorationLocation, location++); }); @@ -3453,6 +3453,9 @@ void CompilerGLSL::emit_resources() for (auto global : global_variables) { auto &var = get(global); + if (is_hidden_variable(var, true)) + continue; + if (var.storage != StorageClassOutput) { if (!variable_is_lut(var)) diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp index c080ff66..c74fb5f6 100644 --- a/spirv_hlsl.cpp +++ b/spirv_hlsl.cpp @@ -1362,7 +1362,8 @@ void CompilerHLSL::emit_resources() } if (var.storage != StorageClassFunction && !is_builtin_variable(var) && !var.remapped_variable && - type.pointer && (type.storage == StorageClassUniformConstant || type.storage == StorageClassAtomicCounter)) + type.pointer && (type.storage == StorageClassUniformConstant || type.storage == StorageClassAtomicCounter) && + !is_hidden_variable(var)) { emit_uniform(var); emitted = true; @@ -1516,6 +1517,9 @@ void CompilerHLSL::emit_resources() for (auto global : global_variables) { auto &var = get(global); + if (is_hidden_variable(var, true)) + continue; + if (var.storage != StorageClassOutput) { if (!variable_is_lut(var)) diff --git a/test_shaders.py b/test_shaders.py index 2643b096..f73df409 100755 --- a/test_shaders.py +++ b/test_shaders.py @@ -413,7 +413,8 @@ def cross_compile_hlsl(shader, spirv, opt, force_no_external_validation, iterati spirv_path = create_temporary() hlsl_path = create_temporary(os.path.basename(shader)) - spirv_cmd = [paths.spirv_as, '--target-env', 'vulkan1.1', '-o', spirv_path, shader] + spirv_env = 'vulkan1.1spv1.4' if '.spv14.' in shader else 'vulkan1.1' + spirv_cmd = [paths.spirv_as, '--target-env', spirv_env, '-o', spirv_path, shader] if '.preserve.' in shader: spirv_cmd.append('--preserve-numeric-ids') @@ -446,7 +447,7 @@ def cross_compile_hlsl(shader, spirv, opt, force_no_external_validation, iterati subprocess.check_call(hlsl_args) if not shader_is_invalid_spirv(hlsl_path): - subprocess.check_call([paths.spirv_val, '--scalar-block-layout', '--target-env', 'vulkan1.1', spirv_path]) + subprocess.check_call([paths.spirv_val, '--scalar-block-layout', '--target-env', spirv_env, spirv_path]) validate_shader_hlsl(hlsl_path, force_no_external_validation, paths)