validate_memory_semantics.cpp 11 KB

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