validate_scopes.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/val/validate_scopes.h"
  15. #include "source/diagnostic.h"
  16. #include "source/spirv_target_env.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. bool IsValidScope(uint32_t scope) {
  22. // Deliberately avoid a default case so we have to update the list when the
  23. // scopes list changes.
  24. switch (static_cast<SpvScope>(scope)) {
  25. case SpvScopeCrossDevice:
  26. case SpvScopeDevice:
  27. case SpvScopeWorkgroup:
  28. case SpvScopeSubgroup:
  29. case SpvScopeInvocation:
  30. case SpvScopeQueueFamilyKHR:
  31. return true;
  32. case SpvScopeMax:
  33. break;
  34. }
  35. return false;
  36. }
  37. spv_result_t ValidateScope(ValidationState_t& _, const Instruction* inst,
  38. uint32_t scope) {
  39. SpvOp opcode = inst->opcode();
  40. bool is_int32 = false, is_const_int32 = false;
  41. uint32_t value = 0;
  42. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  43. if (!is_int32) {
  44. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  45. << spvOpcodeString(opcode) << ": expected scope to be a 32-bit int";
  46. }
  47. if (!is_const_int32) {
  48. if (_.HasCapability(SpvCapabilityShader) &&
  49. !_.HasCapability(SpvCapabilityCooperativeMatrixNV)) {
  50. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  51. << "Scope ids must be OpConstant when Shader capability is "
  52. << "present";
  53. }
  54. if (_.HasCapability(SpvCapabilityShader) &&
  55. _.HasCapability(SpvCapabilityCooperativeMatrixNV) &&
  56. !spvOpcodeIsConstant(_.GetIdOpcode(scope))) {
  57. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  58. << "Scope ids must be constant or specialization constant when "
  59. << "CooperativeMatrixNV capability is present";
  60. }
  61. }
  62. if (is_const_int32 && !IsValidScope(value)) {
  63. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  64. << "Invalid scope value:\n " << _.Disassemble(*_.FindDef(scope));
  65. }
  66. return SPV_SUCCESS;
  67. }
  68. spv_result_t ValidateExecutionScope(ValidationState_t& _,
  69. const Instruction* inst, uint32_t scope) {
  70. SpvOp opcode = inst->opcode();
  71. bool is_int32 = false, is_const_int32 = false;
  72. uint32_t value = 0;
  73. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  74. if (auto error = ValidateScope(_, inst, scope)) {
  75. return error;
  76. }
  77. if (!is_const_int32) {
  78. return SPV_SUCCESS;
  79. }
  80. // Vulkan specific rules
  81. if (spvIsVulkanEnv(_.context()->target_env)) {
  82. // Vulkan 1.1 specific rules
  83. if (_.context()->target_env != SPV_ENV_VULKAN_1_0) {
  84. // Scope for Non Uniform Group Operations must be limited to Subgroup
  85. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  86. value != SpvScopeSubgroup) {
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << spvOpcodeString(opcode)
  89. << ": in Vulkan environment Execution scope is limited to "
  90. << "Subgroup";
  91. }
  92. }
  93. // If OpControlBarrier is used in fragment, vertex, tessellation evaluation,
  94. // or geometry stages, the execution Scope must be Subgroup.
  95. if (opcode == SpvOpControlBarrier && value != SpvScopeSubgroup) {
  96. _.function(inst->function()->id())
  97. ->RegisterExecutionModelLimitation([](SpvExecutionModel model,
  98. std::string* message) {
  99. if (model == SpvExecutionModelFragment ||
  100. model == SpvExecutionModelVertex ||
  101. model == SpvExecutionModelGeometry ||
  102. model == SpvExecutionModelTessellationEvaluation) {
  103. if (message) {
  104. *message =
  105. "in Vulkan evironment, OpControlBarrier execution scope "
  106. "must be Subgroup for Fragment, Vertex, Geometry and "
  107. "TessellationEvaluation execution models";
  108. }
  109. return false;
  110. }
  111. return true;
  112. });
  113. }
  114. // Vulkan generic rules
  115. // Scope for execution must be limited to Workgroup or Subgroup
  116. if (value != SpvScopeWorkgroup && value != SpvScopeSubgroup) {
  117. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  118. << spvOpcodeString(opcode)
  119. << ": in Vulkan environment Execution Scope is limited to "
  120. << "Workgroup and Subgroup";
  121. }
  122. }
  123. // WebGPU Specific rules
  124. if (spvIsWebGPUEnv(_.context()->target_env)) {
  125. if (value != SpvScopeWorkgroup) {
  126. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  127. << spvOpcodeString(opcode)
  128. << ": in WebGPU environment Execution Scope is limited to "
  129. << "Workgroup";
  130. }
  131. }
  132. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  133. // General SPIRV rules
  134. // Scope for execution must be limited to Workgroup or Subgroup for
  135. // non-uniform operations
  136. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  137. value != SpvScopeSubgroup && value != SpvScopeWorkgroup) {
  138. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  139. << spvOpcodeString(opcode)
  140. << ": Execution scope is limited to Subgroup or Workgroup";
  141. }
  142. return SPV_SUCCESS;
  143. }
  144. spv_result_t ValidateMemoryScope(ValidationState_t& _, const Instruction* inst,
  145. uint32_t scope) {
  146. const SpvOp opcode = inst->opcode();
  147. bool is_int32 = false, is_const_int32 = false;
  148. uint32_t value = 0;
  149. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  150. if (auto error = ValidateScope(_, inst, scope)) {
  151. return error;
  152. }
  153. if (!is_const_int32) {
  154. return SPV_SUCCESS;
  155. }
  156. if (value == SpvScopeQueueFamilyKHR) {
  157. if (_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  158. return SPV_SUCCESS;
  159. } else {
  160. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  161. << spvOpcodeString(opcode)
  162. << ": Memory Scope QueueFamilyKHR requires capability "
  163. << "VulkanMemoryModelKHR";
  164. }
  165. }
  166. if (value == SpvScopeDevice &&
  167. _.HasCapability(SpvCapabilityVulkanMemoryModelKHR) &&
  168. !_.HasCapability(SpvCapabilityVulkanMemoryModelDeviceScopeKHR)) {
  169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  170. << "Use of device scope with VulkanKHR memory model requires the "
  171. << "VulkanMemoryModelDeviceScopeKHR capability";
  172. }
  173. // Vulkan Specific rules
  174. if (spvIsVulkanEnv(_.context()->target_env)) {
  175. if (value == SpvScopeCrossDevice) {
  176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  177. << spvOpcodeString(opcode)
  178. << ": in Vulkan environment, Memory Scope cannot be CrossDevice";
  179. }
  180. // Vulkan 1.0 specifc rules
  181. if (_.context()->target_env == SPV_ENV_VULKAN_1_0 &&
  182. value != SpvScopeDevice && value != SpvScopeWorkgroup &&
  183. value != SpvScopeInvocation) {
  184. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  185. << spvOpcodeString(opcode)
  186. << ": in Vulkan 1.0 environment Memory Scope is limited to "
  187. << "Device, Workgroup and Invocation";
  188. }
  189. // Vulkan 1.1 specifc rules
  190. if (_.context()->target_env == SPV_ENV_VULKAN_1_1 &&
  191. value != SpvScopeDevice && value != SpvScopeWorkgroup &&
  192. value != SpvScopeSubgroup && value != SpvScopeInvocation) {
  193. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  194. << spvOpcodeString(opcode)
  195. << ": in Vulkan 1.1 environment Memory Scope is limited to "
  196. << "Device, Workgroup and Invocation";
  197. }
  198. }
  199. // WebGPU specific rules
  200. if (spvIsWebGPUEnv(_.context()->target_env)) {
  201. switch (inst->opcode()) {
  202. case SpvOpControlBarrier:
  203. if (value != SpvScopeWorkgroup) {
  204. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  205. << spvOpcodeString(opcode)
  206. << ": in WebGPU environment Memory Scope is limited to "
  207. << "Workgroup for OpControlBarrier";
  208. }
  209. break;
  210. case SpvOpMemoryBarrier:
  211. if (value != SpvScopeWorkgroup) {
  212. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  213. << spvOpcodeString(opcode)
  214. << ": in WebGPU environment Memory Scope is limited to "
  215. << "Workgroup for OpMemoryBarrier";
  216. }
  217. break;
  218. default:
  219. if (spvOpcodeIsAtomicOp(inst->opcode())) {
  220. if (value != SpvScopeQueueFamilyKHR) {
  221. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  222. << spvOpcodeString(opcode)
  223. << ": in WebGPU environment Memory Scope is limited to "
  224. << "QueueFamilyKHR for OpAtomic* operations";
  225. }
  226. }
  227. if (value != SpvScopeWorkgroup && value != SpvScopeInvocation &&
  228. value != SpvScopeQueueFamilyKHR) {
  229. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  230. << spvOpcodeString(opcode)
  231. << ": in WebGPU environment Memory Scope is limited to "
  232. << "Workgroup, Invocation, and QueueFamilyKHR";
  233. }
  234. break;
  235. }
  236. }
  237. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  238. return SPV_SUCCESS;
  239. }
  240. } // namespace val
  241. } // namespace spvtools