validate_memory_semantics.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 set: Acquire, Release, AcquireRelease or "
  61. "SequentiallyConsistent";
  62. }
  63. if (_.memory_model() == spv::MemoryModel::VulkanKHR &&
  64. value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent)) {
  65. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  66. << "SequentiallyConsistent memory "
  67. "semantics cannot be used with "
  68. "the VulkanKHR memory model.";
  69. }
  70. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR) &&
  71. !_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  72. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  73. << spvOpcodeString(opcode)
  74. << ": Memory Semantics MakeAvailableKHR requires capability "
  75. << "VulkanMemoryModelKHR";
  76. }
  77. if (value & uint32_t(spv::MemorySemanticsMask::MakeVisibleKHR) &&
  78. !_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  79. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  80. << spvOpcodeString(opcode)
  81. << ": Memory Semantics MakeVisibleKHR requires capability "
  82. << "VulkanMemoryModelKHR";
  83. }
  84. if (value & uint32_t(spv::MemorySemanticsMask::OutputMemoryKHR) &&
  85. !_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  86. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  87. << spvOpcodeString(opcode)
  88. << ": Memory Semantics OutputMemoryKHR requires capability "
  89. << "VulkanMemoryModelKHR";
  90. }
  91. if (value & uint32_t(spv::MemorySemanticsMask::Volatile)) {
  92. if (!_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  93. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  94. << spvOpcodeString(opcode)
  95. << ": Memory Semantics Volatile requires capability "
  96. "VulkanMemoryModelKHR";
  97. }
  98. if (!spvOpcodeIsAtomicOp(inst->opcode())) {
  99. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  100. << "Memory Semantics Volatile can only be used with atomic "
  101. "instructions";
  102. }
  103. }
  104. if (value & uint32_t(spv::MemorySemanticsMask::UniformMemory) &&
  105. !_.HasCapability(spv::Capability::Shader)) {
  106. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  107. << spvOpcodeString(opcode)
  108. << ": Memory Semantics UniformMemory requires capability Shader";
  109. }
  110. // Checking for spv::Capability::AtomicStorage is intentionally not done here.
  111. // See https://github.com/KhronosGroup/glslang/issues/1618 for the reasoning
  112. // why.
  113. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR |
  114. spv::MemorySemanticsMask::MakeVisibleKHR)) {
  115. const bool includes_storage_class =
  116. value & uint32_t(spv::MemorySemanticsMask::UniformMemory |
  117. spv::MemorySemanticsMask::SubgroupMemory |
  118. spv::MemorySemanticsMask::WorkgroupMemory |
  119. spv::MemorySemanticsMask::CrossWorkgroupMemory |
  120. spv::MemorySemanticsMask::AtomicCounterMemory |
  121. spv::MemorySemanticsMask::ImageMemory |
  122. spv::MemorySemanticsMask::OutputMemoryKHR);
  123. if (!includes_storage_class) {
  124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  125. << spvOpcodeString(opcode)
  126. << ": expected Memory Semantics to include a storage class";
  127. }
  128. }
  129. if (value & uint32_t(spv::MemorySemanticsMask::MakeVisibleKHR) &&
  130. !(value & uint32_t(spv::MemorySemanticsMask::Acquire |
  131. spv::MemorySemanticsMask::AcquireRelease))) {
  132. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  133. << spvOpcodeString(opcode)
  134. << ": MakeVisibleKHR Memory Semantics also requires either Acquire "
  135. "or AcquireRelease Memory Semantics";
  136. }
  137. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR) &&
  138. !(value & uint32_t(spv::MemorySemanticsMask::Release |
  139. spv::MemorySemanticsMask::AcquireRelease))) {
  140. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  141. << spvOpcodeString(opcode)
  142. << ": MakeAvailableKHR Memory Semantics also requires either "
  143. "Release or AcquireRelease Memory Semantics";
  144. }
  145. if (spvIsVulkanEnv(_.context()->target_env)) {
  146. const bool includes_storage_class =
  147. value & uint32_t(spv::MemorySemanticsMask::UniformMemory |
  148. spv::MemorySemanticsMask::WorkgroupMemory |
  149. spv::MemorySemanticsMask::ImageMemory |
  150. spv::MemorySemanticsMask::OutputMemoryKHR);
  151. if (opcode == spv::Op::OpMemoryBarrier && !num_memory_order_set_bits) {
  152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  153. << _.VkErrorID(4732) << spvOpcodeString(opcode)
  154. << ": Vulkan specification requires Memory Semantics to have "
  155. "one of the following bits set: Acquire, Release, "
  156. "AcquireRelease or SequentiallyConsistent";
  157. } else if (opcode != spv::Op::OpMemoryBarrier &&
  158. num_memory_order_set_bits) {
  159. // should leave only atomics and control barriers for Vulkan env
  160. bool memory_is_int32 = false, memory_is_const_int32 = false;
  161. uint32_t memory_value = 0;
  162. std::tie(memory_is_int32, memory_is_const_int32, memory_value) =
  163. _.EvalInt32IfConst(memory_scope);
  164. if (memory_is_int32 &&
  165. spv::Scope(memory_value) == spv::Scope::Invocation) {
  166. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  167. << _.VkErrorID(4641) << spvOpcodeString(opcode)
  168. << ": Vulkan specification requires Memory Semantics to be None "
  169. "if used with Invocation Memory Scope";
  170. }
  171. }
  172. if (opcode == spv::Op::OpMemoryBarrier && !includes_storage_class) {
  173. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  174. << _.VkErrorID(4733) << spvOpcodeString(opcode)
  175. << ": expected Memory Semantics to include a Vulkan-supported "
  176. "storage class";
  177. }
  178. if (opcode == spv::Op::OpControlBarrier && value) {
  179. if (!num_memory_order_set_bits) {
  180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  181. << _.VkErrorID(10609) << spvOpcodeString(opcode)
  182. << ": Vulkan specification requires non-zero Memory Semantics "
  183. "to have one of the following bits set: Acquire, Release, "
  184. "AcquireRelease or SequentiallyConsistent";
  185. }
  186. if (!includes_storage_class) {
  187. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  188. << _.VkErrorID(4650) << spvOpcodeString(opcode)
  189. << ": expected Memory Semantics to include a Vulkan-supported "
  190. "storage class if Memory Semantics is not None";
  191. }
  192. }
  193. }
  194. if (opcode == spv::Op::OpAtomicFlagClear &&
  195. (value & uint32_t(spv::MemorySemanticsMask::Acquire) ||
  196. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease))) {
  197. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  198. << "Memory Semantics Acquire and AcquireRelease cannot be used "
  199. "with "
  200. << spvOpcodeString(opcode);
  201. }
  202. if (opcode == spv::Op::OpAtomicCompareExchange && operand_index == 5 &&
  203. (value & uint32_t(spv::MemorySemanticsMask::Release) ||
  204. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease))) {
  205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  206. << spvOpcodeString(opcode)
  207. << ": Memory Semantics Release and AcquireRelease cannot be "
  208. "used "
  209. "for operand Unequal";
  210. }
  211. if (spvIsVulkanEnv(_.context()->target_env)) {
  212. if (opcode == spv::Op::OpAtomicLoad &&
  213. (value & uint32_t(spv::MemorySemanticsMask::Release) ||
  214. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease) ||
  215. value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent))) {
  216. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  217. << _.VkErrorID(4731)
  218. << "Vulkan spec disallows OpAtomicLoad with Memory Semantics "
  219. "Release, AcquireRelease and SequentiallyConsistent";
  220. }
  221. if (opcode == spv::Op::OpAtomicStore &&
  222. (value & uint32_t(spv::MemorySemanticsMask::Acquire) ||
  223. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease) ||
  224. value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent))) {
  225. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  226. << _.VkErrorID(4730)
  227. << "Vulkan spec disallows OpAtomicStore with Memory Semantics "
  228. "Acquire, AcquireRelease and SequentiallyConsistent";
  229. }
  230. }
  231. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  232. return SPV_SUCCESS;
  233. }
  234. } // namespace val
  235. } // namespace spvtools