validate_memory_semantics.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 SpvOp 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(SpvCapabilityShader) &&
  38. !_.HasCapability(SpvCapabilityCooperativeMatrixNV)) {
  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(SpvCapabilityShader) &&
  44. _.HasCapability(SpvCapabilityCooperativeMatrixNV) &&
  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 & (SpvMemorySemanticsAcquireMask | SpvMemorySemanticsReleaseMask |
  54. SpvMemorySemanticsAcquireReleaseMask |
  55. SpvMemorySemanticsSequentiallyConsistentMask));
  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 "
  61. "set: Acquire, Release, AcquireRelease or "
  62. "SequentiallyConsistent";
  63. }
  64. if (_.memory_model() == SpvMemoryModelVulkanKHR &&
  65. value & SpvMemorySemanticsSequentiallyConsistentMask) {
  66. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  67. << "SequentiallyConsistent memory "
  68. "semantics cannot be used with "
  69. "the VulkanKHR memory model.";
  70. }
  71. if (value & SpvMemorySemanticsMakeAvailableKHRMask &&
  72. !_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  73. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  74. << spvOpcodeString(opcode)
  75. << ": Memory Semantics MakeAvailableKHR requires capability "
  76. << "VulkanMemoryModelKHR";
  77. }
  78. if (value & SpvMemorySemanticsMakeVisibleKHRMask &&
  79. !_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  80. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  81. << spvOpcodeString(opcode)
  82. << ": Memory Semantics MakeVisibleKHR requires capability "
  83. << "VulkanMemoryModelKHR";
  84. }
  85. if (value & SpvMemorySemanticsOutputMemoryKHRMask &&
  86. !_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << spvOpcodeString(opcode)
  89. << ": Memory Semantics OutputMemoryKHR requires capability "
  90. << "VulkanMemoryModelKHR";
  91. }
  92. if (value & SpvMemorySemanticsVolatileMask) {
  93. if (!_.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  94. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  95. << spvOpcodeString(opcode)
  96. << ": Memory Semantics Volatile requires capability "
  97. "VulkanMemoryModelKHR";
  98. }
  99. if (!spvOpcodeIsAtomicOp(inst->opcode())) {
  100. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  101. << "Memory Semantics Volatile can only be used with atomic "
  102. "instructions";
  103. }
  104. }
  105. if (value & SpvMemorySemanticsUniformMemoryMask &&
  106. !_.HasCapability(SpvCapabilityShader)) {
  107. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  108. << spvOpcodeString(opcode)
  109. << ": Memory Semantics UniformMemory requires capability Shader";
  110. }
  111. // Checking for SpvCapabilityAtomicStorage is intentionally not done here. See
  112. // https://github.com/KhronosGroup/glslang/issues/1618 for the reasoning why.
  113. if (value & (SpvMemorySemanticsMakeAvailableKHRMask |
  114. SpvMemorySemanticsMakeVisibleKHRMask)) {
  115. const bool includes_storage_class =
  116. value & (SpvMemorySemanticsUniformMemoryMask |
  117. SpvMemorySemanticsSubgroupMemoryMask |
  118. SpvMemorySemanticsWorkgroupMemoryMask |
  119. SpvMemorySemanticsCrossWorkgroupMemoryMask |
  120. SpvMemorySemanticsAtomicCounterMemoryMask |
  121. SpvMemorySemanticsImageMemoryMask |
  122. SpvMemorySemanticsOutputMemoryKHRMask);
  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 & SpvMemorySemanticsMakeVisibleKHRMask &&
  130. !(value & (SpvMemorySemanticsAcquireMask |
  131. SpvMemorySemanticsAcquireReleaseMask))) {
  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 & SpvMemorySemanticsMakeAvailableKHRMask &&
  138. !(value & (SpvMemorySemanticsReleaseMask |
  139. SpvMemorySemanticsAcquireReleaseMask))) {
  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 & (SpvMemorySemanticsUniformMemoryMask |
  148. SpvMemorySemanticsWorkgroupMemoryMask |
  149. SpvMemorySemanticsImageMemoryMask |
  150. SpvMemorySemanticsOutputMemoryKHRMask);
  151. if (opcode == SpvOpMemoryBarrier && !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 "
  156. "of the following bits set: Acquire, Release, "
  157. "AcquireRelease "
  158. "or SequentiallyConsistent";
  159. } else if (opcode != SpvOpMemoryBarrier && num_memory_order_set_bits) {
  160. // should leave only atomics and control barriers for Vulkan env
  161. bool memory_is_int32 = false, memory_is_const_int32 = false;
  162. uint32_t memory_value = 0;
  163. std::tie(memory_is_int32, memory_is_const_int32, memory_value) =
  164. _.EvalInt32IfConst(memory_scope);
  165. if (memory_is_int32 && memory_value == SpvScopeInvocation) {
  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 == SpvOpMemoryBarrier && !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 0
  179. // TODO([email protected]): this check fails Vulkan CTS, reenable once fixed.
  180. if (opcode == SpvOpControlBarrier && value && !includes_storage_class) {
  181. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  182. << spvOpcodeString(opcode)
  183. << ": expected Memory Semantics to include a Vulkan-supported "
  184. "storage class if Memory Semantics is not None";
  185. }
  186. #endif
  187. }
  188. if (opcode == SpvOpAtomicFlagClear &&
  189. (value & SpvMemorySemanticsAcquireMask ||
  190. value & SpvMemorySemanticsAcquireReleaseMask)) {
  191. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  192. << "Memory Semantics Acquire and AcquireRelease cannot be used "
  193. "with "
  194. << spvOpcodeString(opcode);
  195. }
  196. if (opcode == SpvOpAtomicCompareExchange && operand_index == 5 &&
  197. (value & SpvMemorySemanticsReleaseMask ||
  198. value & SpvMemorySemanticsAcquireReleaseMask)) {
  199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  200. << spvOpcodeString(opcode)
  201. << ": Memory Semantics Release and AcquireRelease cannot be "
  202. "used "
  203. "for operand Unequal";
  204. }
  205. if (spvIsVulkanEnv(_.context()->target_env)) {
  206. if (opcode == SpvOpAtomicLoad &&
  207. (value & SpvMemorySemanticsReleaseMask ||
  208. value & SpvMemorySemanticsAcquireReleaseMask ||
  209. value & SpvMemorySemanticsSequentiallyConsistentMask)) {
  210. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  211. << _.VkErrorID(4731)
  212. << "Vulkan spec disallows OpAtomicLoad with Memory Semantics "
  213. "Release, AcquireRelease and SequentiallyConsistent";
  214. }
  215. if (opcode == SpvOpAtomicStore &&
  216. (value & SpvMemorySemanticsAcquireMask ||
  217. value & SpvMemorySemanticsAcquireReleaseMask ||
  218. value & SpvMemorySemanticsSequentiallyConsistentMask)) {
  219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  220. << _.VkErrorID(4730)
  221. << "Vulkan spec disallows OpAtomicStore with Memory Semantics "
  222. "Acquire, AcquireRelease and SequentiallyConsistent";
  223. }
  224. }
  225. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  226. return SPV_SUCCESS;
  227. }
  228. } // namespace val
  229. } // namespace spvtools