Debian Codegraph @

test/val/val_logicals_test.cpp

raw
1 // Copyright (c) 2017 Google 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 // Tests for unique type declaration rules validator.
16
17 #include <string>
18
19 #include "gmock/gmock.h"
20 #include "test/unit_spirv.h"
21 #include "test/val/val_fixtures.h"
22
23 namespace spvtools {
24 namespace val {
25 namespace {
26
27 using ::testing::Eq;
28 using ::testing::HasSubstr;
29 using ::testing::Not;
30
31 using ValidateLogicals = spvtest::ValidateBase<bool>;
32
33 std::string GenerateShaderCode(
34 const std::string& body,
35 const std::string& capabilities_and_extensions = "") {
36 const std::string capabilities =
37 R"(
38 OpCapability Shader
39 OpCapability Int64
40 OpCapability Float64)";
41
42 const std::string after_extension_before_body =
43 R"(
44 %ext_inst = OpExtInstImport "GLSL.std.450"
45 OpMemoryModel Logical GLSL450
46 OpEntryPoint Fragment %main "main"
47 OpExecutionMode %main OriginUpperLeft
48 %void = OpTypeVoid
49 %func = OpTypeFunction %void
50 %bool = OpTypeBool
51 %f32 = OpTypeFloat 32
52 %u32 = OpTypeInt 32 0
53 %s32 = OpTypeInt 32 1
54 %f64 = OpTypeFloat 64
55 %u64 = OpTypeInt 64 0
56 %s64 = OpTypeInt 64 1
57 %boolvec2 = OpTypeVector %bool 2
58 %s32vec2 = OpTypeVector %s32 2
59 %u32vec2 = OpTypeVector %u32 2
60 %u64vec2 = OpTypeVector %u64 2
61 %f32vec2 = OpTypeVector %f32 2
62 %f64vec2 = OpTypeVector %f64 2
63 %boolvec3 = OpTypeVector %bool 3
64 %u32vec3 = OpTypeVector %u32 3
65 %u64vec3 = OpTypeVector %u64 3
66 %s32vec3 = OpTypeVector %s32 3
67 %f32vec3 = OpTypeVector %f32 3
68 %f64vec3 = OpTypeVector %f64 3
69 %boolvec4 = OpTypeVector %bool 4
70 %u32vec4 = OpTypeVector %u32 4
71 %u64vec4 = OpTypeVector %u64 4
72 %s32vec4 = OpTypeVector %s32 4
73 %f32vec4 = OpTypeVector %f32 4
74 %f64vec4 = OpTypeVector %f64 4
75
76 %f32_0 = OpConstant %f32 0
77 %f32_1 = OpConstant %f32 1
78 %f32_2 = OpConstant %f32 2
79 %f32_3 = OpConstant %f32 3
80 %f32_4 = OpConstant %f32 4
81
82 %s32_0 = OpConstant %s32 0
83 %s32_1 = OpConstant %s32 1
84 %s32_2 = OpConstant %s32 2
85 %s32_3 = OpConstant %s32 3
86 %s32_4 = OpConstant %s32 4
87 %s32_m1 = OpConstant %s32 -1
88
89 %u32_0 = OpConstant %u32 0
90 %u32_1 = OpConstant %u32 1
91 %u32_2 = OpConstant %u32 2
92 %u32_3 = OpConstant %u32 3
93 %u32_4 = OpConstant %u32 4
94
95 %f64_0 = OpConstant %f64 0
96 %f64_1 = OpConstant %f64 1
97 %f64_2 = OpConstant %f64 2
98 %f64_3 = OpConstant %f64 3
99 %f64_4 = OpConstant %f64 4
100
101 %s64_0 = OpConstant %s64 0
102 %s64_1 = OpConstant %s64 1
103 %s64_2 = OpConstant %s64 2
104 %s64_3 = OpConstant %s64 3
105 %s64_4 = OpConstant %s64 4
106 %s64_m1 = OpConstant %s64 -1
107
108 %u64_0 = OpConstant %u64 0
109 %u64_1 = OpConstant %u64 1
110 %u64_2 = OpConstant %u64 2
111 %u64_3 = OpConstant %u64 3
112 %u64_4 = OpConstant %u64 4
113
114 %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
115 %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
116 %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
117 %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
118 %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
119 %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
120
121 %s32vec2_01 = OpConstantComposite %s32vec2 %s32_0 %s32_1
122 %s32vec2_12 = OpConstantComposite %s32vec2 %s32_1 %s32_2
123 %s32vec3_012 = OpConstantComposite %s32vec3 %s32_0 %s32_1 %s32_2
124 %s32vec3_123 = OpConstantComposite %s32vec3 %s32_1 %s32_2 %s32_3
125 %s32vec4_0123 = OpConstantComposite %s32vec4 %s32_0 %s32_1 %s32_2 %s32_3
126 %s32vec4_1234 = OpConstantComposite %s32vec4 %s32_1 %s32_2 %s32_3 %s32_4
127
128 %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
129 %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
130 %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
131 %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
132 %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
133 %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
134
135 %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
136 %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
137 %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
138 %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
139 %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
140 %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
141
142 %true = OpConstantTrue %bool
143 %false = OpConstantFalse %bool
144 %boolvec2_tf = OpConstantComposite %boolvec2 %true %false
145 %boolvec3_tft = OpConstantComposite %boolvec3 %true %false %true
146 %boolvec4_tftf = OpConstantComposite %boolvec4 %true %false %true %false
147
148 %arr_u32_2 = OpTypeArray %u32 %u32_2
149 %st_u32_u32 = OpTypeStruct %u32 %u32
150 %mat_f32_2_2 = OpTypeMatrix %f32vec2 2
151
152 %nul_arr_u32_2 = OpConstantNull %arr_u32_2
153 %nul_st_u32_u32 = OpConstantNull %st_u32_u32
154 %nul_mat_f32_2_2 = OpConstantNull %mat_f32_2_2
155
156 %arr_u32_2_1_2 = OpConstantComposite %arr_u32_2 %u32_1 %u32_2
157 %st_u32_u32_1_2 = OpConstantComposite %st_u32_u32 %u32_1 %u32_2
158 %mat_f32_2_2_01_12 = OpConstantComposite %mat_f32_2_2 %f32vec2_01 %f32vec2_12
159
160 %f32vec4ptr = OpTypePointer Function %f32vec4
161
162 %main = OpFunction %void None %func
163 %main_entry = OpLabel)";
164
165 const std::string after_body =
166 R"(
167 OpReturn
168 OpFunctionEnd)";
169
170 return capabilities + capabilities_and_extensions +
171 after_extension_before_body + body + after_body;
172 }
173
174 std::string GenerateKernelCode(
175 const std::string& body,
176 const std::string& capabilities_and_extensions = "") {
177 const std::string capabilities =
178 R"(
179 OpCapability Addresses
180 OpCapability Kernel
181 OpCapability Linkage
182 OpCapability Int64
183 OpCapability Float64)";
184
185 const std::string after_extension_before_body =
186 R"(
187 OpMemoryModel Physical32 OpenCL
188 %void = OpTypeVoid
189 %func = OpTypeFunction %void
190 %bool = OpTypeBool
191 %f32 = OpTypeFloat 32
192 %u32 = OpTypeInt 32 0
193 %f64 = OpTypeFloat 64
194 %u64 = OpTypeInt 64 0
195 %boolvec2 = OpTypeVector %bool 2
196 %u32vec2 = OpTypeVector %u32 2
197 %u64vec2 = OpTypeVector %u64 2
198 %f32vec2 = OpTypeVector %f32 2
199 %f64vec2 = OpTypeVector %f64 2
200 %boolvec3 = OpTypeVector %bool 3
201 %u32vec3 = OpTypeVector %u32 3
202 %u64vec3 = OpTypeVector %u64 3
203 %f32vec3 = OpTypeVector %f32 3
204 %f64vec3 = OpTypeVector %f64 3
205 %boolvec4 = OpTypeVector %bool 4
206 %u32vec4 = OpTypeVector %u32 4
207 %u64vec4 = OpTypeVector %u64 4
208 %f32vec4 = OpTypeVector %f32 4
209 %f64vec4 = OpTypeVector %f64 4
210
211 %f32_0 = OpConstant %f32 0
212 %f32_1 = OpConstant %f32 1
213 %f32_2 = OpConstant %f32 2
214 %f32_3 = OpConstant %f32 3
215 %f32_4 = OpConstant %f32 4
216
217 %u32_0 = OpConstant %u32 0
218 %u32_1 = OpConstant %u32 1
219 %u32_2 = OpConstant %u32 2
220 %u32_3 = OpConstant %u32 3
221 %u32_4 = OpConstant %u32 4
222
223 %f64_0 = OpConstant %f64 0
224 %f64_1 = OpConstant %f64 1
225 %f64_2 = OpConstant %f64 2
226 %f64_3 = OpConstant %f64 3
227 %f64_4 = OpConstant %f64 4
228
229 %u64_0 = OpConstant %u64 0
230 %u64_1 = OpConstant %u64 1
231 %u64_2 = OpConstant %u64 2
232 %u64_3 = OpConstant %u64 3
233 %u64_4 = OpConstant %u64 4
234
235 %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
236 %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
237 %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
238 %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
239 %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
240 %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
241
242 %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
243 %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
244 %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
245 %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
246 %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
247 %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
248
249 %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
250 %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
251 %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
252 %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
253 %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
254 %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
255
256 %true = OpConstantTrue %bool
257 %false = OpConstantFalse %bool
258 %boolvec2_tf = OpConstantComposite %boolvec2 %true %false
259 %boolvec3_tft = OpConstantComposite %boolvec3 %true %false %true
260 %boolvec4_tftf = OpConstantComposite %boolvec4 %true %false %true %false
261
262 %f32vec4ptr = OpTypePointer Function %f32vec4
263
264 %main = OpFunction %void None %func
265 %main_entry = OpLabel)";
266
267 const std::string after_body =
268 R"(
269 OpReturn
270 OpFunctionEnd)";
271
272 return capabilities + capabilities_and_extensions +
273 after_extension_before_body + body + after_body;
274 }
275
276 TEST_F(ValidateLogicals, OpAnySuccess) {
277 const std::string body = R"(
278 %val1 = OpAny %bool %boolvec2_tf
279 %val2 = OpAny %bool %boolvec3_tft
280 %val3 = OpAny %bool %boolvec4_tftf
281 )";
282
283 CompileSuccessfully(GenerateShaderCode(body).c_str());
284 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
285 }
286
287 TEST_F(ValidateLogicals, OpAnyWrongTypeId) {
288 const std::string body = R"(
289 %val = OpAny %u32 %boolvec2_tf
290 )";
291
292 CompileSuccessfully(GenerateShaderCode(body).c_str());
293 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
294 EXPECT_THAT(getDiagnosticString(),
295 HasSubstr("Expected bool scalar type as Result Type: Any"));
296 }
297
298 TEST_F(ValidateLogicals, OpAnyWrongOperand) {
299 const std::string body = R"(
300 %val = OpAny %bool %u32vec3_123
301 )";
302
303 CompileSuccessfully(GenerateShaderCode(body).c_str());
304 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
305 EXPECT_THAT(getDiagnosticString(),
306 HasSubstr("Expected operand to be vector bool: Any"));
307 }
308
309 TEST_F(ValidateLogicals, OpIsNanSuccess) {
310 const std::string body = R"(
311 %val1 = OpIsNan %bool %f32_1
312 %val2 = OpIsNan %bool %f64_0
313 %val3 = OpIsNan %boolvec2 %f32vec2_12
314 %val4 = OpIsNan %boolvec3 %f32vec3_123
315 %val5 = OpIsNan %boolvec4 %f32vec4_1234
316 )";
317
318 CompileSuccessfully(GenerateShaderCode(body).c_str());
319 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
320 }
321
322 TEST_F(ValidateLogicals, OpIsNanWrongTypeId) {
323 const std::string body = R"(
324 %val1 = OpIsNan %u32 %f32_1
325 )";
326
327 CompileSuccessfully(GenerateShaderCode(body).c_str());
328 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
329 EXPECT_THAT(
330 getDiagnosticString(),
331 HasSubstr("Expected bool scalar or vector type as Result Type: IsNan"));
332 }
333
334 TEST_F(ValidateLogicals, OpIsNanOperandNotFloat) {
335 const std::string body = R"(
336 %val1 = OpIsNan %bool %u32_1
337 )";
338
339 CompileSuccessfully(GenerateShaderCode(body).c_str());
340 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
341 EXPECT_THAT(
342 getDiagnosticString(),
343 HasSubstr("Expected operand to be scalar or vector float: IsNan"));
344 }
345
346 TEST_F(ValidateLogicals, OpIsNanOperandWrongSize) {
347 const std::string body = R"(
348 %val1 = OpIsNan %bool %f32vec2_12
349 )";
350
351 CompileSuccessfully(GenerateShaderCode(body).c_str());
352 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
353 EXPECT_THAT(
354 getDiagnosticString(),
355 HasSubstr(
356 "Expected vector sizes of Result Type and the operand to be equal: "
357 "IsNan"));
358 }
359
360 TEST_F(ValidateLogicals, OpLessOrGreaterSuccess) {
361 const std::string body = R"(
362 %val1 = OpLessOrGreater %bool %f32_0 %f32_1
363 %val2 = OpLessOrGreater %bool %f64_0 %f64_0
364 %val3 = OpLessOrGreater %boolvec2 %f32vec2_12 %f32vec2_12
365 %val4 = OpLessOrGreater %boolvec3 %f32vec3_123 %f32vec3_123
366 %val5 = OpLessOrGreater %boolvec4 %f32vec4_1234 %f32vec4_1234
367 )";
368
369 CompileSuccessfully(GenerateKernelCode(body).c_str());
370 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
371 }
372
373 TEST_F(ValidateLogicals, OpLessOrGreaterWrongTypeId) {
374 const std::string body = R"(
375 %val1 = OpLessOrGreater %u32 %f32_1 %f32_1
376 )";
377
378 CompileSuccessfully(GenerateKernelCode(body).c_str());
379 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
380 EXPECT_THAT(
381 getDiagnosticString(),
382 HasSubstr(
383 "Expected bool scalar or vector type as Result Type: LessOrGreater"));
384 }
385
386 TEST_F(ValidateLogicals, OpLessOrGreaterLeftOperandNotFloat) {
387 const std::string body = R"(
388 %val1 = OpLessOrGreater %bool %u32_1 %f32_1
389 )";
390
391 CompileSuccessfully(GenerateKernelCode(body).c_str());
392 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
393 EXPECT_THAT(
394 getDiagnosticString(),
395 HasSubstr(
396 "Expected operands to be scalar or vector float: LessOrGreater"));
397 }
398
399 TEST_F(ValidateLogicals, OpLessOrGreaterLeftOperandWrongSize) {
400 const std::string body = R"(
401 %val1 = OpLessOrGreater %bool %f32vec2_12 %f32_1
402 )";
403
404 CompileSuccessfully(GenerateKernelCode(body).c_str());
405 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
406 EXPECT_THAT(
407 getDiagnosticString(),
408 HasSubstr(
409 "Expected vector sizes of Result Type and the operands to be equal: "
410 "LessOrGreater"));
411 }
412
413 TEST_F(ValidateLogicals, OpLessOrGreaterOperandsDifferentType) {
414 const std::string body = R"(
415 %val1 = OpLessOrGreater %bool %f32_1 %f64_1
416 )";
417
418 CompileSuccessfully(GenerateKernelCode(body).c_str());
419 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
420 EXPECT_THAT(
421 getDiagnosticString(),
422 HasSubstr("Expected left and right operands to have the same type: "
423 "LessOrGreater"));
424 }
425
426 TEST_F(ValidateLogicals, OpFOrdEqualSuccess) {
427 const std::string body = R"(
428 %val1 = OpFOrdEqual %bool %f32_0 %f32_1
429 %val2 = OpFOrdEqual %bool %f64_0 %f64_0
430 %val3 = OpFOrdEqual %boolvec2 %f32vec2_12 %f32vec2_12
431 %val4 = OpFOrdEqual %boolvec3 %f32vec3_123 %f32vec3_123
432 %val5 = OpFOrdEqual %boolvec4 %f32vec4_1234 %f32vec4_1234
433 )";
434
435 CompileSuccessfully(GenerateShaderCode(body).c_str());
436 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
437 }
438
439 TEST_F(ValidateLogicals, OpFOrdEqualWrongTypeId) {
440 const std::string body = R"(
441 %val1 = OpFOrdEqual %u32 %f32_1 %f32_1
442 )";
443
444 CompileSuccessfully(GenerateShaderCode(body).c_str());
445 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
446 EXPECT_THAT(
447 getDiagnosticString(),
448 HasSubstr(
449 "Expected bool scalar or vector type as Result Type: FOrdEqual"));
450 }
451
452 TEST_F(ValidateLogicals, OpFOrdEqualLeftOperandNotFloat) {
453 const std::string body = R"(
454 %val1 = OpFOrdEqual %bool %u32_1 %f32_1
455 )";
456
457 CompileSuccessfully(GenerateShaderCode(body).c_str());
458 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
459 EXPECT_THAT(
460 getDiagnosticString(),
461 HasSubstr("Expected operands to be scalar or vector float: FOrdEqual"));
462 }
463
464 TEST_F(ValidateLogicals, OpFOrdEqualLeftOperandWrongSize) {
465 const std::string body = R"(
466 %val1 = OpFOrdEqual %bool %f32vec2_12 %f32_1
467 )";
468
469 CompileSuccessfully(GenerateShaderCode(body).c_str());
470 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
471 EXPECT_THAT(
472 getDiagnosticString(),
473 HasSubstr(
474 "Expected vector sizes of Result Type and the operands to be equal: "
475 "FOrdEqual"));
476 }
477
478 TEST_F(ValidateLogicals, OpFOrdEqualOperandsDifferentType) {
479 const std::string body = R"(
480 %val1 = OpFOrdEqual %bool %f32_1 %f64_1
481 )";
482
483 CompileSuccessfully(GenerateShaderCode(body).c_str());
484 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
485 EXPECT_THAT(
486 getDiagnosticString(),
487 HasSubstr("Expected left and right operands to have the same type: "
488 "FOrdEqual"));
489 }
490
491 TEST_F(ValidateLogicals, OpLogicalEqualSuccess) {
492 const std::string body = R"(
493 %val1 = OpLogicalEqual %bool %true %false
494 %val2 = OpLogicalEqual %boolvec2 %boolvec2_tf %boolvec2_tf
495 %val3 = OpLogicalEqual %boolvec3 %boolvec3_tft %boolvec3_tft
496 %val4 = OpLogicalEqual %boolvec4 %boolvec4_tftf %boolvec4_tftf
497 )";
498
499 CompileSuccessfully(GenerateKernelCode(body).c_str());
500 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
501 }
502
503 TEST_F(ValidateLogicals, OpLogicalEqualWrongTypeId) {
504 const std::string body = R"(
505 %val1 = OpLogicalEqual %u32 %true %false
506 )";
507
508 CompileSuccessfully(GenerateKernelCode(body).c_str());
509 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
510 EXPECT_THAT(
511 getDiagnosticString(),
512 HasSubstr(
513 "Expected bool scalar or vector type as Result Type: LogicalEqual"));
514 }
515
516 TEST_F(ValidateLogicals, OpLogicalEqualWrongLeftOperand) {
517 const std::string body = R"(
518 %val1 = OpLogicalEqual %bool %boolvec2_tf %false
519 )";
520
521 CompileSuccessfully(GenerateKernelCode(body).c_str());
522 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
523 EXPECT_THAT(
524 getDiagnosticString(),
525 HasSubstr("Expected both operands to be of Result Type: LogicalEqual"));
526 }
527
528 TEST_F(ValidateLogicals, OpLogicalEqualWrongRightOperand) {
529 const std::string body = R"(
530 %val1 = OpLogicalEqual %boolvec2 %boolvec2_tf %false
531 )";
532
533 CompileSuccessfully(GenerateKernelCode(body).c_str());
534 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
535 EXPECT_THAT(
536 getDiagnosticString(),
537 HasSubstr("Expected both operands to be of Result Type: LogicalEqual"));
538 }
539
540 TEST_F(ValidateLogicals, OpLogicalNotSuccess) {
541 const std::string body = R"(
542 %val1 = OpLogicalNot %bool %true
543 %val2 = OpLogicalNot %boolvec2 %boolvec2_tf
544 %val3 = OpLogicalNot %boolvec3 %boolvec3_tft
545 %val4 = OpLogicalNot %boolvec4 %boolvec4_tftf
546 )";
547
548 CompileSuccessfully(GenerateKernelCode(body).c_str());
549 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
550 }
551
552 TEST_F(ValidateLogicals, OpLogicalNotWrongTypeId) {
553 const std::string body = R"(
554 %val1 = OpLogicalNot %u32 %true
555 )";
556
557 CompileSuccessfully(GenerateKernelCode(body).c_str());
558 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
559 EXPECT_THAT(
560 getDiagnosticString(),
561 HasSubstr(
562 "Expected bool scalar or vector type as Result Type: LogicalNot"));
563 }
564
565 TEST_F(ValidateLogicals, OpLogicalNotWrongOperand) {
566 const std::string body = R"(
567 %val1 = OpLogicalNot %bool %boolvec2_tf
568 )";
569
570 CompileSuccessfully(GenerateKernelCode(body).c_str());
571 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
572 EXPECT_THAT(getDiagnosticString(),
573 HasSubstr("Expected operand to be of Result Type: LogicalNot"));
574 }
575
576 TEST_F(ValidateLogicals, OpSelectSuccess) {
577 const std::string body = R"(
578 %val1 = OpSelect %u32 %true %u32_0 %u32_1
579 %val2 = OpSelect %f32 %true %f32_0 %f32_1
580 %val3 = OpSelect %f64 %true %f64_0 %f64_1
581 %val4 = OpSelect %f32vec2 %boolvec2_tf %f32vec2_01 %f32vec2_12
582 %val5 = OpSelect %f32vec4 %boolvec4_tftf %f32vec4_0123 %f32vec4_1234
583 )";
584
585 CompileSuccessfully(GenerateShaderCode(body).c_str());
586 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
587 }
588
589 TEST_F(ValidateLogicals, OpSelectWrongTypeId) {
590 const std::string body = R"(
591 %val1 = OpSelect %void %true %u32_0 %u32_1
592 )";
593
594 CompileSuccessfully(GenerateShaderCode(body).c_str());
595 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
596 EXPECT_THAT(
597 getDiagnosticString(),
598 HasSubstr("Expected scalar or vector type as Result Type: Select"));
599 }
600
601 TEST_F(ValidateLogicals, OpSelectWrongTypeIdV14) {
602 // In 1.4, the message changes to allow composites.
603 const std::string body = R"(
604 %val1 = OpSelect %void %true %u32_0 %u32_1
605 )";
606
607 CompileSuccessfully(GenerateShaderCode(body).c_str(), SPV_ENV_UNIVERSAL_1_4);
608 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
609 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
610 EXPECT_THAT(
611 getDiagnosticString(),
612 HasSubstr("Expected scalar or composite type as Result Type: Select"));
613 }
614
615 TEST_F(ValidateLogicals, OpSelectPointerNoCapability) {
616 const std::string body = R"(
617 %x = OpVariable %f32vec4ptr Function
618 %y = OpVariable %f32vec4ptr Function
619 OpStore %x %f32vec4_0123
620 OpStore %y %f32vec4_1234
621 %val1 = OpSelect %f32vec4ptr %true %x %y
622 )";
623
624 CompileSuccessfully(GenerateShaderCode(body).c_str());
625 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
626 EXPECT_THAT(
627 getDiagnosticString(),
628 HasSubstr(
629 "Using pointers with OpSelect requires capability VariablePointers "
630 "or VariablePointersStorageBuffer"));
631 }
632
633 TEST_F(ValidateLogicals, OpSelectPointerWithCapability1) {
634 const std::string spirv = R"(
635 OpCapability Shader
636 OpCapability VariablePointers
637 OpExtension "SPV_KHR_variable_pointers"
638 OpMemoryModel Logical GLSL450
639 OpEntryPoint GLCompute %main "main"
640 %void = OpTypeVoid
641 %int = OpTypeInt 32 0
642 %bool = OpTypeBool
643 %undef = OpUndef %bool
644 %ptr = OpTypePointer Workgroup %int
645 %var = OpVariable %ptr Workgroup
646 %null = OpConstantNull %ptr
647 %void_fn = OpTypeFunction %void
648 %main = OpFunction %void None %void_fn
649 %entry = OpLabel
650 %sel = OpSelect %ptr %undef %var %null
651 OpReturn
652 OpFunctionEnd
653 )";
654
655 CompileSuccessfully(spirv);
656 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
657 }
658
659 TEST_F(ValidateLogicals, OpSelectPointerWithCapability2) {
660 const std::string spirv = R"(
661 OpCapability Shader
662 OpCapability VariablePointersStorageBuffer
663 OpExtension "SPV_KHR_variable_pointers"
664 OpMemoryModel Logical GLSL450
665 OpEntryPoint GLCompute %main "main"
666 %void = OpTypeVoid
667 %int = OpTypeInt 32 0
668 %bool = OpTypeBool
669 %undef = OpUndef %bool
670 %ptr = OpTypePointer StorageBuffer %int
671 %var = OpVariable %ptr StorageBuffer
672 %null = OpConstantNull %ptr
673 %void_fn = OpTypeFunction %void
674 %main = OpFunction %void None %void_fn
675 %entry = OpLabel
676 %sel = OpSelect %ptr %undef %var %null
677 OpReturn
678 OpFunctionEnd
679 )";
680
681 CompileSuccessfully(spirv);
682 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
683 }
684
685 TEST_F(ValidateLogicals, OpSelectWrongCondition) {
686 const std::string body = R"(
687 %val1 = OpSelect %u32 %u32_1 %u32_0 %u32_1
688 )";
689
690 CompileSuccessfully(GenerateShaderCode(body).c_str());
691 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
692 EXPECT_THAT(
693 getDiagnosticString(),
694 HasSubstr("Expected bool scalar or vector type as condition: Select"));
695 }
696
697 TEST_F(ValidateLogicals, OpSelectWrongConditionDimension) {
698 const std::string body = R"(
699 %val1 = OpSelect %u32vec2 %true %u32vec2_01 %u32vec2_12
700 )";
701
702 CompileSuccessfully(GenerateShaderCode(body).c_str());
703 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
704 EXPECT_THAT(
705 getDiagnosticString(),
706 HasSubstr(
707 "Expected vector sizes of Result Type and the condition to be equal: "
708 "Select"));
709 }
710
711 TEST_F(ValidateLogicals, OpSelectWrongLeftObject) {
712 const std::string body = R"(
713 %val1 = OpSelect %bool %true %u32vec2_01 %u32_1
714 )";
715
716 CompileSuccessfully(GenerateShaderCode(body).c_str());
717 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
718 EXPECT_THAT(getDiagnosticString(),
719 HasSubstr("Expected both objects to be of Result Type: Select"));
720 }
721
722 TEST_F(ValidateLogicals, OpSelectWrongRightObject) {
723 const std::string body = R"(
724 %val1 = OpSelect %bool %true %u32_1 %u32vec2_01
725 )";
726
727 CompileSuccessfully(GenerateShaderCode(body).c_str());
728 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
729 EXPECT_THAT(getDiagnosticString(),
730 HasSubstr("Expected both objects to be of Result Type: Select"));
731 }
732
733 TEST_F(ValidateLogicals, OpSelectArrayV13Bad) {
734 const std::string body = R"(
735 %val1 = OpSelect %arr_u32_2 %true %nul_arr_u32_2 %arr_u32_2_1_2
736 )";
737
738 CompileSuccessfully(GenerateShaderCode(body).c_str());
739 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
740 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
741 EXPECT_THAT(
742 getDiagnosticString(),
743 HasSubstr("Expected scalar or vector type as Result Type: Select"));
744 }
745
746 TEST_F(ValidateLogicals, OpSelectArrayV13TargetV14Bad) {
747 const std::string body = R"(
748 %val1 = OpSelect %arr_u32_2 %true %nul_arr_u32_2 %arr_u32_2_1_2
749 )";
750
751 CompileSuccessfully(GenerateShaderCode(body).c_str());
752 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
753 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
754 EXPECT_THAT(getDiagnosticString(),
755 HasSubstr("Expected scalar or vector type as Result Type"));
756 }
757
758 TEST_F(ValidateLogicals, OpSelectArrayV14Good) {
759 const std::string body = R"(
760 %val1 = OpSelect %arr_u32_2 %true %nul_arr_u32_2 %arr_u32_2_1_2
761 )";
762
763 CompileSuccessfully(GenerateShaderCode(body).c_str(), SPV_ENV_UNIVERSAL_1_4);
764 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
765 EXPECT_THAT(getDiagnosticString(), Eq(""));
766 }
767
768 TEST_F(ValidateLogicals, OpSelectStructV13Bad) {
769 const std::string body = R"(
770 %val1 = OpSelect %st_u32_u32 %true %nul_st_u32_u32 %st_u32_u32_1_2
771 )";
772
773 CompileSuccessfully(GenerateShaderCode(body).c_str());
774 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
775 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
776 EXPECT_THAT(
777 getDiagnosticString(),
778 HasSubstr("Expected scalar or vector type as Result Type: Select"));
779 }
780
781 TEST_F(ValidateLogicals, OpSelectStructV13TargetV14Bad) {
782 const std::string body = R"(
783 %val1 = OpSelect %st_u32_u32 %true %nul_st_u32_u32 %st_u32_u32_1_2
784 )";
785
786 CompileSuccessfully(GenerateShaderCode(body).c_str());
787 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
788 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
789 EXPECT_THAT(getDiagnosticString(),
790 HasSubstr("Expected scalar or vector type as Result Type"));
791 }
792
793 TEST_F(ValidateLogicals, OpSelectStructV14Good) {
794 const std::string body = R"(
795 %val1 = OpSelect %st_u32_u32 %true %nul_st_u32_u32 %st_u32_u32_1_2
796 )";
797
798 CompileSuccessfully(GenerateShaderCode(body).c_str(), SPV_ENV_UNIVERSAL_1_4);
799 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
800 EXPECT_THAT(getDiagnosticString(), Eq(""));
801 }
802
803 TEST_F(ValidateLogicals, OpSelectMatrixV13Bad) {
804 const std::string body = R"(
805 %val1 = OpSelect %mat_f32_2_2 %true %nul_mat_f32_2_2 %mat_f32_2_2_01_12
806 )";
807
808 CompileSuccessfully(GenerateShaderCode(body).c_str());
809 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
810 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
811 EXPECT_THAT(
812 getDiagnosticString(),
813 HasSubstr("Expected scalar or vector type as Result Type: Select"));
814 }
815
816 TEST_F(ValidateLogicals, OpSelectMatrixV13TargetV14Bad) {
817 const std::string body = R"(
818 %val1 = OpSelect %mat_f32_2_2 %true %nul_mat_f32_2_2 %mat_f32_2_2_01_12
819 )";
820
821 CompileSuccessfully(GenerateShaderCode(body).c_str());
822 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
823 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
824 EXPECT_THAT(getDiagnosticString(),
825 HasSubstr("Expected scalar or vector type as Result Type"));
826 }
827
828 TEST_F(ValidateLogicals, OpSelectMatrixV14Good) {
829 const std::string body = R"(
830 %val1 = OpSelect %mat_f32_2_2 %true %nul_mat_f32_2_2 %mat_f32_2_2_01_12
831 )";
832
833 CompileSuccessfully(GenerateShaderCode(body).c_str(), SPV_ENV_UNIVERSAL_1_4);
834 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
835 EXPECT_THAT(getDiagnosticString(), Eq(""));
836 }
837
838 TEST_F(ValidateLogicals, OpIEqualSuccess) {
839 const std::string body = R"(
840 %val1 = OpIEqual %bool %u32_0 %s32_1
841 %val2 = OpIEqual %bool %s64_0 %u64_0
842 %val3 = OpIEqual %boolvec2 %s32vec2_12 %u32vec2_12
843 %val4 = OpIEqual %boolvec3 %s32vec3_123 %u32vec3_123
844 %val5 = OpIEqual %boolvec4 %s32vec4_1234 %u32vec4_1234
845 )";
846
847 CompileSuccessfully(GenerateShaderCode(body).c_str());
848 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
849 }
850
851 TEST_F(ValidateLogicals, OpIEqualWrongTypeId) {
852 const std::string body = R"(
853 %val1 = OpIEqual %u32 %s32_1 %s32_1
854 )";
855
856 CompileSuccessfully(GenerateShaderCode(body).c_str());
857 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
858 EXPECT_THAT(
859 getDiagnosticString(),
860 HasSubstr("Expected bool scalar or vector type as Result Type: IEqual"));
861 }
862
863 TEST_F(ValidateLogicals, OpIEqualLeftOperandNotInt) {
864 const std::string body = R"(
865 %val1 = OpIEqual %bool %f32_1 %s32_1
866 )";
867
868 CompileSuccessfully(GenerateShaderCode(body).c_str());
869 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
870 EXPECT_THAT(
871 getDiagnosticString(),
872 HasSubstr("Expected operands to be scalar or vector int: IEqual"));
873 }
874
875 TEST_F(ValidateLogicals, OpIEqualLeftOperandWrongSize) {
876 const std::string body = R"(
877 %val1 = OpIEqual %bool %s32vec2_12 %s32_1
878 )";
879
880 CompileSuccessfully(GenerateShaderCode(body).c_str());
881 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
882 EXPECT_THAT(
883 getDiagnosticString(),
884 HasSubstr(
885 "Expected vector sizes of Result Type and the operands to be equal: "
886 "IEqual"));
887 }
888
889 TEST_F(ValidateLogicals, OpIEqualRightOperandNotInt) {
890 const std::string body = R"(
891 %val1 = OpIEqual %bool %u32_1 %f32_1
892 )";
893
894 CompileSuccessfully(GenerateShaderCode(body).c_str());
895 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
896 EXPECT_THAT(
897 getDiagnosticString(),
898 HasSubstr("Expected operands to be scalar or vector int: IEqual"));
899 }
900
901 TEST_F(ValidateLogicals, OpIEqualDifferentBitWidth) {
902 const std::string body = R"(
903 %val1 = OpIEqual %bool %u32_1 %u64_1
904 )";
905
906 CompileSuccessfully(GenerateShaderCode(body).c_str());
907 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
908 EXPECT_THAT(getDiagnosticString(),
909 HasSubstr("Expected both operands to have the same component bit "
910 "width: IEqual"));
911 }
912
913 TEST_F(ValidateLogicals, OpUGreaterThanSuccess) {
914 const std::string body = R"(
915 %val1 = OpUGreaterThan %bool %u32_0 %u32_1
916 %val2 = OpUGreaterThan %bool %s32_0 %u32_1
917 %val3 = OpUGreaterThan %bool %u64_0 %u64_0
918 %val4 = OpUGreaterThan %bool %u64_0 %s64_0
919 %val5 = OpUGreaterThan %boolvec2 %u32vec2_12 %u32vec2_12
920 %val6 = OpUGreaterThan %boolvec3 %s32vec3_123 %u32vec3_123
921 %val7 = OpUGreaterThan %boolvec4 %u32vec4_1234 %u32vec4_1234
922 )";
923
924 CompileSuccessfully(GenerateShaderCode(body).c_str());
925 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
926 }
927
928 TEST_F(ValidateLogicals, OpUGreaterThanWrongTypeId) {
929 const std::string body = R"(
930 %val1 = OpUGreaterThan %u32 %u32_1 %u32_1
931 )";
932
933 CompileSuccessfully(GenerateShaderCode(body).c_str());
934 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
935 EXPECT_THAT(
936 getDiagnosticString(),
937 HasSubstr(
938 "Expected bool scalar or vector type as Result Type: UGreaterThan"));
939 }
940
941 TEST_F(ValidateLogicals, OpUGreaterThanLeftOperandNotInt) {
942 const std::string body = R"(
943 %val1 = OpUGreaterThan %bool %f32_1 %u32_1
944 )";
945
946 CompileSuccessfully(GenerateShaderCode(body).c_str());
947 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
948 EXPECT_THAT(
949 getDiagnosticString(),
950 HasSubstr("Expected operands to be scalar or vector int: UGreaterThan"));
951 }
952
953 TEST_F(ValidateLogicals, OpUGreaterThanLeftOperandWrongSize) {
954 const std::string body = R"(
955 %val1 = OpUGreaterThan %bool %u32vec2_12 %u32_1
956 )";
957
958 CompileSuccessfully(GenerateShaderCode(body).c_str());
959 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
960 EXPECT_THAT(
961 getDiagnosticString(),
962 HasSubstr(
963 "Expected vector sizes of Result Type and the operands to be equal: "
964 "UGreaterThan"));
965 }
966
967 TEST_F(ValidateLogicals, OpUGreaterThanRightOperandNotInt) {
968 const std::string body = R"(
969 %val1 = OpUGreaterThan %bool %u32_1 %f32_1
970 )";
971
972 CompileSuccessfully(GenerateShaderCode(body).c_str());
973 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
974 EXPECT_THAT(
975 getDiagnosticString(),
976 HasSubstr("Expected operands to be scalar or vector int: UGreaterThan"));
977 }
978
979 TEST_F(ValidateLogicals, OpUGreaterThanDifferentBitWidth) {
980 const std::string body = R"(
981 %val1 = OpUGreaterThan %bool %u32_1 %u64_1
982 )";
983
984 CompileSuccessfully(GenerateShaderCode(body).c_str());
985 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
986 EXPECT_THAT(
987 getDiagnosticString(),
988 HasSubstr("Expected both operands to have the same component bit width: "
989 "UGreaterThan"));
990 }
991
992 TEST_F(ValidateLogicals, OpSGreaterThanSuccess) {
993 const std::string body = R"(
994 %val1 = OpSGreaterThan %bool %s32_0 %s32_1
995 %val2 = OpSGreaterThan %bool %u32_0 %s32_1
996 %val3 = OpSGreaterThan %bool %s64_0 %s64_0
997 %val4 = OpSGreaterThan %bool %s64_0 %u64_0
998 %val5 = OpSGreaterThan %boolvec2 %s32vec2_12 %s32vec2_12
999 %val6 = OpSGreaterThan %boolvec3 %s32vec3_123 %u32vec3_123
1000 %val7 = OpSGreaterThan %boolvec4 %s32vec4_1234 %s32vec4_1234
1001 )";
1002
1003 CompileSuccessfully(GenerateShaderCode(body).c_str());
1004 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1005 }
1006
1007 TEST_F(ValidateLogicals, OpSGreaterThanWrongTypeId) {
1008 const std::string body = R"(
1009 %val1 = OpSGreaterThan %s32 %s32_1 %s32_1
1010 )";
1011
1012 CompileSuccessfully(GenerateShaderCode(body).c_str());
1013 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1014 EXPECT_THAT(
1015 getDiagnosticString(),
1016 HasSubstr(
1017 "Expected bool scalar or vector type as Result Type: SGreaterThan"));
1018 }
1019
1020 TEST_F(ValidateLogicals, OpSGreaterThanLeftOperandNotInt) {
1021 const std::string body = R"(
1022 %val1 = OpSGreaterThan %bool %f32_1 %s32_1
1023 )";
1024
1025 CompileSuccessfully(GenerateShaderCode(body).c_str());
1026 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1027 EXPECT_THAT(
1028 getDiagnosticString(),
1029 HasSubstr("Expected operands to be scalar or vector int: SGreaterThan"));
1030 }
1031
1032 TEST_F(ValidateLogicals, OpSGreaterThanLeftOperandWrongSize) {
1033 const std::string body = R"(
1034 %val1 = OpSGreaterThan %bool %s32vec2_12 %s32_1
1035 )";
1036
1037 CompileSuccessfully(GenerateShaderCode(body).c_str());
1038 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1039 EXPECT_THAT(
1040 getDiagnosticString(),
1041 HasSubstr(
1042 "Expected vector sizes of Result Type and the operands to be equal: "
1043 "SGreaterThan"));
1044 }
1045
1046 TEST_F(ValidateLogicals, OpSGreaterThanRightOperandNotInt) {
1047 const std::string body = R"(
1048 %val1 = OpSGreaterThan %bool %s32_1 %f32_1
1049 )";
1050
1051 CompileSuccessfully(GenerateShaderCode(body).c_str());
1052 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1053 EXPECT_THAT(
1054 getDiagnosticString(),
1055 HasSubstr("Expected operands to be scalar or vector int: SGreaterThan"));
1056 }
1057
1058 TEST_F(ValidateLogicals, OpSGreaterThanDifferentBitWidth) {
1059 const std::string body = R"(
1060 %val1 = OpSGreaterThan %bool %s32_1 %s64_1
1061 )";
1062
1063 CompileSuccessfully(GenerateShaderCode(body).c_str());
1064 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1065 EXPECT_THAT(getDiagnosticString(),
1066 HasSubstr("Expected both operands to have the same component bit "
1067 "width: SGreaterThan"));
1068 }
1069
1070 TEST_F(ValidateLogicals, PSBSelectSuccess) {
1071 const std::string body = R"(
1072 OpCapability PhysicalStorageBufferAddresses
1073 OpCapability Int64
1074 OpCapability Shader
1075 OpExtension "SPV_EXT_physical_storage_buffer"
1076 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1077 OpEntryPoint Fragment %main "main"
1078 OpExecutionMode %main OriginUpperLeft
1079 OpDecorate %val1 AliasedPointer
1080 %uint64 = OpTypeInt 64 0
1081 %bool = OpTypeBool
1082 %true = OpConstantTrue %bool
1083 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1084 %pptr_f = OpTypePointer Function %ptr
1085 %void = OpTypeVoid
1086 %voidfn = OpTypeFunction %void
1087 %main = OpFunction %void None %voidfn
1088 %entry = OpLabel
1089 %val1 = OpVariable %pptr_f Function
1090 %val2 = OpLoad %ptr %val1
1091 %val3 = OpSelect %ptr %true %val2 %val2
1092 OpReturn
1093 OpFunctionEnd
1094 )";
1095
1096 CompileSuccessfully(body.c_str());
1097 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1098 }
1099
1100 TEST_F(ValidateLogicals, SelectVectorsScalarCondition) {
1101 const std::string spirv = R"(
1102 OpCapability Shader
1103 OpCapability Linkage
1104 OpMemoryModel Logical GLSL450
1105 %void = OpTypeVoid
1106 %bool = OpTypeBool
1107 %int = OpTypeInt 32 0
1108 %int4 = OpTypeVector %int 4
1109 %int4_0 = OpConstantNull %int4
1110 %true = OpConstantTrue %bool
1111 %void_fn = OpTypeFunction %void
1112 %func = OpFunction %void None %void_fn
1113 %1 = OpLabel
1114 %select = OpSelect %int4 %true %int4_0 %int4_0
1115 OpReturn
1116 OpFunctionEnd
1117 )";
1118
1119 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1120 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1121 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1122 EXPECT_THAT(getDiagnosticString(),
1123 HasSubstr("Expected vector sizes of Result Type and the "
1124 "condition to be equal: Select"));
1125 }
1126
1127 TEST_F(ValidateLogicals, SelectVectorsScalarCondition1p4) {
1128 const std::string spirv = R"(
1129 OpCapability Shader
1130 OpCapability Linkage
1131 OpMemoryModel Logical GLSL450
1132 %void = OpTypeVoid
1133 %bool = OpTypeBool
1134 %int = OpTypeInt 32 0
1135 %int4 = OpTypeVector %int 4
1136 %int4_0 = OpConstantNull %int4
1137 %true = OpConstantTrue %bool
1138 %void_fn = OpTypeFunction %void
1139 %func = OpFunction %void None %void_fn
1140 %1 = OpLabel
1141 %select = OpSelect %int4 %true %int4_0 %int4_0
1142 OpReturn
1143 OpFunctionEnd
1144 )";
1145
1146 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1147 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1148 }
1149
1150 TEST_F(ValidateLogicals, SelectVectorsVectorConditionMismatchedDimensions1p4) {
1151 const std::string spirv = R"(
1152 OpCapability Shader
1153 OpCapability Linkage
1154 OpMemoryModel Logical GLSL450
1155 %void = OpTypeVoid
1156 %bool = OpTypeBool
1157 %bool3 = OpTypeVector %bool 3
1158 %int = OpTypeInt 32 0
1159 %int4 = OpTypeVector %int 4
1160 %int4_0 = OpConstantNull %int4
1161 %bool3_null = OpConstantNull %bool3
1162 %void_fn = OpTypeFunction %void
1163 %func = OpFunction %void None %void_fn
1164 %1 = OpLabel
1165 %select = OpSelect %int4 %bool3_null %int4_0 %int4_0
1166 OpReturn
1167 OpFunctionEnd
1168 )";
1169
1170 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1171 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1172 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1173 EXPECT_THAT(getDiagnosticString(),
1174 HasSubstr("Expected vector sizes of Result Type and the "
1175 "condition to be equal: Select"));
1176 }
1177
1178 TEST_F(ValidateLogicals, SelectNVBindlessSamplers) {
1179 const std::string spirv = R"(
1180 OpCapability Shader
1181 OpCapability BindlessTextureNV
1182 OpExtension "SPV_NV_bindless_texture"
1183 %1 = OpExtInstImport "GLSL.std.450"
1184 OpMemoryModel Logical GLSL450
1185 OpSamplerImageAddressingModeNV 64
1186 OpEntryPoint Fragment %main "main"
1187 OpExecutionMode %main OriginUpperLeft
1188 OpSource GLSL 450
1189 OpSourceExtension "GL_NV_bindless_texture"
1190 OpName %main "main"
1191 OpName %s2D "s2D"
1192 OpName %pickhandle "pickhandle"
1193 OpName %Sampler1 "Sampler1"
1194 OpName %Sampler2 "Sampler2"
1195 OpDecorate %pickhandle Flat
1196 OpDecorate %Sampler1 DescriptorSet 0
1197 OpDecorate %Sampler1 Binding 0
1198 OpDecorate %Sampler1 BindlessSamplerNV
1199 OpDecorate %Sampler2 DescriptorSet 0
1200 OpDecorate %Sampler2 Binding 1
1201 OpDecorate %Sampler2 BindlessSamplerNV
1202 %void = OpTypeVoid
1203 %3 = OpTypeFunction %void
1204 %float = OpTypeFloat 32
1205 %7 = OpTypeImage %float 2D 0 0 0 1 Unknown
1206 %8 = OpTypeSampledImage %7
1207 %_ptr_Function_8 = OpTypePointer Function %8
1208 %int = OpTypeInt 32 1
1209 %_ptr_Function_int = OpTypePointer Function %int
1210 %int_0 = OpConstant %int 0
1211 %bool = OpTypeBool
1212 %_ptr_UniformConstant_8 = OpTypePointer UniformConstant %8
1213 %Sampler1 = OpVariable %_ptr_UniformConstant_8 UniformConstant
1214 %Sampler2 = OpVariable %_ptr_UniformConstant_8 UniformConstant
1215 %main = OpFunction %void None %3
1216 %5 = OpLabel
1217 %s2D = OpVariable %_ptr_Function_8 Function
1218 %pickhandle = OpVariable %_ptr_Function_int Function
1219 %14 = OpLoad %int %pickhandle
1220 %17 = OpIEqual %bool %14 %int_0
1221 %20 = OpLoad %8 %Sampler1
1222 %22 = OpLoad %8 %Sampler2
1223 %23 = OpSelect %8 %17 %20 %22
1224 OpStore %s2D %23
1225 OpReturn
1226 OpFunctionEnd
1227 )";
1228
1229 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1230 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1231 }
1232
1233 } // namespace
1234 } // namespace val
1235 } // namespace spvtools