Debian Codegraph @

test/val/val_arithmetics_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::HasSubstr;
28 using ::testing::Not;
29
30 using ValidateArithmetics = spvtest::ValidateBase<bool>;
31
32 std::string GenerateCode(const std::string& main_body) {
33 const std::string prefix =
34 R"(
35 OpCapability Shader
36 OpCapability Int64
37 OpCapability Float64
38 OpCapability Matrix
39 %ext_inst = OpExtInstImport "GLSL.std.450"
40 OpMemoryModel Logical GLSL450
41 OpEntryPoint Fragment %main "main"
42 OpExecutionMode %main OriginUpperLeft
43 %void = OpTypeVoid
44 %func = OpTypeFunction %void
45 %bool = OpTypeBool
46 %f32 = OpTypeFloat 32
47 %u32 = OpTypeInt 32 0
48 %s32 = OpTypeInt 32 1
49 %f64 = OpTypeFloat 64
50 %u64 = OpTypeInt 64 0
51 %s64 = OpTypeInt 64 1
52 %boolvec2 = OpTypeVector %bool 2
53 %s32vec2 = OpTypeVector %s32 2
54 %u32vec2 = OpTypeVector %u32 2
55 %u64vec2 = OpTypeVector %u64 2
56 %f32vec2 = OpTypeVector %f32 2
57 %f64vec2 = OpTypeVector %f64 2
58 %boolvec3 = OpTypeVector %bool 3
59 %u32vec3 = OpTypeVector %u32 3
60 %u64vec3 = OpTypeVector %u64 3
61 %s32vec3 = OpTypeVector %s32 3
62 %f32vec3 = OpTypeVector %f32 3
63 %f64vec3 = OpTypeVector %f64 3
64 %boolvec4 = OpTypeVector %bool 4
65 %u32vec4 = OpTypeVector %u32 4
66 %u64vec4 = OpTypeVector %u64 4
67 %s32vec4 = OpTypeVector %s32 4
68 %f32vec4 = OpTypeVector %f32 4
69 %f64vec4 = OpTypeVector %f64 4
70
71 %f32mat22 = OpTypeMatrix %f32vec2 2
72 %f32mat23 = OpTypeMatrix %f32vec2 3
73 %f32mat32 = OpTypeMatrix %f32vec3 2
74 %f32mat33 = OpTypeMatrix %f32vec3 3
75 %f64mat22 = OpTypeMatrix %f64vec2 2
76
77 %struct_f32_f32 = OpTypeStruct %f32 %f32
78 %struct_u32_u32 = OpTypeStruct %u32 %u32
79 %struct_u32_u32_u32 = OpTypeStruct %u32 %u32 %u32
80 %struct_s32_s32 = OpTypeStruct %s32 %s32
81 %struct_s32_u32 = OpTypeStruct %s32 %u32
82 %struct_u32vec2_u32vec2 = OpTypeStruct %u32vec2 %u32vec2
83 %struct_s32vec2_s32vec2 = OpTypeStruct %s32vec2 %s32vec2
84
85 %f32_0 = OpConstant %f32 0
86 %f32_1 = OpConstant %f32 1
87 %f32_2 = OpConstant %f32 2
88 %f32_3 = OpConstant %f32 3
89 %f32_4 = OpConstant %f32 4
90 %f32_pi = OpConstant %f32 3.14159
91
92 %s32_0 = OpConstant %s32 0
93 %s32_1 = OpConstant %s32 1
94 %s32_2 = OpConstant %s32 2
95 %s32_3 = OpConstant %s32 3
96 %s32_4 = OpConstant %s32 4
97 %s32_m1 = OpConstant %s32 -1
98
99 %u32_0 = OpConstant %u32 0
100 %u32_1 = OpConstant %u32 1
101 %u32_2 = OpConstant %u32 2
102 %u32_3 = OpConstant %u32 3
103 %u32_4 = OpConstant %u32 4
104
105 %f64_0 = OpConstant %f64 0
106 %f64_1 = OpConstant %f64 1
107 %f64_2 = OpConstant %f64 2
108 %f64_3 = OpConstant %f64 3
109 %f64_4 = OpConstant %f64 4
110
111 %s64_0 = OpConstant %s64 0
112 %s64_1 = OpConstant %s64 1
113 %s64_2 = OpConstant %s64 2
114 %s64_3 = OpConstant %s64 3
115 %s64_4 = OpConstant %s64 4
116 %s64_m1 = OpConstant %s64 -1
117
118 %u64_0 = OpConstant %u64 0
119 %u64_1 = OpConstant %u64 1
120 %u64_2 = OpConstant %u64 2
121 %u64_3 = OpConstant %u64 3
122 %u64_4 = OpConstant %u64 4
123
124 %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
125 %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
126 %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
127 %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
128 %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
129 %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
130
131 %s32vec2_01 = OpConstantComposite %s32vec2 %s32_0 %s32_1
132 %s32vec2_12 = OpConstantComposite %s32vec2 %s32_1 %s32_2
133 %s32vec3_012 = OpConstantComposite %s32vec3 %s32_0 %s32_1 %s32_2
134 %s32vec3_123 = OpConstantComposite %s32vec3 %s32_1 %s32_2 %s32_3
135 %s32vec4_0123 = OpConstantComposite %s32vec4 %s32_0 %s32_1 %s32_2 %s32_3
136 %s32vec4_1234 = OpConstantComposite %s32vec4 %s32_1 %s32_2 %s32_3 %s32_4
137
138 %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
139 %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
140 %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
141 %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
142 %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
143 %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
144
145 %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
146 %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
147 %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
148 %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
149 %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
150 %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
151
152 %f32mat22_1212 = OpConstantComposite %f32mat22 %f32vec2_12 %f32vec2_12
153 %f32mat23_121212 = OpConstantComposite %f32mat23 %f32vec2_12 %f32vec2_12 %f32vec2_12
154 %f32mat32_123123 = OpConstantComposite %f32mat32 %f32vec3_123 %f32vec3_123
155 %f32mat33_123123123 = OpConstantComposite %f32mat33 %f32vec3_123 %f32vec3_123 %f32vec3_123
156
157 %f64mat22_1212 = OpConstantComposite %f64mat22 %f64vec2_12 %f64vec2_12
158
159 %main = OpFunction %void None %func
160 %main_entry = OpLabel)";
161
162 const std::string suffix =
163 R"(
164 OpReturn
165 OpFunctionEnd)";
166
167 return prefix + main_body + suffix;
168 }
169
170 TEST_F(ValidateArithmetics, F32Success) {
171 const std::string body = R"(
172 %val1 = OpFMul %f32 %f32_0 %f32_1
173 %val2 = OpFSub %f32 %f32_2 %f32_0
174 %val3 = OpFAdd %f32 %val1 %val2
175 %val4 = OpFNegate %f32 %val3
176 %val5 = OpFDiv %f32 %val4 %val1
177 %val6 = OpFRem %f32 %val4 %f32_2
178 %val7 = OpFMod %f32 %val4 %f32_2
179 )";
180
181 CompileSuccessfully(GenerateCode(body).c_str());
182 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
183 }
184
185 TEST_F(ValidateArithmetics, F64Success) {
186 const std::string body = R"(
187 %val1 = OpFMul %f64 %f64_0 %f64_1
188 %val2 = OpFSub %f64 %f64_2 %f64_0
189 %val3 = OpFAdd %f64 %val1 %val2
190 %val4 = OpFNegate %f64 %val3
191 %val5 = OpFDiv %f64 %val4 %val1
192 %val6 = OpFRem %f64 %val4 %f64_2
193 %val7 = OpFMod %f64 %val4 %f64_2
194 )";
195
196 CompileSuccessfully(GenerateCode(body).c_str());
197 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
198 }
199
200 TEST_F(ValidateArithmetics, Int32Success) {
201 const std::string body = R"(
202 %val1 = OpIMul %u32 %s32_0 %u32_1
203 %val2 = OpIMul %s32 %s32_2 %u32_1
204 %val3 = OpIAdd %u32 %val1 %val2
205 %val4 = OpIAdd %s32 %val1 %val2
206 %val5 = OpISub %u32 %val3 %val4
207 %val6 = OpISub %s32 %val4 %val3
208 %val7 = OpSDiv %s32 %val4 %val3
209 %val8 = OpSNegate %s32 %val7
210 %val9 = OpSRem %s32 %val4 %val3
211 %val10 = OpSMod %s32 %val4 %val3
212 )";
213
214 CompileSuccessfully(GenerateCode(body).c_str());
215 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
216 }
217
218 TEST_F(ValidateArithmetics, Int64Success) {
219 const std::string body = R"(
220 %val1 = OpIMul %u64 %s64_0 %u64_1
221 %val2 = OpIMul %s64 %s64_2 %u64_1
222 %val3 = OpIAdd %u64 %val1 %val2
223 %val4 = OpIAdd %s64 %val1 %val2
224 %val5 = OpISub %u64 %val3 %val4
225 %val6 = OpISub %s64 %val4 %val3
226 %val7 = OpSDiv %s64 %val4 %val3
227 %val8 = OpSNegate %s64 %val7
228 %val9 = OpSRem %s64 %val4 %val3
229 %val10 = OpSMod %s64 %val4 %val3
230 )";
231
232 CompileSuccessfully(GenerateCode(body).c_str());
233 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
234 }
235
236 TEST_F(ValidateArithmetics, F32Vec2Success) {
237 const std::string body = R"(
238 %val1 = OpFMul %f32vec2 %f32vec2_01 %f32vec2_12
239 %val2 = OpFSub %f32vec2 %f32vec2_12 %f32vec2_01
240 %val3 = OpFAdd %f32vec2 %val1 %val2
241 %val4 = OpFNegate %f32vec2 %val3
242 %val5 = OpFDiv %f32vec2 %val4 %val1
243 %val6 = OpFRem %f32vec2 %val4 %f32vec2_12
244 %val7 = OpFMod %f32vec2 %val4 %f32vec2_12
245 )";
246
247 CompileSuccessfully(GenerateCode(body).c_str());
248 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
249 }
250
251 TEST_F(ValidateArithmetics, F64Vec2Success) {
252 const std::string body = R"(
253 %val1 = OpFMul %f64vec2 %f64vec2_01 %f64vec2_12
254 %val2 = OpFSub %f64vec2 %f64vec2_12 %f64vec2_01
255 %val3 = OpFAdd %f64vec2 %val1 %val2
256 %val4 = OpFNegate %f64vec2 %val3
257 %val5 = OpFDiv %f64vec2 %val4 %val1
258 %val6 = OpFRem %f64vec2 %val4 %f64vec2_12
259 %val7 = OpFMod %f64vec2 %val4 %f64vec2_12
260 )";
261
262 CompileSuccessfully(GenerateCode(body).c_str());
263 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
264 }
265
266 TEST_F(ValidateArithmetics, U32Vec2Success) {
267 const std::string body = R"(
268 %val1 = OpIMul %u32vec2 %u32vec2_01 %u32vec2_12
269 %val2 = OpISub %u32vec2 %u32vec2_12 %u32vec2_01
270 %val3 = OpIAdd %u32vec2 %val1 %val2
271 %val4 = OpSNegate %u32vec2 %val3
272 %val5 = OpSDiv %u32vec2 %val4 %val1
273 %val6 = OpSRem %u32vec2 %val4 %u32vec2_12
274 %val7 = OpSMod %u32vec2 %val4 %u32vec2_12
275 )";
276
277 CompileSuccessfully(GenerateCode(body).c_str());
278 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
279 }
280
281 TEST_F(ValidateArithmetics, FNegateTypeIdU32) {
282 const std::string body = R"(
283 %val = OpFNegate %u32 %u32_0
284 )";
285
286 CompileSuccessfully(GenerateCode(body).c_str());
287 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
288 EXPECT_THAT(
289 getDiagnosticString(),
290 HasSubstr(
291 "Expected floating scalar or vector type as Result Type: FNegate"));
292 }
293
294 TEST_F(ValidateArithmetics, FNegateTypeIdVec2U32) {
295 const std::string body = R"(
296 %val = OpFNegate %u32vec2 %u32vec2_01
297 )";
298
299 CompileSuccessfully(GenerateCode(body).c_str());
300 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
301 EXPECT_THAT(
302 getDiagnosticString(),
303 HasSubstr(
304 "Expected floating scalar or vector type as Result Type: FNegate"));
305 }
306
307 TEST_F(ValidateArithmetics, FNegateWrongOperand) {
308 const std::string body = R"(
309 %val = OpFNegate %f32 %u32_0
310 )";
311
312 CompileSuccessfully(GenerateCode(body).c_str());
313 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
314 EXPECT_THAT(getDiagnosticString(),
315 HasSubstr("Expected arithmetic operands to be of Result Type: "
316 "FNegate operand index 2"));
317 }
318
319 TEST_F(ValidateArithmetics, FMulTypeIdU32) {
320 const std::string body = R"(
321 %val = OpFMul %u32 %u32_0 %u32_1
322 )";
323
324 CompileSuccessfully(GenerateCode(body).c_str());
325 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
326 EXPECT_THAT(
327 getDiagnosticString(),
328 HasSubstr(
329 "Expected floating scalar or vector type as Result Type: FMul"));
330 }
331
332 TEST_F(ValidateArithmetics, FMulTypeIdVec2U32) {
333 const std::string body = R"(
334 %val = OpFMul %u32vec2 %u32vec2_01 %u32vec2_12
335 )";
336
337 CompileSuccessfully(GenerateCode(body).c_str());
338 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
339 EXPECT_THAT(
340 getDiagnosticString(),
341 HasSubstr(
342 "Expected floating scalar or vector type as Result Type: FMul"));
343 }
344
345 TEST_F(ValidateArithmetics, FMulWrongOperand1) {
346 const std::string body = R"(
347 %val = OpFMul %f32 %u32_0 %f32_1
348 )";
349
350 CompileSuccessfully(GenerateCode(body).c_str());
351 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
352 EXPECT_THAT(getDiagnosticString(),
353 HasSubstr("Expected arithmetic operands to be of Result Type: "
354 "FMul operand index 2"));
355 }
356
357 TEST_F(ValidateArithmetics, FMulWrongOperand2) {
358 const std::string body = R"(
359 %val = OpFMul %f32 %f32_0 %u32_1
360 )";
361
362 CompileSuccessfully(GenerateCode(body).c_str());
363 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
364 EXPECT_THAT(getDiagnosticString(),
365 HasSubstr("Expected arithmetic operands to be of Result Type: "
366 "FMul operand index 3"));
367 }
368
369 TEST_F(ValidateArithmetics, FMulWrongVectorOperand1) {
370 const std::string body = R"(
371 %val = OpFMul %f64vec3 %f32vec3_123 %f64vec3_012
372 )";
373
374 CompileSuccessfully(GenerateCode(body).c_str());
375 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
376 EXPECT_THAT(getDiagnosticString(),
377 HasSubstr("Expected arithmetic operands to be of Result Type: "
378 "FMul operand index 2"));
379 }
380
381 TEST_F(ValidateArithmetics, FMulWrongVectorOperand2) {
382 const std::string body = R"(
383 %val = OpFMul %f32vec3 %f32vec3_123 %f64vec3_012
384 )";
385
386 CompileSuccessfully(GenerateCode(body).c_str());
387 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
388 EXPECT_THAT(getDiagnosticString(),
389 HasSubstr("Expected arithmetic operands to be of Result Type: "
390 "FMul operand index 3"));
391 }
392
393 TEST_F(ValidateArithmetics, IMulFloatTypeId) {
394 const std::string body = R"(
395 %val = OpIMul %f32 %u32_0 %s32_1
396 )";
397
398 CompileSuccessfully(GenerateCode(body).c_str());
399 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
400 EXPECT_THAT(
401 getDiagnosticString(),
402 HasSubstr("Expected int scalar or vector type as Result Type: IMul"));
403 }
404
405 TEST_F(ValidateArithmetics, IMulFloatOperand1) {
406 const std::string body = R"(
407 %val = OpIMul %u32 %f32_0 %s32_1
408 )";
409
410 CompileSuccessfully(GenerateCode(body).c_str());
411 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
412 EXPECT_THAT(getDiagnosticString(),
413 HasSubstr("Expected int scalar or vector type as operand: "
414 "IMul operand index 2"));
415 }
416
417 TEST_F(ValidateArithmetics, IMulFloatOperand2) {
418 const std::string body = R"(
419 %val = OpIMul %u32 %s32_0 %f32_1
420 )";
421
422 CompileSuccessfully(GenerateCode(body).c_str());
423 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
424 EXPECT_THAT(getDiagnosticString(),
425 HasSubstr("Expected int scalar or vector type as operand: "
426 "IMul operand index 3"));
427 }
428
429 TEST_F(ValidateArithmetics, IMulWrongBitWidthOperand1) {
430 const std::string body = R"(
431 %val = OpIMul %u64 %u32_0 %s64_1
432 )";
433
434 CompileSuccessfully(GenerateCode(body).c_str());
435 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
436 EXPECT_THAT(
437 getDiagnosticString(),
438 HasSubstr("Expected arithmetic operands to have the same bit width "
439 "as Result Type: IMul operand index 2"));
440 }
441
442 TEST_F(ValidateArithmetics, IMulWrongBitWidthOperand2) {
443 const std::string body = R"(
444 %val = OpIMul %u32 %u32_0 %s64_1
445 )";
446
447 CompileSuccessfully(GenerateCode(body).c_str());
448 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
449 EXPECT_THAT(
450 getDiagnosticString(),
451 HasSubstr("Expected arithmetic operands to have the same bit width "
452 "as Result Type: IMul operand index 3"));
453 }
454
455 TEST_F(ValidateArithmetics, IMulWrongBitWidthVector) {
456 const std::string body = R"(
457 %val = OpIMul %u64vec3 %u32vec3_012 %u32vec3_123
458 )";
459
460 CompileSuccessfully(GenerateCode(body).c_str());
461 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
462 EXPECT_THAT(
463 getDiagnosticString(),
464 HasSubstr("Expected arithmetic operands to have the same bit width "
465 "as Result Type: IMul operand index 2"));
466 }
467
468 TEST_F(ValidateArithmetics, IMulVectorScalarOperand1) {
469 const std::string body = R"(
470 %val = OpIMul %u32vec2 %u32_0 %u32vec2_01
471 )";
472
473 CompileSuccessfully(GenerateCode(body).c_str());
474 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
475 EXPECT_THAT(
476 getDiagnosticString(),
477 HasSubstr("Expected arithmetic operands to have the same dimension "
478 "as Result Type: IMul operand index 2"));
479 }
480
481 TEST_F(ValidateArithmetics, IMulVectorScalarOperand2) {
482 const std::string body = R"(
483 %val = OpIMul %u32vec2 %u32vec2_01 %u32_0
484 )";
485
486 CompileSuccessfully(GenerateCode(body).c_str());
487 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
488 EXPECT_THAT(
489 getDiagnosticString(),
490 HasSubstr("Expected arithmetic operands to have the same dimension "
491 "as Result Type: IMul operand index 3"));
492 }
493
494 TEST_F(ValidateArithmetics, IMulScalarVectorOperand1) {
495 const std::string body = R"(
496 %val = OpIMul %s32 %u32vec2_01 %u32_0
497 )";
498
499 CompileSuccessfully(GenerateCode(body).c_str());
500 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
501 EXPECT_THAT(
502 getDiagnosticString(),
503 HasSubstr("Expected arithmetic operands to have the same dimension "
504 "as Result Type: IMul operand index 2"));
505 }
506
507 TEST_F(ValidateArithmetics, IMulScalarVectorOperand2) {
508 const std::string body = R"(
509 %val = OpIMul %u32 %u32_0 %s32vec2_01
510 )";
511
512 CompileSuccessfully(GenerateCode(body).c_str());
513 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
514 EXPECT_THAT(
515 getDiagnosticString(),
516 HasSubstr("Expected arithmetic operands to have the same dimension "
517 "as Result Type: IMul operand index 3"));
518 }
519
520 TEST_F(ValidateArithmetics, SNegateFloat) {
521 const std::string body = R"(
522 %val = OpSNegate %s32 %f32_1
523 )";
524
525 CompileSuccessfully(GenerateCode(body).c_str());
526 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
527 EXPECT_THAT(getDiagnosticString(),
528 HasSubstr("Expected int scalar or vector type as operand: "
529 "SNegate operand index 2"));
530 }
531
532 TEST_F(ValidateArithmetics, UDivFloatType) {
533 const std::string body = R"(
534 %val = OpUDiv %f32 %u32_2 %u32_1
535 )";
536
537 CompileSuccessfully(GenerateCode(body).c_str());
538 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
539 EXPECT_THAT(
540 getDiagnosticString(),
541 HasSubstr(
542 "Expected unsigned int scalar or vector type as Result Type: UDiv"));
543 }
544
545 TEST_F(ValidateArithmetics, UDivSignedIntType) {
546 const std::string body = R"(
547 %val = OpUDiv %s32 %u32_2 %u32_1
548 )";
549
550 CompileSuccessfully(GenerateCode(body).c_str());
551 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
552 EXPECT_THAT(
553 getDiagnosticString(),
554 HasSubstr(
555 "Expected unsigned int scalar or vector type as Result Type: UDiv"));
556 }
557
558 TEST_F(ValidateArithmetics, UDivWrongOperand1) {
559 const std::string body = R"(
560 %val = OpUDiv %u64 %f64_2 %u64_1
561 )";
562
563 CompileSuccessfully(GenerateCode(body).c_str());
564 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
565 EXPECT_THAT(getDiagnosticString(),
566 HasSubstr("Expected arithmetic operands to be of Result Type: "
567 "UDiv operand index 2"));
568 }
569
570 TEST_F(ValidateArithmetics, UDivWrongOperand2) {
571 const std::string body = R"(
572 %val = OpUDiv %u64 %u64_2 %u32_1
573 )";
574
575 CompileSuccessfully(GenerateCode(body).c_str());
576 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
577 EXPECT_THAT(getDiagnosticString(),
578 HasSubstr("Expected arithmetic operands to be of Result Type: "
579 "UDiv operand index 3"));
580 }
581
582 TEST_F(ValidateArithmetics, DotSuccess) {
583 const std::string body = R"(
584 %val = OpDot %f32 %f32vec2_01 %f32vec2_12
585 )";
586
587 CompileSuccessfully(GenerateCode(body).c_str());
588 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
589 }
590
591 TEST_F(ValidateArithmetics, DotWrongTypeId) {
592 const std::string body = R"(
593 %val = OpDot %u32 %u32vec2_01 %u32vec2_12
594 )";
595
596 CompileSuccessfully(GenerateCode(body).c_str());
597 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
598 EXPECT_THAT(getDiagnosticString(),
599 HasSubstr("Expected float scalar type as Result Type: Dot"));
600 }
601
602 TEST_F(ValidateArithmetics, DotNotVectorTypeOperand1) {
603 const std::string body = R"(
604 %val = OpDot %f32 %f32 %f32vec2_12
605 )";
606
607 CompileSuccessfully(GenerateCode(body).c_str());
608 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
609 EXPECT_THAT(getDiagnosticString(),
610 HasSubstr("Operand '6[%float]' cannot be a "
611 "type"));
612 }
613
614 TEST_F(ValidateArithmetics, DotNotVectorTypeOperand2) {
615 const std::string body = R"(
616 %val = OpDot %f32 %f32vec3_012 %f32_1
617 )";
618
619 CompileSuccessfully(GenerateCode(body).c_str());
620 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
621 EXPECT_THAT(
622 getDiagnosticString(),
623 HasSubstr("Expected float vector as operand: Dot operand index 3"));
624 }
625
626 TEST_F(ValidateArithmetics, DotWrongComponentOperand1) {
627 const std::string body = R"(
628 %val = OpDot %f64 %f32vec2_01 %f64vec2_12
629 )";
630
631 CompileSuccessfully(GenerateCode(body).c_str());
632 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
633 EXPECT_THAT(getDiagnosticString(),
634 HasSubstr("Expected component type to be equal to Result Type: "
635 "Dot operand index 2"));
636 }
637
638 TEST_F(ValidateArithmetics, DotWrongComponentOperand2) {
639 const std::string body = R"(
640 %val = OpDot %f32 %f32vec2_01 %f64vec2_12
641 )";
642
643 CompileSuccessfully(GenerateCode(body).c_str());
644 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
645 EXPECT_THAT(getDiagnosticString(),
646 HasSubstr("Expected component type to be equal to Result Type: "
647 "Dot operand index 3"));
648 }
649
650 TEST_F(ValidateArithmetics, DotDifferentVectorSize) {
651 const std::string body = R"(
652 %val = OpDot %f32 %f32vec2_01 %f32vec3_123
653 )";
654
655 CompileSuccessfully(GenerateCode(body).c_str());
656 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
657 EXPECT_THAT(
658 getDiagnosticString(),
659 HasSubstr(
660 "Expected operands to have the same number of components: Dot"));
661 }
662
663 TEST_F(ValidateArithmetics, VectorTimesScalarSuccess) {
664 const std::string body = R"(
665 %val = OpVectorTimesScalar %f32vec2 %f32vec2_01 %f32_2
666 )";
667
668 CompileSuccessfully(GenerateCode(body).c_str());
669 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
670 }
671
672 TEST_F(ValidateArithmetics, VectorTimesScalarWrongTypeId) {
673 const std::string body = R"(
674 %val = OpVectorTimesScalar %u32vec2 %f32vec2_01 %f32_2
675 )";
676
677 CompileSuccessfully(GenerateCode(body).c_str());
678 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
679 EXPECT_THAT(getDiagnosticString(),
680 HasSubstr("Expected float vector type as Result Type: "
681 "VectorTimesScalar"));
682 }
683
684 TEST_F(ValidateArithmetics, VectorTimesScalarWrongVector) {
685 const std::string body = R"(
686 %val = OpVectorTimesScalar %f32vec2 %f32vec3_012 %f32_2
687 )";
688
689 CompileSuccessfully(GenerateCode(body).c_str());
690 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
691 EXPECT_THAT(
692 getDiagnosticString(),
693 HasSubstr("Expected vector operand type to be equal to Result Type: "
694 "VectorTimesScalar"));
695 }
696
697 TEST_F(ValidateArithmetics, VectorTimesScalarWrongScalar) {
698 const std::string body = R"(
699 %val = OpVectorTimesScalar %f32vec2 %f32vec2_01 %f64_2
700 )";
701
702 CompileSuccessfully(GenerateCode(body).c_str());
703 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
704 EXPECT_THAT(
705 getDiagnosticString(),
706 HasSubstr("Expected scalar operand type to be equal to the component "
707 "type of the vector operand: VectorTimesScalar"));
708 }
709
710 TEST_F(ValidateArithmetics, MatrixTimesScalarSuccess) {
711 const std::string body = R"(
712 %val = OpMatrixTimesScalar %f32mat22 %f32mat22_1212 %f32_2
713 )";
714
715 CompileSuccessfully(GenerateCode(body).c_str());
716 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
717 }
718
719 TEST_F(ValidateArithmetics, MatrixTimesScalarWrongTypeId) {
720 const std::string body = R"(
721 %val = OpMatrixTimesScalar %f32vec2 %f32mat22_1212 %f32_2
722 )";
723
724 CompileSuccessfully(GenerateCode(body).c_str());
725 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
726 EXPECT_THAT(getDiagnosticString(),
727 HasSubstr("Expected float matrix type as Result Type: "
728 "MatrixTimesScalar"));
729 }
730
731 TEST_F(ValidateArithmetics, MatrixTimesScalarWrongMatrix) {
732 const std::string body = R"(
733 %val = OpMatrixTimesScalar %f32mat22 %f32vec2_01 %f32_2
734 )";
735
736 CompileSuccessfully(GenerateCode(body).c_str());
737 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
738 EXPECT_THAT(
739 getDiagnosticString(),
740 HasSubstr("Expected matrix operand type to be equal to Result Type: "
741 "MatrixTimesScalar"));
742 }
743
744 TEST_F(ValidateArithmetics, MatrixTimesScalarWrongScalar) {
745 const std::string body = R"(
746 %val = OpMatrixTimesScalar %f32mat22 %f32mat22_1212 %f64_2
747 )";
748
749 CompileSuccessfully(GenerateCode(body).c_str());
750 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
751 EXPECT_THAT(
752 getDiagnosticString(),
753 HasSubstr("Expected scalar operand type to be equal to the component "
754 "type of the matrix operand: MatrixTimesScalar"));
755 }
756
757 TEST_F(ValidateArithmetics, VectorTimesMatrix2x22Success) {
758 const std::string body = R"(
759 %val = OpVectorTimesMatrix %f32vec2 %f32vec2_12 %f32mat22_1212
760 )";
761
762 CompileSuccessfully(GenerateCode(body).c_str());
763 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
764 }
765
766 TEST_F(ValidateArithmetics, VectorTimesMatrix3x32Success) {
767 const std::string body = R"(
768 %val = OpVectorTimesMatrix %f32vec2 %f32vec3_123 %f32mat32_123123
769 )";
770
771 CompileSuccessfully(GenerateCode(body).c_str());
772 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
773 }
774
775 TEST_F(ValidateArithmetics, VectorTimesMatrixWrongTypeId) {
776 const std::string body = R"(
777 %val = OpVectorTimesMatrix %f32mat22 %f32vec2_12 %f32mat22_1212
778 )";
779
780 CompileSuccessfully(GenerateCode(body).c_str());
781 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
782 EXPECT_THAT(getDiagnosticString(),
783 HasSubstr("Expected float vector type as Result Type: "
784 "VectorTimesMatrix"));
785 }
786
787 TEST_F(ValidateArithmetics, VectorTimesMatrixNotFloatVector) {
788 const std::string body = R"(
789 %val = OpVectorTimesMatrix %f32vec2 %u32vec2_12 %f32mat22_1212
790 )";
791
792 CompileSuccessfully(GenerateCode(body).c_str());
793 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
794 EXPECT_THAT(getDiagnosticString(),
795 HasSubstr("Expected float vector type as left operand: "
796 "VectorTimesMatrix"));
797 }
798
799 TEST_F(ValidateArithmetics, VectorTimesMatrixWrongVectorComponent) {
800 const std::string body = R"(
801 %val = OpVectorTimesMatrix %f32vec2 %f64vec2_12 %f32mat22_1212
802 )";
803
804 CompileSuccessfully(GenerateCode(body).c_str());
805 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
806 EXPECT_THAT(
807 getDiagnosticString(),
808 HasSubstr(
809 "Expected component types of Result Type and vector to be equal: "
810 "VectorTimesMatrix"));
811 }
812
813 TEST_F(ValidateArithmetics, VectorTimesMatrixWrongMatrix) {
814 const std::string body = R"(
815 %val = OpVectorTimesMatrix %f32vec2 %f32vec2_12 %f32vec2_12
816 )";
817
818 CompileSuccessfully(GenerateCode(body).c_str());
819 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
820 EXPECT_THAT(getDiagnosticString(),
821 HasSubstr("Expected float matrix type as right operand: "
822 "VectorTimesMatrix"));
823 }
824
825 TEST_F(ValidateArithmetics, VectorTimesMatrixWrongMatrixComponent) {
826 const std::string body = R"(
827 %val = OpVectorTimesMatrix %f32vec2 %f32vec2_12 %f64mat22_1212
828 )";
829
830 CompileSuccessfully(GenerateCode(body).c_str());
831 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
832 EXPECT_THAT(
833 getDiagnosticString(),
834 HasSubstr(
835 "Expected component types of Result Type and matrix to be equal: "
836 "VectorTimesMatrix"));
837 }
838
839 TEST_F(ValidateArithmetics, VectorTimesMatrix2eq2x23Fail) {
840 const std::string body = R"(
841 %val = OpVectorTimesMatrix %f32vec2 %f32vec2_12 %f32mat23_121212
842 )";
843
844 CompileSuccessfully(GenerateCode(body).c_str());
845 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
846 EXPECT_THAT(
847 getDiagnosticString(),
848 HasSubstr(
849 "Expected number of columns of the matrix to be equal to Result Type "
850 "vector size: VectorTimesMatrix"));
851 }
852
853 TEST_F(ValidateArithmetics, VectorTimesMatrix2x32Fail) {
854 const std::string body = R"(
855 %val = OpVectorTimesMatrix %f32vec2 %f32vec2_12 %f32mat32_123123
856 )";
857
858 CompileSuccessfully(GenerateCode(body).c_str());
859 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
860 EXPECT_THAT(
861 getDiagnosticString(),
862 HasSubstr(
863 "Expected number of rows of the matrix to be equal to the vector "
864 "operand size: VectorTimesMatrix"));
865 }
866
867 TEST_F(ValidateArithmetics, MatrixTimesVector22x2Success) {
868 const std::string body = R"(
869 %val = OpMatrixTimesVector %f32vec2 %f32mat22_1212 %f32vec2_12
870 )";
871
872 CompileSuccessfully(GenerateCode(body).c_str());
873 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
874 }
875
876 TEST_F(ValidateArithmetics, MatrixTimesVector23x3Success) {
877 const std::string body = R"(
878 %val = OpMatrixTimesVector %f32vec2 %f32mat23_121212 %f32vec3_123
879 )";
880
881 CompileSuccessfully(GenerateCode(body).c_str());
882 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
883 }
884
885 TEST_F(ValidateArithmetics, MatrixTimesVectorWrongTypeId) {
886 const std::string body = R"(
887 %val = OpMatrixTimesVector %f32mat22 %f32mat22_1212 %f32vec2_12
888 )";
889
890 CompileSuccessfully(GenerateCode(body).c_str());
891 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
892 EXPECT_THAT(getDiagnosticString(),
893 HasSubstr("Expected float vector type as Result Type: "
894 "MatrixTimesVector"));
895 }
896
897 TEST_F(ValidateArithmetics, MatrixTimesVectorWrongMatrix) {
898 const std::string body = R"(
899 %val = OpMatrixTimesVector %f32vec3 %f32vec3_123 %f32vec3_123
900 )";
901
902 CompileSuccessfully(GenerateCode(body).c_str());
903 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
904 EXPECT_THAT(getDiagnosticString(),
905 HasSubstr("Expected float matrix type as left operand: "
906 "MatrixTimesVector"));
907 }
908
909 TEST_F(ValidateArithmetics, MatrixTimesVectorWrongMatrixCol) {
910 const std::string body = R"(
911 %val = OpMatrixTimesVector %f32vec3 %f32mat23_121212 %f32vec3_123
912 )";
913
914 CompileSuccessfully(GenerateCode(body).c_str());
915 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
916 EXPECT_THAT(
917 getDiagnosticString(),
918 HasSubstr(
919 "Expected column type of the matrix to be equal to Result Type: "
920 "MatrixTimesVector"));
921 }
922
923 TEST_F(ValidateArithmetics, MatrixTimesVectorWrongVector) {
924 const std::string body = R"(
925 %val = OpMatrixTimesVector %f32vec2 %f32mat22_1212 %u32vec2_12
926 )";
927
928 CompileSuccessfully(GenerateCode(body).c_str());
929 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
930 EXPECT_THAT(getDiagnosticString(),
931 HasSubstr("Expected float vector type as right operand: "
932 "MatrixTimesVector"));
933 }
934
935 TEST_F(ValidateArithmetics, MatrixTimesVectorDifferentComponents) {
936 const std::string body = R"(
937 %val = OpMatrixTimesVector %f32vec2 %f32mat22_1212 %f64vec2_12
938 )";
939
940 CompileSuccessfully(GenerateCode(body).c_str());
941 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
942 EXPECT_THAT(getDiagnosticString(),
943 HasSubstr("Expected component types of the operands to be equal: "
944 "MatrixTimesVector"));
945 }
946
947 TEST_F(ValidateArithmetics, MatrixTimesVector22x3Fail) {
948 const std::string body = R"(
949 %val = OpMatrixTimesVector %f32vec2 %f32mat22_1212 %f32vec3_123
950 )";
951
952 CompileSuccessfully(GenerateCode(body).c_str());
953 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
954 EXPECT_THAT(
955 getDiagnosticString(),
956 HasSubstr(
957 "Expected number of columns of the matrix to be equal to the vector "
958 "size: MatrixTimesVector"));
959 }
960
961 TEST_F(ValidateArithmetics, MatrixTimesMatrix22x22Success) {
962 const std::string body = R"(
963 %val = OpMatrixTimesMatrix %f32mat22 %f32mat22_1212 %f32mat22_1212
964 )";
965
966 CompileSuccessfully(GenerateCode(body).c_str());
967 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
968 }
969
970 TEST_F(ValidateArithmetics, MatrixTimesMatrix23x32Success) {
971 const std::string body = R"(
972 %val = OpMatrixTimesMatrix %f32mat22 %f32mat23_121212 %f32mat32_123123
973 )";
974
975 CompileSuccessfully(GenerateCode(body).c_str());
976 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
977 }
978
979 TEST_F(ValidateArithmetics, MatrixTimesMatrix33x33Success) {
980 const std::string body = R"(
981 %val = OpMatrixTimesMatrix %f32mat33 %f32mat33_123123123 %f32mat33_123123123
982 )";
983
984 CompileSuccessfully(GenerateCode(body).c_str());
985 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
986 }
987
988 TEST_F(ValidateArithmetics, MatrixTimesMatrixWrongTypeId) {
989 const std::string body = R"(
990 %val = OpMatrixTimesMatrix %f32vec2 %f32mat22_1212 %f32mat22_1212
991 )";
992
993 CompileSuccessfully(GenerateCode(body).c_str());
994 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
995 EXPECT_THAT(
996 getDiagnosticString(),
997 HasSubstr(
998 "Expected float matrix type as Result Type: MatrixTimesMatrix"));
999 }
1000
1001 TEST_F(ValidateArithmetics, MatrixTimesMatrixWrongLeftOperand) {
1002 const std::string body = R"(
1003 %val = OpMatrixTimesMatrix %f32mat22 %f32vec2_12 %f32mat22_1212
1004 )";
1005
1006 CompileSuccessfully(GenerateCode(body).c_str());
1007 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1008 EXPECT_THAT(
1009 getDiagnosticString(),
1010 HasSubstr(
1011 "Expected float matrix type as left operand: MatrixTimesMatrix"));
1012 }
1013
1014 TEST_F(ValidateArithmetics, MatrixTimesMatrixWrongRightOperand) {
1015 const std::string body = R"(
1016 %val = OpMatrixTimesMatrix %f32mat22 %f32mat22_1212 %f32vec2_12
1017 )";
1018
1019 CompileSuccessfully(GenerateCode(body).c_str());
1020 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1021 EXPECT_THAT(
1022 getDiagnosticString(),
1023 HasSubstr(
1024 "Expected float matrix type as right operand: MatrixTimesMatrix"));
1025 }
1026
1027 TEST_F(ValidateArithmetics, MatrixTimesMatrix32x23Fail) {
1028 const std::string body = R"(
1029 %val = OpMatrixTimesMatrix %f32mat22 %f32mat32_123123 %f32mat23_121212
1030 )";
1031
1032 CompileSuccessfully(GenerateCode(body).c_str());
1033 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1034 EXPECT_THAT(
1035 getDiagnosticString(),
1036 HasSubstr(
1037 "Expected column types of Result Type and left matrix to be equal: "
1038 "MatrixTimesMatrix"));
1039 }
1040
1041 TEST_F(ValidateArithmetics, MatrixTimesMatrixDifferentComponents) {
1042 const std::string body = R"(
1043 %val = OpMatrixTimesMatrix %f32mat22 %f32mat22_1212 %f64mat22_1212
1044 )";
1045
1046 CompileSuccessfully(GenerateCode(body).c_str());
1047 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1048 EXPECT_THAT(getDiagnosticString(),
1049 HasSubstr("Expected component types of Result Type and right "
1050 "matrix to be equal: "
1051 "MatrixTimesMatrix"));
1052 }
1053
1054 TEST_F(ValidateArithmetics, MatrixTimesMatrix23x23Fail) {
1055 const std::string body = R"(
1056 %val = OpMatrixTimesMatrix %f32mat22 %f32mat23_121212 %f32mat23_121212
1057 )";
1058
1059 CompileSuccessfully(GenerateCode(body).c_str());
1060 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1061 EXPECT_THAT(getDiagnosticString(),
1062 HasSubstr("Expected number of columns of Result Type and right "
1063 "matrix to be equal: "
1064 "MatrixTimesMatrix"));
1065 }
1066
1067 TEST_F(ValidateArithmetics, MatrixTimesMatrix23x22Fail) {
1068 const std::string body = R"(
1069 %val = OpMatrixTimesMatrix %f32mat22 %f32mat23_121212 %f32mat22_1212
1070 )";
1071
1072 CompileSuccessfully(GenerateCode(body).c_str());
1073 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1074 EXPECT_THAT(getDiagnosticString(),
1075 HasSubstr("Expected number of columns of left matrix and number "
1076 "of rows of right "
1077 "matrix to be equal: MatrixTimesMatrix"));
1078 }
1079
1080 TEST_F(ValidateArithmetics, OuterProduct2x2Success) {
1081 const std::string body = R"(
1082 %val = OpOuterProduct %f32mat22 %f32vec2_12 %f32vec2_01
1083 )";
1084
1085 CompileSuccessfully(GenerateCode(body).c_str());
1086 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1087 }
1088
1089 TEST_F(ValidateArithmetics, OuterProduct3x2Success) {
1090 const std::string body = R"(
1091 %val = OpOuterProduct %f32mat32 %f32vec3_123 %f32vec2_01
1092 )";
1093
1094 CompileSuccessfully(GenerateCode(body).c_str());
1095 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1096 }
1097
1098 TEST_F(ValidateArithmetics, OuterProduct2x3Success) {
1099 const std::string body = R"(
1100 %val = OpOuterProduct %f32mat23 %f32vec2_01 %f32vec3_123
1101 )";
1102
1103 CompileSuccessfully(GenerateCode(body).c_str());
1104 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1105 }
1106
1107 TEST_F(ValidateArithmetics, OuterProductWrongTypeId) {
1108 const std::string body = R"(
1109 %val = OpOuterProduct %f32vec2 %f32vec2_01 %f32vec3_123
1110 )";
1111
1112 CompileSuccessfully(GenerateCode(body).c_str());
1113 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1114 EXPECT_THAT(getDiagnosticString(),
1115 HasSubstr("Expected float matrix type as Result Type: "
1116 "OuterProduct"));
1117 }
1118
1119 TEST_F(ValidateArithmetics, OuterProductWrongLeftOperand) {
1120 const std::string body = R"(
1121 %val = OpOuterProduct %f32mat22 %f32vec3_123 %f32vec2_01
1122 )";
1123
1124 CompileSuccessfully(GenerateCode(body).c_str());
1125 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1126 EXPECT_THAT(
1127 getDiagnosticString(),
1128 HasSubstr("Expected column type of Result Type to be equal to the type "
1129 "of the left operand: OuterProduct"));
1130 }
1131
1132 TEST_F(ValidateArithmetics, OuterProductRightOperandNotFloatVector) {
1133 const std::string body = R"(
1134 %val = OpOuterProduct %f32mat22 %f32vec2_12 %u32vec2_01
1135 )";
1136
1137 CompileSuccessfully(GenerateCode(body).c_str());
1138 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1139 EXPECT_THAT(
1140 getDiagnosticString(),
1141 HasSubstr("Expected float vector type as right operand: OuterProduct"));
1142 }
1143
1144 TEST_F(ValidateArithmetics, OuterProductRightOperandWrongComponent) {
1145 const std::string body = R"(
1146 %val = OpOuterProduct %f32mat22 %f32vec2_12 %f64vec2_01
1147 )";
1148
1149 CompileSuccessfully(GenerateCode(body).c_str());
1150 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1151 EXPECT_THAT(getDiagnosticString(),
1152 HasSubstr("Expected component types of the operands to be equal: "
1153 "OuterProduct"));
1154 }
1155
1156 TEST_F(ValidateArithmetics, OuterProductRightOperandWrongDimension) {
1157 const std::string body = R"(
1158 %val = OpOuterProduct %f32mat22 %f32vec2_12 %f32vec3_123
1159 )";
1160
1161 CompileSuccessfully(GenerateCode(body).c_str());
1162 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1163 EXPECT_THAT(
1164 getDiagnosticString(),
1165 HasSubstr("Expected number of columns of the matrix to be equal to the "
1166 "vector size of the right operand: OuterProduct"));
1167 }
1168
1169 std::string GenerateBFloatCode(const std::string& main_body) {
1170 const std::string prefix =
1171 R"(
1172 OpCapability Shader
1173 OpCapability BFloat16TypeKHR
1174 OpCapability BFloat16DotProductKHR
1175 OpCapability BFloat16CooperativeMatrixKHR
1176 OpExtension "SPV_KHR_bfloat16"
1177 %1 = OpExtInstImport "GLSL.std.450"
1178 OpMemoryModel Logical GLSL450
1179 OpEntryPoint GLCompute %main "main"
1180 OpExecutionMode %main LocalSize 1 1 1
1181 OpSource GLSL 450
1182 OpName %main "main"
1183 %void = OpTypeVoid
1184 %func = OpTypeFunction %void
1185 %bfloat16 = OpTypeFloat 16 BFloat16KHR
1186 %_ptr_Function_bfloat16 = OpTypePointer Function %bfloat16
1187 %v2bfloat16 = OpTypeVector %bfloat16 2
1188 %_ptr_Function_v2bfloat16 = OpTypePointer Function %v2bfloat16
1189 %main = OpFunction %void None %func
1190 %main_entry = OpLabel)";
1191
1192 const std::string suffix =
1193 R"(
1194 OpReturn
1195 OpFunctionEnd)";
1196
1197 return prefix + main_body + suffix;
1198 }
1199
1200 TEST_F(ValidateArithmetics, DotBfloat16) {
1201 const std::string body = R"(
1202 %v1 = OpVariable %_ptr_Function_v2bfloat16 Function
1203 %v2 = OpVariable %_ptr_Function_v2bfloat16 Function
1204 %12 = OpLoad %v2bfloat16 %v1
1205 %14 = OpLoad %v2bfloat16 %v2
1206 %15 = OpDot %bfloat16 %12 %14
1207 )";
1208
1209 CompileSuccessfully(GenerateBFloatCode(body).c_str());
1210 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1211 }
1212
1213 std::string GenerateFmaCode(const std::string& main_body) {
1214 const std::string prefix =
1215 R"(
1216 OpCapability Shader
1217 OpCapability Int64
1218 OpCapability Float16
1219 OpCapability Float64
1220 OpCapability FMAKHR
1221 OpExtension "SPV_KHR_fma"
1222 OpMemoryModel Logical GLSL450
1223 OpEntryPoint Fragment %main "main"
1224 OpExecutionMode %main OriginUpperLeft
1225 %void = OpTypeVoid
1226 %func = OpTypeFunction %void
1227 %bool = OpTypeBool
1228 %f16 = OpTypeFloat 16
1229 %f32 = OpTypeFloat 32
1230 %u32 = OpTypeInt 32 0
1231 %f64 = OpTypeFloat 64
1232 %f16vec2 = OpTypeVector %f16 2
1233 %f32vec2 = OpTypeVector %f32 2
1234 %f64vec2 = OpTypeVector %f64 2
1235 %f16vec3 = OpTypeVector %f16 3
1236 %f32vec3 = OpTypeVector %f32 3
1237 %f64vec3 = OpTypeVector %f64 3
1238 %f16vec4 = OpTypeVector %f16 4
1239 %f32vec4 = OpTypeVector %f32 4
1240 %f64vec4 = OpTypeVector %f64 4
1241 %u32vec2 = OpTypeVector %u32 2
1242
1243 %f16_1 = OpConstant %f16 1
1244 %f16_2 = OpConstant %f16 2
1245 %f16_4 = OpConstant %f16 4
1246
1247 %f32_1 = OpConstant %f32 1
1248 %f32_2 = OpConstant %f32 2
1249 %f32_4 = OpConstant %f32 4
1250
1251 %f64_1 = OpConstant %f64 1
1252 %f64_2 = OpConstant %f64 2
1253 %f64_4 = OpConstant %f64 4
1254
1255 %u32_1 = OpConstant %u32 1
1256 %u32_2 = OpConstant %u32 2
1257 %u32_4 = OpConstant %u32 4
1258
1259 %f16vec2_12 = OpConstantComposite %f16vec2 %f16_1 %f16_2
1260 %f16vec2_24 = OpConstantComposite %f16vec2 %f16_2 %f16_4
1261 %f16vec3_124 = OpConstantComposite %f16vec3 %f16_1 %f16_2 %f16_4
1262 %f16vec4_1241 = OpConstantComposite %f16vec4 %f16_1 %f16_2 %f16_4 %f16_1
1263
1264 %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
1265 %f32vec2_24 = OpConstantComposite %f32vec2 %f32_2 %f32_4
1266 %f32vec3_124 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_4
1267 %f32vec4_1241 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_4 %f32_1
1268
1269 %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
1270 %f64vec2_24 = OpConstantComposite %f64vec2 %f64_2 %f64_4
1271 %f64vec3_124 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_4
1272 %f64vec4_1241 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_4 %f64_1
1273
1274 %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
1275 %u32vec2_24 = OpConstantComposite %u32vec2 %u32_2 %u32_4
1276
1277 %main = OpFunction %void None %func
1278 %main_entry = OpLabel)";
1279
1280 const std::string suffix =
1281 R"(
1282 OpReturn
1283 OpFunctionEnd)";
1284
1285 return prefix + main_body + suffix;
1286 }
1287
1288 TEST_F(ValidateArithmetics, FmaSuccess) {
1289 const std::string body = R"(
1290 %val1 = OpFmaKHR %f16 %f16_1 %f16_2 %f16_4
1291 %val2 = OpFmaKHR %f32 %f32_1 %f32_2 %f32_4
1292 %val3 = OpFmaKHR %f64 %f64_1 %f64_2 %f64_4
1293 %val4 = OpFmaKHR %f16vec2 %f16vec2_12 %f16vec2_24 %f16vec2_12
1294 %val5 = OpFmaKHR %f32vec2 %f32vec2_12 %f32vec2_24 %f32vec2_12
1295 %val6 = OpFmaKHR %f64vec2 %f64vec2_12 %f64vec2_24 %f64vec2_12
1296 %val7 = OpFmaKHR %f16vec3 %f16vec3_124 %f16vec3_124 %f16vec3_124
1297 %val8 = OpFmaKHR %f32vec3 %f32vec3_124 %f32vec3_124 %f32vec3_124
1298 %val9 = OpFmaKHR %f64vec3 %f64vec3_124 %f64vec3_124 %f64vec3_124
1299 %val10 = OpFmaKHR %f16vec4 %f16vec4_1241 %f16vec4_1241 %f16vec4_1241
1300 %val11 = OpFmaKHR %f32vec4 %f32vec4_1241 %f32vec4_1241 %f32vec4_1241
1301 %val12 = OpFmaKHR %f64vec4 %f64vec4_1241 %f64vec4_1241 %f64vec4_1241
1302 )";
1303
1304 CompileSuccessfully(GenerateFmaCode(body).c_str());
1305 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1306 }
1307
1308 TEST_F(ValidateArithmetics, FmaTypeIdU32) {
1309 const std::string body = R"(
1310 %val = OpFmaKHR %u32 %u32_1 %u32_2 %u32_4
1311 )";
1312
1313 CompileSuccessfully(GenerateFmaCode(body).c_str());
1314 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1315 EXPECT_THAT(
1316 getDiagnosticString(),
1317 HasSubstr(
1318 "Expected floating scalar or vector type as Result Type: FmaKHR"));
1319 }
1320
1321 TEST_F(ValidateArithmetics, FmaTypeIdVec2U32) {
1322 const std::string body = R"(
1323 %val = OpFmaKHR %u32vec2 %u32vec2_12 %u32vec2_24 %u32vec2_12
1324 )";
1325
1326 CompileSuccessfully(GenerateFmaCode(body).c_str());
1327 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1328 EXPECT_THAT(
1329 getDiagnosticString(),
1330 HasSubstr(
1331 "Expected floating scalar or vector type as Result Type: FmaKHR"));
1332 }
1333
1334 TEST_F(ValidateArithmetics, FmaWrongOperand1) {
1335 const std::string body = R"(
1336 %val = OpFmaKHR %f32 %u32_1 %f32_2 %f32_4
1337 )";
1338
1339 CompileSuccessfully(GenerateFmaCode(body).c_str());
1340 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1341 EXPECT_THAT(getDiagnosticString(),
1342 HasSubstr("Expected arithmetic operands to be of Result Type: "
1343 "FmaKHR operand index 2"));
1344 }
1345
1346 TEST_F(ValidateArithmetics, FmaWrongOperand2) {
1347 const std::string body = R"(
1348 %val = OpFmaKHR %f32 %f32_1 %u32_2 %f32_4
1349 )";
1350
1351 CompileSuccessfully(GenerateFmaCode(body).c_str());
1352 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1353 EXPECT_THAT(getDiagnosticString(),
1354 HasSubstr("Expected arithmetic operands to be of Result Type: "
1355 "FmaKHR operand index 3"));
1356 }
1357
1358 TEST_F(ValidateArithmetics, FmaWrongOperand3) {
1359 const std::string body = R"(
1360 %val = OpFmaKHR %f32 %f32_1 %f32_2 %u32_4
1361 )";
1362
1363 CompileSuccessfully(GenerateFmaCode(body).c_str());
1364 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1365 EXPECT_THAT(getDiagnosticString(),
1366 HasSubstr("Expected arithmetic operands to be of Result Type: "
1367 "FmaKHR operand index 4"));
1368 }
1369
1370 TEST_F(ValidateArithmetics, FmaWrongVectorOperand1) {
1371 const std::string body = R"(
1372 %val = OpFmaKHR %f64vec3 %f32vec3_124 %f64vec3_124 %f64vec3_124
1373 )";
1374
1375 CompileSuccessfully(GenerateFmaCode(body).c_str());
1376 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1377 EXPECT_THAT(getDiagnosticString(),
1378 HasSubstr("Expected arithmetic operands to be of Result Type: "
1379 "FmaKHR operand index 2"));
1380 }
1381
1382 TEST_F(ValidateArithmetics, FmaWrongVectorOperand2) {
1383 const std::string body = R"(
1384 %val = OpFmaKHR %f32vec3 %f32vec3_124 %f64vec3_124 %f32vec3_124
1385 )";
1386
1387 CompileSuccessfully(GenerateFmaCode(body).c_str());
1388 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1389 EXPECT_THAT(getDiagnosticString(),
1390 HasSubstr("Expected arithmetic operands to be of Result Type: "
1391 "FmaKHR operand index 3"));
1392 }
1393
1394 TEST_F(ValidateArithmetics, FmaWrongVectorOperand3) {
1395 const std::string body = R"(
1396 %val = OpFmaKHR %f32vec3 %f32vec3_124 %f32vec3_124 %f64vec3_124
1397 )";
1398
1399 CompileSuccessfully(GenerateFmaCode(body).c_str());
1400 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1401 EXPECT_THAT(getDiagnosticString(),
1402 HasSubstr("Expected arithmetic operands to be of Result Type: "
1403 "FmaKHR operand index 4"));
1404 }
1405
1406 std::string GenerateCoopMatCode(const std::string& extra_types,
1407 const std::string& main_body) {
1408 const std::string prefix =
1409 R"(
1410 OpCapability Shader
1411 OpCapability Float16
1412 OpCapability CooperativeMatrixNV
1413 OpExtension "SPV_NV_cooperative_matrix"
1414 OpMemoryModel Logical GLSL450
1415 OpEntryPoint GLCompute %main "main"
1416 OpExecutionMode %main LocalSize 32 1 1
1417 %void = OpTypeVoid
1418 %func = OpTypeFunction %void
1419 %bool = OpTypeBool
1420 %f16 = OpTypeFloat 16
1421 %f32 = OpTypeFloat 32
1422 %u32 = OpTypeInt 32 0
1423 %s32 = OpTypeInt 32 1
1424
1425 %u32_8 = OpConstant %u32 8
1426 %u32_16 = OpConstant %u32 16
1427 %u32_4 = OpConstant %u32 4
1428 %subgroup = OpConstant %u32 3
1429
1430 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
1431 %u32mat = OpTypeCooperativeMatrixNV %u32 %subgroup %u32_8 %u32_8
1432 %s32mat = OpTypeCooperativeMatrixNV %s32 %subgroup %u32_8 %u32_8
1433
1434 %f16_1 = OpConstant %f16 1
1435 %f32_1 = OpConstant %f32 1
1436 %u32_1 = OpConstant %u32 1
1437 %s32_1 = OpConstant %s32 1
1438
1439 %f16mat_1 = OpConstantComposite %f16mat %f16_1
1440 %u32mat_1 = OpConstantComposite %u32mat %u32_1
1441 %s32mat_1 = OpConstantComposite %s32mat %s32_1
1442
1443 %u32_c1 = OpSpecConstant %u32 1
1444 %u32_c2 = OpSpecConstant %u32 2
1445
1446 %f16matc = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_c1 %u32_c2
1447 %f16matc_1 = OpConstantComposite %f16matc %f16_1
1448
1449 %mat16x4 = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_16 %u32_4
1450 %mat4x16 = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_4 %u32_16
1451 %mat16x16 = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_16 %u32_16
1452 %f16mat_16x4_1 = OpConstantComposite %mat16x4 %f16_1
1453 %f16mat_4x16_1 = OpConstantComposite %mat4x16 %f16_1
1454 %f16mat_16x16_1 = OpConstantComposite %mat16x16 %f16_1)";
1455
1456 const std::string func_begin =
1457 R"(
1458 %main = OpFunction %void None %func
1459 %main_entry = OpLabel)";
1460
1461 const std::string suffix =
1462 R"(
1463 OpReturn
1464 OpFunctionEnd)";
1465
1466 return prefix + extra_types + func_begin + main_body + suffix;
1467 }
1468
1469 TEST_F(ValidateArithmetics, CoopMatSuccess) {
1470 const std::string body = R"(
1471 %val1 = OpFAdd %f16mat %f16mat_1 %f16mat_1
1472 %val2 = OpFSub %f16mat %f16mat_1 %f16mat_1
1473 %val3 = OpFDiv %f16mat %f16mat_1 %f16mat_1
1474 %val4 = OpFNegate %f16mat %f16mat_1
1475 %val5 = OpIAdd %u32mat %u32mat_1 %u32mat_1
1476 %val6 = OpISub %u32mat %u32mat_1 %u32mat_1
1477 %val7 = OpUDiv %u32mat %u32mat_1 %u32mat_1
1478 %val8 = OpIAdd %s32mat %s32mat_1 %s32mat_1
1479 %val9 = OpISub %s32mat %s32mat_1 %s32mat_1
1480 %val10 = OpSDiv %s32mat %s32mat_1 %s32mat_1
1481 %val11 = OpSNegate %s32mat %s32mat_1
1482 %val12 = OpMatrixTimesScalar %f16mat %f16mat_1 %f16_1
1483 %val13 = OpMatrixTimesScalar %u32mat %u32mat_1 %u32_1
1484 %val14 = OpMatrixTimesScalar %s32mat %s32mat_1 %s32_1
1485 %val15 = OpCooperativeMatrixMulAddNV %mat16x16 %f16mat_16x4_1 %f16mat_4x16_1 %f16mat_16x16_1
1486 %val16 = OpCooperativeMatrixMulAddNV %f16matc %f16matc_1 %f16matc_1 %f16matc_1
1487 )";
1488
1489 CompileSuccessfully(GenerateCoopMatCode("", body).c_str());
1490 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1491 }
1492
1493 TEST_F(ValidateArithmetics, CoopMatFMulFail) {
1494 const std::string body = R"(
1495 %val1 = OpFMul %f16mat %f16mat_1 %f16mat_1
1496 )";
1497
1498 CompileSuccessfully(GenerateCoopMatCode("", body).c_str());
1499 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1500 EXPECT_THAT(
1501 getDiagnosticString(),
1502 HasSubstr(
1503 "Expected floating scalar or vector type as Result Type: FMul"));
1504 }
1505
1506 TEST_F(ValidateArithmetics, CoopMatMatrixTimesScalarMismatchFail) {
1507 const std::string body = R"(
1508 %val1 = OpMatrixTimesScalar %f16mat %f16mat_1 %f32_1
1509 )";
1510
1511 CompileSuccessfully(GenerateCoopMatCode("", body).c_str());
1512 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1513 EXPECT_THAT(
1514 getDiagnosticString(),
1515 HasSubstr("Expected scalar operand type to be equal to the component "
1516 "type of the matrix operand: MatrixTimesScalar"));
1517 }
1518
1519 TEST_F(ValidateArithmetics, CoopMatScopeFail) {
1520 const std::string types = R"(
1521 %workgroup = OpConstant %u32 2
1522
1523 %mat16x16_dv = OpTypeCooperativeMatrixNV %f16 %workgroup %u32_16 %u32_16
1524 %f16matdv_16x16_1 = OpConstantComposite %mat16x16_dv %f16_1
1525 )";
1526
1527 const std::string body = R"(
1528 %val1 = OpCooperativeMatrixMulAddNV %mat16x16 %f16mat_16x4_1 %f16mat_4x16_1 %f16matdv_16x16_1
1529 )";
1530
1531 CompileSuccessfully(GenerateCoopMatCode(types, body).c_str());
1532 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1533 EXPECT_THAT(
1534 getDiagnosticString(),
1535 HasSubstr(
1536 "Cooperative matrix scopes must match: CooperativeMatrixMulAddNV"));
1537 }
1538
1539 TEST_F(ValidateArithmetics, CoopMatDimFail) {
1540 const std::string body = R"(
1541 %val1 = OpCooperativeMatrixMulAddNV %mat16x16 %f16mat_4x16_1 %f16mat_16x4_1 %f16mat_16x16_1
1542 )";
1543
1544 CompileSuccessfully(GenerateCoopMatCode("", body).c_str());
1545 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1546 EXPECT_THAT(
1547 getDiagnosticString(),
1548 HasSubstr("Cooperative matrix 'M' mismatch: CooperativeMatrixMulAddNV"));
1549 }
1550
1551 TEST_F(ValidateArithmetics, CoopMatComponentTypeNotScalarNumeric) {
1552 const std::string types = R"(
1553 %bad = OpTypeCooperativeMatrixNV %bool %subgroup %u32_8 %u32_8
1554 )";
1555
1556 CompileSuccessfully(GenerateCoopMatCode(types, "").c_str());
1557 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1558 EXPECT_THAT(getDiagnosticString(),
1559 HasSubstr("OpTypeCooperativeMatrix Component Type <id> "
1560 "'4[%bool]' is not a scalar numerical type."));
1561 }
1562
1563 TEST_F(ValidateArithmetics, CoopMatScopeNotConstantInt) {
1564 const std::string types = R"(
1565 %bad = OpTypeCooperativeMatrixNV %f16 %f32_1 %u32_8 %u32_8
1566 )";
1567
1568 CompileSuccessfully(GenerateCoopMatCode(types, "").c_str());
1569 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1570 EXPECT_THAT(
1571 getDiagnosticString(),
1572 HasSubstr("OpTypeCooperativeMatrix Scope <id> '17[%float_1]' is not a "
1573 "constant instruction with scalar integer type."));
1574 }
1575
1576 TEST_F(ValidateArithmetics, CoopMatRowsNotConstantInt) {
1577 const std::string types = R"(
1578 %bad = OpTypeCooperativeMatrixNV %f16 %subgroup %f32_1 %u32_8
1579 )";
1580
1581 CompileSuccessfully(GenerateCoopMatCode(types, "").c_str());
1582 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1583 EXPECT_THAT(
1584 getDiagnosticString(),
1585 HasSubstr("OpTypeCooperativeMatrix Rows <id> '17[%float_1]' is not a "
1586 "constant instruction with scalar integer type."));
1587 }
1588
1589 TEST_F(ValidateArithmetics, CoopMatColumnsNotConstantInt) {
1590 const std::string types = R"(
1591 %bad = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %f32_1
1592 )";
1593
1594 CompileSuccessfully(GenerateCoopMatCode(types, "").c_str());
1595 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1596 EXPECT_THAT(
1597 getDiagnosticString(),
1598 HasSubstr("OpTypeCooperativeMatrix Cols <id> '17[%float_1]' is not a "
1599 "constant instruction with scalar integer type."));
1600 }
1601
1602 TEST_F(ValidateArithmetics, IAddCarrySuccess) {
1603 const std::string body = R"(
1604 %val1 = OpIAddCarry %struct_u32_u32 %u32_0 %u32_1
1605 %val2 = OpIAddCarry %struct_u32vec2_u32vec2 %u32vec2_01 %u32vec2_12
1606 )";
1607
1608 CompileSuccessfully(GenerateCode(body).c_str());
1609 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1610 }
1611
1612 TEST_F(ValidateArithmetics, IAddCarryResultTypeNotStruct) {
1613 const std::string body = R"(
1614 %val = OpIAddCarry %u32 %u32_0 %u32_1
1615 )";
1616
1617 CompileSuccessfully(GenerateCode(body).c_str());
1618 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1619 EXPECT_THAT(getDiagnosticString(),
1620 HasSubstr("Expected a struct as Result Type: IAddCarry"));
1621 }
1622
1623 TEST_F(ValidateArithmetics, IAddCarryResultTypeNotTwoMembers) {
1624 const std::string body = R"(
1625 %val = OpIAddCarry %struct_u32_u32_u32 %u32_0 %u32_1
1626 )";
1627
1628 CompileSuccessfully(GenerateCode(body).c_str());
1629 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1630 EXPECT_THAT(
1631 getDiagnosticString(),
1632 HasSubstr("Expected Result Type struct to have two members: IAddCarry"));
1633 }
1634
1635 TEST_F(ValidateArithmetics, IAddCarryResultTypeMemberNotUnsignedInt) {
1636 const std::string body = R"(
1637 %val = OpIAddCarry %struct_s32_s32 %s32_0 %s32_1
1638 )";
1639
1640 CompileSuccessfully(GenerateCode(body).c_str());
1641 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1642 EXPECT_THAT(getDiagnosticString(),
1643 HasSubstr("Expected Result Type struct member types to be "
1644 "unsigned integer scalar "
1645 "or vector: IAddCarry"));
1646 }
1647
1648 TEST_F(ValidateArithmetics, IAddCarryWrongLeftOperand) {
1649 const std::string body = R"(
1650 %val = OpIAddCarry %struct_u32_u32 %s32_0 %u32_1
1651 )";
1652
1653 CompileSuccessfully(GenerateCode(body).c_str());
1654 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1655 EXPECT_THAT(getDiagnosticString(),
1656 HasSubstr("Expected both operands to be of Result Type member "
1657 "type: IAddCarry"));
1658 }
1659
1660 TEST_F(ValidateArithmetics, IAddCarryWrongRightOperand) {
1661 const std::string body = R"(
1662 %val = OpIAddCarry %struct_u32_u32 %u32_0 %s32_1
1663 )";
1664
1665 CompileSuccessfully(GenerateCode(body).c_str());
1666 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1667 EXPECT_THAT(getDiagnosticString(),
1668 HasSubstr("Expected both operands to be of Result Type member "
1669 "type: IAddCarry"));
1670 }
1671
1672 TEST_F(ValidateArithmetics, OpSMulExtendedSuccess) {
1673 const std::string body = R"(
1674 %val1 = OpSMulExtended %struct_u32_u32 %u32_0 %u32_1
1675 %val2 = OpSMulExtended %struct_s32_s32 %s32_0 %s32_1
1676 %val3 = OpSMulExtended %struct_u32vec2_u32vec2 %u32vec2_01 %u32vec2_12
1677 %val4 = OpSMulExtended %struct_s32vec2_s32vec2 %s32vec2_01 %s32vec2_12
1678 )";
1679
1680 CompileSuccessfully(GenerateCode(body).c_str());
1681 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1682 }
1683
1684 TEST_F(ValidateArithmetics, SMulExtendedResultTypeMemberNotInt) {
1685 const std::string body = R"(
1686 %val = OpSMulExtended %struct_f32_f32 %f32_0 %f32_1
1687 )";
1688
1689 CompileSuccessfully(GenerateCode(body).c_str());
1690 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1691 EXPECT_THAT(
1692 getDiagnosticString(),
1693 HasSubstr("Expected Result Type struct member types to be integer scalar "
1694 "or vector: SMulExtended"));
1695 }
1696
1697 TEST_F(ValidateArithmetics, SMulExtendedResultTypeMembersNotIdentical) {
1698 const std::string body = R"(
1699 %val = OpSMulExtended %struct_s32_u32 %s32_0 %s32_1
1700 )";
1701
1702 CompileSuccessfully(GenerateCode(body).c_str());
1703 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1704 EXPECT_THAT(
1705 getDiagnosticString(),
1706 HasSubstr("Expected Result Type struct member types to be identical: "
1707 "SMulExtended"));
1708 }
1709
1710 std::string GenerateCoopMatKHRCode(const std::string& extra_types,
1711 const std::string& main_body) {
1712 const std::string prefix = R"(
1713 OpCapability Shader
1714 OpCapability Float16
1715 OpCapability CooperativeMatrixKHR
1716 OpCapability CooperativeMatrixReductionsNV
1717 OpCapability CooperativeMatrixPerElementOperationsNV
1718 OpCapability VulkanMemoryModel
1719 OpExtension "SPV_KHR_cooperative_matrix"
1720 OpExtension "SPV_NV_cooperative_matrix2"
1721 OpExtension "SPV_KHR_vulkan_memory_model"
1722 OpMemoryModel Logical Vulkan
1723 OpEntryPoint GLCompute %main "main"
1724 OpExecutionMode %main LocalSize 32 1 1
1725 %void = OpTypeVoid
1726 %func = OpTypeFunction %void
1727 %bool = OpTypeBool
1728 %f16 = OpTypeFloat 16
1729 %f32 = OpTypeFloat 32
1730 %u32 = OpTypeInt 32 0
1731 %s32 = OpTypeInt 32 1
1732
1733 %u32_8 = OpConstant %u32 8
1734 %u32_16 = OpConstant %u32 16
1735 %u32_4 = OpConstant %u32 4
1736 %subgroup = OpConstant %u32 3
1737 %useA = OpConstant %u32 0
1738 %useB = OpConstant %u32 1
1739 %useC = OpConstant %u32 2
1740
1741 %f16matA = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_16 %u32_16 %useA
1742 %u32matA = OpTypeCooperativeMatrixKHR %u32 %subgroup %u32_16 %u32_16 %useA
1743 %s32matA = OpTypeCooperativeMatrixKHR %s32 %subgroup %u32_16 %u32_16 %useA
1744
1745 %f16matB = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_16 %u32_16 %useB
1746 %u32matB = OpTypeCooperativeMatrixKHR %u32 %subgroup %u32_16 %u32_16 %useB
1747 %s32matB = OpTypeCooperativeMatrixKHR %s32 %subgroup %u32_16 %u32_16 %useB
1748
1749 %f16matC = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_16 %u32_16 %useC
1750 %f32matC = OpTypeCooperativeMatrixKHR %f32 %subgroup %u32_16 %u32_16 %useC
1751 %u32matC = OpTypeCooperativeMatrixKHR %u32 %subgroup %u32_16 %u32_16 %useC
1752 %s32matC = OpTypeCooperativeMatrixKHR %s32 %subgroup %u32_16 %u32_16 %useC
1753
1754 %f16_1 = OpConstant %f16 1
1755 %f32_1 = OpConstant %f32 1
1756 %u32_1 = OpConstant %u32 1
1757 %s32_1 = OpConstant %s32 1
1758
1759 %f16mat_A_1 = OpConstantComposite %f16matA %f16_1
1760 %u32mat_A_1 = OpConstantComposite %u32matA %u32_1
1761 %s32mat_A_1 = OpConstantComposite %s32matA %s32_1
1762
1763 %f16mat_B_1 = OpConstantComposite %f16matB %f16_1
1764 %u32mat_B_1 = OpConstantComposite %u32matB %u32_1
1765 %s32mat_B_1 = OpConstantComposite %s32matB %s32_1
1766
1767 %f16mat_C_1 = OpConstantComposite %f16matC %f16_1
1768 %u32mat_C_1 = OpConstantComposite %u32matC %u32_1
1769 %s32mat_C_1 = OpConstantComposite %s32matC %s32_1
1770
1771 )";
1772
1773 const std::string func_begin = R"(
1774 %main = OpFunction %void None %func
1775 %main_entry = OpLabel)";
1776
1777 const std::string suffix = R"(
1778 OpReturn
1779 OpFunctionEnd)";
1780
1781 return prefix + extra_types + func_begin + main_body + suffix;
1782 }
1783
1784 TEST_F(ValidateArithmetics, CoopMatKHRSuccess) {
1785 const std::string body = R"(
1786 %val1 = OpFAdd %f16matA %f16mat_A_1 %f16mat_A_1
1787 %val2 = OpFSub %f16matA %f16mat_A_1 %f16mat_A_1
1788 %val3 = OpFMul %f16matA %f16mat_A_1 %f16mat_A_1
1789 %val4 = OpFDiv %f16matA %f16mat_A_1 %f16mat_A_1
1790 %val5 = OpFNegate %f16matA %f16mat_A_1
1791 %val6 = OpIAdd %u32matA %u32mat_A_1 %u32mat_A_1
1792 %val7 = OpISub %u32matA %u32mat_A_1 %u32mat_A_1
1793 %val8 = OpUDiv %u32matA %u32mat_A_1 %u32mat_A_1
1794 %val9 = OpIAdd %s32matA %s32mat_A_1 %s32mat_A_1
1795 %val10 = OpISub %s32matA %s32mat_A_1 %s32mat_A_1
1796 %val11 = OpSDiv %s32matA %s32mat_A_1 %s32mat_A_1
1797 %val12 = OpSNegate %s32matA %s32mat_A_1
1798 %val13 = OpMatrixTimesScalar %f16matA %f16mat_A_1 %f16_1
1799 %val14 = OpMatrixTimesScalar %u32matA %u32mat_A_1 %u32_1
1800 %val15 = OpMatrixTimesScalar %s32matA %s32mat_A_1 %s32_1
1801 %val16 = OpCooperativeMatrixMulAddKHR %f32matC %f16mat_A_1 %f16mat_B_1 %f16mat_C_1
1802 %val17 = OpCooperativeMatrixMulAddKHR %s32matC %s32mat_A_1 %s32mat_B_1 %s32mat_C_1
1803 MatrixASignedComponentsKHR|MatrixBSignedComponentsKHR|MatrixCSignedComponentsKHR|MatrixResultSignedComponentsKHR
1804 %val18 = OpCooperativeMatrixMulAddKHR %u32matC %u32mat_A_1 %u32mat_B_1 %u32mat_C_1
1805 )";
1806
1807 CompileSuccessfully(GenerateCoopMatKHRCode("", body).c_str(),
1808 SPV_ENV_UNIVERSAL_1_3);
1809 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1810 }
1811
1812 TEST_F(ValidateArithmetics, CoopMatMatrixKHRTimesScalarMismatchFail) {
1813 const std::string body = R"(
1814 %val1 = OpMatrixTimesScalar %f16matA %f16mat_A_1 %f32_1
1815 )";
1816
1817 CompileSuccessfully(GenerateCoopMatKHRCode("", body).c_str(),
1818 SPV_ENV_UNIVERSAL_1_3);
1819 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1820 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1821 EXPECT_THAT(
1822 getDiagnosticString(),
1823 HasSubstr("Expected scalar operand type to be equal to the component "
1824 "type of the matrix operand: MatrixTimesScalar"));
1825 }
1826
1827 TEST_F(ValidateArithmetics, CoopMatKHRScopeFail) {
1828 const std::string types = R"(
1829 %workgroup = OpConstant %u32 2
1830 %mat16x16_dv = OpTypeCooperativeMatrixKHR %f16 %workgroup %u32_16 %u32_16 %useC
1831 %f16matdv_16x16_1 = OpConstantComposite %mat16x16_dv %f16_1
1832 )";
1833
1834 const std::string body = R"(
1835 %val1 = OpFAdd %f16matA %f16matdv_16x16_1 %f16mat_A_1
1836 )";
1837
1838 CompileSuccessfully(GenerateCoopMatKHRCode(types, body).c_str(),
1839 SPV_ENV_UNIVERSAL_1_3);
1840 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1841 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1842 EXPECT_THAT(
1843 getDiagnosticString(),
1844 HasSubstr("Expected scopes of Matrix and Result Type to be identical"));
1845 }
1846
1847 TEST_F(ValidateArithmetics, CoopMatKHRDimFail) {
1848 const std::string types = R"(
1849 %mat16x4 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_16 %u32_4 %useC
1850 %mat16x4_C_1 = OpConstantComposite %mat16x4 %f16_1
1851 )";
1852
1853 const std::string body = R"(
1854 %val1 = OpCooperativeMatrixMulAddKHR %mat16x4 %f16mat_A_1 %f16mat_B_1 %mat16x4_C_1
1855 )";
1856
1857 CompileSuccessfully(GenerateCoopMatKHRCode(types, body).c_str(),
1858 SPV_ENV_UNIVERSAL_1_3);
1859 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1860 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1861 EXPECT_THAT(
1862 getDiagnosticString(),
1863 HasSubstr("Cooperative matrix 'N' mismatch: CooperativeMatrixMulAddKHR"));
1864 }
1865
1866 TEST_F(ValidateArithmetics, CoopMat2ReduceSuccess) {
1867 const std::string extra_types = R"(
1868
1869 %f16matC8 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_8 %useC
1870 %f16matC16x8 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_16 %u32_8 %useC
1871 %f16matC8x16 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_16 %useC
1872
1873 %functy = OpTypeFunction %f16 %f16 %f16
1874 %reducefunc = OpFunction %f16 None %functy
1875 %x = OpFunctionParameter %f16
1876 %y = OpFunctionParameter %f16
1877 %entry2 = OpLabel
1878 %sum = OpFAdd %f16 %x %y
1879 OpReturnValue %sum
1880 OpFunctionEnd
1881
1882 )";
1883 const std::string body = R"(
1884 %val1 = OpCooperativeMatrixReduceNV %f16matC8 %f16mat_C_1 2x2 %reducefunc
1885 %val2 = OpCooperativeMatrixReduceNV %f16matC16x8 %f16mat_C_1 Row %reducefunc
1886 %val3 = OpCooperativeMatrixReduceNV %f16matC8x16 %f16mat_C_1 Column %reducefunc
1887 %val4 = OpCooperativeMatrixReduceNV %f16matC %f16mat_C_1 Row|Column %reducefunc
1888 %val5 = OpCooperativeMatrixReduceNV %f16matC8 %f16mat_C_1 Row|Column %reducefunc
1889 )";
1890
1891 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
1892 SPV_ENV_UNIVERSAL_1_3);
1893 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1894 }
1895
1896 TEST_F(ValidateArithmetics, CoopMat2Reduce2x2DimFail) {
1897 const std::string extra_types = R"(
1898
1899 %functy = OpTypeFunction %f16 %f16 %f16
1900 %reducefunc = OpFunction %f16 None %functy
1901 %x = OpFunctionParameter %f16
1902 %y = OpFunctionParameter %f16
1903 %entry2 = OpLabel
1904 %sum = OpFAdd %f16 %x %y
1905 OpReturnValue %sum
1906 OpFunctionEnd
1907
1908 )";
1909 const std::string body = R"(
1910 %val1 = OpCooperativeMatrixReduceNV %f16matC %f16mat_C_1 2x2 %reducefunc
1911 )";
1912
1913 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
1914 SPV_ENV_UNIVERSAL_1_3);
1915 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1916 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1917 EXPECT_THAT(getDiagnosticString(),
1918 HasSubstr("For Reduce2x2, result rows/cols must be half of "
1919 "matrix rows/cols: CooperativeMatrixReduceNV"));
1920 }
1921
1922 TEST_F(ValidateArithmetics, CoopMat2ReduceRowDimFail) {
1923 const std::string extra_types = R"(
1924
1925 %f16matC8x16 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_16 %useC
1926
1927 %functy = OpTypeFunction %f16 %f16 %f16
1928 %reducefunc = OpFunction %f16 None %functy
1929 %x = OpFunctionParameter %f16
1930 %y = OpFunctionParameter %f16
1931 %entry2 = OpLabel
1932 %sum = OpFAdd %f16 %x %y
1933 OpReturnValue %sum
1934 OpFunctionEnd
1935
1936 )";
1937 const std::string body = R"(
1938 %val1 = OpCooperativeMatrixReduceNV %f16matC8x16 %f16mat_C_1 Row %reducefunc
1939 )";
1940
1941 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
1942 SPV_ENV_UNIVERSAL_1_3);
1943 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1944 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1945 EXPECT_THAT(getDiagnosticString(),
1946 HasSubstr("For ReduceRow, result rows must match matrix rows: "
1947 "CooperativeMatrixReduceNV"));
1948 }
1949
1950 TEST_F(ValidateArithmetics, CoopMat2ReduceColDimFail) {
1951 const std::string extra_types = R"(
1952
1953 %f16matC16x8 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_16 %u32_8 %useC
1954
1955 %functy = OpTypeFunction %f16 %f16 %f16
1956 %reducefunc = OpFunction %f16 None %functy
1957 %x = OpFunctionParameter %f16
1958 %y = OpFunctionParameter %f16
1959 %entry2 = OpLabel
1960 %sum = OpFAdd %f16 %x %y
1961 OpReturnValue %sum
1962 OpFunctionEnd
1963
1964 )";
1965 const std::string body = R"(
1966 %val1 = OpCooperativeMatrixReduceNV %f16matC16x8 %f16mat_C_1 Column %reducefunc
1967 )";
1968
1969 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
1970 SPV_ENV_UNIVERSAL_1_3);
1971 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1972 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1973 EXPECT_THAT(getDiagnosticString(),
1974 HasSubstr("For ReduceColumn, result cols must match matrix cols: "
1975 "CooperativeMatrixReduceNV"));
1976 }
1977
1978 TEST_F(ValidateArithmetics, CoopMat2ReduceMaskFail) {
1979 const std::string extra_types = R"(
1980
1981 %f16matC8 = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_8 %useC
1982
1983 %functy = OpTypeFunction %f16 %f16 %f16
1984 %reducefunc = OpFunction %f16 None %functy
1985 %x = OpFunctionParameter %f16
1986 %y = OpFunctionParameter %f16
1987 %entry2 = OpLabel
1988 %sum = OpFAdd %f16 %x %y
1989 OpReturnValue %sum
1990 OpFunctionEnd
1991
1992 )";
1993 const std::string body = R"(
1994 %val1 = OpCooperativeMatrixReduceNV %f16matC8 %f16mat_C_1 Row|Column|2x2 %reducefunc
1995 )";
1996
1997 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
1998 SPV_ENV_UNIVERSAL_1_3);
1999 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
2000 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2001 EXPECT_THAT(getDiagnosticString(),
2002 HasSubstr("Reduce 2x2 must not be used with Row/Column: "
2003 "CooperativeMatrixReduceNV"));
2004 }
2005
2006 TEST_F(ValidateArithmetics, CoopMat2ReduceFuncTypeFail) {
2007 const std::string extra_types = R"(
2008
2009 %functy = OpTypeFunction %f32 %f32 %f32
2010 %reducefunc = OpFunction %f32 None %functy
2011 %x = OpFunctionParameter %f32
2012 %y = OpFunctionParameter %f32
2013 %entry2 = OpLabel
2014 %sum = OpFAdd %f32 %x %y
2015 OpReturnValue %sum
2016 OpFunctionEnd
2017
2018 )";
2019 const std::string body = R"(
2020 %val1 = OpCooperativeMatrixReduceNV %f16matC %f16mat_C_1 Row|Column %reducefunc
2021 )";
2022
2023 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
2024 SPV_ENV_UNIVERSAL_1_3);
2025 ASSERT_EQ(SPV_ERROR_INVALID_DATA,
2026 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2027 EXPECT_THAT(getDiagnosticString(),
2028 HasSubstr("CombineFunc return type and parameters must match "
2029 "matrix component type: CooperativeMatrixReduceNV"));
2030 }
2031
2032 TEST_F(ValidateArithmetics, CoopMat2PerElementOpSuccess) {
2033 const std::string extra_types = R"(
2034
2035 %functy = OpTypeFunction %f16 %u32 %u32 %f16
2036 %functy2 = OpTypeFunction %f16 %u32 %u32 %f16 %u32
2037
2038 %elemfunc = OpFunction %f16 None %functy
2039 %row = OpFunctionParameter %u32
2040 %col = OpFunctionParameter %u32
2041 %el = OpFunctionParameter %f16
2042 %entry2 = OpLabel
2043 OpReturnValue %el
2044 OpFunctionEnd
2045
2046 %elemfunc2 = OpFunction %f16 None %functy2
2047 %row2 = OpFunctionParameter %u32
2048 %col2 = OpFunctionParameter %u32
2049 %el2 = OpFunctionParameter %f16
2050 %x = OpFunctionParameter %u32
2051 %entry3 = OpLabel
2052 OpReturnValue %el2
2053 OpFunctionEnd
2054
2055 )";
2056 const std::string body = R"(
2057 %val1 = OpCooperativeMatrixPerElementOpNV %f16matC %f16mat_C_1 %elemfunc
2058 %val2 = OpCooperativeMatrixPerElementOpNV %f16matC %f16mat_C_1 %elemfunc2 %f16_1
2059 )";
2060
2061 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
2062 SPV_ENV_UNIVERSAL_1_3);
2063 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2064 }
2065
2066 TEST_F(ValidateArithmetics, CoopMat2PerElementOpElemTyFail) {
2067 const std::string extra_types = R"(
2068
2069 %functy = OpTypeFunction %f32 %u32 %u32 %f32
2070
2071 %elemfunc = OpFunction %f32 None %functy
2072 %row = OpFunctionParameter %u32
2073 %col = OpFunctionParameter %u32
2074 %el = OpFunctionParameter %f32
2075 %entry2 = OpLabel
2076 OpReturnValue %el
2077 OpFunctionEnd
2078
2079 )";
2080 const std::string body = R"(
2081 %val1 = OpCooperativeMatrixPerElementOpNV %f16matC %f16mat_C_1 %elemfunc
2082 )";
2083
2084 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
2085 SPV_ENV_UNIVERSAL_1_3);
2086 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2087 EXPECT_THAT(getDiagnosticString(),
2088 HasSubstr("must match matrix component type"));
2089 }
2090
2091 TEST_F(ValidateArithmetics, CoopMat2PerElementOpRowTyFail) {
2092 const std::string extra_types = R"(
2093
2094 %functy = OpTypeFunction %f16 %f16 %u32 %f16
2095
2096 %elemfunc = OpFunction %f16 None %functy
2097 %row = OpFunctionParameter %f16
2098 %col = OpFunctionParameter %u32
2099 %el = OpFunctionParameter %f16
2100 %entry2 = OpLabel
2101 OpReturnValue %el
2102 OpFunctionEnd
2103
2104 )";
2105 const std::string body = R"(
2106 %val1 = OpCooperativeMatrixPerElementOpNV %f16matC %f16mat_C_1 %elemfunc
2107 )";
2108
2109 CompileSuccessfully(GenerateCoopMatKHRCode(extra_types, body).c_str(),
2110 SPV_ENV_UNIVERSAL_1_3);
2111 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2112 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a 32-bit integer"));
2113 }
2114
2115 std::string GenerateCoopVecCode(const std::string& extra_types,
2116 const std::string& main_body) {
2117 const std::string prefix =
2118 R"(
2119 OpCapability Shader
2120 OpCapability Float16
2121 OpCapability CooperativeVectorNV
2122 OpCapability ReplicatedCompositesEXT
2123 OpExtension "SPV_NV_cooperative_vector"
2124 OpExtension "SPV_EXT_replicated_composites"
2125 %ext_inst = OpExtInstImport "GLSL.std.450"
2126 OpMemoryModel Logical GLSL450
2127 OpEntryPoint GLCompute %main "main"
2128 %void = OpTypeVoid
2129 %func = OpTypeFunction %void
2130 %bool = OpTypeBool
2131 %f16 = OpTypeFloat 16
2132 %f32 = OpTypeFloat 32
2133 %u32 = OpTypeInt 32 0
2134 %s32 = OpTypeInt 32 1
2135
2136 %u32_8 = OpConstant %u32 8
2137 %u32_16 = OpConstant %u32 16
2138 %u32_4 = OpConstant %u32 4
2139 %subgroup = OpConstant %u32 3
2140
2141 %f16vec = OpTypeCooperativeVectorNV %f16 %u32_8
2142 %f16vec4 = OpTypeCooperativeVectorNV %f16 %u32_4
2143 %u32vec = OpTypeCooperativeVectorNV %u32 %u32_8
2144 %s32vec = OpTypeCooperativeVectorNV %s32 %u32_8
2145
2146 %f16_1 = OpConstant %f16 1
2147 %f32_1 = OpConstant %f32 1
2148 %u32_1 = OpConstant %u32 1
2149 %s32_1 = OpConstant %s32 1
2150
2151 %f16vec4_1 = OpConstantComposite %f16vec4 %f16_1 %f16_1 %f16_1 %f16_1
2152 %f16vec_1 = OpConstantComposite %f16vec %f16_1 %f16_1 %f16_1 %f16_1 %f16_1 %f16_1 %f16_1 %f16_1
2153 %u32vec_1 = OpConstantComposite %u32vec %u32_1 %u32_1 %u32_1 %u32_1 %u32_1 %u32_1 %u32_1 %u32_1
2154 %s32vec_1 = OpConstantComposite %s32vec %s32_1 %s32_1 %s32_1 %s32_1 %s32_1 %s32_1 %s32_1 %s32_1
2155
2156 %u32_c1 = OpSpecConstant %u32 1
2157 %u32_c2 = OpSpecConstant %u32 2
2158
2159 %f16vecc = OpTypeCooperativeVectorNV %f16 %u32_c1
2160 %f16vecc_1 = OpConstantCompositeReplicateEXT %f16vecc %f16_1
2161 )";
2162
2163 const std::string func_begin =
2164 R"(
2165 %main = OpFunction %void None %func
2166 %main_entry = OpLabel)";
2167
2168 const std::string suffix =
2169 R"(
2170 OpReturn
2171 OpFunctionEnd)";
2172
2173 return prefix + extra_types + func_begin + main_body + suffix;
2174 }
2175
2176 TEST_F(ValidateArithmetics, CoopVecSuccess) {
2177 const std::string body = R"(
2178 %val1 = OpFAdd %f16vec %f16vec_1 %f16vec_1
2179 %val2 = OpFSub %f16vec %f16vec_1 %f16vec_1
2180 %val3 = OpFDiv %f16vec %f16vec_1 %f16vec_1
2181 %val4 = OpFNegate %f16vec %f16vec_1
2182 %val5 = OpIAdd %u32vec %u32vec_1 %u32vec_1
2183 %val6 = OpISub %u32vec %u32vec_1 %u32vec_1
2184 %val7 = OpUDiv %u32vec %u32vec_1 %u32vec_1
2185 %val8 = OpIAdd %s32vec %s32vec_1 %s32vec_1
2186 %val9 = OpISub %s32vec %s32vec_1 %s32vec_1
2187 %val10 = OpSDiv %s32vec %s32vec_1 %s32vec_1
2188 %val11 = OpSNegate %s32vec %s32vec_1
2189 %val12 = OpVectorTimesScalar %f16vec %f16vec_1 %f16_1
2190 %val13 = OpExtInst %f16vec %ext_inst FMin %f16vec_1 %f16vec_1
2191 %val14 = OpExtInst %f16vec %ext_inst FMax %f16vec_1 %f16vec_1
2192 %val15 = OpExtInst %f16vec %ext_inst FClamp %f16vec_1 %f16vec_1 %f16vec_1
2193 %val16 = OpExtInst %f16vec %ext_inst NClamp %f16vec_1 %f16vec_1 %f16vec_1
2194 %val17 = OpExtInst %f16vec %ext_inst Step %f16vec_1 %f16vec_1
2195 %val18 = OpExtInst %f16vec %ext_inst Exp %f16vec_1
2196 %val19 = OpExtInst %f16vec %ext_inst Log %f16vec_1
2197 %val20 = OpExtInst %f16vec %ext_inst Tanh %f16vec_1
2198 %val21 = OpExtInst %f16vec %ext_inst Atan %f16vec_1
2199 %val22 = OpExtInst %f16vec %ext_inst Fma %f16vec_1 %f16vec_1 %f16vec_1
2200 %val23 = OpExtInst %u32vec %ext_inst UMin %u32vec_1 %u32vec_1
2201 %val24 = OpExtInst %u32vec %ext_inst UMax %u32vec_1 %u32vec_1
2202 %val25 = OpExtInst %u32vec %ext_inst UClamp %u32vec_1 %u32vec_1 %u32vec_1
2203 %val26 = OpExtInst %s32vec %ext_inst SMin %s32vec_1 %s32vec_1
2204 %val27 = OpExtInst %s32vec %ext_inst SMax %s32vec_1 %s32vec_1
2205 %val28 = OpExtInst %s32vec %ext_inst SClamp %s32vec_1 %s32vec_1 %s32vec_1
2206 %val29 = OpShiftRightLogical %u32vec %u32vec_1 %u32vec_1
2207 %val30 = OpShiftRightArithmetic %u32vec %u32vec_1 %u32vec_1
2208 %val31 = OpShiftLeftLogical %u32vec %u32vec_1 %u32vec_1
2209 %val32 = OpBitwiseOr %u32vec %u32vec_1 %u32vec_1
2210 %val33 = OpBitwiseXor %u32vec %u32vec_1 %u32vec_1
2211 %val34 = OpBitwiseAnd %u32vec %u32vec_1 %u32vec_1
2212 %val35 = OpNot %u32vec %u32vec_1
2213 )";
2214
2215 CompileSuccessfully(GenerateCoopVecCode("", body).c_str());
2216 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2217 }
2218
2219 TEST_F(ValidateArithmetics, CoopVecFMulPass) {
2220 const std::string body = R"(
2221 %val1 = OpFMul %f16vec %f16vec_1 %f16vec_1
2222 )";
2223
2224 CompileSuccessfully(GenerateCoopVecCode("", body).c_str());
2225 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2226 }
2227
2228 TEST_F(ValidateArithmetics, CoopVecVectorTimesScalarMismatchFail) {
2229 const std::string body = R"(
2230 %val1 = OpVectorTimesScalar %f16vec %f16vec_1 %f32_1
2231 )";
2232
2233 CompileSuccessfully(GenerateCoopVecCode("", body).c_str());
2234 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
2235 EXPECT_THAT(
2236 getDiagnosticString(),
2237 HasSubstr("Expected scalar operand type to be equal to the component "
2238 "type of the vector operand: VectorTimesScalar"));
2239 }
2240
2241 TEST_F(ValidateArithmetics, CoopVecDimFail) {
2242 const std::string body = R"(
2243 %val1 = OpFMul %f16vec %f16vec_1 %f16vec4_1
2244 )";
2245
2246 CompileSuccessfully(GenerateCoopVecCode("", body).c_str());
2247 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
2248 EXPECT_THAT(getDiagnosticString(),
2249 HasSubstr("Expected number of components to be identical"));
2250 }
2251
2252 TEST_F(ValidateArithmetics, CoopVecComponentTypeNotScalarNumeric) {
2253 const std::string types = R"(
2254 %bad = OpTypeCooperativeVectorNV %f16vec %u32_8
2255 )";
2256
2257 CompileSuccessfully(GenerateCoopVecCode(types, "").c_str());
2258 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2259 EXPECT_THAT(getDiagnosticString(),
2260 HasSubstr("OpTypeVectorIdEXT Component Type <id> "
2261 "'14[%14]' is not a scalar numerical type."));
2262 }
2263
2264 TEST_F(ValidateArithmetics, CoopVecDimNotConstantInt) {
2265 const std::string types = R"(
2266 %bad = OpTypeCooperativeVectorNV %f16 %f32_1
2267 )";
2268
2269 CompileSuccessfully(GenerateCoopVecCode(types, "").c_str());
2270 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2271 EXPECT_THAT(getDiagnosticString(),
2272 HasSubstr("OpTypeVectorIdEXT component count type <id> "
2273 "'7[%float]' is not a 32-bit integer type"));
2274 }
2275
2276 } // namespace
2277 } // namespace val
2278 } // namespace spvtools