validate_memory_semantics.cpp 13 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/spirv_target_env.h"
  16. #include "source/util/bitutils.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. spv_result_t ValidateMemorySemantics(ValidationState_t& _,
  22. const Instruction* inst,
  23. uint32_t operand_index,
  24. uint32_t memory_scope) {
  25. const spv::Op 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. const bool is_vulkan = spvIsVulkanEnv(_.context()->target_env) ||
  31. _.memory_model() == spv::MemoryModel::VulkanKHR;
  32. if (!is_int32) {
  33. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  34. << spvOpcodeString(opcode)
  35. << ": expected Memory Semantics to be a 32-bit int";
  36. }
  37. if (!is_const_int32) {
  38. if (_.HasCapability(spv::Capability::Shader) &&
  39. !_.HasCapability(spv::Capability::CooperativeMatrixNV)) {
  40. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  41. << "Memory Semantics ids must be OpConstant when Shader "
  42. "capability is present";
  43. }
  44. if (_.HasCapability(spv::Capability::Shader) &&
  45. _.HasCapability(spv::Capability::CooperativeMatrixNV) &&
  46. !spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  47. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  48. << "Memory Semantics must be a constant instruction when "
  49. "CooperativeMatrixNV capability is present";
  50. }
  51. return SPV_SUCCESS;
  52. }
  53. if (value & uint32_t(spv::MemorySemanticsMask::UniformMemory) &&
  54. !_.HasCapability(spv::Capability::Shader)) {
  55. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  56. << spvOpcodeString(opcode)
  57. << ": Memory Semantics UniformMemory requires capability Shader";
  58. }
  59. if (value & uint32_t(spv::MemorySemanticsMask::OutputMemoryKHR) &&
  60. !_.HasCapability(spv::Capability::VulkanMemoryModel)) {
  61. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  62. << spvOpcodeString(opcode)
  63. << ": Memory Semantics OutputMemoryKHR requires capability "
  64. << "VulkanMemoryModelKHR";
  65. }
  66. const size_t num_memory_order_set_bits = spvtools::utils::CountSetBits(
  67. value & uint32_t(spv::MemorySemanticsMask::Acquire |
  68. spv::MemorySemanticsMask::Release |
  69. spv::MemorySemanticsMask::AcquireRelease |
  70. spv::MemorySemanticsMask::SequentiallyConsistent));
  71. if (num_memory_order_set_bits > 1) {
  72. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  73. << _.VkErrorID(10865) << spvOpcodeString(opcode)
  74. << ": Memory Semantics must have at most one non-relaxed "
  75. "memory order bit set";
  76. }
  77. if (is_vulkan &&
  78. (value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent))) {
  79. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  80. << _.VkErrorID(10866) << spvOpcodeString(opcode)
  81. << ": Memory Semantics with SequentiallyConsistent memory order "
  82. "must not be used in the Vulkan API";
  83. }
  84. if ((opcode == spv::Op::OpAtomicStore ||
  85. opcode == spv::Op::OpAtomicFlagClear) &&
  86. (value & uint32_t(spv::MemorySemanticsMask::Acquire) ||
  87. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease))) {
  88. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  89. << _.VkErrorID(10867) << spvOpcodeString(opcode)
  90. << ": MemorySemantics must not use Acquire or AcquireRelease "
  91. "memory order with "
  92. << spvOpcodeString(opcode);
  93. }
  94. if (opcode == spv::Op::OpAtomicLoad &&
  95. (value & uint32_t(spv::MemorySemanticsMask::Release) ||
  96. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease))) {
  97. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  98. << _.VkErrorID(10868) << spvOpcodeString(opcode)
  99. << ": MemorySemantics must not use Release or AcquireRelease "
  100. "memory order with "
  101. << spvOpcodeString(opcode);
  102. }
  103. // In OpenCL, a relaxed fence has no effect but is not explicitly forbidden
  104. if (is_vulkan && opcode == spv::Op::OpMemoryBarrier &&
  105. !num_memory_order_set_bits) {
  106. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  107. << _.VkErrorID(10869) << spvOpcodeString(opcode)
  108. << ": MemorySemantics must not use Relaxed memory order with "
  109. << spvOpcodeString(opcode);
  110. }
  111. if (is_vulkan) {
  112. const bool includes_storage_class =
  113. value & uint32_t(spv::MemorySemanticsMask::UniformMemory |
  114. spv::MemorySemanticsMask::WorkgroupMemory |
  115. spv::MemorySemanticsMask::ImageMemory |
  116. spv::MemorySemanticsMask::OutputMemoryKHR);
  117. if (num_memory_order_set_bits && !includes_storage_class) {
  118. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  119. << _.VkErrorID(10870) << spvOpcodeString(opcode)
  120. << ": Memory Semantics with a non-relaxed memory order (Acquire, "
  121. "Release, or AcquireRelease) must have at least one "
  122. "Vulkan-supported storage class semantics bit set "
  123. "(UniformMemory, WorkgroupMemory, ImageMemory, or "
  124. "OutputMemory)";
  125. }
  126. if (!num_memory_order_set_bits && includes_storage_class) {
  127. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  128. << _.VkErrorID(10871) << spvOpcodeString(opcode)
  129. << ": Memory Semantics with at least one Vulkan-supported "
  130. "storage class semantics bit set (UniformMemory, "
  131. "WorkgroupMemory, ImageMemory, or OutputMemory) must use "
  132. "a non-relaxed memory order (Acquire, Release, or "
  133. "AcquireRelease)";
  134. }
  135. }
  136. if (value & uint32_t(spv::MemorySemanticsMask::MakeAvailableKHR)) {
  137. if (!_.HasCapability(spv::Capability::VulkanMemoryModel)) {
  138. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  139. << spvOpcodeString(opcode)
  140. << ": Memory Semantics MakeAvailableKHR requires capability "
  141. << "VulkanMemoryModelKHR";
  142. }
  143. if (!(value & uint32_t(spv::MemorySemanticsMask::Release |
  144. spv::MemorySemanticsMask::AcquireRelease))) {
  145. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  146. << _.VkErrorID(10872) << spvOpcodeString(opcode)
  147. << ": Memory Semantics with MakeAvailable bit set must use "
  148. "Release or AcquireRelease memory order";
  149. }
  150. }
  151. if (value & uint32_t(spv::MemorySemanticsMask::MakeVisibleKHR)) {
  152. if (!_.HasCapability(spv::Capability::VulkanMemoryModel)) {
  153. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  154. << spvOpcodeString(opcode)
  155. << ": Memory Semantics MakeVisibleKHR requires capability "
  156. << "VulkanMemoryModelKHR";
  157. }
  158. if (!(value & uint32_t(spv::MemorySemanticsMask::Acquire |
  159. spv::MemorySemanticsMask::AcquireRelease))) {
  160. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  161. << _.VkErrorID(10873) << spvOpcodeString(opcode)
  162. << ": Memory Semantics with MakeVisible bit set must use Acquire "
  163. "or AcquireRelease memory order";
  164. }
  165. }
  166. if (value & uint32_t(spv::MemorySemanticsMask::Volatile)) {
  167. if (!_.HasCapability(spv::Capability::VulkanMemoryModel)) {
  168. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  169. << spvOpcodeString(opcode)
  170. << ": Memory Semantics Volatile requires capability "
  171. "VulkanMemoryModelKHR";
  172. }
  173. if (!spvOpcodeIsAtomicOp(inst->opcode())) {
  174. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  175. << _.VkErrorID(10874) << spvOpcodeString(opcode)
  176. << ": Memory Semantics with Volatile bit set must not be used "
  177. "with barrier instructions";
  178. }
  179. }
  180. if ((opcode == spv::Op::OpAtomicCompareExchange ||
  181. opcode == spv::Op::OpAtomicCompareExchangeWeak) &&
  182. operand_index == 5) {
  183. if (value & uint32_t(spv::MemorySemanticsMask::Release) ||
  184. value & uint32_t(spv::MemorySemanticsMask::AcquireRelease)) {
  185. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  186. << _.VkErrorID(10875) << spvOpcodeString(opcode)
  187. << " Unequal Memory Semantics must not use Release or "
  188. "AcquireRelease memory order";
  189. }
  190. bool is_equal_int32 = false;
  191. bool is_equal_const = false;
  192. uint32_t equal_value = 0;
  193. std::tie(is_equal_int32, is_equal_const, equal_value) =
  194. _.EvalInt32IfConst(inst->GetOperandAs<uint32_t>(4));
  195. const auto equal_mask_seq_cst =
  196. uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent);
  197. const auto equal_mask_acquire = uint32_t(
  198. // Allow EqualMemorySemantics Release with UnequalMemorySemantics
  199. // Acquire, since the C standard doesn't clearly forbid it.
  200. spv::MemorySemanticsMask::SequentiallyConsistent |
  201. spv::MemorySemanticsMask::AcquireRelease |
  202. spv::MemorySemanticsMask::Release | spv::MemorySemanticsMask::Acquire);
  203. if (((value & uint32_t(spv::MemorySemanticsMask::SequentiallyConsistent)) &&
  204. !(equal_value & equal_mask_seq_cst)) ||
  205. ((value & uint32_t(spv::MemorySemanticsMask::Acquire)) &&
  206. !(equal_value & equal_mask_acquire))) {
  207. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  208. << _.VkErrorID(10876) << spvOpcodeString(opcode)
  209. << " Unequal Memory Semantics must not use a stronger memory "
  210. "order than the corresponding Equal Memory Semantics";
  211. }
  212. if (is_vulkan) {
  213. auto storage_class_semantics_mask =
  214. uint32_t(spv::MemorySemanticsMask::UniformMemory |
  215. spv::MemorySemanticsMask::WorkgroupMemory |
  216. spv::MemorySemanticsMask::ImageMemory |
  217. spv::MemorySemanticsMask::OutputMemoryKHR);
  218. if (value & ~equal_value & storage_class_semantics_mask) {
  219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  220. << _.VkErrorID(10877) << spvOpcodeString(opcode)
  221. << " Unequal Memory Semantics must not have any "
  222. "Vulkan-supported storage class semantics bit set "
  223. "(UniformMemory, WorkgroupMemory, ImageMemory, or "
  224. "OutputMemory) unless this bit is also set in the "
  225. "corresponding Equal Memory Semantics";
  226. }
  227. if (value & ~equal_value &
  228. uint32_t(spv::MemorySemanticsMask::MakeVisibleKHR)) {
  229. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  230. << _.VkErrorID(10878) << spvOpcodeString(opcode)
  231. << " Unequal Memory Semantics must not have MakeVisible bit set "
  232. "unless this bit is also set in the corresponding Equal "
  233. "Memory Semantics";
  234. }
  235. if ((equal_value & uint32_t(spv::MemorySemanticsMask::Volatile)) ^
  236. (value & uint32_t(spv::MemorySemanticsMask::Volatile))) {
  237. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  238. << _.VkErrorID(10879) << spvOpcodeString(opcode)
  239. << " Unequal Memory Semantics must have Volatile bit set if and "
  240. "only if this bit is also set in the corresponding Equal "
  241. "Memory Semantics";
  242. }
  243. }
  244. }
  245. if (is_vulkan && num_memory_order_set_bits) {
  246. bool memory_is_int32 = false, memory_is_const_int32 = false;
  247. uint32_t memory_value = 0;
  248. std::tie(memory_is_int32, memory_is_const_int32, memory_value) =
  249. _.EvalInt32IfConst(memory_scope);
  250. if (memory_is_int32 && spv::Scope(memory_value) == spv::Scope::Invocation) {
  251. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  252. << _.VkErrorID(4641) << spvOpcodeString(opcode)
  253. << ": Vulkan specification requires Memory Semantics to be "
  254. "Relaxed if used with Invocation Memory Scope";
  255. }
  256. }
  257. // TODO([email protected]) Add checks for OpenCL and OpenGL environments.
  258. return SPV_SUCCESS;
  259. }
  260. } // namespace val
  261. } // namespace spvtools