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