validate_memory_semantics.cpp 9.5 KB

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