validate_memory_semantics.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_memory_semantics.h"
  15. #include "source/spirv_target_env.h"
  16. #include "source/util/bitutils.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. spv_result_t ValidateMemorySemantics(ValidationState_t& _,
  22. const Instruction* inst,
  23. uint32_t operand_index,
  24. uint32_t memory_scope) {
  25. const spv::Op opcode = inst->opcode();
  26. const auto id = inst->GetOperandAs<const uint32_t>(operand_index);
  27. bool is_int32 = false, is_const_int32 = false;
  28. uint32_t value = 0;
  29. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(id);
  30. if (!is_int32) {
  31. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  32. << spvOpcodeString(opcode)
  33. << ": expected Memory Semantics to be a 32-bit int";
  34. }
  35. if (!is_const_int32) {
  36. if (_.HasCapability(spv::Capability::Shader) &&
  37. !_.HasCapability(spv::Capability::CooperativeMatrixNV)) {
  38. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  39. << "Memory Semantics ids must be OpConstant when Shader "
  40. "capability is present";
  41. }
  42. if (_.HasCapability(spv::Capability::Shader) &&
  43. _.HasCapability(spv::Capability::CooperativeMatrixNV) &&
  44. !spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  45. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  46. << "Memory Semantics must be a constant instruction when "
  47. "CooperativeMatrixNV capability is present";
  48. }
  49. return SPV_SUCCESS;
  50. }
  51. const size_t num_memory_order_set_bits = spvtools::utils::CountSetBits(
  52. value & uint32_t(spv::MemorySemanticsMask::Acquire |
  53. spv::MemorySemanticsMask::Release |
  54. spv::MemorySemanticsMask::AcquireRelease |
  55. spv::MemorySemanticsMask::SequentiallyConsistent));
  56. if (num_memory_order_set_bits > 1) {
  57. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  58. << spvOpcodeString(opcode)
  59. << ": Memory Semantics can have at most one of the following "
  60. "bits "
  61. "set: Acquire, Release, AcquireRelease or "
  62. "SequentiallyConsistent";
  63. }
  64. if (_.memory_model() == spv::MemoryModel::VulkanKHR &&
  65. value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent)) {
  66. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  67. << "SequentiallyConsistent memory "
  68. "semantics cannot be used with "
  69. "the VulkanKHR memory model.";
  70. }
  71. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR) &&
  72. !_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  73. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  74. << spvOpcodeString(opcode)
  75. << ": Memory Semantics MakeAvailableKHR requires capability "
  76. << "VulkanMemoryModelKHR";
  77. }
  78. if (value & uint32_t(spv::MemorySemanticsMask::MakeVisibleKHR) &&
  79. !_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  80. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  81. << spvOpcodeString(opcode)
  82. << ": Memory Semantics MakeVisibleKHR requires capability "
  83. << "VulkanMemoryModelKHR";
  84. }
  85. if (value & uint32_t(spv::MemorySemanticsMask::OutputMemoryKHR) &&
  86. !_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << spvOpcodeString(opcode)
  89. << ": Memory Semantics OutputMemoryKHR requires capability "
  90. << "VulkanMemoryModelKHR";
  91. }
  92. if (value & uint32_t(spv::MemorySemanticsMask::Volatile)) {
  93. if (!_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  94. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  95. << spvOpcodeString(opcode)
  96. << ": Memory Semantics Volatile requires capability "
  97. "VulkanMemoryModelKHR";
  98. }
  99. if (!spvOpcodeIsAtomicOp(inst->opcode())) {
  100. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  101. << "Memory Semantics Volatile can only be used with atomic "
  102. "instructions";
  103. }
  104. }
  105. if (value & uint32_t(spv::MemorySemanticsMask::UniformMemory) &&
  106. !_.HasCapability(spv::Capability::Shader)) {
  107. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  108. << spvOpcodeString(opcode)
  109. << ": Memory Semantics UniformMemory requires capability Shader";
  110. }
  111. // Checking for spv::Capability::AtomicStorage is intentionally not done here.
  112. // See https://github.com/KhronosGroup/glslang/issues/1618 for the reasoning
  113. // why.
  114. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR |
  115. spv::MemorySemanticsMask::MakeVisibleKHR)) {
  116. const bool includes_storage_class =
  117. value & uint32_t(spv::MemorySemanticsMask::UniformMemory |
  118. spv::MemorySemanticsMask::SubgroupMemory |
  119. spv::MemorySemanticsMask::WorkgroupMemory |
  120. spv::MemorySemanticsMask::CrossWorkgroupMemory |
  121. spv::MemorySemanticsMask::AtomicCounterMemory |
  122. spv::MemorySemanticsMask::ImageMemory |
  123. spv::MemorySemanticsMask::OutputMemoryKHR);
  124. if (!includes_storage_class) {
  125. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  126. << spvOpcodeString(opcode)
  127. << ": expected Memory Semantics to include a storage class";
  128. }
  129. }
  130. if (value & uint32_t(spv::MemorySemanticsMask::MakeVisibleKHR) &&
  131. !(value & uint32_t(spv::MemorySemanticsMask::Acquire |
  132. spv::MemorySemanticsMask::AcquireRelease))) {
  133. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  134. << spvOpcodeString(opcode)
  135. << ": MakeVisibleKHR Memory Semantics also requires either Acquire "
  136. "or AcquireRelease Memory Semantics";
  137. }
  138. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR) &&
  139. !(value & uint32_t(spv::MemorySemanticsMask::Release |
  140. spv::MemorySemanticsMask::AcquireRelease))) {
  141. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  142. << spvOpcodeString(opcode)
  143. << ": MakeAvailableKHR Memory Semantics also requires either "
  144. "Release or AcquireRelease Memory Semantics";
  145. }
  146. if (spvIsVulkanEnv(_.context()->target_env)) {
  147. const bool includes_storage_class =
  148. value & uint32_t(spv::MemorySemanticsMask::UniformMemory |
  149. spv::MemorySemanticsMask::WorkgroupMemory |
  150. spv::MemorySemanticsMask::ImageMemory |
  151. spv::MemorySemanticsMask::OutputMemoryKHR);
  152. if (opcode == spv::Op::OpMemoryBarrier && !num_memory_order_set_bits) {
  153. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  154. << _.VkErrorID(4732) << spvOpcodeString(opcode)
  155. << ": Vulkan specification requires Memory Semantics to have "
  156. "one "
  157. "of the following bits set: Acquire, Release, "
  158. "AcquireRelease "
  159. "or SequentiallyConsistent";
  160. } else if (opcode != spv::Op::OpMemoryBarrier &&
  161. num_memory_order_set_bits) {
  162. // should leave only atomics and control barriers for Vulkan env
  163. bool memory_is_int32 = false, memory_is_const_int32 = false;
  164. uint32_t memory_value = 0;
  165. std::tie(memory_is_int32, memory_is_const_int32, memory_value) =
  166. _.EvalInt32IfConst(memory_scope);
  167. if (memory_is_int32 &&
  168. spv::Scope(memory_value) == spv::Scope::Invocation) {
  169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  170. << _.VkErrorID(4641) << spvOpcodeString(opcode)
  171. << ": Vulkan specification requires Memory Semantics to be None "
  172. "if used with Invocation Memory Scope";
  173. }
  174. }
  175. if (opcode == spv::Op::OpMemoryBarrier && !includes_storage_class) {
  176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  177. << _.VkErrorID(4733) << spvOpcodeString(opcode)
  178. << ": expected Memory Semantics to include a Vulkan-supported "
  179. "storage class";
  180. }
  181. if (opcode == spv::Op::OpControlBarrier && value && !includes_storage_class) {
  182. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  183. << _.VkErrorID(4650) << spvOpcodeString(opcode)
  184. << ": expected Memory Semantics to include a Vulkan-supported "
  185. "storage class if Memory Semantics is not None";
  186. }
  187. }
  188. if (opcode == spv::Op::OpAtomicFlagClear &&
  189. (value & uint32_t(spv::MemorySemanticsMask::Acquire) ||
  190. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease))) {
  191. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  192. << "Memory Semantics Acquire and AcquireRelease cannot be used "
  193. "with "
  194. << spvOpcodeString(opcode);
  195. }
  196. if (opcode == spv::Op::OpAtomicCompareExchange && operand_index == 5 &&
  197. (value & uint32_t(spv::MemorySemanticsMask::Release) ||
  198. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease))) {
  199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  200. << spvOpcodeString(opcode)
  201. << ": Memory Semantics Release and AcquireRelease cannot be "
  202. "used "
  203. "for operand Unequal";
  204. }
  205. if (spvIsVulkanEnv(_.context()->target_env)) {
  206. if (opcode == spv::Op::OpAtomicLoad &&
  207. (value & uint32_t(spv::MemorySemanticsMask::Release) ||
  208. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease) ||
  209. value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent))) {
  210. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  211. << _.VkErrorID(4731)
  212. << "Vulkan spec disallows OpAtomicLoad with Memory Semantics "
  213. "Release, AcquireRelease and SequentiallyConsistent";
  214. }
  215. if (opcode == spv::Op::OpAtomicStore &&
  216. (value & uint32_t(spv::MemorySemanticsMask::Acquire) ||
  217. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease) ||
  218. value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent))) {
  219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  220. << _.VkErrorID(4730)
  221. << "Vulkan spec disallows OpAtomicStore with Memory Semantics "
  222. "Acquire, AcquireRelease and SequentiallyConsistent";
  223. }
  224. }
  225. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  226. return SPV_SUCCESS;
  227. }
  228. } // namespace val
  229. } // namespace spvtools