validate_scopes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // OpControlBarrier must only use Subgroup execution scope for a subset of
  95. // execution models.
  96. if (opcode == SpvOpControlBarrier && value != SpvScopeSubgroup) {
  97. std::string errorVUID = _.VkErrorID(4682);
  98. _.function(inst->function()->id())
  99. ->RegisterExecutionModelLimitation([errorVUID](
  100. SpvExecutionModel model,
  101. std::string* message) {
  102. if (model == SpvExecutionModelFragment ||
  103. model == SpvExecutionModelVertex ||
  104. model == SpvExecutionModelGeometry ||
  105. model == SpvExecutionModelTessellationEvaluation ||
  106. model == SpvExecutionModelRayGenerationKHR ||
  107. model == SpvExecutionModelIntersectionKHR ||
  108. model == SpvExecutionModelAnyHitKHR ||
  109. model == SpvExecutionModelClosestHitKHR ||
  110. model == SpvExecutionModelMissKHR) {
  111. if (message) {
  112. *message =
  113. errorVUID +
  114. "in Vulkan environment, OpControlBarrier execution scope "
  115. "must be Subgroup for Fragment, Vertex, Geometry, "
  116. "TessellationEvaluation, RayGeneration, Intersection, "
  117. "AnyHit, ClosestHit, and Miss execution models";
  118. }
  119. return false;
  120. }
  121. return true;
  122. });
  123. }
  124. // Only subset of execution models support Workgroup.
  125. if (value == SpvScopeWorkgroup) {
  126. std::string errorVUID = _.VkErrorID(4637);
  127. _.function(inst->function()->id())
  128. ->RegisterExecutionModelLimitation(
  129. [errorVUID](SpvExecutionModel model, std::string* message) {
  130. if (model != SpvExecutionModelTaskNV &&
  131. model != SpvExecutionModelMeshNV &&
  132. model != SpvExecutionModelTessellationControl &&
  133. model != SpvExecutionModelGLCompute) {
  134. if (message) {
  135. *message =
  136. errorVUID +
  137. "in Vulkan environment, Workgroup execution scope is "
  138. "only for TaskNV, MeshNV, TessellationControl, and "
  139. "GLCompute execution models";
  140. }
  141. return false;
  142. }
  143. return true;
  144. });
  145. }
  146. // Vulkan generic rules
  147. // Scope for execution must be limited to Workgroup or Subgroup
  148. if (value != SpvScopeWorkgroup && value != SpvScopeSubgroup) {
  149. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  150. << _.VkErrorID(4636) << spvOpcodeString(opcode)
  151. << ": in Vulkan environment Execution Scope is limited to "
  152. << "Workgroup and Subgroup";
  153. }
  154. }
  155. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  156. // General SPIRV rules
  157. // Scope for execution must be limited to Workgroup or Subgroup for
  158. // non-uniform operations
  159. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  160. value != SpvScopeSubgroup && value != SpvScopeWorkgroup) {
  161. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  162. << spvOpcodeString(opcode)
  163. << ": Execution scope is limited to Subgroup or Workgroup";
  164. }
  165. return SPV_SUCCESS;
  166. }
  167. spv_result_t ValidateMemoryScope(ValidationState_t& _, const Instruction* inst,
  168. uint32_t scope) {
  169. const SpvOp opcode = inst->opcode();
  170. bool is_int32 = false, is_const_int32 = false;
  171. uint32_t value = 0;
  172. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  173. if (auto error = ValidateScope(_, inst, scope)) {
  174. return error;
  175. }
  176. if (!is_const_int32) {
  177. return SPV_SUCCESS;
  178. }
  179. if (value == SpvScopeQueueFamilyKHR) {
  180. if (_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  181. return SPV_SUCCESS;
  182. } else {
  183. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  184. << spvOpcodeString(opcode)
  185. << ": Memory Scope QueueFamilyKHR requires capability "
  186. << "VulkanMemoryModelKHR";
  187. }
  188. }
  189. if (value == SpvScopeDevice &&
  190. _.HasCapability(SpvCapabilityVulkanMemoryModelKHR) &&
  191. !_.HasCapability(SpvCapabilityVulkanMemoryModelDeviceScopeKHR)) {
  192. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  193. << "Use of device scope with VulkanKHR memory model requires the "
  194. << "VulkanMemoryModelDeviceScopeKHR capability";
  195. }
  196. // Vulkan Specific rules
  197. if (spvIsVulkanEnv(_.context()->target_env)) {
  198. if (value == SpvScopeCrossDevice) {
  199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  200. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  201. << ": in Vulkan environment, Memory Scope cannot be CrossDevice";
  202. }
  203. // Vulkan 1.0 specifc rules
  204. if (_.context()->target_env == SPV_ENV_VULKAN_1_0 &&
  205. value != SpvScopeDevice && value != SpvScopeWorkgroup &&
  206. value != SpvScopeInvocation) {
  207. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  208. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  209. << ": in Vulkan 1.0 environment Memory Scope is limited to "
  210. << "Device, Workgroup and Invocation";
  211. }
  212. // Vulkan 1.1 specifc rules
  213. if ((_.context()->target_env == SPV_ENV_VULKAN_1_1 ||
  214. _.context()->target_env == SPV_ENV_VULKAN_1_2) &&
  215. value != SpvScopeDevice && value != SpvScopeWorkgroup &&
  216. value != SpvScopeSubgroup && value != SpvScopeInvocation &&
  217. value != SpvScopeShaderCallKHR) {
  218. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  219. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  220. << ": in Vulkan 1.1 and 1.2 environment Memory Scope is limited "
  221. << "to Device, Workgroup, Invocation, and ShaderCall";
  222. }
  223. if (value == SpvScopeShaderCallKHR) {
  224. std::string errorVUID = _.VkErrorID(4640);
  225. _.function(inst->function()->id())
  226. ->RegisterExecutionModelLimitation(
  227. [errorVUID](SpvExecutionModel model, std::string* message) {
  228. if (model != SpvExecutionModelRayGenerationKHR &&
  229. model != SpvExecutionModelIntersectionKHR &&
  230. model != SpvExecutionModelAnyHitKHR &&
  231. model != SpvExecutionModelClosestHitKHR &&
  232. model != SpvExecutionModelMissKHR &&
  233. model != SpvExecutionModelCallableKHR) {
  234. if (message) {
  235. *message =
  236. errorVUID +
  237. "ShaderCallKHR Memory Scope requires a ray tracing "
  238. "execution model";
  239. }
  240. return false;
  241. }
  242. return true;
  243. });
  244. }
  245. if (value == SpvScopeWorkgroup) {
  246. std::string errorVUID = _.VkErrorID(4639);
  247. _.function(inst->function()->id())
  248. ->RegisterExecutionModelLimitation(
  249. [errorVUID](SpvExecutionModel model, std::string* message) {
  250. if (model != SpvExecutionModelGLCompute &&
  251. model != SpvExecutionModelTaskNV &&
  252. model != SpvExecutionModelMeshNV) {
  253. if (message) {
  254. *message = errorVUID +
  255. "Workgroup Memory Scope is limited to MeshNV, "
  256. "TaskNV, and GLCompute execution model";
  257. }
  258. return false;
  259. }
  260. return true;
  261. });
  262. }
  263. }
  264. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  265. return SPV_SUCCESS;
  266. }
  267. } // namespace val
  268. } // namespace spvtools