1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gmock/gmock.h"
16 #include "src/reader/spirv/function.h"
17 #include "src/reader/spirv/parser_impl_test_helper.h"
18 #include "src/reader/spirv/spirv_tools_helpers_test.h"
19
20 namespace tint {
21 namespace reader {
22 namespace spirv {
23 namespace {
24
25 using ::testing::Eq;
26 using ::testing::HasSubstr;
27
Caps()28 std::string Caps() {
29 return R"(
30 OpCapability Shader
31 OpMemoryModel Logical Simple
32 OpEntryPoint GLCompute %100 "main"
33 OpExecutionMode %100 LocalSize 1 1 1
34 )";
35 }
36
CommonTypes()37 std::string CommonTypes() {
38 return R"(
39 %void = OpTypeVoid
40 %voidfn = OpTypeFunction %void
41
42 %uint = OpTypeInt 32 0
43 %int = OpTypeInt 32 1
44 %float = OpTypeFloat 32
45
46 %uint_10 = OpConstant %uint 10
47 %uint_20 = OpConstant %uint 20
48 %uint_3 = OpConstant %uint 3
49 %uint_4 = OpConstant %uint 4
50 %uint_5 = OpConstant %uint 5
51 %int_1 = OpConstant %int 1
52 %int_30 = OpConstant %int 30
53 %int_40 = OpConstant %int 40
54 %float_50 = OpConstant %float 50
55 %float_60 = OpConstant %float 60
56 %float_70 = OpConstant %float 70
57
58 %v2uint = OpTypeVector %uint 2
59 %v3uint = OpTypeVector %uint 3
60 %v4uint = OpTypeVector %uint 4
61 %v2int = OpTypeVector %int 2
62 %v2float = OpTypeVector %float 2
63
64 %m3v2float = OpTypeMatrix %v2float 3
65 %m3v2float_0 = OpConstantNull %m3v2float
66
67 %s_v2f_u_i = OpTypeStruct %v2float %uint %int
68 %a_u_5 = OpTypeArray %uint %uint_5
69
70 %v2uint_3_4 = OpConstantComposite %v2uint %uint_3 %uint_4
71 %v2uint_4_3 = OpConstantComposite %v2uint %uint_4 %uint_3
72 %v2float_50_60 = OpConstantComposite %v2float %float_50 %float_60
73 %v2float_60_50 = OpConstantComposite %v2float %float_60 %float_50
74 %v2float_70_70 = OpConstantComposite %v2float %float_70 %float_70
75 )";
76 }
77
Preamble()78 std::string Preamble() {
79 return Caps() + CommonTypes();
80 }
81
82 using SpvParserTest_Composite_Construct = SpvParserTest;
83
TEST_F(SpvParserTest_Composite_Construct,Vector)84 TEST_F(SpvParserTest_Composite_Construct, Vector) {
85 const auto assembly = Preamble() + R"(
86 %100 = OpFunction %void None %voidfn
87 %entry = OpLabel
88 %1 = OpCompositeConstruct %v2uint %uint_10 %uint_20
89 %2 = OpCompositeConstruct %v2int %int_30 %int_40
90 %3 = OpCompositeConstruct %v2float %float_50 %float_60
91 OpReturn
92 OpFunctionEnd
93 )";
94 auto p = parser(test::Assemble(assembly));
95 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
96 auto fe = p->function_emitter(100);
97 EXPECT_TRUE(fe.EmitBody()) << p->error();
98 auto ast_body = fe.ast_body();
99 EXPECT_THAT(test::ToString(p->program(), ast_body),
100 HasSubstr(R"(let x_1 : vec2<u32> = vec2<u32>(10u, 20u);
101 let x_2 : vec2<i32> = vec2<i32>(30, 40);
102 let x_3 : vec2<f32> = vec2<f32>(50.0, 60.0);
103 )"));
104 }
105
TEST_F(SpvParserTest_Composite_Construct,Matrix)106 TEST_F(SpvParserTest_Composite_Construct, Matrix) {
107 const auto assembly = Preamble() + R"(
108 %100 = OpFunction %void None %voidfn
109 %entry = OpLabel
110 %1 = OpCompositeConstruct %m3v2float %v2float_50_60 %v2float_60_50 %v2float_70_70
111 OpReturn
112 OpFunctionEnd
113 )";
114 auto p = parser(test::Assemble(assembly));
115 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
116 auto fe = p->function_emitter(100);
117 EXPECT_TRUE(fe.EmitBody()) << p->error();
118 auto ast_body = fe.ast_body();
119 EXPECT_THAT(test::ToString(p->program(), ast_body),
120 HasSubstr("let x_1 : mat3x2<f32> = mat3x2<f32>("
121 "vec2<f32>(50.0, 60.0), "
122 "vec2<f32>(60.0, 50.0), "
123 "vec2<f32>(70.0, 70.0));"));
124 }
125
TEST_F(SpvParserTest_Composite_Construct,Array)126 TEST_F(SpvParserTest_Composite_Construct, Array) {
127 const auto assembly = Preamble() + R"(
128 %100 = OpFunction %void None %voidfn
129 %entry = OpLabel
130 %1 = OpCompositeConstruct %a_u_5 %uint_10 %uint_20 %uint_3 %uint_4 %uint_5
131 OpReturn
132 OpFunctionEnd
133 )";
134 auto p = parser(test::Assemble(assembly));
135 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
136 auto fe = p->function_emitter(100);
137 EXPECT_TRUE(fe.EmitBody()) << p->error();
138 auto ast_body = fe.ast_body();
139 EXPECT_THAT(
140 test::ToString(p->program(), ast_body),
141 HasSubstr(
142 "let x_1 : array<u32, 5u> = array<u32, 5u>(10u, 20u, 3u, 4u, 5u);"));
143 }
144
TEST_F(SpvParserTest_Composite_Construct,Struct)145 TEST_F(SpvParserTest_Composite_Construct, Struct) {
146 const auto assembly = Preamble() + R"(
147 %100 = OpFunction %void None %voidfn
148 %entry = OpLabel
149 %1 = OpCompositeConstruct %s_v2f_u_i %v2float_50_60 %uint_5 %int_30
150 OpReturn
151 OpFunctionEnd
152 )";
153 auto p = parser(test::Assemble(assembly));
154 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
155 auto fe = p->function_emitter(100);
156 EXPECT_TRUE(fe.EmitBody()) << p->error();
157 auto ast_body = fe.ast_body();
158 EXPECT_THAT(test::ToString(p->program(), ast_body),
159 HasSubstr("let x_1 : S = S(vec2<f32>(50.0, 60.0), 5u, 30);"));
160 }
161
TEST_F(SpvParserTest_Composite_Construct,ConstantComposite_Struct_NoDeduplication)162 TEST_F(SpvParserTest_Composite_Construct,
163 ConstantComposite_Struct_NoDeduplication) {
164 const auto assembly = Preamble() + R"(
165 %200 = OpTypeStruct %uint
166 %300 = OpTypeStruct %uint ; isomorphic structures
167
168 %201 = OpConstantComposite %200 %uint_10
169 %301 = OpConstantComposite %300 %uint_10 ; isomorphic constants
170
171 %100 = OpFunction %void None %voidfn
172 %entry = OpLabel
173 %2 = OpCopyObject %200 %201
174 %3 = OpCopyObject %300 %301
175 OpReturn
176 OpFunctionEnd
177 )";
178 auto p = parser(test::Assemble(assembly));
179 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
180 auto fe = p->function_emitter(100);
181 EXPECT_TRUE(fe.EmitBody()) << p->error();
182 auto ast_body = fe.ast_body();
183 const auto got = test::ToString(p->program(), ast_body);
184 const auto expected = std::string(
185 R"(let x_2 : S_1 = S_1(10u);
186 let x_3 : S_2 = S_2(10u);
187 return;
188 )");
189 EXPECT_EQ(got, expected) << got;
190 }
191
192 using SpvParserTest_CompositeExtract = SpvParserTest;
193
TEST_F(SpvParserTest_CompositeExtract,Vector)194 TEST_F(SpvParserTest_CompositeExtract, Vector) {
195 const auto assembly = Preamble() + R"(
196 %100 = OpFunction %void None %voidfn
197 %entry = OpLabel
198 %1 = OpCompositeExtract %float %v2float_50_60 1
199 OpReturn
200 OpFunctionEnd
201 )";
202 auto p = parser(test::Assemble(assembly));
203 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
204 auto fe = p->function_emitter(100);
205 EXPECT_TRUE(fe.EmitBody()) << p->error();
206 auto ast_body = fe.ast_body();
207 EXPECT_THAT(test::ToString(p->program(), ast_body),
208 HasSubstr("let x_1 : f32 = vec2<f32>(50.0, 60.0).y;"));
209 }
210
TEST_F(SpvParserTest_CompositeExtract,Vector_IndexTooBigError)211 TEST_F(SpvParserTest_CompositeExtract, Vector_IndexTooBigError) {
212 const auto assembly = Preamble() + R"(
213 %100 = OpFunction %void None %voidfn
214 %entry = OpLabel
215 %1 = OpCompositeExtract %float %v2float_50_60 900
216 OpReturn
217 OpFunctionEnd
218 )";
219 auto p = parser(test::Assemble(assembly));
220 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
221 auto fe = p->function_emitter(100);
222 EXPECT_FALSE(fe.EmitBody());
223 EXPECT_THAT(p->error(), Eq("OpCompositeExtract %1 index value 900 is out of "
224 "bounds for vector of 2 elements"));
225 }
226
TEST_F(SpvParserTest_CompositeExtract,Matrix)227 TEST_F(SpvParserTest_CompositeExtract, Matrix) {
228 const auto assembly = Preamble() + R"(
229 %ptr = OpTypePointer Function %m3v2float
230
231 %100 = OpFunction %void None %voidfn
232 %entry = OpLabel
233 %var = OpVariable %ptr Function
234 %1 = OpLoad %m3v2float %var
235 %2 = OpCompositeExtract %v2float %1 2
236 OpReturn
237 OpFunctionEnd
238 )";
239 auto p = parser(test::Assemble(assembly));
240 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
241 auto fe = p->function_emitter(100);
242 EXPECT_TRUE(fe.EmitBody()) << p->error();
243 auto ast_body = fe.ast_body();
244 EXPECT_THAT(test::ToString(p->program(), ast_body),
245 HasSubstr("let x_2 : vec2<f32> = x_1[2u];"));
246 }
247
TEST_F(SpvParserTest_CompositeExtract,Matrix_IndexTooBigError)248 TEST_F(SpvParserTest_CompositeExtract, Matrix_IndexTooBigError) {
249 const auto assembly = Preamble() + R"(
250 %ptr = OpTypePointer Function %m3v2float
251
252 %100 = OpFunction %void None %voidfn
253 %entry = OpLabel
254 %var = OpVariable %ptr Function
255 %1 = OpLoad %m3v2float %var
256 %2 = OpCompositeExtract %v2float %1 3
257 OpReturn
258 OpFunctionEnd
259 )";
260 auto p = parser(test::Assemble(assembly));
261 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
262 auto fe = p->function_emitter(100);
263 EXPECT_FALSE(fe.EmitBody()) << p->error();
264 EXPECT_THAT(p->error(), Eq("OpCompositeExtract %2 index value 3 is out of "
265 "bounds for matrix of 3 elements"));
266 }
267
TEST_F(SpvParserTest_CompositeExtract,Matrix_Vector)268 TEST_F(SpvParserTest_CompositeExtract, Matrix_Vector) {
269 const auto assembly = Preamble() + R"(
270 %ptr = OpTypePointer Function %m3v2float
271
272 %100 = OpFunction %void None %voidfn
273 %entry = OpLabel
274 %var = OpVariable %ptr Function
275 %1 = OpLoad %m3v2float %var
276 %2 = OpCompositeExtract %float %1 2 1
277 OpReturn
278 OpFunctionEnd
279 )";
280 auto p = parser(test::Assemble(assembly));
281 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
282 auto fe = p->function_emitter(100);
283 EXPECT_TRUE(fe.EmitBody()) << p->error();
284 auto ast_body = fe.ast_body();
285 EXPECT_THAT(test::ToString(p->program(), ast_body),
286 HasSubstr("let x_2 : f32 = x_1[2u].y;"));
287 }
288
TEST_F(SpvParserTest_CompositeExtract,Array)289 TEST_F(SpvParserTest_CompositeExtract, Array) {
290 const auto assembly = Preamble() + R"(
291 %ptr = OpTypePointer Function %a_u_5
292
293 %100 = OpFunction %void None %voidfn
294 %entry = OpLabel
295 %var = OpVariable %ptr Function
296 %1 = OpLoad %a_u_5 %var
297 %2 = OpCompositeExtract %uint %1 3
298 OpReturn
299 OpFunctionEnd
300 )";
301 auto p = parser(test::Assemble(assembly));
302 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
303 auto fe = p->function_emitter(100);
304 EXPECT_TRUE(fe.EmitBody()) << p->error();
305 auto ast_body = fe.ast_body();
306 EXPECT_THAT(test::ToString(p->program(), ast_body),
307 HasSubstr("let x_2 : u32 = x_1[3u];"));
308 }
309
TEST_F(SpvParserTest_CompositeExtract,RuntimeArray_IsError)310 TEST_F(SpvParserTest_CompositeExtract, RuntimeArray_IsError) {
311 const auto assembly = Preamble() + R"(
312 %rtarr = OpTypeRuntimeArray %uint
313 %ptr = OpTypePointer Function %rtarr
314
315 %100 = OpFunction %void None %voidfn
316 %entry = OpLabel
317 %var = OpVariable %ptr Function
318 %1 = OpLoad %rtarr %var
319 %2 = OpCompositeExtract %uint %1 3
320 OpReturn
321 OpFunctionEnd
322 )";
323 auto p = parser(test::Assemble(assembly));
324 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
325 auto fe = p->function_emitter(100);
326 EXPECT_FALSE(fe.EmitBody()) << p->error();
327 EXPECT_THAT(p->error(),
328 HasSubstr("can't do OpCompositeExtract on a runtime array: "));
329 }
330
TEST_F(SpvParserTest_CompositeExtract,Struct)331 TEST_F(SpvParserTest_CompositeExtract, Struct) {
332 const auto assembly = Preamble() + R"(
333 %ptr = OpTypePointer Function %s_v2f_u_i
334
335 %100 = OpFunction %void None %voidfn
336 %entry = OpLabel
337 %var = OpVariable %ptr Function
338 %1 = OpLoad %s_v2f_u_i %var
339 %2 = OpCompositeExtract %int %1 2
340 OpReturn
341 OpFunctionEnd
342 )";
343 auto p = parser(test::Assemble(assembly));
344 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
345 auto fe = p->function_emitter(100);
346 EXPECT_TRUE(fe.EmitBody()) << p->error();
347 auto ast_body = fe.ast_body();
348 EXPECT_THAT(test::ToString(p->program(), ast_body),
349 HasSubstr("let x_2 : i32 = x_1.field2;"));
350 }
351
TEST_F(SpvParserTest_CompositeExtract,Struct_DifferOnlyInMemberName)352 TEST_F(SpvParserTest_CompositeExtract, Struct_DifferOnlyInMemberName) {
353 const std::string assembly = R"(
354 OpCapability Shader
355 OpMemoryModel Logical Simple
356 OpEntryPoint Fragment %100 "main"
357 OpExecutionMode %100 OriginUpperLeft
358
359 OpMemberName %s0 0 "algo"
360 OpMemberName %s1 0 "rithm"
361
362 %void = OpTypeVoid
363 %voidfn = OpTypeFunction %void
364
365 %uint = OpTypeInt 32 0
366
367 %s0 = OpTypeStruct %uint
368 %s1 = OpTypeStruct %uint
369 %ptr0 = OpTypePointer Function %s0
370 %ptr1 = OpTypePointer Function %s1
371
372 %100 = OpFunction %void None %voidfn
373 %entry = OpLabel
374 %var0 = OpVariable %ptr0 Function
375 %var1 = OpVariable %ptr1 Function
376 %1 = OpLoad %s0 %var0
377 %2 = OpCompositeExtract %uint %1 0
378 %3 = OpLoad %s1 %var1
379 %4 = OpCompositeExtract %uint %3 0
380 OpReturn
381 OpFunctionEnd
382 )";
383 auto p = parser(test::Assemble(assembly));
384 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
385 auto fe = p->function_emitter(100);
386 EXPECT_TRUE(fe.EmitBody()) << p->error();
387 auto got = fe.ast_body();
388 auto program = p->program();
389 EXPECT_THAT(test::ToString(program, got),
390 HasSubstr("let x_2 : u32 = x_1.algo;"))
391 << test::ToString(program, got);
392 EXPECT_THAT(test::ToString(program, got),
393 HasSubstr("let x_4 : u32 = x_3.rithm;"))
394 << test::ToString(program, got);
395 p->SkipDumpingPending("crbug.com/tint/863");
396 }
397
TEST_F(SpvParserTest_CompositeExtract,Struct_IndexTooBigError)398 TEST_F(SpvParserTest_CompositeExtract, Struct_IndexTooBigError) {
399 const auto assembly = Preamble() + R"(
400 %ptr = OpTypePointer Function %s_v2f_u_i
401
402 %100 = OpFunction %void None %voidfn
403 %entry = OpLabel
404 %var = OpVariable %ptr Function
405 %1 = OpLoad %s_v2f_u_i %var
406 %2 = OpCompositeExtract %int %1 40
407 OpReturn
408 OpFunctionEnd
409 )";
410 auto p = parser(test::Assemble(assembly));
411 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
412 auto fe = p->function_emitter(100);
413 EXPECT_FALSE(fe.EmitBody());
414 EXPECT_THAT(p->error(), Eq("OpCompositeExtract %2 index value 40 is out of "
415 "bounds for structure %26 having 3 members"));
416 }
417
TEST_F(SpvParserTest_CompositeExtract,Struct_Array_Matrix_Vector)418 TEST_F(SpvParserTest_CompositeExtract, Struct_Array_Matrix_Vector) {
419 const auto assembly = Preamble() + R"(
420 %a_mat = OpTypeArray %m3v2float %uint_3
421 %s = OpTypeStruct %uint %a_mat
422 %ptr = OpTypePointer Function %s
423
424 %100 = OpFunction %void None %voidfn
425 %entry = OpLabel
426 %var = OpVariable %ptr Function
427 %1 = OpLoad %s %var
428 %2 = OpCompositeExtract %float %1 1 2 0 1
429 OpReturn
430 OpFunctionEnd
431 )";
432 auto p = parser(test::Assemble(assembly));
433 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
434 auto fe = p->function_emitter(100);
435 EXPECT_TRUE(fe.EmitBody()) << p->error();
436 auto ast_body = fe.ast_body();
437 EXPECT_THAT(test::ToString(p->program(), ast_body),
438 HasSubstr("let x_2 : f32 = x_1.field1[2u][0u].y;"));
439 }
440
441 using SpvParserTest_CompositeInsert = SpvParserTest;
442
TEST_F(SpvParserTest_CompositeInsert,Vector)443 TEST_F(SpvParserTest_CompositeInsert, Vector) {
444 const auto assembly = Preamble() + R"(
445 %100 = OpFunction %void None %voidfn
446 %entry = OpLabel
447 %1 = OpCompositeInsert %v2float %float_70 %v2float_50_60 1
448 OpReturn
449 OpFunctionEnd
450 )";
451 auto p = parser(test::Assemble(assembly));
452 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
453 auto fe = p->function_emitter(100);
454 EXPECT_TRUE(fe.EmitBody()) << p->error();
455 auto ast_body = fe.ast_body();
456 auto got = test::ToString(p->program(), ast_body);
457 const auto* expected =
458 R"(var x_1_1 : vec2<f32> = vec2<f32>(50.0, 60.0);
459 x_1_1.y = 70.0;
460 let x_1 : vec2<f32> = x_1_1;
461 return;
462 )";
463 EXPECT_EQ(got, expected);
464 }
465
TEST_F(SpvParserTest_CompositeInsert,Vector_IndexTooBigError)466 TEST_F(SpvParserTest_CompositeInsert, Vector_IndexTooBigError) {
467 const auto assembly = Preamble() + R"(
468 %100 = OpFunction %void None %voidfn
469 %entry = OpLabel
470 %1 = OpCompositeInsert %v2float %float_70 %v2float_50_60 900
471 OpReturn
472 OpFunctionEnd
473 )";
474 auto p = parser(test::Assemble(assembly));
475 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
476 auto fe = p->function_emitter(100);
477 EXPECT_FALSE(fe.EmitBody());
478 EXPECT_THAT(p->error(), Eq("OpCompositeInsert %1 index value 900 is out of "
479 "bounds for vector of 2 elements"));
480 }
481
TEST_F(SpvParserTest_CompositeInsert,Matrix)482 TEST_F(SpvParserTest_CompositeInsert, Matrix) {
483 const auto assembly = Preamble() + R"(
484 %ptr = OpTypePointer Function %m3v2float
485
486 %100 = OpFunction %void None %voidfn
487 %entry = OpLabel
488 %var = OpVariable %ptr Function
489 %1 = OpLoad %m3v2float %var
490 %2 = OpCompositeInsert %m3v2float %v2float_50_60 %1 2
491 OpReturn
492 OpFunctionEnd
493 )";
494 auto p = parser(test::Assemble(assembly));
495 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
496 auto fe = p->function_emitter(100);
497 EXPECT_TRUE(fe.EmitBody()) << p->error();
498 auto ast_body = fe.ast_body();
499 auto body_str = test::ToString(p->program(), ast_body);
500 EXPECT_THAT(body_str, HasSubstr(R"(var x_2_1 : mat3x2<f32> = x_1;
501 x_2_1[2u] = vec2<f32>(50.0, 60.0);
502 let x_2 : mat3x2<f32> = x_2_1;
503 )")) << body_str;
504 }
505
TEST_F(SpvParserTest_CompositeInsert,Matrix_IndexTooBigError)506 TEST_F(SpvParserTest_CompositeInsert, Matrix_IndexTooBigError) {
507 const auto assembly = Preamble() + R"(
508 %ptr = OpTypePointer Function %m3v2float
509
510 %100 = OpFunction %void None %voidfn
511 %entry = OpLabel
512 %var = OpVariable %ptr Function
513 %1 = OpLoad %m3v2float %var
514 %2 = OpCompositeInsert %m3v2float %v2float_50_60 %1 3
515 OpReturn
516 OpFunctionEnd
517 )";
518 auto p = parser(test::Assemble(assembly));
519 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
520 auto fe = p->function_emitter(100);
521 EXPECT_FALSE(fe.EmitBody()) << p->error();
522 EXPECT_THAT(p->error(), Eq("OpCompositeInsert %2 index value 3 is out of "
523 "bounds for matrix of 3 elements"));
524 }
525
TEST_F(SpvParserTest_CompositeInsert,Matrix_Vector)526 TEST_F(SpvParserTest_CompositeInsert, Matrix_Vector) {
527 const auto assembly = Preamble() + R"(
528 %ptr = OpTypePointer Function %m3v2float
529
530 %100 = OpFunction %void None %voidfn
531 %entry = OpLabel
532 %var = OpVariable %ptr Function
533 %1 = OpLoad %m3v2float %var
534 %2 = OpCompositeInsert %m3v2float %v2float_50_60 %1 2
535 OpReturn
536 OpFunctionEnd
537 )";
538 auto p = parser(test::Assemble(assembly));
539 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
540 auto fe = p->function_emitter(100);
541 EXPECT_TRUE(fe.EmitBody()) << p->error();
542 auto ast_body = fe.ast_body();
543 auto body_str = test::ToString(p->program(), ast_body);
544 EXPECT_THAT(body_str, HasSubstr(R"(var x_2_1 : mat3x2<f32> = x_1;
545 x_2_1[2u] = vec2<f32>(50.0, 60.0);
546 let x_2 : mat3x2<f32> = x_2_1;
547 return;
548 )")) << body_str;
549 }
550
TEST_F(SpvParserTest_CompositeInsert,Array)551 TEST_F(SpvParserTest_CompositeInsert, Array) {
552 const auto assembly = Preamble() + R"(
553 %ptr = OpTypePointer Function %a_u_5
554
555 %100 = OpFunction %void None %voidfn
556 %entry = OpLabel
557 %var = OpVariable %ptr Function
558 %1 = OpLoad %a_u_5 %var
559 %2 = OpCompositeInsert %a_u_5 %uint_20 %1 3
560 OpReturn
561 OpFunctionEnd
562 )";
563 auto p = parser(test::Assemble(assembly));
564 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
565 auto fe = p->function_emitter(100);
566 EXPECT_TRUE(fe.EmitBody()) << p->error();
567 auto ast_body = fe.ast_body();
568 auto body_str = test::ToString(p->program(), ast_body);
569 EXPECT_THAT(body_str, HasSubstr(R"(var x_2_1 : array<u32, 5u> = x_1;
570 x_2_1[3u] = 20u;
571 let x_2 : array<u32, 5u> = x_2_1;
572 )")) << body_str;
573 }
574
TEST_F(SpvParserTest_CompositeInsert,RuntimeArray_IsError)575 TEST_F(SpvParserTest_CompositeInsert, RuntimeArray_IsError) {
576 const auto assembly = Preamble() + R"(
577 %rtarr = OpTypeRuntimeArray %uint
578 %ptr = OpTypePointer Function %rtarr
579
580 %100 = OpFunction %void None %voidfn
581 %entry = OpLabel
582 %var = OpVariable %ptr Function
583 %1 = OpLoad %rtarr %var
584 %2 = OpCompositeInsert %rtarr %uint_20 %1 3
585 OpReturn
586 OpFunctionEnd
587 )";
588 auto p = parser(test::Assemble(assembly));
589 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
590 auto fe = p->function_emitter(100);
591 EXPECT_FALSE(fe.EmitBody()) << p->error();
592 EXPECT_THAT(p->error(),
593 HasSubstr("can't do OpCompositeInsert on a runtime array: "));
594 }
595
TEST_F(SpvParserTest_CompositeInsert,Struct)596 TEST_F(SpvParserTest_CompositeInsert, Struct) {
597 const auto assembly = Preamble() + R"(
598 %ptr = OpTypePointer Function %s_v2f_u_i
599
600 %100 = OpFunction %void None %voidfn
601 %entry = OpLabel
602 %var = OpVariable %ptr Function
603 %1 = OpLoad %s_v2f_u_i %var
604 %2 = OpCompositeInsert %s_v2f_u_i %int_30 %1 2
605 OpReturn
606 OpFunctionEnd
607 )";
608 auto p = parser(test::Assemble(assembly));
609 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
610 auto fe = p->function_emitter(100);
611 EXPECT_TRUE(fe.EmitBody()) << p->error();
612 auto ast_body = fe.ast_body();
613 auto body_str = test::ToString(p->program(), ast_body);
614 EXPECT_THAT(body_str, HasSubstr(R"(var x_35 : S;
615 let x_1 : S = x_35;
616 var x_2_1 : S = x_1;
617 x_2_1.field2 = 30;
618 let x_2 : S = x_2_1;
619 )")) << body_str;
620 }
621
TEST_F(SpvParserTest_CompositeInsert,Struct_DifferOnlyInMemberName)622 TEST_F(SpvParserTest_CompositeInsert, Struct_DifferOnlyInMemberName) {
623 const std::string assembly = R"(
624 OpCapability Shader
625 OpMemoryModel Logical Simple
626 OpEntryPoint Fragment %100 "main"
627 OpExecutionMode %100 OriginUpperLeft
628
629 OpName %var0 "var0"
630 OpName %var1 "var1"
631 OpMemberName %s0 0 "algo"
632 OpMemberName %s1 0 "rithm"
633
634 %void = OpTypeVoid
635 %voidfn = OpTypeFunction %void
636
637 %uint = OpTypeInt 32 0
638 %uint_10 = OpConstant %uint 10
639 %uint_11 = OpConstant %uint 11
640
641 %s0 = OpTypeStruct %uint
642 %s1 = OpTypeStruct %uint
643 %ptr0 = OpTypePointer Function %s0
644 %ptr1 = OpTypePointer Function %s1
645
646 %100 = OpFunction %void None %voidfn
647 %entry = OpLabel
648 %var0 = OpVariable %ptr0 Function
649 %var1 = OpVariable %ptr1 Function
650 %1 = OpLoad %s0 %var0
651 %2 = OpCompositeInsert %s0 %uint_10 %1 0
652 %3 = OpLoad %s1 %var1
653 %4 = OpCompositeInsert %s1 %uint_11 %3 0
654 OpReturn
655 OpFunctionEnd
656 )";
657 auto p = parser(test::Assemble(assembly));
658 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
659 auto fe = p->function_emitter(100);
660 EXPECT_TRUE(fe.EmitBody()) << p->error();
661 auto ast_body = fe.ast_body();
662 const auto got = test::ToString(p->program(), ast_body);
663 const std::string expected = R"(var var0 : S;
664 var var1 : S_1;
665 let x_1 : S = var0;
666 var x_2_1 : S = x_1;
667 x_2_1.algo = 10u;
668 let x_2 : S = x_2_1;
669 let x_3 : S_1 = var1;
670 var x_4_1 : S_1 = x_3;
671 x_4_1.rithm = 11u;
672 let x_4 : S_1 = x_4_1;
673 return;
674 )";
675 EXPECT_EQ(got, expected) << got;
676 }
677
TEST_F(SpvParserTest_CompositeInsert,Struct_IndexTooBigError)678 TEST_F(SpvParserTest_CompositeInsert, Struct_IndexTooBigError) {
679 const auto assembly = Preamble() + R"(
680 %ptr = OpTypePointer Function %s_v2f_u_i
681
682 %100 = OpFunction %void None %voidfn
683 %entry = OpLabel
684 %var = OpVariable %ptr Function
685 %1 = OpLoad %s_v2f_u_i %var
686 %2 = OpCompositeInsert %s_v2f_u_i %uint_10 %1 40
687 OpReturn
688 OpFunctionEnd
689 )";
690 auto p = parser(test::Assemble(assembly));
691 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
692 auto fe = p->function_emitter(100);
693 EXPECT_FALSE(fe.EmitBody());
694 EXPECT_THAT(p->error(), Eq("OpCompositeInsert %2 index value 40 is out of "
695 "bounds for structure %26 having 3 members"));
696 }
697
TEST_F(SpvParserTest_CompositeInsert,Struct_Array_Matrix_Vector)698 TEST_F(SpvParserTest_CompositeInsert, Struct_Array_Matrix_Vector) {
699 const auto assembly = Preamble() + R"(
700 %a_mat = OpTypeArray %m3v2float %uint_3
701 %s = OpTypeStruct %uint %a_mat
702 %ptr = OpTypePointer Function %s
703
704 %100 = OpFunction %void None %voidfn
705 %entry = OpLabel
706 %var = OpVariable %ptr Function
707 %1 = OpLoad %s %var
708 %2 = OpCompositeInsert %s %float_70 %1 1 2 0 1
709 OpReturn
710 OpFunctionEnd
711 )";
712 auto p = parser(test::Assemble(assembly));
713 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
714 auto fe = p->function_emitter(100);
715 EXPECT_TRUE(fe.EmitBody()) << p->error();
716 auto ast_body = fe.ast_body();
717 auto body_str = test::ToString(p->program(), ast_body);
718 EXPECT_THAT(body_str, HasSubstr(R"(var x_37 : S_1;
719 let x_1 : S_1 = x_37;
720 var x_2_1 : S_1 = x_1;
721 x_2_1.field1[2u][0u].y = 70.0;
722 let x_2 : S_1 = x_2_1;
723 )")) << body_str;
724 }
725
726 using SpvParserTest_CopyObject = SpvParserTest;
727
TEST_F(SpvParserTest_CopyObject,Scalar)728 TEST_F(SpvParserTest_CopyObject, Scalar) {
729 const auto assembly = Preamble() + R"(
730 %100 = OpFunction %void None %voidfn
731 %entry = OpLabel
732 %1 = OpCopyObject %uint %uint_3
733 %2 = OpCopyObject %uint %1
734 OpReturn
735 OpFunctionEnd
736 )";
737 auto p = parser(test::Assemble(assembly));
738 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
739 auto fe = p->function_emitter(100);
740 EXPECT_TRUE(fe.EmitBody()) << p->error();
741 auto ast_body = fe.ast_body();
742 EXPECT_THAT(test::ToString(p->program(), ast_body),
743 HasSubstr(R"(let x_1 : u32 = 3u;
744 let x_2 : u32 = x_1;
745 )"));
746 }
747
TEST_F(SpvParserTest_CopyObject,Pointer)748 TEST_F(SpvParserTest_CopyObject, Pointer) {
749 const auto assembly = Preamble() + R"(
750 %ptr = OpTypePointer Function %uint
751
752 %100 = OpFunction %void None %voidfn
753 %entry = OpLabel
754 %10 = OpVariable %ptr Function
755 %1 = OpCopyObject %ptr %10
756 %2 = OpCopyObject %ptr %1
757 OpReturn
758 OpFunctionEnd
759 )";
760 auto p = parser(test::Assemble(assembly));
761 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
762 auto fe = p->function_emitter(100);
763 EXPECT_TRUE(fe.EmitBody()) << p->error();
764 auto ast_body = fe.ast_body();
765 EXPECT_THAT(test::ToString(p->program(), ast_body),
766 HasSubstr(R"(let x_1 : ptr<function, u32> = &(x_10);
767 let x_2 : ptr<function, u32> = x_1;
768 )"));
769 }
770
771 using SpvParserTest_VectorShuffle = SpvParserTest;
772
TEST_F(SpvParserTest_VectorShuffle,FunctionScopeOperands_UseBoth)773 TEST_F(SpvParserTest_VectorShuffle, FunctionScopeOperands_UseBoth) {
774 // Note that variables are generated for the vector operands.
775 const auto assembly = Preamble() + R"(
776 %100 = OpFunction %void None %voidfn
777 %entry = OpLabel
778 %1 = OpCopyObject %v2uint %v2uint_3_4
779 %2 = OpIAdd %v2uint %v2uint_4_3 %v2uint_3_4
780 %10 = OpVectorShuffle %v4uint %1 %2 3 2 1 0
781 OpReturn
782 OpFunctionEnd
783 )";
784
785 auto p = parser(test::Assemble(assembly));
786 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
787 auto fe = p->function_emitter(100);
788 EXPECT_TRUE(fe.EmitBody()) << p->error();
789 auto ast_body = fe.ast_body();
790 EXPECT_THAT(
791 test::ToString(p->program(), ast_body),
792 HasSubstr(
793 "let x_10 : vec4<u32> = vec4<u32>(x_2.y, x_2.x, x_1.y, x_1.x);"));
794 }
795
TEST_F(SpvParserTest_VectorShuffle,ConstantOperands_UseBoth)796 TEST_F(SpvParserTest_VectorShuffle, ConstantOperands_UseBoth) {
797 const auto assembly = Preamble() + R"(
798 %100 = OpFunction %void None %voidfn
799 %entry = OpLabel
800 %10 = OpVectorShuffle %v4uint %v2uint_3_4 %v2uint_4_3 3 2 1 0
801 OpReturn
802 OpFunctionEnd
803 )";
804
805 auto p = parser(test::Assemble(assembly));
806 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
807 auto fe = p->function_emitter(100);
808 EXPECT_TRUE(fe.EmitBody()) << p->error();
809 auto ast_body = fe.ast_body();
810 EXPECT_THAT(test::ToString(p->program(), ast_body),
811 HasSubstr("let x_10 : vec4<u32> = vec4<u32>("
812 "vec2<u32>(4u, 3u).y, "
813 "vec2<u32>(4u, 3u).x, "
814 "vec2<u32>(3u, 4u).y, "
815 "vec2<u32>(3u, 4u).x);"));
816 }
817
TEST_F(SpvParserTest_VectorShuffle,ConstantOperands_AllOnesMapToNull)818 TEST_F(SpvParserTest_VectorShuffle, ConstantOperands_AllOnesMapToNull) {
819 const auto assembly = Preamble() + R"(
820 %100 = OpFunction %void None %voidfn
821 %entry = OpLabel
822 %1 = OpCopyObject %v2uint %v2uint_4_3
823 %10 = OpVectorShuffle %v2uint %1 %1 0xFFFFFFFF 1
824 OpReturn
825 OpFunctionEnd
826 )";
827
828 auto p = parser(test::Assemble(assembly));
829 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
830 auto fe = p->function_emitter(100);
831 EXPECT_TRUE(fe.EmitBody()) << p->error();
832 auto ast_body = fe.ast_body();
833 EXPECT_THAT(test::ToString(p->program(), ast_body),
834 HasSubstr("let x_10 : vec2<u32> = vec2<u32>(0u, x_1.y);"));
835 }
836
TEST_F(SpvParserTest_VectorShuffle,FunctionScopeOperands_MixedInputOperandSizes)837 TEST_F(SpvParserTest_VectorShuffle,
838 FunctionScopeOperands_MixedInputOperandSizes) {
839 // Note that variables are generated for the vector operands.
840 const auto assembly = Preamble() + R"(
841 %v3uint_3_4_5 = OpConstantComposite %v3uint %uint_3 %uint_4 %uint_5
842 %100 = OpFunction %void None %voidfn
843 %entry = OpLabel
844 %1 = OpCopyObject %v2uint %v2uint_3_4
845 %3 = OpCopyObject %v3uint %v3uint_3_4_5
846 %10 = OpVectorShuffle %v2uint %1 %3 1 4
847 OpReturn
848 OpFunctionEnd
849 )";
850
851 auto p = parser(test::Assemble(assembly));
852 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
853 auto fe = p->function_emitter(100);
854 EXPECT_TRUE(fe.EmitBody()) << p->error();
855 auto ast_body = fe.ast_body();
856 EXPECT_THAT(test::ToString(p->program(), ast_body),
857 HasSubstr("let x_10 : vec2<u32> = vec2<u32>(x_1.y, x_3.z);"));
858 }
859
TEST_F(SpvParserTest_VectorShuffle,IndexTooBig_IsError)860 TEST_F(SpvParserTest_VectorShuffle, IndexTooBig_IsError) {
861 const auto assembly = Preamble() + R"(
862 %100 = OpFunction %void None %voidfn
863 %entry = OpLabel
864 %10 = OpVectorShuffle %v4uint %v2uint_3_4 %v2uint_4_3 9 2 1 0
865 OpReturn
866 OpFunctionEnd
867 )";
868
869 auto p = parser(test::Assemble(assembly));
870 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
871 auto fe = p->function_emitter(100);
872 EXPECT_FALSE(fe.EmitBody()) << p->error();
873 EXPECT_THAT(p->error(),
874 Eq("invalid vectorshuffle ID %10: index too large: 9"));
875 }
876
877 using SpvParserTest_VectorExtractDynamic = SpvParserTest;
878
TEST_F(SpvParserTest_VectorExtractDynamic,SignedIndex)879 TEST_F(SpvParserTest_VectorExtractDynamic, SignedIndex) {
880 const auto assembly = Preamble() + R"(
881 %100 = OpFunction %void None %voidfn
882 %entry = OpLabel
883 %1 = OpCopyObject %v2uint %v2uint_3_4
884 %2 = OpCopyObject %int %int_1
885 %10 = OpVectorExtractDynamic %uint %1 %2
886 OpReturn
887 OpFunctionEnd
888 )";
889
890 auto p = parser(test::Assemble(assembly));
891 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
892 auto fe = p->function_emitter(100);
893 EXPECT_TRUE(fe.EmitBody()) << p->error();
894 auto ast_body = fe.ast_body();
895 const auto got = test::ToString(p->program(), ast_body);
896 EXPECT_THAT(got, HasSubstr("let x_10 : u32 = x_1[x_2];")) << got;
897 }
898
TEST_F(SpvParserTest_VectorExtractDynamic,UnsignedIndex)899 TEST_F(SpvParserTest_VectorExtractDynamic, UnsignedIndex) {
900 const auto assembly = Preamble() + R"(
901 %100 = OpFunction %void None %voidfn
902 %entry = OpLabel
903 %1 = OpCopyObject %v2uint %v2uint_3_4
904 %2 = OpCopyObject %uint %uint_3
905 %10 = OpVectorExtractDynamic %uint %1 %2
906 OpReturn
907 OpFunctionEnd
908 )";
909
910 auto p = parser(test::Assemble(assembly));
911 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
912 auto fe = p->function_emitter(100);
913 EXPECT_TRUE(fe.EmitBody()) << p->error();
914 auto ast_body = fe.ast_body();
915 const auto got = test::ToString(p->program(), ast_body);
916 EXPECT_THAT(got, HasSubstr("let x_10 : u32 = x_1[x_2];")) << got;
917 }
918
919 using SpvParserTest_VectorInsertDynamic = SpvParserTest;
920
TEST_F(SpvParserTest_VectorInsertDynamic,Sample)921 TEST_F(SpvParserTest_VectorInsertDynamic, Sample) {
922 const auto assembly = Preamble() + R"(
923 %100 = OpFunction %void None %voidfn
924 %entry = OpLabel
925 %1 = OpCopyObject %v2uint %v2uint_3_4
926 %2 = OpCopyObject %uint %uint_3
927 %3 = OpCopyObject %int %int_1
928 %10 = OpVectorInsertDynamic %v2uint %1 %2 %3
929 OpReturn
930 OpFunctionEnd
931 )";
932
933 auto p = parser(test::Assemble(assembly));
934 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
935 auto fe = p->function_emitter(100);
936 EXPECT_TRUE(fe.EmitBody()) << p->error();
937 auto ast_body = fe.ast_body();
938 const auto got = test::ToString(p->program(), ast_body);
939 EXPECT_THAT(got, HasSubstr(R"(var x_10_1 : vec2<u32> = x_1;
940 x_10_1[x_3] = x_2;
941 let x_10 : vec2<u32> = x_10_1;
942 )")) << got
943 << assembly;
944 }
945
TEST_F(SpvParserTest,DISABLED_WorkgroupSize_Overridable)946 TEST_F(SpvParserTest, DISABLED_WorkgroupSize_Overridable) {
947 // TODO(dneto): Support specializable workgroup size. crbug.com/tint/504
948 const auto* assembly = R"(
949 OpCapability Shader
950 OpMemoryModel Logical Simple
951 OpEntryPoint GLCompute %100 "main"
952 OpDecorate %1 BuiltIn WorkgroupSize
953 OpDecorate %uint_2 SpecId 0
954 OpDecorate %uint_4 SpecId 1
955 OpDecorate %uint_8 SpecId 2
956
957 %uint = OpTypeInt 32 0
958 %uint_2 = OpSpecConstant %uint 2
959 %uint_4 = OpSpecConstant %uint 4
960 %uint_8 = OpSpecConstant %uint 8
961 %v3uint = OpTypeVector %uint 3
962 %1 = OpSpecConstantComposite %v3uint %uint_2 %uint_4 %uint_8
963 %void = OpTypeVoid
964 %voidfn = OpTypeFunction %void
965
966 %100 = OpFunction %void None %voidfn
967 %entry = OpLabel
968 %10 = OpCopyObject %v3uint %1
969 %11 = OpCopyObject %uint %uint_2
970 %12 = OpCopyObject %uint %uint_4
971 %13 = OpCopyObject %uint %uint_8
972 OpReturn
973 OpFunctionEnd
974 )";
975
976 auto p = parser(test::Assemble(assembly));
977 ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << assembly;
978 auto fe = p->function_emitter(100);
979 EXPECT_TRUE(fe.Emit()) << p->error();
980 const auto got = test::ToString(p->program());
981 EXPECT_THAT(got, HasSubstr(R"(
982 VariableConst{
983 Decorations{
984 OverrideDecoration{0}
985 }
986 x_2
987 none
988 __u32
989 {
990 ScalarConstructor[not set]{2}
991 }
992 }
993 VariableConst{
994 Decorations{
995 OverrideDecoration{1}
996 }
997 x_3
998 none
999 __u32
1000 {
1001 ScalarConstructor[not set]{4}
1002 }
1003 }
1004 VariableConst{
1005 Decorations{
1006 OverrideDecoration{2}
1007 }
1008 x_4
1009 none
1010 __u32
1011 {
1012 ScalarConstructor[not set]{8}
1013 }
1014 }
1015 )")) << got;
1016 EXPECT_THAT(got, HasSubstr(R"(
1017 VariableDeclStatement{
1018 VariableConst{
1019 x_10
1020 none
1021 __vec_3__u32
1022 {
1023 TypeConstructor[not set]{
1024 __vec_3__u32
1025 ScalarConstructor[not set]{2}
1026 ScalarConstructor[not set]{4}
1027 ScalarConstructor[not set]{8}
1028 }
1029 }
1030 }
1031 }
1032 VariableDeclStatement{
1033 VariableConst{
1034 x_11
1035 none
1036 __u32
1037 {
1038 Identifier[not set]{x_2}
1039 }
1040 }
1041 }
1042 VariableDeclStatement{
1043 VariableConst{
1044 x_12
1045 none
1046 __u32
1047 {
1048 Identifier[not set]{x_3}
1049 }
1050 }
1051 }
1052 VariableDeclStatement{
1053 VariableConst{
1054 x_13
1055 none
1056 __u32
1057 {
1058 Identifier[not set]{x_4}
1059 }
1060 }
1061 })"))
1062 << got << assembly;
1063 }
1064
1065 } // namespace
1066 } // namespace spirv
1067 } // namespace reader
1068 } // namespace tint
1069