diff --git a/source/spirv_target_env.cpp b/source/spirv_target_env.cpp index a3aa0af03..1dc25081a 100644 --- a/source/spirv_target_env.cpp +++ b/source/spirv_target_env.cpp @@ -248,3 +248,39 @@ bool spvIsWebGPUEnv(spv_target_env env) { } return false; } + +std::string spvLogStringForEnv(spv_target_env env) { + switch (env) { + case SPV_ENV_OPENCL_1_2: + case SPV_ENV_OPENCL_2_0: + case SPV_ENV_OPENCL_2_1: + case SPV_ENV_OPENCL_2_2: + case SPV_ENV_OPENCL_EMBEDDED_1_2: + case SPV_ENV_OPENCL_EMBEDDED_2_0: + case SPV_ENV_OPENCL_EMBEDDED_2_1: + case SPV_ENV_OPENCL_EMBEDDED_2_2: { + return "OpenCL"; + } + case SPV_ENV_OPENGL_4_0: + case SPV_ENV_OPENGL_4_1: + case SPV_ENV_OPENGL_4_2: + case SPV_ENV_OPENGL_4_3: + case SPV_ENV_OPENGL_4_5: { + return "OpenGL"; + } + case SPV_ENV_VULKAN_1_0: + case SPV_ENV_VULKAN_1_1: { + return "Vulkan"; + } + case SPV_ENV_WEBGPU_0: { + return "WebGPU"; + } + case SPV_ENV_UNIVERSAL_1_0: + case SPV_ENV_UNIVERSAL_1_1: + case SPV_ENV_UNIVERSAL_1_2: + case SPV_ENV_UNIVERSAL_1_3: { + return "Universal"; + } + } + return "Unknown"; +} diff --git a/source/spirv_target_env.h b/source/spirv_target_env.h index d1bd83512..9061a3ae6 100644 --- a/source/spirv_target_env.h +++ b/source/spirv_target_env.h @@ -15,6 +15,8 @@ #ifndef SOURCE_SPIRV_TARGET_ENV_H_ #define SOURCE_SPIRV_TARGET_ENV_H_ +#include + #include "spirv-tools/libspirv.h" // Parses s into *env and returns true if successful. If unparsable, returns @@ -33,4 +35,8 @@ bool spvIsWebGPUEnv(spv_target_env env); // Returns the version number for the given SPIR-V target environment. uint32_t spvVersionForTargetEnv(spv_target_env env); +// Returns a string to use in logging messages that indicates the class of +// environment, i.e. "Vulkan", "WebGPU", "OpenCL", etc. +std::string spvLogStringForEnv(spv_target_env env); + #endif // SOURCE_SPIRV_TARGET_ENV_H_ diff --git a/source/val/validate_type.cpp b/source/val/validate_type.cpp index 94ea66034..365d4cc70 100644 --- a/source/val/validate_type.cpp +++ b/source/val/validate_type.cpp @@ -110,11 +110,10 @@ spv_result_t ValidateTypeArray(ValidationState_t& _, const Instruction* inst) { if ((spvIsVulkanEnv(_.context()->target_env) || spvIsWebGPUEnv(_.context()->target_env)) && element_type->opcode() == SpvOpTypeRuntimeArray) { - const char* env_text = - spvIsVulkanEnv(_.context()->target_env) ? "Vulkan" : "WebGPU"; return _.diag(SPV_ERROR_INVALID_ID, inst) << "OpTypeArray Element Type '" << _.getIdName(element_type_id) - << "' is not valid in " << env_text << " environment."; + << "' is not valid in " + << spvLogStringForEnv(_.context()->target_env) << " environments."; } const auto length_index = 2; @@ -175,12 +174,10 @@ spv_result_t ValidateTypeRuntimeArray(ValidationState_t& _, if ((spvIsVulkanEnv(_.context()->target_env) || spvIsWebGPUEnv(_.context()->target_env)) && element_type->opcode() == SpvOpTypeRuntimeArray) { - const char* env_text = - spvIsVulkanEnv(_.context()->target_env) ? "Vulkan" : "WebGPU"; return _.diag(SPV_ERROR_INVALID_ID, inst) << "OpTypeRuntimeArray Element Type '" - << _.getIdName(element_id) << "' is not valid in " << env_text - << " environment."; + << _.getIdName(element_id) << "' is not valid in " + << spvLogStringForEnv(_.context()->target_env) << " environments."; } return SPV_SUCCESS; @@ -235,11 +232,10 @@ spv_result_t ValidateTypeStruct(ValidationState_t& _, const Instruction* inst) { const bool is_last_member = member_type_index == inst->operands().size() - 1; if (!is_last_member) { - const char* env_text = - spvIsVulkanEnv(_.context()->target_env) ? "Vulkan" : "WebGPU"; return _.diag(SPV_ERROR_INVALID_ID, inst) - << "In " << env_text << ", OpTypeRuntimeArray must only be used " - << "for the last member of an OpTypeStruct"; + << "In " << spvLogStringForEnv(_.context()->target_env) + << ", OpTypeRuntimeArray must only be used for the last member " + "of an OpTypeStruct"; } } } diff --git a/test/val/val_memory_test.cpp b/test/val/val_memory_test.cpp index 097477c31..167b11f87 100644 --- a/test/val/val_memory_test.cpp +++ b/test/val/val_memory_test.cpp @@ -2179,7 +2179,7 @@ OpFunctionEnd getDiagnosticString(), HasSubstr( "OpTypeRuntimeArray Element Type '3[%_runtimearr_2]' is not " - "valid in Vulkan environment.\n %_runtimearr__runtimearr_2 = " + "valid in Vulkan environments.\n %_runtimearr__runtimearr_2 = " "OpTypeRuntimeArray %_runtimearr_2\n")); } @@ -2210,7 +2210,7 @@ OpFunctionEnd getDiagnosticString(), HasSubstr( "OpTypeRuntimeArray Element Type '3[%_runtimearr_2]' is not " - "valid in WebGPU environment.\n %_runtimearr__runtimearr_2 = " + "valid in WebGPU environments.\n %_runtimearr__runtimearr_2 = " "OpTypeRuntimeArray %_runtimearr_2\n")); } @@ -2242,7 +2242,7 @@ OpFunctionEnd getDiagnosticString(), HasSubstr( "OpTypeRuntimeArray Element Type '4[%_runtimearr_uint]' is not " - "valid in Vulkan environment.\n %_runtimearr__runtimearr_uint = " + "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = " "OpTypeRuntimeArray %_runtimearr_uint\n")); } @@ -2304,7 +2304,7 @@ OpFunctionEnd getDiagnosticString(), HasSubstr( "OpTypeRuntimeArray Element Type '5[%_runtimearr_uint]' is not " - "valid in Vulkan environment.\n %_runtimearr__runtimearr_uint = " + "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = " "OpTypeRuntimeArray %_runtimearr_uint\n")); } @@ -2340,7 +2340,7 @@ OpFunctionEnd getDiagnosticString(), HasSubstr( "OpTypeRuntimeArray Element Type '5[%_runtimearr_uint]' is not " - "valid in Vulkan environment.\n %_runtimearr__runtimearr_uint = " + "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = " "OpTypeRuntimeArray %_runtimearr_uint\n")); } @@ -2370,7 +2370,7 @@ OpFunctionEnd EXPECT_THAT( getDiagnosticString(), HasSubstr("OpTypeArray Element Type '5[%_runtimearr_4]' is not " - "valid in Vulkan environment.\n %_arr__runtimearr_4_uint_1 = " + "valid in Vulkan environments.\n %_arr__runtimearr_4_uint_1 = " "OpTypeArray %_runtimearr_4 %uint_1\n")); } @@ -2402,7 +2402,7 @@ OpFunctionEnd EXPECT_THAT( getDiagnosticString(), HasSubstr("OpTypeArray Element Type '5[%_runtimearr_4]' is not " - "valid in WebGPU environment.\n %_arr__runtimearr_4_uint_1 = " + "valid in WebGPU environments.\n %_arr__runtimearr_4_uint_1 = " "OpTypeArray %_runtimearr_4 %uint_1\n")); } @@ -2436,7 +2436,7 @@ OpFunctionEnd getDiagnosticString(), HasSubstr( "OpTypeRuntimeArray Element Type '6[%_runtimearr_uint]' is not " - "valid in Vulkan environment.\n %_runtimearr__runtimearr_uint = " + "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = " "OpTypeRuntimeArray %_runtimearr_uint\n")); } @@ -2468,9 +2468,10 @@ OpFunctionEnd EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1)); EXPECT_THAT( getDiagnosticString(), - HasSubstr("OpTypeArray Element Type '6[%_runtimearr_uint]' is not " - "valid in Vulkan environment.\n %_arr__runtimearr_uint_uint_1 " - "= OpTypeArray %_runtimearr_uint %uint_1\n")); + HasSubstr( + "OpTypeArray Element Type '6[%_runtimearr_uint]' is not " + "valid in Vulkan environments.\n %_arr__runtimearr_uint_uint_1 " + "= OpTypeArray %_runtimearr_uint %uint_1\n")); } TEST_F(ValidateMemory, @@ -2504,9 +2505,10 @@ OpFunctionEnd EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1)); EXPECT_THAT( getDiagnosticString(), - HasSubstr("OpTypeArray Element Type '6[%_runtimearr_uint]' is not " - "valid in Vulkan environment.\n %_arr__runtimearr_uint_uint_1 " - "= OpTypeArray %_runtimearr_uint %uint_1\n")); + HasSubstr( + "OpTypeArray Element Type '6[%_runtimearr_uint]' is not " + "valid in Vulkan environments.\n %_arr__runtimearr_uint_uint_1 " + "= OpTypeArray %_runtimearr_uint %uint_1\n")); } TEST_F(ValidateMemory, VulkanRTAStructInsideRTAWithRuntimeDescriptorArrayGood) {