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 // Disabling this check until
123 // https://github.com/KhronosGroup/glslang/issues/1618 is resolved.
124 // if (value & SpvMemorySemanticsAtomicCounterMemoryMask &&
125 // !_.HasCapability(SpvCapabilityAtomicStorage)) {
126 // return _.diag(SPV_ERROR_INVALID_DATA, inst)
127 // << spvOpcodeString(opcode)
128 // << ": Memory Semantics AtomicCounterMemory requires capability "
129 // "AtomicStorage";
130 // }
131
132 if (value & (SpvMemorySemanticsMakeAvailableKHRMask |
133 SpvMemorySemanticsMakeVisibleKHRMask)) {
134 const bool includes_storage_class =
135 value & (SpvMemorySemanticsUniformMemoryMask |
136 SpvMemorySemanticsSubgroupMemoryMask |
137 SpvMemorySemanticsWorkgroupMemoryMask |
138 SpvMemorySemanticsCrossWorkgroupMemoryMask |
139 SpvMemorySemanticsAtomicCounterMemoryMask |
140 SpvMemorySemanticsImageMemoryMask |
141 SpvMemorySemanticsOutputMemoryKHRMask);
142
143 if (!includes_storage_class) {
144 return _.diag(SPV_ERROR_INVALID_DATA, inst)
145 << spvOpcodeString(opcode)
146 << ": expected Memory Semantics to include a storage class";
147 }
148 }
149
150 if (value & SpvMemorySemanticsMakeVisibleKHRMask &&
151 !(value & (SpvMemorySemanticsAcquireMask |
152 SpvMemorySemanticsAcquireReleaseMask))) {
153 return _.diag(SPV_ERROR_INVALID_DATA, inst)
154 << spvOpcodeString(opcode)
155 << ": MakeVisibleKHR Memory Semantics also requires either Acquire "
156 "or AcquireRelease Memory Semantics";
157 }
158
159 if (value & SpvMemorySemanticsMakeAvailableKHRMask &&
160 !(value & (SpvMemorySemanticsReleaseMask |
161 SpvMemorySemanticsAcquireReleaseMask))) {
162 return _.diag(SPV_ERROR_INVALID_DATA, inst)
163 << spvOpcodeString(opcode)
164 << ": MakeAvailableKHR Memory Semantics also requires either "
165 "Release or AcquireRelease Memory Semantics";
166 }
167
168 if (spvIsVulkanEnv(_.context()->target_env)) {
169 const bool includes_storage_class =
170 value & (SpvMemorySemanticsUniformMemoryMask |
171 SpvMemorySemanticsWorkgroupMemoryMask |
172 SpvMemorySemanticsImageMemoryMask |
173 SpvMemorySemanticsOutputMemoryKHRMask);
174
175 if (opcode == SpvOpMemoryBarrier && !num_memory_order_set_bits) {
176 return _.diag(SPV_ERROR_INVALID_DATA, inst)
177 << spvOpcodeString(opcode)
178 << ": Vulkan specification requires Memory Semantics to have "
179 "one "
180 "of the following bits set: Acquire, Release, "
181 "AcquireRelease "
182 "or SequentiallyConsistent";
183 }
184
185 if (opcode == SpvOpMemoryBarrier && !includes_storage_class) {
186 return _.diag(SPV_ERROR_INVALID_DATA, inst)
187 << spvOpcodeString(opcode)
188 << ": expected Memory Semantics to include a Vulkan-supported "
189 "storage class";
190 }
191
192 #if 0
193 // TODO(atgoo@github.com): this check fails Vulkan CTS, reenable once fixed.
194 if (opcode == SpvOpControlBarrier && value && !includes_storage_class) {
195 return _.diag(SPV_ERROR_INVALID_DATA, inst)
196 << spvOpcodeString(opcode)
197 << ": expected Memory Semantics to include a Vulkan-supported "
198 "storage class if Memory Semantics is not None";
199 }
200 #endif
201 }
202
203 if (opcode == SpvOpAtomicFlagClear &&
204 (value & SpvMemorySemanticsAcquireMask ||
205 value & SpvMemorySemanticsAcquireReleaseMask)) {
206 return _.diag(SPV_ERROR_INVALID_DATA, inst)
207 << "Memory Semantics Acquire and AcquireRelease cannot be used "
208 "with "
209 << spvOpcodeString(opcode);
210 }
211
212 if (opcode == SpvOpAtomicCompareExchange && operand_index == 5 &&
213 (value & SpvMemorySemanticsReleaseMask ||
214 value & SpvMemorySemanticsAcquireReleaseMask)) {
215 return _.diag(SPV_ERROR_INVALID_DATA, inst)
216 << spvOpcodeString(opcode)
217 << ": Memory Semantics Release and AcquireRelease cannot be "
218 "used "
219 "for operand Unequal";
220 }
221
222 if (spvIsVulkanEnv(_.context()->target_env)) {
223 if (opcode == SpvOpAtomicLoad &&
224 (value & SpvMemorySemanticsReleaseMask ||
225 value & SpvMemorySemanticsAcquireReleaseMask ||
226 value & SpvMemorySemanticsSequentiallyConsistentMask)) {
227 return _.diag(SPV_ERROR_INVALID_DATA, inst)
228 << "Vulkan spec disallows OpAtomicLoad with Memory Semantics "
229 "Release, AcquireRelease and SequentiallyConsistent";
230 }
231
232 if (opcode == SpvOpAtomicStore &&
233 (value & SpvMemorySemanticsAcquireMask ||
234 value & SpvMemorySemanticsAcquireReleaseMask ||
235 value & SpvMemorySemanticsSequentiallyConsistentMask)) {
236 return _.diag(SPV_ERROR_INVALID_DATA, inst)
237 << "Vulkan spec disallows OpAtomicStore with Memory Semantics "
238 "Acquire, AcquireRelease and SequentiallyConsistent";
239 }
240 }
241
242 // TODO(atgoo@github.com) Add checks for OpenCL and OpenGL environments.
243
244 return SPV_SUCCESS;
245 }
246
247 } // namespace val
248 } // namespace spvtools
249