Debian Codegraph @

test/val/val_opencl_test.cpp

raw
1 // Copyright (c) 2019 The Khronos Group Inc.
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 OpenCL env specific checks
16
17 #include <string>
18
19 #include "gmock/gmock.h"
20 #include "test/val/val_fixtures.h"
21
22 namespace spvtools {
23 namespace val {
24 namespace {
25
26 using testing::Eq;
27 using testing::HasSubstr;
28
29 using ValidateOpenCL = spvtest::ValidateBase<bool>;
30
31 TEST_F(ValidateOpenCL, NonPhysicalAddressingModelBad) {
32 std::string spirv = R"(
33 OpCapability Kernel
34 OpMemoryModel Logical OpenCL
35 )";
36
37 CompileSuccessfully(spirv);
38
39 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
40 EXPECT_THAT(getDiagnosticString(),
41 HasSubstr("Addressing model must be Physical32 or Physical64 "
42 "in the OpenCL environment.\n OpMemoryModel Logical "
43 "OpenCL\n"));
44 }
45
46 TEST_F(ValidateOpenCL, NonVoidSampledTypeImageBad) {
47 std::string spirv = R"(
48 OpCapability Addresses
49 OpCapability Kernel
50 OpMemoryModel Physical32 OpenCL
51 %1 = OpTypeInt 32 0
52 %2 = OpTypeImage %1 2D 0 0 0 0 Unknown ReadOnly
53 )";
54
55 CompileSuccessfully(spirv);
56
57 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
58 EXPECT_THAT(
59 getDiagnosticString(),
60 HasSubstr("Sampled Type must be OpTypeVoid in the OpenCL environment."
61 "\n %2 = OpTypeImage %uint 2D 0 0 0 0 Unknown ReadOnly\n"));
62 }
63
64 TEST_F(ValidateOpenCL, NonZeroMSImageBad) {
65 std::string spirv = R"(
66 OpCapability Addresses
67 OpCapability Kernel
68 OpMemoryModel Physical32 OpenCL
69 %1 = OpTypeVoid
70 %2 = OpTypeImage %1 2D 0 0 1 0 Unknown ReadOnly
71 )";
72
73 CompileSuccessfully(spirv);
74
75 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
76 EXPECT_THAT(
77 getDiagnosticString(),
78 HasSubstr("MS must be 0 in the OpenCL environment."
79 "\n %2 = OpTypeImage %void 2D 0 0 1 0 Unknown ReadOnly\n"));
80 }
81
82 TEST_F(ValidateOpenCL, Non1D2DArrayedImageBad) {
83 std::string spirv = R"(
84 OpCapability Addresses
85 OpCapability Kernel
86 OpMemoryModel Physical32 OpenCL
87 %1 = OpTypeVoid
88 %2 = OpTypeImage %1 3D 0 1 0 0 Unknown ReadOnly
89 )";
90
91 CompileSuccessfully(spirv);
92
93 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
94 EXPECT_THAT(
95 getDiagnosticString(),
96 HasSubstr("In the OpenCL environment, Arrayed may only be set to 1 "
97 "when Dim is either 1D or 2D."
98 "\n %2 = OpTypeImage %void 3D 0 1 0 0 Unknown ReadOnly\n"));
99 }
100
101 TEST_F(ValidateOpenCL, NonZeroSampledImageBad) {
102 std::string spirv = R"(
103 OpCapability Addresses
104 OpCapability Kernel
105 OpMemoryModel Physical32 OpenCL
106 %1 = OpTypeVoid
107 %2 = OpTypeImage %1 3D 0 0 0 1 Unknown ReadOnly
108 )";
109
110 CompileSuccessfully(spirv);
111
112 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
113 EXPECT_THAT(
114 getDiagnosticString(),
115 HasSubstr("Sampled must be 0 in the OpenCL environment."
116 "\n %2 = OpTypeImage %void 3D 0 0 0 1 Unknown ReadOnly\n"));
117 }
118
119 TEST_F(ValidateOpenCL, NoAccessQualifierImageBad) {
120 std::string spirv = R"(
121 OpCapability Addresses
122 OpCapability Kernel
123 OpMemoryModel Physical32 OpenCL
124 %1 = OpTypeVoid
125 %2 = OpTypeImage %1 3D 0 0 0 0 Unknown
126 )";
127
128 CompileSuccessfully(spirv);
129
130 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
131 EXPECT_THAT(getDiagnosticString(),
132 HasSubstr("In the OpenCL environment, the optional "
133 "Access Qualifier must be present."
134 "\n %2 = OpTypeImage %void 3D 0 0 0 0 Unknown\n"));
135 }
136
137 TEST_F(ValidateOpenCL, ImageWriteWithOptionalImageOperandsBad) {
138 std::string spirv = R"(
139 OpCapability Addresses
140 OpCapability Kernel
141 OpCapability ImageBasic
142 OpMemoryModel Physical64 OpenCL
143 OpEntryPoint Kernel %5 "test"
144 %uint = OpTypeInt 32 0
145 %uint_7 = OpConstant %uint 7
146 %uint_3 = OpConstant %uint 3
147 %uint_1 = OpConstant %uint 1
148 %uint_2 = OpConstant %uint 2
149 %uint_4 = OpConstant %uint 4
150 %void = OpTypeVoid
151 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown WriteOnly
152 %4 = OpTypeFunction %void %3
153 %v2uint = OpTypeVector %uint 2
154 %v4uint = OpTypeVector %uint 4
155 %12 = OpConstantComposite %v2uint %uint_7 %uint_3
156 %17 = OpConstantComposite %v4uint %uint_1 %uint_2 %uint_3 %uint_4
157 %5 = OpFunction %void None %4
158 %img = OpFunctionParameter %3
159 %entry = OpLabel
160 OpImageWrite %img %12 %17 ConstOffset %12
161 OpReturn
162 OpFunctionEnd
163 )";
164
165 CompileSuccessfully(spirv);
166
167 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
168 EXPECT_THAT(getDiagnosticString(),
169 HasSubstr("Optional Image Operands are not allowed in the "
170 "OpenCL environment."
171 "\n OpImageWrite %15 %13 %14 ConstOffset %13\n"));
172 }
173
174 TEST_F(ValidateOpenCL, ImageReadWithConstOffsetBad) {
175 std::string spirv = R"(
176 OpCapability Addresses
177 OpCapability Kernel
178 OpCapability ImageBasic
179 OpMemoryModel Physical64 OpenCL
180 OpEntryPoint Kernel %5 "image_kernel"
181 OpName %img "img"
182 OpName %coord "coord"
183 OpName %call "call"
184 %uint = OpTypeInt 32 0
185 %uint_7 = OpConstant %uint 7
186 %uint_3 = OpConstant %uint 3
187 %void = OpTypeVoid
188 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
189 %4 = OpTypeFunction %void %3
190 %v4uint = OpTypeVector %uint 4
191 %v2uint = OpTypeVector %uint 2
192 %coord = OpConstantComposite %v2uint %uint_7 %uint_3
193 %5 = OpFunction %void None %4
194 %img = OpFunctionParameter %3
195 %entry = OpLabel
196 %call = OpImageRead %v4uint %img %coord ConstOffset %coord
197 OpReturn
198 OpFunctionEnd
199 )";
200
201 CompileSuccessfully(spirv);
202
203 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
204 EXPECT_THAT(
205 getDiagnosticString(),
206 HasSubstr(
207 "ConstOffset image operand not allowed in the OpenCL environment."
208 "\n %call = OpImageRead %v4uint %img %coord ConstOffset %coord\n"));
209 }
210
211 TEST_F(ValidateOpenCL, ImageRead_NonDepthScalarFloatResult_Bad) {
212 std::string spirv = R"(
213 OpCapability Addresses
214 OpCapability Kernel
215 OpCapability ImageBasic
216 OpMemoryModel Physical64 OpenCL
217 OpEntryPoint Kernel %5 "image_kernel"
218 OpName %img "img"
219 OpName %coord "coord"
220 OpName %call "call"
221 %uint = OpTypeInt 32 0
222 %v2uint = OpTypeVector %uint 2
223 %coord = OpConstantNull %v2uint
224 %void = OpTypeVoid
225 %float = OpTypeFloat 32
226 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
227 %4 = OpTypeFunction %void %3
228 %5 = OpFunction %void None %4
229 %img = OpFunctionParameter %3
230 %entry = OpLabel
231 %call = OpImageRead %float %img %coord
232 OpReturn
233 OpFunctionEnd
234 )";
235
236 CompileSuccessfully(spirv);
237
238 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
239 EXPECT_THAT(getDiagnosticString(),
240 HasSubstr("Expected Result Type to have 4 components"));
241 }
242
243 TEST_F(ValidateOpenCL, ImageRead_NonDepthScalarIntResult_Bad) {
244 std::string spirv = R"(
245 OpCapability Addresses
246 OpCapability Kernel
247 OpCapability ImageBasic
248 OpMemoryModel Physical64 OpenCL
249 OpEntryPoint Kernel %5 "image_kernel"
250 OpName %img "img"
251 OpName %coord "coord"
252 OpName %call "call"
253 %uint = OpTypeInt 32 0
254 %v2uint = OpTypeVector %uint 2
255 %coord = OpConstantNull %v2uint
256 %void = OpTypeVoid
257 %float = OpTypeFloat 32
258 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
259 %4 = OpTypeFunction %void %3
260 %5 = OpFunction %void None %4
261 %img = OpFunctionParameter %3
262 %entry = OpLabel
263 %call = OpImageRead %uint %img %coord
264 OpReturn
265 OpFunctionEnd
266 )";
267
268 CompileSuccessfully(spirv);
269
270 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
271 EXPECT_THAT(getDiagnosticString(),
272 HasSubstr("Expected Result Type to have 4 components"));
273 }
274
275 TEST_F(ValidateOpenCL, ImageRead_NonDepthVector3FloatResult_Bad) {
276 std::string spirv = R"(
277 OpCapability Addresses
278 OpCapability Kernel
279 OpCapability ImageBasic
280 OpMemoryModel Physical64 OpenCL
281 OpEntryPoint Kernel %5 "image_kernel"
282 OpName %img "img"
283 OpName %coord "coord"
284 OpName %call "call"
285 %uint = OpTypeInt 32 0
286 %v2uint = OpTypeVector %uint 2
287 %coord = OpConstantNull %v2uint
288 %void = OpTypeVoid
289 %float = OpTypeFloat 32
290 %v3float = OpTypeVector %float 3
291 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
292 %4 = OpTypeFunction %void %3
293 %5 = OpFunction %void None %4
294 %img = OpFunctionParameter %3
295 %entry = OpLabel
296 %call = OpImageRead %v3float %img %coord
297 OpReturn
298 OpFunctionEnd
299 )";
300
301 CompileSuccessfully(spirv);
302
303 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
304 EXPECT_THAT(getDiagnosticString(),
305 HasSubstr("Expected Result Type to have 4 components"));
306 }
307
308 TEST_F(ValidateOpenCL, ImageRead_NonDepthVector4FloatResult_Ok) {
309 std::string spirv = R"(
310 OpCapability Addresses
311 OpCapability Kernel
312 OpCapability ImageBasic
313 OpMemoryModel Physical64 OpenCL
314 OpEntryPoint Kernel %5 "image_kernel"
315 OpName %img "img"
316 OpName %coord "coord"
317 OpName %call "call"
318 %uint = OpTypeInt 32 0
319 %v2uint = OpTypeVector %uint 2
320 %coord = OpConstantNull %v2uint
321 %void = OpTypeVoid
322 %float = OpTypeFloat 32
323 %v4float = OpTypeVector %float 4
324 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
325 %4 = OpTypeFunction %void %3
326 %5 = OpFunction %void None %4
327 %img = OpFunctionParameter %3
328 %entry = OpLabel
329 %call = OpImageRead %v4float %img %coord
330 OpReturn
331 OpFunctionEnd
332 )";
333
334 CompileSuccessfully(spirv);
335
336 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
337 EXPECT_THAT(getDiagnosticString(), Eq(""));
338 }
339
340 TEST_F(ValidateOpenCL, ImageRead_NonDepthVector4IntResult_Ok) {
341 std::string spirv = R"(
342 OpCapability Addresses
343 OpCapability Kernel
344 OpCapability ImageBasic
345 OpMemoryModel Physical64 OpenCL
346 OpEntryPoint Kernel %5 "image_kernel"
347 OpName %img "img"
348 OpName %coord "coord"
349 OpName %call "call"
350 %uint = OpTypeInt 32 0
351 %v2uint = OpTypeVector %uint 2
352 %coord = OpConstantNull %v2uint
353 %void = OpTypeVoid
354 %v4uint = OpTypeVector %uint 4
355 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
356 %4 = OpTypeFunction %void %3
357 %5 = OpFunction %void None %4
358 %img = OpFunctionParameter %3
359 %entry = OpLabel
360 %call = OpImageRead %v4uint %img %coord
361 OpReturn
362 OpFunctionEnd
363 )";
364
365 CompileSuccessfully(spirv);
366
367 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
368 EXPECT_THAT(getDiagnosticString(), Eq(""));
369 }
370
371 TEST_F(ValidateOpenCL, ImageRead_DepthScalarFloatResult_Ok) {
372 std::string spirv = R"(
373 OpCapability Addresses
374 OpCapability Kernel
375 OpCapability ImageBasic
376 OpMemoryModel Physical64 OpenCL
377 OpEntryPoint Kernel %5 "image_kernel"
378 OpName %img "img"
379 OpName %coord "coord"
380 OpName %call "call"
381 %uint = OpTypeInt 32 0
382 %v2uint = OpTypeVector %uint 2
383 %coord = OpConstantNull %v2uint
384 %void = OpTypeVoid
385 %float = OpTypeFloat 32
386 %3 = OpTypeImage %void 2D 1 0 0 0 Unknown ReadOnly
387 %4 = OpTypeFunction %void %3
388 %5 = OpFunction %void None %4
389 %img = OpFunctionParameter %3
390 %entry = OpLabel
391 %call = OpImageRead %float %img %coord
392 OpReturn
393 OpFunctionEnd
394 )";
395
396 CompileSuccessfully(spirv);
397
398 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
399 EXPECT_THAT(getDiagnosticString(), Eq(""));
400 }
401
402 TEST_F(ValidateOpenCL, ImageRead_DepthScalarIntResult_Bad) {
403 std::string spirv = R"(
404 OpCapability Addresses
405 OpCapability Kernel
406 OpCapability ImageBasic
407 OpMemoryModel Physical64 OpenCL
408 OpEntryPoint Kernel %5 "image_kernel"
409 OpName %img "img"
410 OpName %coord "coord"
411 OpName %call "call"
412 %uint = OpTypeInt 32 0
413 %v2uint = OpTypeVector %uint 2
414 %coord = OpConstantNull %v2uint
415 %void = OpTypeVoid
416 %float = OpTypeFloat 32
417 %3 = OpTypeImage %void 2D 1 0 0 0 Unknown ReadOnly
418 %4 = OpTypeFunction %void %3
419 %5 = OpFunction %void None %4
420 %img = OpFunctionParameter %3
421 %entry = OpLabel
422 %call = OpImageRead %uint %img %coord
423 OpReturn
424 OpFunctionEnd
425 )";
426
427 CompileSuccessfully(spirv);
428
429 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
430 EXPECT_THAT(getDiagnosticString(),
431 HasSubstr("Expected Result Type from a depth image "
432 "read to result in a scalar float value"));
433 }
434
435 TEST_F(ValidateOpenCL, ImageRead_DepthVectorFloatResult_Bad) {
436 std::string spirv = R"(
437 OpCapability Addresses
438 OpCapability Kernel
439 OpCapability ImageBasic
440 OpMemoryModel Physical64 OpenCL
441 OpEntryPoint Kernel %5 "image_kernel"
442 OpName %img "img"
443 OpName %coord "coord"
444 OpName %call "call"
445 %uint = OpTypeInt 32 0
446 %v2uint = OpTypeVector %uint 2
447 %coord = OpConstantNull %v2uint
448 %void = OpTypeVoid
449 %float = OpTypeFloat 32
450 %v4float = OpTypeVector %float 4
451 %3 = OpTypeImage %void 2D 1 0 0 0 Unknown ReadOnly
452 %4 = OpTypeFunction %void %3
453 %5 = OpFunction %void None %4
454 %img = OpFunctionParameter %3
455 %entry = OpLabel
456 %call = OpImageRead %v4float %img %coord
457 OpReturn
458 OpFunctionEnd
459 )";
460
461 CompileSuccessfully(spirv);
462
463 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
464 EXPECT_THAT(getDiagnosticString(),
465 HasSubstr("Expected Result Type from a depth image "
466 "read to result in a scalar float value"));
467 }
468
469 TEST_F(ValidateOpenCL, ImageSampleExplicitLodWithConstOffsetBad) {
470 std::string spirv = R"(
471 OpCapability Addresses
472 OpCapability Kernel
473 OpCapability ImageBasic
474 OpCapability LiteralSampler
475 OpMemoryModel Physical64 OpenCL
476 OpEntryPoint Kernel %5 "image_kernel"
477 OpName %img "img"
478 OpName %coord "coord"
479 OpName %call "call"
480 %uint = OpTypeInt 32 0
481 %v2uint = OpTypeVector %uint 2
482 %coord = OpConstantNull %v2uint
483 %void = OpTypeVoid
484 %3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly
485 %4 = OpTypeFunction %void %3
486 %8 = OpTypeSampler
487 %10 = OpTypeSampledImage %3
488 %v4uint = OpTypeVector %uint 4
489 %float = OpTypeFloat 32
490 %9 = OpConstantSampler %8 None 0 Nearest
491 %float_0 = OpConstant %float 0
492 %5 = OpFunction %void None %4
493 %6 = OpFunctionParameter %3
494 %entry = OpLabel
495 %img = OpSampledImage %10 %6 %9
496 %call = OpImageSampleExplicitLod %v4uint %img %coord
497 Lod|ConstOffset %float_0 %coord
498 OpReturn
499 OpFunctionEnd
500 )";
501
502 CompileSuccessfully(spirv);
503
504 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
505 EXPECT_THAT(
506 getDiagnosticString(),
507 HasSubstr(
508 "ConstOffset image operand not allowed in the OpenCL environment."
509 "\n %call = OpImageSampleExplicitLod %v4uint %img "
510 "%coord Lod|ConstOffset %float_0 %coord\n"));
511 }
512
513 } // namespace
514 } // namespace val
515 } // namespace spvtools