validate_scopes.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. case SpvScopeShaderCallKHR:
  32. return true;
  33. case SpvScopeMax:
  34. break;
  35. }
  36. return false;
  37. }
  38. spv_result_t ValidateScope(ValidationState_t& _, const Instruction* inst,
  39. uint32_t scope) {
  40. SpvOp opcode = inst->opcode();
  41. bool is_int32 = false, is_const_int32 = false;
  42. uint32_t value = 0;
  43. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  44. if (!is_int32) {
  45. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  46. << spvOpcodeString(opcode) << ": expected scope to be a 32-bit int";
  47. }
  48. if (!is_const_int32) {
  49. if (_.HasCapability(SpvCapabilityShader) &&
  50. !_.HasCapability(SpvCapabilityCooperativeMatrixNV)) {
  51. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  52. << "Scope ids must be OpConstant when Shader capability is "
  53. << "present";
  54. }
  55. if (_.HasCapability(SpvCapabilityShader) &&
  56. _.HasCapability(SpvCapabilityCooperativeMatrixNV) &&
  57. !spvOpcodeIsConstant(_.GetIdOpcode(scope))) {
  58. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  59. << "Scope ids must be constant or specialization constant when "
  60. << "CooperativeMatrixNV capability is present";
  61. }
  62. }
  63. if (is_const_int32 && !IsValidScope(value)) {
  64. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  65. << "Invalid scope value:\n " << _.Disassemble(*_.FindDef(scope));
  66. }
  67. return SPV_SUCCESS;
  68. }
  69. spv_result_t ValidateExecutionScope(ValidationState_t& _,
  70. const Instruction* inst, uint32_t scope) {
  71. SpvOp opcode = inst->opcode();
  72. bool is_int32 = false, is_const_int32 = false;
  73. uint32_t value = 0;
  74. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  75. if (auto error = ValidateScope(_, inst, scope)) {
  76. return error;
  77. }
  78. if (!is_const_int32) {
  79. return SPV_SUCCESS;
  80. }
  81. // Vulkan specific rules
  82. if (spvIsVulkanEnv(_.context()->target_env)) {
  83. // Vulkan 1.1 specific rules
  84. if (_.context()->target_env != SPV_ENV_VULKAN_1_0) {
  85. // Scope for Non Uniform Group Operations must be limited to Subgroup
  86. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  87. value != SpvScopeSubgroup) {
  88. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  89. << _.VkErrorID(4642) << spvOpcodeString(opcode)
  90. << ": in Vulkan environment Execution scope is limited to "
  91. << "Subgroup";
  92. }
  93. }
  94. // If OpControlBarrier is used in fragment, vertex, tessellation evaluation,
  95. // or geometry stages, the execution Scope must be Subgroup.
  96. if (opcode == SpvOpControlBarrier && value != SpvScopeSubgroup) {
  97. _.function(inst->function()->id())
  98. ->RegisterExecutionModelLimitation([](SpvExecutionModel model,
  99. std::string* message) {
  100. if (model == SpvExecutionModelFragment ||
  101. model == SpvExecutionModelVertex ||
  102. model == SpvExecutionModelGeometry ||
  103. model == SpvExecutionModelTessellationEvaluation) {
  104. if (message) {
  105. *message =
  106. "in Vulkan evironment, OpControlBarrier execution scope "
  107. "must be Subgroup for Fragment, Vertex, Geometry and "
  108. "TessellationEvaluation execution models";
  109. }
  110. return false;
  111. }
  112. return true;
  113. });
  114. }
  115. // Vulkan generic rules
  116. // Scope for execution must be limited to Workgroup or Subgroup
  117. if (value != SpvScopeWorkgroup && value != SpvScopeSubgroup) {
  118. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  119. << spvOpcodeString(opcode)
  120. << ": in Vulkan environment Execution Scope is limited to "
  121. << "Workgroup and Subgroup";
  122. }
  123. }
  124. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  125. // General SPIRV rules
  126. // Scope for execution must be limited to Workgroup or Subgroup for
  127. // non-uniform operations
  128. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  129. value != SpvScopeSubgroup && value != SpvScopeWorkgroup) {
  130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  131. << spvOpcodeString(opcode)
  132. << ": Execution scope is limited to Subgroup or Workgroup";
  133. }
  134. return SPV_SUCCESS;
  135. }
  136. spv_result_t ValidateMemoryScope(ValidationState_t& _, const Instruction* inst,
  137. uint32_t scope) {
  138. const SpvOp opcode = inst->opcode();
  139. bool is_int32 = false, is_const_int32 = false;
  140. uint32_t value = 0;
  141. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  142. if (auto error = ValidateScope(_, inst, scope)) {
  143. return error;
  144. }
  145. if (!is_const_int32) {
  146. return SPV_SUCCESS;
  147. }
  148. if (value == SpvScopeQueueFamilyKHR) {
  149. if (_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  150. return SPV_SUCCESS;
  151. } else {
  152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  153. << spvOpcodeString(opcode)
  154. << ": Memory Scope QueueFamilyKHR requires capability "
  155. << "VulkanMemoryModelKHR";
  156. }
  157. }
  158. if (value == SpvScopeDevice &&
  159. _.HasCapability(SpvCapabilityVulkanMemoryModelKHR) &&
  160. !_.HasCapability(SpvCapabilityVulkanMemoryModelDeviceScopeKHR)) {
  161. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  162. << "Use of device scope with VulkanKHR memory model requires the "
  163. << "VulkanMemoryModelDeviceScopeKHR capability";
  164. }
  165. // Vulkan Specific rules
  166. if (spvIsVulkanEnv(_.context()->target_env)) {
  167. if (value == SpvScopeCrossDevice) {
  168. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  169. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  170. << ": in Vulkan environment, Memory Scope cannot be CrossDevice";
  171. }
  172. // Vulkan 1.0 specifc rules
  173. if (_.context()->target_env == SPV_ENV_VULKAN_1_0 &&
  174. value != SpvScopeDevice && value != SpvScopeWorkgroup &&
  175. value != SpvScopeInvocation) {
  176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  177. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  178. << ": in Vulkan 1.0 environment Memory Scope is limited to "
  179. << "Device, Workgroup and Invocation";
  180. }
  181. // Vulkan 1.1 specifc rules
  182. if ((_.context()->target_env == SPV_ENV_VULKAN_1_1 ||
  183. _.context()->target_env == SPV_ENV_VULKAN_1_2) &&
  184. value != SpvScopeDevice && value != SpvScopeWorkgroup &&
  185. value != SpvScopeSubgroup && value != SpvScopeInvocation &&
  186. value != SpvScopeShaderCallKHR) {
  187. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  188. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  189. << ": in Vulkan 1.1 and 1.2 environment Memory Scope is limited "
  190. << "to Device, Workgroup, Invocation, and ShaderCall";
  191. }
  192. if (value == SpvScopeShaderCallKHR) {
  193. std::string errorVUID = _.VkErrorID(4640);
  194. _.function(inst->function()->id())
  195. ->RegisterExecutionModelLimitation(
  196. [errorVUID](SpvExecutionModel model, std::string* message) {
  197. if (model != SpvExecutionModelRayGenerationKHR &&
  198. model != SpvExecutionModelIntersectionKHR &&
  199. model != SpvExecutionModelAnyHitKHR &&
  200. model != SpvExecutionModelClosestHitKHR &&
  201. model != SpvExecutionModelMissKHR &&
  202. model != SpvExecutionModelCallableKHR) {
  203. if (message) {
  204. *message =
  205. errorVUID +
  206. "ShaderCallKHR Memory Scope requires a ray tracing "
  207. "execution model";
  208. }
  209. return false;
  210. }
  211. return true;
  212. });
  213. }
  214. if (value == SpvScopeWorkgroup) {
  215. std::string errorVUID = _.VkErrorID(4639);
  216. _.function(inst->function()->id())
  217. ->RegisterExecutionModelLimitation(
  218. [errorVUID](SpvExecutionModel model, std::string* message) {
  219. if (model != SpvExecutionModelGLCompute &&
  220. model != SpvExecutionModelTaskNV &&
  221. model != SpvExecutionModelMeshNV) {
  222. if (message) {
  223. *message = errorVUID +
  224. "Workgroup Memory Scope is limited to MeshNV, "
  225. "TaskNV, and GLCompute execution model";
  226. }
  227. return false;
  228. }
  229. return true;
  230. });
  231. }
  232. }
  233. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  234. return SPV_SUCCESS;
  235. }
  236. } // namespace val
  237. } // namespace spvtools