1 // Copyright (c) 2019 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 // Validation tests for misc instructions
16
17 #include <string>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "test/unit_spirv.h"
22 #include "test/val/val_fixtures.h"
23
24 namespace spvtools {
25 namespace val {
26 namespace {
27
28 using ::testing::Eq;
29 using ::testing::HasSubstr;
30
31 using ValidateMisc = spvtest::ValidateBase<bool>;
32
TEST_F(ValidateMisc,UndefRestrictedShort)33 TEST_F(ValidateMisc, UndefRestrictedShort) {
34 const std::string spirv = R"(
35 OpCapability Shader
36 OpCapability Linkage
37 OpCapability StorageBuffer16BitAccess
38 OpExtension "SPV_KHR_16bit_storage"
39 OpMemoryModel Logical GLSL450
40 %short = OpTypeInt 16 0
41 %undef = OpUndef %short
42 )";
43
44 CompileSuccessfully(spirv);
45 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
46 EXPECT_THAT(
47 getDiagnosticString(),
48 HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
49 }
50
TEST_F(ValidateMisc,UndefRestrictedChar)51 TEST_F(ValidateMisc, UndefRestrictedChar) {
52 const std::string spirv = R"(
53 OpCapability Shader
54 OpCapability Linkage
55 OpCapability StorageBuffer8BitAccess
56 OpExtension "SPV_KHR_8bit_storage"
57 OpMemoryModel Logical GLSL450
58 %char = OpTypeInt 8 0
59 %undef = OpUndef %char
60 )";
61
62 CompileSuccessfully(spirv);
63 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
64 EXPECT_THAT(
65 getDiagnosticString(),
66 HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
67 }
68
TEST_F(ValidateMisc,UndefRestrictedHalf)69 TEST_F(ValidateMisc, UndefRestrictedHalf) {
70 const std::string spirv = R"(
71 OpCapability Shader
72 OpCapability Linkage
73 OpCapability StorageBuffer16BitAccess
74 OpExtension "SPV_KHR_16bit_storage"
75 OpMemoryModel Logical GLSL450
76 %half = OpTypeFloat 16
77 %undef = OpUndef %half
78 )";
79
80 CompileSuccessfully(spirv);
81 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
82 EXPECT_THAT(
83 getDiagnosticString(),
84 HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
85 }
86
87 const std::string ShaderClockSpriv = R"(
88 OpCapability Shader
89 OpCapability Int64
90 OpCapability ShaderClockKHR
91 OpExtension "SPV_KHR_shader_clock"
92 %1 = OpExtInstImport "GLSL.std.450"
93 OpMemoryModel Logical GLSL450
94 OpEntryPoint Fragment %main "main"
95 OpExecutionMode %main OriginUpperLeft
96 OpSource GLSL 450
97 OpSourceExtension "GL_ARB_gpu_shader_int64"
98 OpSourceExtension "GL_ARB_shader_clock"
99 OpSourceExtension "GL_EXT_shader_realtime_clock"
100 OpName %main "main"
101 OpName %time1 "time1"
102 %void = OpTypeVoid
103 )";
104
TEST_F(ValidateMisc,ShaderClockInt64)105 TEST_F(ValidateMisc, ShaderClockInt64) {
106 const std::string spirv = ShaderClockSpriv + R"(
107 %3 = OpTypeFunction %void
108 %uint = OpTypeInt 32 0
109 %_ptr_Function_uint = OpTypePointer Function %uint
110 %uint_3 = OpConstant %uint 3
111 %uint_1 = OpConstant %uint 1
112 %main = OpFunction %void None %3
113 %5 = OpLabel
114 %time1 = OpVariable %_ptr_Function_uint Function
115 %11 = OpReadClockKHR %uint %uint_3
116 OpStore %time1 %11
117 OpReturn
118 OpFunctionEnd)";
119
120 CompileSuccessfully(spirv);
121 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
122 EXPECT_THAT(getDiagnosticString(), HasSubstr("or 64bit unsigned integer"));
123 }
124
TEST_F(ValidateMisc,ShaderClockVec2)125 TEST_F(ValidateMisc, ShaderClockVec2) {
126 const std::string spirv = ShaderClockSpriv + R"(
127 %3 = OpTypeFunction %void
128 %ulong = OpTypeInt 64 0
129 %_ptr_Function_ulong = OpTypePointer Function %ulong
130 %uint = OpTypeInt 32 0
131 %uint_3 = OpConstant %uint 3
132 %v2uint = OpTypeVector %ulong 2
133 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
134 %main = OpFunction %void None %3
135 %5 = OpLabel
136 %time1 = OpVariable %_ptr_Function_v2uint Function
137 %15 = OpReadClockKHR %v2uint %uint_3
138 OpStore %time1 %15
139 OpReturn
140 OpFunctionEnd)";
141
142 CompileSuccessfully(spirv);
143 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
144 EXPECT_THAT(getDiagnosticString(), HasSubstr("vector of two components"));
145 }
146
TEST_F(ValidateMisc,ShaderClockInvalidScopeValue)147 TEST_F(ValidateMisc, ShaderClockInvalidScopeValue) {
148 const std::string spirv = ShaderClockSpriv + R"(
149 %3 = OpTypeFunction %void
150 %ulong = OpTypeInt 64 0
151 %uint = OpTypeInt 32 0
152 %_ptr_Function_ulong = OpTypePointer Function %ulong
153 %uint_10 = OpConstant %uint 10
154 %uint_1 = OpConstant %uint 1
155 %main = OpFunction %void None %3
156 %5 = OpLabel
157 %time1 = OpVariable %_ptr_Function_ulong Function
158 %11 = OpReadClockKHR %ulong %uint_10
159 OpStore %time1 %11
160 OpReturn
161 OpFunctionEnd)";
162
163 CompileSuccessfully(spirv);
164 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
165 EXPECT_THAT(getDiagnosticString(), HasSubstr("Invalid scope value"));
166 }
167
TEST_F(ValidateMisc,ShaderClockSubgroupScope)168 TEST_F(ValidateMisc, ShaderClockSubgroupScope) {
169 const std::string spirv = ShaderClockSpriv + R"(
170 %3 = OpTypeFunction %void
171 %ulong = OpTypeInt 64 0
172 %uint = OpTypeInt 32 0
173 %_ptr_Function_ulong = OpTypePointer Function %ulong
174 %subgroup = OpConstant %uint 3
175 %uint_1 = OpConstant %uint 1
176 %main = OpFunction %void None %3
177 %5 = OpLabel
178 %time1 = OpVariable %_ptr_Function_ulong Function
179 %11 = OpReadClockKHR %ulong %subgroup
180 OpStore %time1 %11
181 OpReturn
182 OpFunctionEnd)";
183
184 CompileSuccessfully(spirv);
185 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
186 }
187
TEST_F(ValidateMisc,ShaderClockDeviceScope)188 TEST_F(ValidateMisc, ShaderClockDeviceScope) {
189 const std::string spirv = ShaderClockSpriv + R"(
190 %3 = OpTypeFunction %void
191 %ulong = OpTypeInt 64 0
192 %uint = OpTypeInt 32 0
193 %_ptr_Function_ulong = OpTypePointer Function %ulong
194 %device = OpConstant %uint 1
195 %uint_1 = OpConstant %uint 1
196 %main = OpFunction %void None %3
197 %5 = OpLabel
198 %time1 = OpVariable %_ptr_Function_ulong Function
199 %11 = OpReadClockKHR %ulong %device
200 OpStore %time1 %11
201 OpReturn
202 OpFunctionEnd)";
203
204 CompileSuccessfully(spirv);
205 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
206 }
207
TEST_F(ValidateMisc,ShaderClockWorkgroupScope)208 TEST_F(ValidateMisc, ShaderClockWorkgroupScope) {
209 const std::string spirv = ShaderClockSpriv + R"(
210 %3 = OpTypeFunction %void
211 %ulong = OpTypeInt 64 0
212 %uint = OpTypeInt 32 0
213 %_ptr_Function_ulong = OpTypePointer Function %ulong
214 %workgroup = OpConstant %uint 2
215 %uint_1 = OpConstant %uint 1
216 %main = OpFunction %void None %3
217 %5 = OpLabel
218 %time1 = OpVariable %_ptr_Function_ulong Function
219 %11 = OpReadClockKHR %ulong %workgroup
220 OpStore %time1 %11
221 OpReturn
222 OpFunctionEnd)";
223
224 CompileSuccessfully(spirv);
225 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
226 EXPECT_THAT(getDiagnosticString(),
227 HasSubstr("Scope must be Subgroup or Device"));
228 }
229
TEST_F(ValidateMisc,VulkanShaderClockWorkgroupScope)230 TEST_F(ValidateMisc, VulkanShaderClockWorkgroupScope) {
231 const std::string spirv = ShaderClockSpriv + R"(
232 %3 = OpTypeFunction %void
233 %ulong = OpTypeInt 64 0
234 %uint = OpTypeInt 32 0
235 %_ptr_Function_ulong = OpTypePointer Function %ulong
236 %workgroup = OpConstant %uint 2
237 %uint_1 = OpConstant %uint 1
238 %main = OpFunction %void None %3
239 %5 = OpLabel
240 %time1 = OpVariable %_ptr_Function_ulong Function
241 %11 = OpReadClockKHR %ulong %workgroup
242 OpStore %time1 %11
243 OpReturn
244 OpFunctionEnd)";
245
246 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
247 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
248 EXPECT_THAT(getDiagnosticString(),
249 AnyVUID("VUID-StandaloneSpirv-OpReadClockKHR-04652"));
250 EXPECT_THAT(getDiagnosticString(),
251 HasSubstr("Scope must be Subgroup or Device"));
252 }
253
GenKernelClockSpirv(const std::string & scope)254 std::string GenKernelClockSpirv(const std::string& scope) {
255 const std::string s = R"(
256 OpCapability Kernel
257 OpCapability Addresses
258 OpCapability Int64
259 OpCapability ShaderClockKHR
260 OpExtension "SPV_KHR_shader_clock"
261 OpMemoryModel Physical32 OpenCL
262 OpEntryPoint Kernel %main "main"
263 OpExecutionMode %main ContractionOff
264 OpSource OpenCL_C 200000
265 OpName %main "main"
266 OpName %time1 "time1"
267 %void = OpTypeVoid
268 %3 = OpTypeFunction %void
269 %ulong = OpTypeInt 64 0
270 %uint = OpTypeInt 32 0
271 %_ptr_Function_ulong = OpTypePointer Function %ulong
272 %scope = OpConstant %uint )" +
273 scope + R"(
274 %main = OpFunction %void None %3
275 %5 = OpLabel
276 %time1 = OpVariable %_ptr_Function_ulong Function
277 %11 = OpReadClockKHR %ulong %scope
278 OpStore %time1 %11
279 OpReturn
280 OpFunctionEnd
281 )";
282 return s;
283 }
284
TEST_F(ValidateMisc,KernelClockScopeDevice)285 TEST_F(ValidateMisc, KernelClockScopeDevice) {
286 CompileSuccessfully(GenKernelClockSpirv("1"), SPV_ENV_OPENCL_1_2);
287 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
288 }
289
TEST_F(ValidateMisc,KernelClockScopeWorkgroup)290 TEST_F(ValidateMisc, KernelClockScopeWorkgroup) {
291 CompileSuccessfully(GenKernelClockSpirv("2"), SPV_ENV_OPENCL_1_2);
292 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
293 }
294
TEST_F(ValidateMisc,KernelClockScopeSubgroup)295 TEST_F(ValidateMisc, KernelClockScopeSubgroup) {
296 CompileSuccessfully(GenKernelClockSpirv("3"), SPV_ENV_OPENCL_1_2);
297 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
298 }
299
TEST_F(ValidateMisc,KernelClockScopeInvalid)300 TEST_F(ValidateMisc, KernelClockScopeInvalid) {
301 CompileSuccessfully(GenKernelClockSpirv("0"), SPV_ENV_OPENCL_1_2);
302 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
303 EXPECT_THAT(getDiagnosticString(),
304 HasSubstr("Scope must be Subgroup, Workgroup, or Device"));
305 }
306
TEST_F(ValidateMisc,UndefVoid)307 TEST_F(ValidateMisc, UndefVoid) {
308 const std::string spirv = R"(
309 OpCapability Shader
310 %1 = OpExtInstImport "GLSL.std.450"
311 OpMemoryModel Logical GLSL450
312 OpEntryPoint Fragment %4 "main"
313 OpExecutionMode %4 OriginUpperLeft
314 OpSource ESSL 320
315 %2 = OpTypeVoid
316 %10 = OpUndef %2
317 %3 = OpTypeFunction %2
318 %4 = OpFunction %2 None %3
319 %5 = OpLabel
320 OpReturn
321 OpFunctionEnd
322 )";
323
324 CompileSuccessfully(spirv);
325 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
326 EXPECT_THAT(getDiagnosticString(),
327 HasSubstr("Cannot create undefined values with void type"));
328 }
329
TEST_F(ValidateMisc,VulkanInvalidStorageClass)330 TEST_F(ValidateMisc, VulkanInvalidStorageClass) {
331 const std::string spirv = R"(
332 OpCapability Shader
333 OpMemoryModel Logical GLSL450
334 OpEntryPoint Vertex %func "shader"
335 %int = OpTypeInt 32 0
336 %ptr = OpTypePointer CrossWorkgroup %int
337 %var = OpVariable %ptr CrossWorkgroup
338 %void = OpTypeVoid
339 %void_f = OpTypeFunction %void
340 %func = OpFunction %void None %void_f
341 %label = OpLabel
342 OpReturn
343 OpFunctionEnd
344 )";
345
346 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
347 ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_VULKAN_1_0));
348 EXPECT_THAT(getDiagnosticString(),
349 AnyVUID("VUID-StandaloneSpirv-None-04643"));
350 EXPECT_THAT(getDiagnosticString(),
351 HasSubstr("Invalid storage class for target environment"));
352 }
353 } // namespace
354 } // namespace val
355 } // namespace spvtools
356