spirv-val: Bad direct access to heap as POD (#6929) For https://gitlab.khronos.org/vulkan/vulkan/-/merge_requests/8631/diffs We never tested reading plain data from a heap and decided to ban it from the EXT. - `04655` only covers non-heap descriptor variables - `13706` is what handles the `Data Type` for Vulkan untyped pointers - `13745` / `13746` allow a heap to have no type or the type must matches the heap type - `13744` a heap only holds descriptors, so you can't read raw data out of it For `13743` it is just `11336` but for `OpUntypedImageTexelPointerEXT`
diff --git a/source/val/validate_explicit_layout.cpp b/source/val/validate_explicit_layout.cpp index 7d4e5d6..da75c8a 100644 --- a/source/val/validate_explicit_layout.cpp +++ b/source/val/validate_explicit_layout.cpp
@@ -567,6 +567,8 @@ case spv::Op::OpTypeBufferEXT: case spv::Op::OpTypeAccelerationStructureKHR: return vstate.options()->buffer_descriptor_layout.alignment; + case spv::Op::OpTypeTensorARM: + return vstate.options()->tensor_descriptor_layout.alignment; case spv::Op::OpTypeInt: case spv::Op::OpTypeFloat: return type_inst->GetOperandAs<uint32_t>(1u) / 8; @@ -662,6 +664,8 @@ case spv::Op::OpTypeBufferEXT: case spv::Op::OpTypeAccelerationStructureKHR: return vstate.options()->buffer_descriptor_layout.size; + case spv::Op::OpTypeTensorARM: + return vstate.options()->tensor_descriptor_layout.size; case spv::Op::OpTypeInt: case spv::Op::OpTypeFloat: return type_inst->GetOperandAs<uint32_t>(1u) / 8;
diff --git a/source/val/validate_image.cpp b/source/val/validate_image.cpp index 554442b..6aa399a 100644 --- a/source/val/validate_image.cpp +++ b/source/val/validate_image.cpp
@@ -1578,6 +1578,31 @@ "R32i, or R32ui for Vulkan environment using Op" << spvOpcodeString(inst->opcode()); } + + if (isUntyped) { + const uint32_t image_id = inst->GetOperandAs<uint32_t>(3); + const Instruction* base_variable = + _.FindUntypedBaseVariable(_.FindDef(image_id)); + if (base_variable && + (base_variable->opcode() == spv::Op::OpVariable || + base_variable->opcode() == spv::Op::OpUntypedVariableKHR)) { + const uint32_t base_id = base_variable->id(); + const bool descriptor_set = + _.HasDecoration(base_id, spv::Decoration::DescriptorSet) && + _.HasDecoration(base_id, spv::Decoration::Binding); + if (!descriptor_set && + !_.IsBuiltin(base_id, spv::BuiltIn::ResourceHeapEXT)) { + return _.diag(SPV_ERROR_INVALID_DATA, inst) + << _.VkErrorID(13743) + << "OpUntypedImageTexelPointerEXT Image <id> " + << _.getIdName(image_id) + << " is derived from the variable <id> " + << _.getIdName(base_id) + << ", which must be decorated with Binding and DescriptorSet, " + "or decorated with BuiltIn ResourceHeapEXT"; + } + } + } } return SPV_SUCCESS;
diff --git a/source/val/validate_memory.cpp b/source/val/validate_memory.cpp index 828f578..44832b4 100644 --- a/source/val/validate_memory.cpp +++ b/source/val/validate_memory.cpp
@@ -56,6 +56,76 @@ return false; } +// Returns the SamplerHeapEXT/ResourceHeapEXT variable the pointer is derived +// from, or null if it isn't derived from a heap +const Instruction* FindDescriptorHeapVariable(ValidationState_t& _, + const Instruction* pointer) { + if (!pointer || !_.HasCapability(spv::Capability::DescriptorHeapEXT)) { + return nullptr; + } + uint32_t data_type = 0; + spv::StorageClass storage_class; + if (!_.GetPointerTypeInfo(pointer->type_id(), &data_type, &storage_class) || + storage_class != spv::StorageClass::UniformConstant) { + return nullptr; + } + const Instruction* heap_variable = _.FindUntypedBaseVariable(pointer); + if (!heap_variable || + (heap_variable->opcode() != spv::Op::OpUntypedVariableKHR && + heap_variable->opcode() != spv::Op::OpVariable)) { + return nullptr; + } + if (!_.IsBuiltin(heap_variable->id(), spv::BuiltIn::SamplerHeapEXT) && + !_.IsBuiltin(heap_variable->id(), spv::BuiltIn::ResourceHeapEXT)) { + return nullptr; + } + return heap_variable; +} + +// SPV_EXT_descriptor_heap can only access the heap as a descriptor. +// It does not support reading or writing it as a POD (int, struct, etc) +// directly +// details: https://gitlab.khronos.org/vulkan/vulkan/-/merge_requests/8631 +spv_result_t ValidateDescriptorHeapAccessType(ValidationState_t& _, + const Instruction* inst, + const Instruction* pointer, + const Instruction* type, + bool is_write = false) { + if (!spvIsVulkanEnv(_.context()->target_env)) { + return SPV_SUCCESS; + } + const Instruction* heap_variable = FindDescriptorHeapVariable(_, pointer); + if (!heap_variable) { + return SPV_SUCCESS; + } + + // Each access is a single descriptor, an array of descriptors is not allowed. + // If using OpCopyMemorySized there is no type, + // so we can't prove it is reading a descriptor + if (type && _.IsDescriptorType(type->opcode())) { + return SPV_SUCCESS; + } + + const bool sampler_heap = + _.IsBuiltin(heap_variable->id(), spv::BuiltIn::SamplerHeapEXT); + + auto diag = _.diag(SPV_ERROR_INVALID_ID, inst); + diag << _.VkErrorID(13744) << "Op" << spvOpcodeString(inst->opcode()) + << (is_write ? " writes to" : " reads") << " the variable <id> " + << _.getIdName(heap_variable->id()) << " decorated with " + << (sampler_heap ? "SamplerHeapEXT" : "ResourceHeapEXT"); + if (type) { + diag << " as type <id> " << _.getIdName(type->id()); + } else { + diag << " without a type"; + } + diag << ". A descriptor heap only holds descriptors, so values accessed " + "through it must be a single descriptor type (OpTypeImage, " + "OpTypeSampler, OpTypeAccelerationStructureKHR, OpTypeTensorARM, or " + "OpTypeBufferEXT), an array of descriptors can't be accessed at once"; + return diag; +} + // Returns true if the two instructions represent structs that, as far as the // validator can tell, have the exact same data layout. bool AreLayoutCompatibleStructs(ValidationState_t& _, const Instruction* type1, @@ -718,11 +788,38 @@ // Vulkan Descriptor Set Interface: Check type of UniformConstant and // Uniform variables. if (storage_class == spv::StorageClass::UniformConstant) { - if (!IsAllowedTypeOrArrayOfSame( - _, pointee, - {spv::Op::OpTypeImage, spv::Op::OpTypeSampler, - spv::Op::OpTypeSampledImage, spv::Op::OpTypeTensorARM, - spv::Op::OpTypeAccelerationStructureKHR})) { + const bool sampler_heap = + _.IsBuiltin(inst->id(), spv::BuiltIn::SamplerHeapEXT); + const bool resource_heap = + _.IsBuiltin(inst->id(), spv::BuiltIn::ResourceHeapEXT); + if (sampler_heap || resource_heap) { + const bool allowed = + sampler_heap + ? IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeSampler}) + : IsAllowedTypeOrArrayOfSame( + _, pointee, + {spv::Op::OpTypeImage, spv::Op::OpTypeTensorARM, + spv::Op::OpTypeAccelerationStructureKHR, + spv::Op::OpTypeBufferEXT}); + if (!allowed) { + return _.diag(SPV_ERROR_INVALID_ID, inst) + << _.VkErrorID(sampler_heap ? 13746 : 13745) + << "UniformConstant OpVariable <id> " << _.getIdName(inst->id()) + << " is decorated with " + << (sampler_heap ? "SamplerHeapEXT" : "ResourceHeapEXT") + << ", so it must either be an OpUntypedVariableKHR with no " + "Data Type, or be typed as " + << (sampler_heap + ? "OpTypeSampler or an array of OpTypeSampler" + : "OpTypeImage, OpTypeAccelerationStructureKHR, " + "OpTypeTensorARM, OpTypeBufferEXT, or an array of one " + "of these types"); + } + } else if (!IsAllowedTypeOrArrayOfSame( + _, pointee, + {spv::Op::OpTypeImage, spv::Op::OpTypeSampler, + spv::Op::OpTypeSampledImage, spv::Op::OpTypeTensorARM, + spv::Op::OpTypeAccelerationStructureKHR})) { return _.diag(SPV_ERROR_INVALID_ID, inst) << _.VkErrorID(4655) << "UniformConstant OpVariable <id> " << _.getIdName(inst->id()) << " has illegal type.\n" @@ -1258,6 +1355,18 @@ return _.diag(SPV_ERROR_INVALID_ID, inst) << "Data type must be a type instruction"; } + // Untyped pointers are only permitted in storage classes with an explicit + // layout. UniformConstant only has an explicit layout if it is one of the + // heaps. + if (spvIsVulkanEnv(_.context()->target_env) && + storage_class == spv::StorageClass::UniformConstant && + !_.IsDescriptorHeapBaseVariable(inst)) { + return _.diag(SPV_ERROR_INVALID_ID, inst) + << _.VkErrorID(13706) + << "Storage class is UniformConstant, but Vulkan requires " + "that an OpUntypedVariableKHR be decorated with " + "SamplerHeapEXT or ResourceHeapEXT"; + } } else { if (storage_class == spv::StorageClass::Function || storage_class == spv::StorageClass::Private || @@ -1506,6 +1615,10 @@ } } + if (auto error = + ValidateDescriptorHeapAccessType(_, inst, pointer, result_type)) + return error; + return SPV_SUCCESS; } @@ -1807,8 +1920,21 @@ return error; } + if (auto error = + ValidateDescriptorHeapAccessType(_, inst, source, copied_type)) + return error; + if (auto error = ValidateDescriptorHeapAccessType(_, inst, target, + copied_type, true)) + return error; + if (auto error = CheckMemoryAccess(_, inst, 2)) return error; } else { + if (auto error = ValidateDescriptorHeapAccessType(_, inst, source, nullptr)) + return error; + if (auto error = + ValidateDescriptorHeapAccessType(_, inst, target, nullptr, true)) + return error; + const auto size_id = inst->GetOperandAs<uint32_t>(2); const auto size = _.FindDef(size_id); if (!size) { @@ -2488,6 +2614,20 @@ << " must be a pointer to an OpTypeStruct."; } + if (untyped) { + if (const Instruction* heap_variable = FindDescriptorHeapVariable( + state, state.FindDef(inst->GetOperandAs<uint32_t>(3)))) { + return state.diag(SPV_ERROR_INVALID_ID, inst) + << "OpUntypedArrayLengthKHR Pointer must be a storage buffer " + "descriptor, but it is derived from the variable <id> " + << state.getIdName(heap_variable->id()) << " decorated with " + << (state.IsBuiltin(heap_variable->id(), + spv::BuiltIn::SamplerHeapEXT) + ? "SamplerHeapEXT" + : "ResourceHeapEXT"); + } + } + Instruction* structure_type = nullptr; if (untyped) { structure_type = state.FindDef(inst->GetOperandAs<uint32_t>(2)); @@ -2922,9 +3062,11 @@ } // Buffer operand - auto buffer = - _.FindUntypedBaseVariable(_.FindDef(inst->GetOperandAs<uint32_t>(2))); - if (!buffer || !_.IsBuiltin(buffer->id(), spv::BuiltIn::ResourceHeapEXT)) { + const uint32_t buffer_id = inst->GetOperandAs<uint32_t>(2); + auto buffer = _.FindUntypedBaseVariable(_.FindDef(buffer_id)); + if (_.GetIdOpcode(_.GetTypeId(buffer_id)) != + spv::Op::OpTypeUntypedPointerKHR || + !buffer || !_.IsBuiltin(buffer->id(), spv::BuiltIn::ResourceHeapEXT)) { return _.diag(SPV_ERROR_INVALID_ID, inst) << "OpBufferPointerEXT's buffer must be an untyped pointer" << " into a variable declared with the ResourceHeapEXT built-in";
diff --git a/source/val/validation_state.cpp b/source/val/validation_state.cpp index d52c487..1fa51c3 100644 --- a/source/val/validation_state.cpp +++ b/source/val/validation_state.cpp
@@ -1724,6 +1724,13 @@ case spv::Op::OpUntypedArrayLengthKHR: base_inst = FindDef(base_inst->GetOperandAs<uint32_t>(3)); break; + case spv::Op::OpAccessChain: + case spv::Op::OpInBoundsAccessChain: + case spv::Op::OpPtrAccessChain: + case spv::Op::OpInBoundsPtrAccessChain: + case spv::Op::OpCopyObject: + base_inst = FindDef(base_inst->GetOperandAs<uint32_t>(2)); + break; case spv::Op::OpLoad: case spv::Op::OpAtomicLoad: if (GetIdOpcode(GetOperandTypeId(base_inst, 2)) == @@ -1783,8 +1790,10 @@ IsBuiltin(base_inst->id(), spv::BuiltIn::SamplerHeapEXT) || IsBuiltin(base_inst->id(), spv::BuiltIn::ResourceHeapEXT); - return FindDef(base_inst->id())->opcode() == spv::Op::OpBufferPointerEXT || - (FindDef(base_inst->id())->opcode() == spv::Op::OpUntypedVariableKHR && + const spv::Op base_opcode = base_inst->opcode(); + return base_opcode == spv::Op::OpBufferPointerEXT || + ((base_opcode == spv::Op::OpUntypedVariableKHR || + base_opcode == spv::Op::OpVariable) && is_heap_base); } @@ -3836,6 +3845,16 @@ return VUID_WRAP(VUID-StandaloneSpirv-Base-12469); case 12509: return VUID_WRAP(VUID-StandaloneSpirv-Offset-12509); + case 13706: + return VUID_WRAP(VUID-StandaloneSpirv-UniformConstant-13706); + case 13743: + return VUID_WRAP(VUID-StandaloneSpirv-OpUntypedImageTexelPointerEXT-13743); + case 13744: + return VUID_WRAP(VUID-StandaloneSpirv-ResourceHeapEXT-13744); + case 13745: + return VUID_WRAP(VUID-StandaloneSpirv-UniformConstant-13745); + case 13746: + return VUID_WRAP(VUID-StandaloneSpirv-UniformConstant-13746); default: return ""; // unknown id }
diff --git a/test/val/val_extension_spv_ext_descriptor_heap.cpp b/test/val/val_extension_spv_ext_descriptor_heap.cpp index f20601d..c4f3484 100644 --- a/test/val/val_extension_spv_ext_descriptor_heap.cpp +++ b/test/val/val_extension_spv_ext_descriptor_heap.cpp
@@ -2635,6 +2635,1357 @@ HasSubstr("OffsetIdEXT can only be applied to structure members")); } +TEST_F(ValidateSpvEXTDescriptorHeap, HeapVariableConcreteDataType) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %resource_heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %resource_heap BuiltIn ResourceHeapEXT + OpMemberDecorate %S 0 Offset 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f + %ptr_image = OpTypePointer UniformConstant %image + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant +%resource_heap = OpUntypedVariableKHR %ptr_uc UniformConstant %S + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13745")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("is decorated with ResourceHeapEXT, so it must either " + "be an OpUntypedVariableKHR with no Data Type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, HeapVariableDescriptorDataType) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %resource_heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %resource_heap BuiltIn ResourceHeapEXT + OpMemberDecorate %S 0 Offset 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f + %ptr_image = OpTypePointer UniformConstant %image + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant +%resource_heap = OpUntypedVariableKHR %ptr_uc UniformConstant %image + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, HeapVariableTypedOpVariable) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %resource_heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %resource_heap BuiltIn ResourceHeapEXT + OpMemberDecorate %S 0 Offset 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f + %ptr_image = OpTypePointer UniformConstant %image + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant +%resource_heap = OpVariable %ptr_image UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadImage) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_0 + %load = OpLoad %image %ac + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadThroughBufferPointer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %buffer_array %heap %uint_0 + %buf_ptr = OpBufferPointerEXT %ptr_sb %ac + %member_ptr = OpUntypedAccessChainKHR %ptr_sb %S %buf_ptr %uint_0 + %load = OpLoad %uint %member_ptr + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadData) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %S %heap %uint_0 + %load = OpLoad %uint %ac + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpLoad reads the variable <id> '2[%2]' decorated with " + "ResourceHeapEXT as type <id> '10[%uint]'")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("values accessed through it must be a single " + "descriptor type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadDataNoAccessChain) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %load = OpLoad %uint %heap + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpLoad reads the variable <id> '2[%2]' decorated with " + "ResourceHeapEXT as type <id> '10[%uint]'")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadSampler) { + // The existing heap/type correspondence check fires first. + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %sampler_array %heap %uint_0 + %load = OpLoad %sampler %ac + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-Result-11336")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapLoadSampler) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn SamplerHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %sampler_array %heap %uint_0 + %load = OpLoad %sampler %ac + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapLoadData) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn SamplerHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %S %heap %uint_0 + %load = OpLoad %uint %ac + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpLoad reads the variable <id> '2[%2]' decorated with " + "SamplerHeapEXT as type <id> '10[%uint]'")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("values accessed through it must be a single " + "descriptor type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyMemoryData) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %S %heap %uint_0 + OpCopyMemory %var_uint %ac + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT( + getDiagnosticString(), + HasSubstr("OpCopyMemory reads the variable <id> '2[%2]' decorated " + "with ResourceHeapEXT as type <id> '10[%uint]'")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyMemorySizedData) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %image = OpTypeImage %float 2D 0 0 0 2 Rgba32f +%image_array = OpTypeRuntimeArray %image + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer +%ptr_fn_uint = OpTypePointer Function %uint + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %var_uint = OpVariable %ptr_fn_uint Function + + %ac = OpUntypedAccessChainKHR %ptr_uc %S %heap %uint_0 + OpCopyMemorySized %var_uint %ac %uint_32 + + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpCopyMemorySized reads the variable <id> '2[%2]' " + "decorated with ResourceHeapEXT without a type")); +} + +std::string GenHeapTypeModule(const std::string& builtin, + const std::string& data_type, + const std::string& body) { + return R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn )" + + builtin + R"( + OpMemberDecorate %S 0 Offset 0 + OpDecorateId %image_array ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array ArrayStrideIdEXT %uint_32 + OpDecorateId %image_array4 ArrayStrideIdEXT %uint_32 + OpDecorateId %sampler_array4 ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %uint_32 = OpConstant %uint 32 + %float = OpTypeFloat 32 + %S = OpTypeStruct %uint + %S_array = OpTypeRuntimeArray %S + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui +%image_array = OpTypeRuntimeArray %image +%image_array4 = OpTypeArray %image %uint_4 + %sampler = OpTypeSampler +%sampler_array = OpTypeRuntimeArray %sampler +%sampler_array4 = OpTypeArray %sampler %uint_4 + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant +%ptr_fn_uint = OpTypePointer Function %uint + %ptr_image = OpTypeUntypedPointerKHR Image + %v2uint = OpTypeVector %uint 2 + %coord = OpConstantComposite %v2uint %uint_0 %uint_0 + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant )" + + data_type + R"( + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel +)" + body + + R"( + OpReturn + OpFunctionEnd +)"; +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapVariableConcreteDataType) { + const std::string str = GenHeapTypeModule("SamplerHeapEXT", "%uint", ""); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13746")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("is decorated with SamplerHeapEXT, so it must either " + "be an OpUntypedVariableKHR with no Data Type, or be " + "typed as OpTypeSampler or an array of OpTypeSampler")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapVariableConcreteArrayType) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "%S_array", ""); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13745")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapVariableSamplerArrayDataType) { + const std::string str = + GenHeapTypeModule("SamplerHeapEXT", "%sampler_array4", ""); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapVariableImageDataType) { + const std::string str = GenHeapTypeModule("SamplerHeapEXT", "%image", ""); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13746")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapVariableSamplerDataType) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "%sampler", ""); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13745")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapVariableImageArrayDataType) { + const std::string str = + GenHeapTypeModule("ResourceHeapEXT", "%image_array4", ""); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, UntypedNonHeapVariableDataType) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %var + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %var DescriptorSet 0 + OpDecorate %var Binding 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %var = OpUntypedVariableKHR %ptr_uc UniformConstant %uint + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13706")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadStruct) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %load = OpLoad %S %heap +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("decorated with ResourceHeapEXT as type <id> " + "'3[%_struct_3]'")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadDataUniversal) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %load = OpLoad %S %heap +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_6); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_6)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapLoadImage) { + const std::string str = GenHeapTypeModule("SamplerHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %load = OpLoad %image %ac +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-Result-11337")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapImageTexelPointer) { + const std::string str = GenHeapTypeModule("SamplerHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %texel = OpUntypedImageTexelPointerEXT %ptr_image %image %ac %coord %uint_0 +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT( + getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-OpUntypedImageTexelPointerEXT-13743")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("is derived from the variable <id> '2[%2]', which " + "must be decorated with Binding and DescriptorSet, or " + "decorated with BuiltIn ResourceHeapEXT")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapImageTexelPointer) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %texel = OpUntypedImageTexelPointerEXT %ptr_image %image %ac %coord %uint_0 +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadTensor) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpCapability TensorsARM + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpExtension "SPV_ARM_tensors" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorateId %tensor_array ArrayStrideIdEXT %uint_32 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %uint_32 = OpConstant %uint 32 + %tensor = OpTypeTensorARM %uint %uint_4 +%tensor_array = OpTypeRuntimeArray %tensor + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpUntypedAccessChainKHR %ptr_uc %tensor_array %heap %uint_1 + %load = OpLoad %tensor %ac + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, DescriptorSetImageTexelPointer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %var + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %var DescriptorSet 0 + OpDecorate %var Binding 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %v2uint = OpTypeVector %uint 2 + %coord = OpConstantComposite %v2uint %uint_0 %uint_0 + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %ptr_var = OpTypePointer UniformConstant %image + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_image = OpTypeUntypedPointerKHR Image + %var = OpVariable %ptr_var UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpUntypedAccessChainKHR %ptr_uc %image %var + %texel = OpUntypedImageTexelPointerEXT %ptr_image %image %ac %coord %uint_0 + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, UntypedUniformConstantNonHeapVariable) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %var + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %var DescriptorSet 0 + OpDecorate %var Binding 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %var = OpUntypedVariableKHR %ptr_uc UniformConstant %image + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13706")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("Vulkan requires that an OpUntypedVariableKHR be " + "decorated with SamplerHeapEXT or ResourceHeapEXT")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, TypedResourceHeapLoadImage) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %array = OpTypeArray %image %uint_4 + %ptr_array = OpTypePointer UniformConstant %array + %ptr_image = OpTypePointer UniformConstant %image + %heap = OpVariable %ptr_array UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpAccessChain %ptr_image %heap %uint_1 + %load = OpLoad %image %ac + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, TypedResourceHeapLoadData) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpMemberDecorate %S 0 Offset 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %S = OpTypeStruct %uint + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %array = OpTypeArray %image %uint_4 + %ptr_array = OpTypePointer UniformConstant %array + %ptr_image = OpTypePointer UniformConstant %image + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %heap = OpVariable %ptr_array UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpAccessChain %ptr_image %heap %uint_1 + %u = OpUntypedAccessChainKHR %ptr_uc %S %ac %uint_0 + %load = OpLoad %uint %u + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpLoad reads the variable <id> '2[%2]' decorated " + "with ResourceHeapEXT as type <id> '5[%uint]'")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, TypedDescriptorArrayImageTexelPointer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %var + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %var DescriptorSet 0 + OpDecorate %var Binding 0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %v2uint = OpTypeVector %uint 2 + %coord = OpConstantComposite %v2uint %uint_0 %uint_0 + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %array = OpTypeArray %image %uint_4 + %ptr_array = OpTypePointer UniformConstant %array + %ptr_image = OpTypePointer UniformConstant %image + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_texel = OpTypeUntypedPointerKHR Image + %var = OpVariable %ptr_array UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpAccessChain %ptr_image %var %uint_1 + %u = OpUntypedAccessChainKHR %ptr_uc %image %ac + %texel = OpUntypedImageTexelPointerEXT %ptr_texel %image %u %coord %uint_0 + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, TypedSamplerHeapImageTexelPointer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn SamplerHeapEXT + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %v2uint = OpTypeVector %uint 2 + %coord = OpConstantComposite %v2uint %uint_0 %uint_0 + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %sampler = OpTypeSampler + %array = OpTypeArray %sampler %uint_4 + %ptr_array = OpTypePointer UniformConstant %array +%ptr_sampler = OpTypePointer UniformConstant %sampler + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_texel = OpTypeUntypedPointerKHR Image + %heap = OpVariable %ptr_array UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpAccessChain %ptr_sampler %heap %uint_1 + %u = OpUntypedAccessChainKHR %ptr_uc %image %ac + %texel = OpUntypedImageTexelPointerEXT %ptr_texel %image %u %coord %uint_0 + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT( + getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-OpUntypedImageTexelPointerEXT-13743")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, TypedResourceHeapBufferPointer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %buffer = OpTypeBufferEXT StorageBuffer + %array = OpTypeArray %buffer %uint_4 + %ptr_array = OpTypePointer UniformConstant %array + %ptr_buffer = OpTypePointer UniformConstant %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer + %heap = OpVariable %ptr_array UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpAccessChain %ptr_buffer %heap %uint_1 + %u = OpUntypedAccessChainKHR %ptr_uc %buffer %ac + %buf = OpBufferPointerEXT %ptr_sb %u + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, + TypedResourceHeapBufferPointerTypedBuffer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_1 = OpConstant %uint 1 + %uint_4 = OpConstant %uint 4 + %buffer = OpTypeBufferEXT StorageBuffer + %array = OpTypeArray %buffer %uint_4 + %ptr_array = OpTypePointer UniformConstant %array + %ptr_buffer = OpTypePointer UniformConstant %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer + %heap = OpVariable %ptr_array UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpAccessChain %ptr_buffer %heap %uint_1 + %buf = OpBufferPointerEXT %ptr_sb %ac + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpBufferPointerEXT's buffer must be an untyped " + "pointer into a variable declared with the " + "ResourceHeapEXT built-in")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadImageArray) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %access = OpLoad %image_array4 %heap + %im0 = OpCompositeExtract %image %access 0 +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpLoad reads the variable <id> '2[%2]' decorated with " + "ResourceHeapEXT as type")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("an array of descriptors can't be accessed at once")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadImageArrayAccessChain) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %load = OpLoad %image_array4 %ac +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapLoadSamplerArray) { + const std::string str = GenHeapTypeModule("SamplerHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %sampler_array %heap %uint_1 + %load = OpLoad %sampler_array4 %ac +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("decorated with SamplerHeapEXT as type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadSingleImageFromArray) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array4 %heap %uint_1 + %load = OpLoad %image %ac +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyMemoryIntoHeap) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %var = OpVariable %ptr_fn_uint Function + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + OpCopyMemory %ac %var +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpCopyMemory writes to the variable <id> '2[%2]' " + "decorated with ResourceHeapEXT as type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyMemorySizedIntoHeap) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %var = OpVariable %ptr_fn_uint Function + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + OpCopyMemorySized %ac %var %uint_4 +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpCopyMemorySized writes to the variable <id> " + "'2[%2]' decorated with ResourceHeapEXT without a " + "type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyObjectLoadData) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %copy = OpCopyObject %ptr_uc %ac + %load = OpLoad %uint %copy +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpLoad reads the variable <id> '2[%2]' decorated " + "with ResourceHeapEXT as type")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyObjectOfHeapLoadData) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %copy = OpCopyObject %ptr_uc %heap + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %copy %uint_1 + %load = OpLoad %uint %ac +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-ResourceHeapEXT-13744")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapCopyObjectLoadImage) { + const std::string str = GenHeapTypeModule("ResourceHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %copy = OpCopyObject %ptr_uc %ac + %load = OpLoad %image %copy +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, SamplerHeapCopyObjectLoadImage) { + const std::string str = GenHeapTypeModule("SamplerHeapEXT", "", R"( + %ac = OpUntypedAccessChainKHR %ptr_uc %image_array %heap %uint_1 + %copy = OpCopyObject %ptr_uc %ac + %load = OpLoad %image %copy +)"); + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-Result-11337")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapArrayLength) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorate %Hs Block + OpDecorateId %image_array ArrayStrideIdEXT %image_size + OpMemberDecorateIdEXT %Hs 0 OffsetIdEXT %uint_0 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %image = OpTypeImage %uint 2D 0 0 0 2 R32ui + %image_size = OpConstantSizeOfEXT %uint %image +%image_array = OpTypeRuntimeArray %image + %Hs = OpTypeStruct %image_array + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %n = OpUntypedArrayLengthKHR %uint %Hs %heap 0 + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("OpUntypedArrayLengthKHR Pointer must be a storage " + "buffer descriptor, but it is derived from the " + "variable <id> '2[%2]' decorated with " + "ResourceHeapEXT")); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapLoadBufferDescriptor) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_1 = OpConstant %uint 1 + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpUntypedAccessChainKHR %ptr_uc %buffer_array %heap %uint_1 + %load = OpLoad %buffer %ac + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ArrayLengthThroughBufferPointer) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpDecorateId %buffer_array ArrayStrideIdEXT %buffer_size + OpDecorate %S Block + OpMemberDecorate %S 0 Offset 0 + OpDecorate %uint_array ArrayStride 4 + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_1 = OpConstant %uint 1 + %uint_array = OpTypeRuntimeArray %uint + %S = OpTypeStruct %uint_array + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer +%buffer_array = OpTypeRuntimeArray %buffer + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %ptr_sb = OpTypeUntypedPointerKHR StorageBuffer + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + %ac = OpUntypedAccessChainKHR %ptr_uc %buffer_array %heap %uint_1 + %buf_ptr = OpBufferPointerEXT %ptr_sb %ac + %n = OpUntypedArrayLengthKHR %uint %S %buf_ptr 0 + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3)); +} + +TEST_F(ValidateSpvEXTDescriptorHeap, ResourceHeapVariableStructOfDescriptors) { + const std::string str = R"( + OpCapability Shader + OpCapability UntypedPointersKHR + OpCapability DescriptorHeapEXT + OpExtension "SPV_EXT_descriptor_heap" + OpExtension "SPV_KHR_untyped_pointers" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %heap + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %heap BuiltIn ResourceHeapEXT + OpMemberDecorateIdEXT %heap_type 0 OffsetIdEXT %uint_0 + OpMemberDecorateIdEXT %heap_type 1 OffsetIdEXT %buffer_size + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %uint_0 = OpConstant %uint 0 + %buffer = OpTypeBufferEXT StorageBuffer +%buffer_size = OpConstantSizeOfEXT %uint %buffer + %heap_type = OpTypeStruct %buffer %uint + %ptr_uc = OpTypeUntypedPointerKHR UniformConstant + %heap = OpUntypedVariableKHR %ptr_uc UniformConstant %heap_type + %func = OpTypeFunction %void + %main = OpFunction %void None %func + %label = OpLabel + OpReturn + OpFunctionEnd +)"; + CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_3); + EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3)); + EXPECT_THAT(getDiagnosticString(), + AnyVUID("VUID-StandaloneSpirv-UniformConstant-13745")); +} + } // namespace } // namespace val } // namespace spvtools