From 3d2afb78c21cd489c47374d66e267f27531fbfc7 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 30 Jan 2019 15:46:02 -0500 Subject: [PATCH] Add whitelist of allowed BuiltIn decorations for WebGPU (#2337) Part of resolving #2276 --- source/val/validate_builtins.cpp | 31 +++++++++++++++++++++++++++++++ test/val/val_builtins_test.cpp | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/source/val/validate_builtins.cpp b/source/val/validate_builtins.cpp index 929f25522..729029a35 100644 --- a/source/val/validate_builtins.cpp +++ b/source/val/validate_builtins.cpp @@ -101,6 +101,28 @@ SpvStorageClass GetStorageClass(const Instruction& inst) { return SpvStorageClassMax; } +bool IsBuiltInValidForWebGPU(SpvBuiltIn label) { + switch (label) { + case SpvBuiltInPosition: + case SpvBuiltInVertexIndex: + case SpvBuiltInInstanceIndex: + case SpvBuiltInFrontFacing: + case SpvBuiltInFragCoord: + case SpvBuiltInFragDepth: + case SpvBuiltInNumWorkgroups: + case SpvBuiltInWorkgroupSize: + case SpvBuiltInLocalInvocationId: + case SpvBuiltInGlobalInvocationId: + case SpvBuiltInLocalInvocationIndex: { + return true; + } + default: + break; + } + + return false; +} + // Helper class managing validation of built-ins. // TODO: Generic functionality of this class can be moved into // ValidationState_t to be made available to other users. @@ -2548,6 +2570,15 @@ spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference( spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition( const Decoration& decoration, const Instruction& inst) { const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]); + + if (spvIsWebGPUEnv(_.context()->target_env) && + !IsBuiltInValidForWebGPU(label)) { + return _.diag(SPV_ERROR_INVALID_DATA, &inst) + << "WebGPU does not allow BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]); + } + // If you are adding a new BuiltIn enum, please register it here. // If the newly added enum has validation rules associated with it // consider leaving a TODO and/or creating an issue. diff --git a/test/val/val_builtins_test.cpp b/test/val/val_builtins_test.cpp index 394a9a647..2560b9415 100644 --- a/test/val/val_builtins_test.cpp +++ b/test/val/val_builtins_test.cpp @@ -1790,6 +1790,16 @@ INSTANTIATE_TEST_CASE_P( Values(TestResult(SPV_ERROR_INVALID_DATA, "needs to be a 32-bit int", "is not an int"))), ); +INSTANTIATE_TEST_CASE_P( + WhitelistRejection, + ValidateWebGPUCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize", "ClipDistance", "CullDistance", "VertexId", + "InstanceId", "PointCoord", "SampleMask", "HelperInvocation", + "WorkgroupId"), + Values("Vertex"), Values("Input"), Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "WebGPU does not allow BuiltIn"))), ); + CodeGenerator GetArrayedVariableCodeGenerator(spv_target_env env, const char* const built_in, const char* const execution_model,