validate_scopes.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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<spv::Scope>(scope)) {
  25. case spv::Scope::CrossDevice:
  26. case spv::Scope::Device:
  27. case spv::Scope::Workgroup:
  28. case spv::Scope::Subgroup:
  29. case spv::Scope::Invocation:
  30. case spv::Scope::QueueFamilyKHR:
  31. case spv::Scope::ShaderCallKHR:
  32. return true;
  33. case spv::Scope::Max:
  34. break;
  35. }
  36. return false;
  37. }
  38. spv_result_t ValidateScope(ValidationState_t& _, const Instruction* inst,
  39. uint32_t scope) {
  40. spv::Op 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(spv::Capability::Shader) &&
  50. !_.HasCapability(spv::Capability::CooperativeMatrixNV)) {
  51. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  52. << "Scope ids must be OpConstant when Shader capability is "
  53. << "present";
  54. }
  55. if (_.HasCapability(spv::Capability::Shader) &&
  56. _.HasCapability(spv::Capability::CooperativeMatrixNV) &&
  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. spv::Op opcode = inst->opcode();
  72. bool is_int32 = false, is_const_int32 = false;
  73. uint32_t tmp_value = 0;
  74. std::tie(is_int32, is_const_int32, tmp_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. spv::Scope value = spv::Scope(tmp_value);
  82. // Vulkan specific rules
  83. if (spvIsVulkanEnv(_.context()->target_env)) {
  84. // Vulkan 1.1 specific rules
  85. if (_.context()->target_env != SPV_ENV_VULKAN_1_0) {
  86. // Scope for Non Uniform Group Operations must be limited to Subgroup
  87. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  88. value != spv::Scope::Subgroup) {
  89. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  90. << _.VkErrorID(4642) << spvOpcodeString(opcode)
  91. << ": in Vulkan environment Execution scope is limited to "
  92. << "Subgroup";
  93. }
  94. }
  95. // OpControlBarrier must only use Subgroup execution scope for a subset of
  96. // execution models.
  97. if (opcode == spv::Op::OpControlBarrier && value != spv::Scope::Subgroup) {
  98. std::string errorVUID = _.VkErrorID(4682);
  99. _.function(inst->function()->id())
  100. ->RegisterExecutionModelLimitation([errorVUID](
  101. spv::ExecutionModel model,
  102. std::string* message) {
  103. if (model == spv::ExecutionModel::Fragment ||
  104. model == spv::ExecutionModel::Vertex ||
  105. model == spv::ExecutionModel::Geometry ||
  106. model == spv::ExecutionModel::TessellationEvaluation ||
  107. model == spv::ExecutionModel::RayGenerationKHR ||
  108. model == spv::ExecutionModel::IntersectionKHR ||
  109. model == spv::ExecutionModel::AnyHitKHR ||
  110. model == spv::ExecutionModel::ClosestHitKHR ||
  111. model == spv::ExecutionModel::MissKHR) {
  112. if (message) {
  113. *message =
  114. errorVUID +
  115. "in Vulkan environment, OpControlBarrier execution scope "
  116. "must be Subgroup for Fragment, Vertex, Geometry, "
  117. "TessellationEvaluation, RayGeneration, Intersection, "
  118. "AnyHit, ClosestHit, and Miss execution models";
  119. }
  120. return false;
  121. }
  122. return true;
  123. });
  124. }
  125. // Only subset of execution models support Workgroup.
  126. if (value == spv::Scope::Workgroup) {
  127. std::string errorVUID = _.VkErrorID(4637);
  128. _.function(inst->function()->id())
  129. ->RegisterExecutionModelLimitation(
  130. [errorVUID](spv::ExecutionModel model, std::string* message) {
  131. if (model != spv::ExecutionModel::TaskNV &&
  132. model != spv::ExecutionModel::MeshNV &&
  133. model != spv::ExecutionModel::TaskEXT &&
  134. model != spv::ExecutionModel::MeshEXT &&
  135. model != spv::ExecutionModel::TessellationControl &&
  136. model != spv::ExecutionModel::GLCompute) {
  137. if (message) {
  138. *message =
  139. errorVUID +
  140. "in Vulkan environment, Workgroup execution scope is "
  141. "only for TaskNV, MeshNV, TaskEXT, MeshEXT, "
  142. "TessellationControl, and GLCompute execution models";
  143. }
  144. return false;
  145. }
  146. return true;
  147. });
  148. }
  149. // Vulkan generic rules
  150. // Scope for execution must be limited to Workgroup or Subgroup
  151. if (value != spv::Scope::Workgroup && value != spv::Scope::Subgroup) {
  152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  153. << _.VkErrorID(4636) << spvOpcodeString(opcode)
  154. << ": in Vulkan environment Execution Scope is limited to "
  155. << "Workgroup and Subgroup";
  156. }
  157. }
  158. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  159. // General SPIRV rules
  160. // Scope for execution must be limited to Workgroup or Subgroup for
  161. // non-uniform operations
  162. if (spvOpcodeIsNonUniformGroupOperation(opcode) &&
  163. value != spv::Scope::Subgroup && value != spv::Scope::Workgroup) {
  164. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  165. << spvOpcodeString(opcode)
  166. << ": Execution scope is limited to Subgroup or Workgroup";
  167. }
  168. return SPV_SUCCESS;
  169. }
  170. spv_result_t ValidateMemoryScope(ValidationState_t& _, const Instruction* inst,
  171. uint32_t scope) {
  172. const spv::Op opcode = inst->opcode();
  173. bool is_int32 = false, is_const_int32 = false;
  174. uint32_t tmp_value = 0;
  175. std::tie(is_int32, is_const_int32, tmp_value) = _.EvalInt32IfConst(scope);
  176. if (auto error = ValidateScope(_, inst, scope)) {
  177. return error;
  178. }
  179. if (!is_const_int32) {
  180. return SPV_SUCCESS;
  181. }
  182. spv::Scope value = spv::Scope(tmp_value);
  183. if (value == spv::Scope::QueueFamilyKHR) {
  184. if (_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  185. return SPV_SUCCESS;
  186. } else {
  187. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  188. << spvOpcodeString(opcode)
  189. << ": Memory Scope QueueFamilyKHR requires capability "
  190. << "VulkanMemoryModelKHR";
  191. }
  192. }
  193. if (value == spv::Scope::Device &&
  194. _.HasCapability(spv::Capability::VulkanMemoryModelKHR) &&
  195. !_.HasCapability(spv::Capability::VulkanMemoryModelDeviceScopeKHR)) {
  196. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  197. << "Use of device scope with VulkanKHR memory model requires the "
  198. << "VulkanMemoryModelDeviceScopeKHR capability";
  199. }
  200. // Vulkan Specific rules
  201. if (spvIsVulkanEnv(_.context()->target_env)) {
  202. if (value != spv::Scope::Device && value != spv::Scope::Workgroup &&
  203. value != spv::Scope::Subgroup && value != spv::Scope::Invocation &&
  204. value != spv::Scope::ShaderCallKHR &&
  205. value != spv::Scope::QueueFamily) {
  206. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  207. << _.VkErrorID(4638) << spvOpcodeString(opcode)
  208. << ": in Vulkan environment Memory Scope is limited to Device, "
  209. "QueueFamily, Workgroup, ShaderCallKHR, Subgroup, or "
  210. "Invocation";
  211. } else if (_.context()->target_env == SPV_ENV_VULKAN_1_0 &&
  212. value == spv::Scope::Subgroup &&
  213. !_.HasCapability(spv::Capability::SubgroupBallotKHR) &&
  214. !_.HasCapability(spv::Capability::SubgroupVoteKHR)) {
  215. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  216. << _.VkErrorID(6997) << spvOpcodeString(opcode)
  217. << ": in Vulkan 1.0 environment Memory Scope is can not be "
  218. "Subgroup without SubgroupBallotKHR or SubgroupVoteKHR "
  219. "declared";
  220. }
  221. if (value == spv::Scope::ShaderCallKHR) {
  222. std::string errorVUID = _.VkErrorID(4640);
  223. _.function(inst->function()->id())
  224. ->RegisterExecutionModelLimitation(
  225. [errorVUID](spv::ExecutionModel model, std::string* message) {
  226. if (model != spv::ExecutionModel::RayGenerationKHR &&
  227. model != spv::ExecutionModel::IntersectionKHR &&
  228. model != spv::ExecutionModel::AnyHitKHR &&
  229. model != spv::ExecutionModel::ClosestHitKHR &&
  230. model != spv::ExecutionModel::MissKHR &&
  231. model != spv::ExecutionModel::CallableKHR) {
  232. if (message) {
  233. *message =
  234. errorVUID +
  235. "ShaderCallKHR Memory Scope requires a ray tracing "
  236. "execution model";
  237. }
  238. return false;
  239. }
  240. return true;
  241. });
  242. }
  243. if (value == spv::Scope::Workgroup) {
  244. std::string errorVUID = _.VkErrorID(7321);
  245. _.function(inst->function()->id())
  246. ->RegisterExecutionModelLimitation(
  247. [errorVUID](spv::ExecutionModel model, std::string* message) {
  248. if (model != spv::ExecutionModel::GLCompute &&
  249. model != spv::ExecutionModel::TessellationControl &&
  250. model != spv::ExecutionModel::TaskNV &&
  251. model != spv::ExecutionModel::MeshNV &&
  252. model != spv::ExecutionModel::TaskEXT &&
  253. model != spv::ExecutionModel::MeshEXT) {
  254. if (message) {
  255. *message = errorVUID +
  256. "Workgroup Memory Scope is limited to MeshNV, "
  257. "TaskNV, MeshEXT, TaskEXT, TessellationControl, "
  258. "and GLCompute execution model";
  259. }
  260. return false;
  261. }
  262. return true;
  263. });
  264. if (_.memory_model() == spv::MemoryModel::GLSL450) {
  265. errorVUID = _.VkErrorID(7320);
  266. _.function(inst->function()->id())
  267. ->RegisterExecutionModelLimitation(
  268. [errorVUID](spv::ExecutionModel model, std::string* message) {
  269. if (model == spv::ExecutionModel::TessellationControl) {
  270. if (message) {
  271. *message =
  272. errorVUID +
  273. "Workgroup Memory Scope can't be used with "
  274. "TessellationControl using GLSL450 Memory Model";
  275. }
  276. return false;
  277. }
  278. return true;
  279. });
  280. }
  281. }
  282. }
  283. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  284. return SPV_SUCCESS;
  285. }
  286. } // namespace val
  287. } // namespace spvtools