validate_memory_semantics.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. const SpvOp 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(SpvCapabilityShader) &&
  37. !_.HasCapability(SpvCapabilityCooperativeMatrixNV)) {
  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(SpvCapabilityShader) &&
  43. _.HasCapability(SpvCapabilityCooperativeMatrixNV) &&
  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. if (spvIsWebGPUEnv(_.context()->target_env)) {
  52. uint32_t valid_bits = SpvMemorySemanticsUniformMemoryMask |
  53. SpvMemorySemanticsWorkgroupMemoryMask |
  54. SpvMemorySemanticsImageMemoryMask |
  55. SpvMemorySemanticsOutputMemoryKHRMask |
  56. SpvMemorySemanticsMakeAvailableKHRMask |
  57. SpvMemorySemanticsMakeVisibleKHRMask;
  58. if (!spvOpcodeIsAtomicOp(inst->opcode())) {
  59. valid_bits |= SpvMemorySemanticsAcquireReleaseMask;
  60. }
  61. if (value & ~valid_bits) {
  62. if (spvOpcodeIsAtomicOp(inst->opcode())) {
  63. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  64. << "WebGPU spec disallows, for OpAtomic*, any bit masks in "
  65. "Memory Semantics that are not UniformMemory, "
  66. "WorkgroupMemory, ImageMemory, or OutputMemoryKHR";
  67. } else {
  68. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  69. << "WebGPU spec disallows any bit masks in Memory Semantics "
  70. "that are not AcquireRelease, UniformMemory, "
  71. "WorkgroupMemory, ImageMemory, OutputMemoryKHR, "
  72. "MakeAvailableKHR, or MakeVisibleKHR";
  73. }
  74. }
  75. if (!spvOpcodeIsAtomicOp(inst->opcode()) &&
  76. !(value & SpvMemorySemanticsAcquireReleaseMask)) {
  77. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  78. << "WebGPU spec requires AcquireRelease to set in Memory "
  79. "Semantics.";
  80. }
  81. }
  82. const size_t num_memory_order_set_bits = spvtools::utils::CountSetBits(
  83. value & (SpvMemorySemanticsAcquireMask | SpvMemorySemanticsReleaseMask |
  84. SpvMemorySemanticsAcquireReleaseMask |
  85. SpvMemorySemanticsSequentiallyConsistentMask));
  86. if (num_memory_order_set_bits > 1) {
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << spvOpcodeString(opcode)
  89. << ": Memory Semantics can have at most one of the following "
  90. "bits "
  91. "set: Acquire, Release, AcquireRelease or "
  92. "SequentiallyConsistent";
  93. }
  94. if (_.memory_model() == SpvMemoryModelVulkanKHR &&
  95. value & SpvMemorySemanticsSequentiallyConsistentMask) {
  96. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  97. << "SequentiallyConsistent memory "
  98. "semantics cannot be used with "
  99. "the VulkanKHR memory model.";
  100. }
  101. if (value & SpvMemorySemanticsMakeAvailableKHRMask &&
  102. !_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  103. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  104. << spvOpcodeString(opcode)
  105. << ": Memory Semantics MakeAvailableKHR requires capability "
  106. << "VulkanMemoryModelKHR";
  107. }
  108. if (value & SpvMemorySemanticsMakeVisibleKHRMask &&
  109. !_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  110. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  111. << spvOpcodeString(opcode)
  112. << ": Memory Semantics MakeVisibleKHR requires capability "
  113. << "VulkanMemoryModelKHR";
  114. }
  115. if (value & SpvMemorySemanticsOutputMemoryKHRMask &&
  116. !_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  117. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  118. << spvOpcodeString(opcode)
  119. << ": Memory Semantics OutputMemoryKHR requires capability "
  120. << "VulkanMemoryModelKHR";
  121. }
  122. if (value & SpvMemorySemanticsVolatileMask) {
  123. if (!_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  125. << spvOpcodeString(opcode)
  126. << ": Memory Semantics Volatile requires capability "
  127. "VulkanMemoryModelKHR";
  128. }
  129. if (!spvOpcodeIsAtomicOp(inst->opcode())) {
  130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  131. << "Memory Semantics Volatile can only be used with atomic "
  132. "instructions";
  133. }
  134. }
  135. if (value & SpvMemorySemanticsUniformMemoryMask &&
  136. !_.HasCapability(SpvCapabilityShader)) {
  137. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  138. << spvOpcodeString(opcode)
  139. << ": Memory Semantics UniformMemory requires capability Shader";
  140. }
  141. // Checking for SpvCapabilityAtomicStorage is intentionally not done here. See
  142. // https://github.com/KhronosGroup/glslang/issues/1618 for the reasoning why.
  143. if (value & (SpvMemorySemanticsMakeAvailableKHRMask |
  144. SpvMemorySemanticsMakeVisibleKHRMask)) {
  145. const bool includes_storage_class =
  146. value & (SpvMemorySemanticsUniformMemoryMask |
  147. SpvMemorySemanticsSubgroupMemoryMask |
  148. SpvMemorySemanticsWorkgroupMemoryMask |
  149. SpvMemorySemanticsCrossWorkgroupMemoryMask |
  150. SpvMemorySemanticsAtomicCounterMemoryMask |
  151. SpvMemorySemanticsImageMemoryMask |
  152. SpvMemorySemanticsOutputMemoryKHRMask);
  153. if (!includes_storage_class) {
  154. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  155. << spvOpcodeString(opcode)
  156. << ": expected Memory Semantics to include a storage class";
  157. }
  158. }
  159. if (value & SpvMemorySemanticsMakeVisibleKHRMask &&
  160. !(value & (SpvMemorySemanticsAcquireMask |
  161. SpvMemorySemanticsAcquireReleaseMask))) {
  162. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  163. << spvOpcodeString(opcode)
  164. << ": MakeVisibleKHR Memory Semantics also requires either Acquire "
  165. "or AcquireRelease Memory Semantics";
  166. }
  167. if (value & SpvMemorySemanticsMakeAvailableKHRMask &&
  168. !(value & (SpvMemorySemanticsReleaseMask |
  169. SpvMemorySemanticsAcquireReleaseMask))) {
  170. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  171. << spvOpcodeString(opcode)
  172. << ": MakeAvailableKHR Memory Semantics also requires either "
  173. "Release or AcquireRelease Memory Semantics";
  174. }
  175. if (spvIsVulkanEnv(_.context()->target_env)) {
  176. const bool includes_storage_class =
  177. value & (SpvMemorySemanticsUniformMemoryMask |
  178. SpvMemorySemanticsWorkgroupMemoryMask |
  179. SpvMemorySemanticsImageMemoryMask |
  180. SpvMemorySemanticsOutputMemoryKHRMask);
  181. if (opcode == SpvOpMemoryBarrier && !num_memory_order_set_bits) {
  182. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  183. << spvOpcodeString(opcode)
  184. << ": Vulkan specification requires Memory Semantics to have "
  185. "one "
  186. "of the following bits set: Acquire, Release, "
  187. "AcquireRelease "
  188. "or SequentiallyConsistent";
  189. }
  190. if (opcode == SpvOpMemoryBarrier && !includes_storage_class) {
  191. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  192. << spvOpcodeString(opcode)
  193. << ": expected Memory Semantics to include a Vulkan-supported "
  194. "storage class";
  195. }
  196. #if 0
  197. // TODO([email protected]): this check fails Vulkan CTS, reenable once fixed.
  198. if (opcode == SpvOpControlBarrier && value && !includes_storage_class) {
  199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  200. << spvOpcodeString(opcode)
  201. << ": expected Memory Semantics to include a Vulkan-supported "
  202. "storage class if Memory Semantics is not None";
  203. }
  204. #endif
  205. }
  206. if (opcode == SpvOpAtomicFlagClear &&
  207. (value & SpvMemorySemanticsAcquireMask ||
  208. value & SpvMemorySemanticsAcquireReleaseMask)) {
  209. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  210. << "Memory Semantics Acquire and AcquireRelease cannot be used "
  211. "with "
  212. << spvOpcodeString(opcode);
  213. }
  214. if (opcode == SpvOpAtomicCompareExchange && operand_index == 5 &&
  215. (value & SpvMemorySemanticsReleaseMask ||
  216. value & SpvMemorySemanticsAcquireReleaseMask)) {
  217. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  218. << spvOpcodeString(opcode)
  219. << ": Memory Semantics Release and AcquireRelease cannot be "
  220. "used "
  221. "for operand Unequal";
  222. }
  223. if (spvIsVulkanEnv(_.context()->target_env)) {
  224. if (opcode == SpvOpAtomicLoad &&
  225. (value & SpvMemorySemanticsReleaseMask ||
  226. value & SpvMemorySemanticsAcquireReleaseMask ||
  227. value & SpvMemorySemanticsSequentiallyConsistentMask)) {
  228. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  229. << "Vulkan spec disallows OpAtomicLoad with Memory Semantics "
  230. "Release, AcquireRelease and SequentiallyConsistent";
  231. }
  232. if (opcode == SpvOpAtomicStore &&
  233. (value & SpvMemorySemanticsAcquireMask ||
  234. value & SpvMemorySemanticsAcquireReleaseMask ||
  235. value & SpvMemorySemanticsSequentiallyConsistentMask)) {
  236. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  237. << "Vulkan spec disallows OpAtomicStore with Memory Semantics "
  238. "Acquire, AcquireRelease and SequentiallyConsistent";
  239. }
  240. }
  241. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  242. return SPV_SUCCESS;
  243. }
  244. } // namespace val
  245. } // namespace spvtools