Skip to content

Commit

Permalink
spirv-val: Validate small types for Kernel
Browse files Browse the repository at this point in the history
Small type uses are the same for Shader modules as Kernel modules.
Currently, the validation rules are disabled for any module which
does not advertise the Shader capability. This commit removes this
check, enabling the validator to also check these rules for OpenCL
kernel modules.
  • Loading branch information
Snektron committed Feb 23, 2025
1 parent f2f6c12 commit a961dda
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/val/validate_small_type_uses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace val {

spv_result_t ValidateSmallTypeUses(ValidationState_t& _,
const Instruction* inst) {
if (!_.HasCapability(spv::Capability::Shader) || inst->type_id() == 0 ||
if (inst->type_id() == 0 ||
!_.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
return SPV_SUCCESS;
}
Expand Down
47 changes: 47 additions & 0 deletions test/val/val_small_type_uses_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,53 @@ INSTANTIATE_TEST_SUITE_P(
"%inst = OpFunctionCall %void %half_func %ld_half",
"%inst = OpFunctionCall %void %half_func %float_to_half"));

TEST_F(ValidateSmallTypeUses, F16OpsFail) {
const std::string body = R"(
OpCapability Addresses
OpCapability Kernel
OpCapability Float16Buffer
OpCapability Linkage
OpMemoryModel Physical64 OpenCL
%f16 = OpTypeFloat 16
%func = OpTypeFunction %f16 %f16 %f16
%add = OpFunction %f16 None %func
%a = OpFunctionParameter %f16
%b = OpFunctionParameter %f16
%add_entry = OpLabel
%result = OpFAdd %f16 %a %b
OpReturnValue %result
OpFunctionEnd
)";

CompileSuccessfully(body.c_str());
ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Invalid use of 8- or 16-bit result"));
}

TEST_F(ValidateSmallTypeUses, F16OpsSuccess) {
const std::string body = R"(
OpCapability Addresses
OpCapability Kernel
OpCapability Float16Buffer
OpCapability Float16
OpCapability Linkage
OpMemoryModel Physical64 OpenCL
%f16 = OpTypeFloat 16
%func = OpTypeFunction %f16 %f16 %f16
%add = OpFunction %f16 None %func
%a = OpFunctionParameter %f16
%b = OpFunctionParameter %f16
%add_entry = OpLabel
%result = OpFAdd %f16 %a %b
OpReturnValue %result
OpFunctionEnd
)";

CompileSuccessfully(body.c_str());
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

} // namespace
} // namespace val
} // namespace spvtools

0 comments on commit a961dda

Please sign in to comment.