validate_memory_semantics.cpp 9.5 KB

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