validate_memory_semantics.cpp 11 KB

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