Debian Codegraph @

test/opt/type_manager_test.cpp

raw
1 // Copyright (c) 2016 Google Inc.
2 // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
3 // reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 #include "source/opt/type_manager.h"
18
19 #include <memory>
20 #include <utility>
21 #include <vector>
22
23 #include "effcee/effcee.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "source/opt/build_module.h"
27 #include "source/opt/instruction.h"
28 #include "spirv-tools/libspirv.hpp"
29
30 namespace spvtools {
31 namespace opt {
32 namespace analysis {
33 namespace {
34
35 bool Validate(const std::vector<uint32_t>& bin) {
36 spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
37 spv_context spvContext = spvContextCreate(target_env);
38 spv_diagnostic diagnostic = nullptr;
39 spv_const_binary_t binary = {bin.data(), bin.size()};
40 spv_result_t error = spvValidate(spvContext, &binary, &diagnostic);
41 if (error != 0) spvDiagnosticPrint(diagnostic);
42 spvDiagnosticDestroy(diagnostic);
43 spvContextDestroy(spvContext);
44 return error == 0;
45 }
46
47 void Match(const std::string& original, IRContext* context,
48 bool do_validation = true) {
49 std::vector<uint32_t> bin;
50 context->module()->ToBinary(&bin, true);
51 if (do_validation) {
52 EXPECT_TRUE(Validate(bin));
53 }
54 std::string assembly;
55 SpirvTools tools(SPV_ENV_UNIVERSAL_1_2);
56 EXPECT_TRUE(
57 tools.Disassemble(bin, &assembly, SpirvTools::kDefaultDisassembleOption))
58 << "Disassembling failed for shader:\n"
59 << assembly << std::endl;
60 auto match_result = effcee::Match(assembly, original);
61 EXPECT_EQ(effcee::Result::Status::Ok, match_result.status())
62 << match_result.message() << "\nChecking result:\n"
63 << assembly;
64 }
65
66 std::vector<std::unique_ptr<Type>> GenerateAllTypes() {
67 // Types in this test case are only equal to themselves, nothing else.
68 std::vector<std::unique_ptr<Type>> types;
69
70 // Void, Bool
71 types.emplace_back(new Void());
72 auto* voidt = types.back().get();
73 types.emplace_back(new Bool());
74 auto* boolt = types.back().get();
75
76 // Integer
77 types.emplace_back(new Integer(32, true));
78 auto* s32 = types.back().get();
79 types.emplace_back(new Integer(32, false));
80 types.emplace_back(new Integer(64, true));
81 types.emplace_back(new Integer(64, false));
82 auto* u64 = types.back().get();
83
84 // Float
85 types.emplace_back(new Float(32));
86 auto* f32 = types.back().get();
87 types.emplace_back(new Float(64));
88
89 // Vector
90 types.emplace_back(new Vector(s32, 2));
91 types.emplace_back(new Vector(s32, 3));
92 auto* v3s32 = types.back().get();
93 types.emplace_back(new Vector(u64, 4));
94 types.emplace_back(new Vector(f32, 3));
95 auto* v3f32 = types.back().get();
96
97 // Matrix
98 types.emplace_back(new Matrix(v3s32, 3));
99 types.emplace_back(new Matrix(v3s32, 4));
100 types.emplace_back(new Matrix(v3f32, 4));
101
102 // Images
103 types.emplace_back(new Image(s32, spv::Dim::Dim2D, 0, 0, 0, 0,
104 spv::ImageFormat::Rg8,
105 spv::AccessQualifier::ReadOnly));
106 auto* image1 = types.back().get();
107 types.emplace_back(new Image(s32, spv::Dim::Dim2D, 0, 1, 0, 0,
108 spv::ImageFormat::Rg8,
109 spv::AccessQualifier::ReadOnly));
110 types.emplace_back(new Image(s32, spv::Dim::Dim3D, 0, 1, 0, 0,
111 spv::ImageFormat::Rg8,
112 spv::AccessQualifier::ReadOnly));
113 types.emplace_back(new Image(voidt, spv::Dim::Dim3D, 0, 1, 0, 1,
114 spv::ImageFormat::Rg8,
115 spv::AccessQualifier::ReadWrite));
116 auto* image2 = types.back().get();
117
118 // Sampler
119 types.emplace_back(new Sampler());
120
121 // Sampled Image
122 types.emplace_back(new SampledImage(image1));
123 types.emplace_back(new SampledImage(image2));
124
125 // Array
126 types.emplace_back(new Array(f32, Array::LengthInfo{100, {0, 100u}}));
127 types.emplace_back(new Array(f32, Array::LengthInfo{42, {0, 42u}}));
128 auto* a42f32 = types.back().get();
129 types.emplace_back(new Array(u64, Array::LengthInfo{24, {0, 24u}}));
130
131 // RuntimeArray
132 types.emplace_back(new RuntimeArray(v3f32));
133 types.emplace_back(new RuntimeArray(v3s32));
134 auto* rav3s32 = types.back().get();
135
136 // Struct
137 types.emplace_back(new Struct(std::vector<const Type*>{s32}));
138 types.emplace_back(new Struct(std::vector<const Type*>{s32, f32}));
139 auto* sts32f32 = types.back().get();
140 types.emplace_back(
141 new Struct(std::vector<const Type*>{u64, a42f32, rav3s32}));
142
143 // Opaque
144 types.emplace_back(new Opaque(""));
145 types.emplace_back(new Opaque("hello"));
146 types.emplace_back(new Opaque("world"));
147
148 // Pointer
149 types.emplace_back(new Pointer(f32, spv::StorageClass::Input));
150 types.emplace_back(new Pointer(sts32f32, spv::StorageClass::Function));
151 types.emplace_back(new Pointer(a42f32, spv::StorageClass::Function));
152 types.emplace_back(new Pointer(nullptr, spv::StorageClass::Uniform));
153
154 // Function
155 types.emplace_back(new Function(voidt, {}));
156 types.emplace_back(new Function(voidt, {boolt}));
157 types.emplace_back(new Function(voidt, {boolt, s32}));
158 types.emplace_back(new Function(s32, {boolt, s32}));
159
160 // Event, Device Event, Reserve Id, Queue,
161 types.emplace_back(new Event());
162 types.emplace_back(new DeviceEvent());
163 types.emplace_back(new ReserveId());
164 types.emplace_back(new Queue());
165
166 // Pipe, Forward Pointer, PipeStorage, NamedBarrier, AccelerationStructureNV,
167 // CooperativeMatrixNV
168 types.emplace_back(new Pipe(spv::AccessQualifier::ReadWrite));
169 types.emplace_back(new Pipe(spv::AccessQualifier::ReadOnly));
170 types.emplace_back(new ForwardPointer(1, spv::StorageClass::Input));
171 types.emplace_back(new ForwardPointer(2, spv::StorageClass::Input));
172 types.emplace_back(new ForwardPointer(2, spv::StorageClass::Uniform));
173 types.emplace_back(new PipeStorage());
174 types.emplace_back(new NamedBarrier());
175 types.emplace_back(new AccelerationStructureNV());
176 types.emplace_back(new CooperativeMatrixNV(f32, 24, 24, 24));
177 types.emplace_back(new CooperativeMatrixKHR(f32, 8, 8, 8, 1002));
178 types.emplace_back(new RayQueryKHR());
179 types.emplace_back(new HitObjectNV());
180 types.emplace_back(new HitObjectEXT());
181 types.emplace_back(new CooperativeVectorNV(f32, 16));
182
183 // SPV_AMDX_shader_enqueue
184 types.emplace_back(new NodePayloadArrayAMDX(sts32f32));
185
186 // Tensors
187 types.emplace_back(new TensorARM(f32));
188 auto* tensor_f32 = types.back().get();
189 types.emplace_back(new TensorARM(f32, 4));
190 auto* tensor_f32_ranked = types.back().get();
191 types.emplace_back(new TensorARM(f32, 4, 44));
192 auto* tensor_f32_shaped = types.back().get();
193
194 // BufferEXT (SPV_EXT_descriptor_heap)
195 types.emplace_back(new BufferEXT(spv::StorageClass::StorageBuffer));
196
197 // Graph
198 types.emplace_back(new GraphARM(0, {tensor_f32}));
199 types.emplace_back(new GraphARM(1, {tensor_f32_ranked, tensor_f32_ranked}));
200 types.emplace_back(new GraphARM(1, {tensor_f32_shaped, tensor_f32_shaped}));
201
202 types.emplace_back(new TensorLayoutNV(1002, 1000));
203 types.emplace_back(new TensorViewNV(1002, 1003, {1000, 1001}));
204
205 return types;
206 }
207
208 TEST(TypeManager, GenerateAllTypesGeneratesAllTypes) {
209 std::set<Type::Kind> generated_types;
210 for (auto& type : GenerateAllTypes()) {
211 generated_types.insert(type->kind());
212 }
213
214 std::vector<Type::Kind> all_types;
215 for (uint32_t kind = 0; kind != Type::Kind::kLast; ++kind) {
216 all_types.push_back(static_cast<Type::Kind>(kind));
217 }
218
219 EXPECT_THAT(generated_types, testing::UnorderedElementsAreArray(all_types));
220 }
221
222 TEST(TypeManager, TypeStrings) {
223 const std::string text = R"(
224 OpDecorate %spec_const_with_id SpecId 99
225 OpTypeForwardPointer %p Uniform
226 %void = OpTypeVoid
227 %bool = OpTypeBool
228 %u32 = OpTypeInt 32 0
229 %id4 = OpConstant %u32 4
230 %s32 = OpTypeInt 32 1
231 %f64 = OpTypeFloat 64
232 %v3u32 = OpTypeVector %u32 3
233 %m3x3 = OpTypeMatrix %v3u32 3
234 %img1 = OpTypeImage %s32 Cube 0 1 1 0 R32f ReadWrite
235 %img2 = OpTypeImage %s32 Cube 0 1 1 0 R32f
236 %sampler = OpTypeSampler
237 %si1 = OpTypeSampledImage %img1
238 %si2 = OpTypeSampledImage %img2
239 %a5u32 = OpTypeArray %u32 %id4
240 %af64 = OpTypeRuntimeArray %f64
241 %st1 = OpTypeStruct %u32
242 %st2 = OpTypeStruct %f64 %s32 %v3u32
243 %opaque1 = OpTypeOpaque ""
244 %opaque2 = OpTypeOpaque "opaque"
245 %p = OpTypePointer Uniform %st1
246 %f = OpTypeFunction %void %u32 %u32
247 %event = OpTypeEvent
248 %de = OpTypeDeviceEvent
249 %ri = OpTypeReserveId
250 %queue = OpTypeQueue
251 %pipe = OpTypePipe ReadOnly
252 %ps = OpTypePipeStorage
253 %nb = OpTypeNamedBarrier
254 %rtacc = OpTypeAccelerationStructureNV
255 ; Set up other kinds of OpTypeArray
256 %s64 = OpTypeInt 64 1
257 ; ID 32
258 %spec_const_without_id = OpSpecConstant %s32 44
259 %spec_const_with_id = OpSpecConstant %s32 42 ;; This is ID 1
260 %long_constant = OpConstant %s64 5000000000
261 %spec_const_op = OpSpecConstantOp %s32 IAdd %id4 %id4
262 ; ID 35
263 %arr_spec_const_without_id = OpTypeArray %s32 %spec_const_without_id
264 %arr_spec_const_with_id = OpTypeArray %s32 %spec_const_with_id
265 %arr_long_constant = OpTypeArray %s32 %long_constant
266 %arr_spec_const_op = OpTypeArray %s32 %spec_const_op
267 %cm = OpTypeCooperativeMatrixNV %f64 %id4 %id4 %id4
268 %id2 = OpConstant %u32 2
269 %cmkhr = OpTypeCooperativeMatrixKHR %f64 %id4 %id4 %id4 %id2
270 %untyped = OpTypeUntypedPointerKHR Uniform
271 ; ID 43
272 %ts_shape = OpConstantComposite %a5u32 %id4 %id4 %id4 %id4
273 %ts = OpTypeTensorARM %u32
274 %tsr = OpTypeTensorARM %u32 %id4
275 %tss = OpTypeTensorARM %u32 %id4 %ts_shape
276 %g_noin = OpTypeGraphARM 0 %ts
277 %g_onein = OpTypeGraphARM 1 %tsr %tsr
278 %g_shaped = OpTypeGraphARM 1 %tss %tss
279 )";
280
281 std::vector<std::pair<uint32_t, std::string>> type_id_strs = {
282 {3, "void"},
283 {4, "bool"},
284 {5, "uint32"},
285 // Id 6 is used by the constant.
286 {7, "sint32"},
287 {8, "float64"},
288 {9, "<uint32, 3>"},
289 {10, "<<uint32, 3>, 3>"},
290 {11, "image(sint32, 3, 0, 1, 1, 0, 3, 2)"},
291 {12, "image(sint32, 3, 0, 1, 1, 0, 3, 0)"},
292 {13, "sampler"},
293 {14, "sampled_image(image(sint32, 3, 0, 1, 1, 0, 3, 2))"},
294 {15, "sampled_image(image(sint32, 3, 0, 1, 1, 0, 3, 0))"},
295 {16, "[uint32, id(6), words(0,4)]"},
296 {17, "[float64]"},
297 {18, "{uint32}"},
298 {19, "{float64, sint32, <uint32, 3>}"},
299 {20, "opaque('')"},
300 {21, "opaque('opaque')"},
301 {2, "{uint32} 2*"}, // Include storage class number
302 {22, "(uint32, uint32) -> void"},
303 {23, "event"},
304 {24, "device_event"},
305 {25, "reserve_id"},
306 {26, "queue"},
307 {27, "pipe(0)"},
308 {28, "pipe_storage"},
309 {29, "named_barrier"},
310 {30, "accelerationStructureNV"},
311 {31, "sint64"},
312 {35, "[sint32, id(32), words(0,44)]"},
313 {36, "[sint32, id(1), words(1,99,42)]"},
314 {37, "[sint32, id(33), words(0,705032704,1)]"},
315 {38, "[sint32, id(34), words(2,34)]"},
316 {39, "<float64, 6, 6, 6>"},
317 {41, "<float64, 6, 6, 6, 40>"},
318 {42, "untyped_ptr 2*"}, // Include storage class number
319 // Id 43 is OpConstantComposite %a5u32 %id4 %id4 %id4 %id4
320 {44, "tensor<uint32, id(0), id(0)>"},
321 {45, "tensor<uint32, id(6), id(0)>"},
322 {46, "tensor<uint32, id(6), id(43)>"},
323 {47, "graph<0,tensor<uint32, id(0), id(0)>>"},
324 {48,
325 "graph<1,tensor<uint32, id(6), id(0)>,tensor<uint32, id(6), id(0)>>"},
326 {49,
327 "graph<1,tensor<uint32, id(6), id(43)>,tensor<uint32, id(6), id(43)>>"},
328 };
329
330 std::unique_ptr<IRContext> context =
331 BuildModule(SPV_ENV_UNIVERSAL_1_4, nullptr, text);
332 ASSERT_NE(nullptr, context.get()); // It assembled
333 TypeManager manager(nullptr, context.get());
334
335 EXPECT_EQ(type_id_strs.size(), manager.NumTypes());
336
337 for (const auto& p : type_id_strs) {
338 ASSERT_NE(nullptr, manager.GetType(p.first));
339 EXPECT_EQ(p.second, manager.GetType(p.first)->str())
340 << " id is " << p.first;
341 EXPECT_EQ(p.first, manager.GetId(manager.GetType(p.first)));
342 }
343 }
344
345 TEST(TypeManager, StructWithFwdPtr) {
346 const std::string text = R"(
347 OpCapability Addresses
348 OpCapability Kernel
349 %1 = OpExtInstImport "OpenCL.std"
350 OpMemoryModel Physical64 OpenCL
351 OpEntryPoint Kernel %7 "test"
352 OpSource OpenCL_C 102000
353 OpDecorate %11 FuncParamAttr NoCapture
354 %11 = OpDecorationGroup
355 OpGroupDecorate %11 %8 %9
356 OpTypeForwardPointer %100 CrossWorkgroup
357 %void = OpTypeVoid
358 %150 = OpTypeStruct %100
359 %100 = OpTypePointer CrossWorkgroup %150
360 %6 = OpTypeFunction %void %100 %100
361 %7 = OpFunction %void Pure %6
362 %8 = OpFunctionParameter %100
363 %9 = OpFunctionParameter %100
364 %10 = OpLabel
365 OpReturn
366 OpFunctionEnd
367 )";
368
369 std::unique_ptr<IRContext> context =
370 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
371 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
372 TypeManager manager(nullptr, context.get());
373
374 Type* p100 = manager.GetType(100);
375 Type* s150 = manager.GetType(150);
376
377 EXPECT_TRUE(p100->AsPointer());
378 EXPECT_EQ(p100->AsPointer()->pointee_type(), s150);
379
380 EXPECT_TRUE(s150->AsStruct());
381 EXPECT_EQ(s150->AsStruct()->element_types()[0], p100);
382 }
383
384 TEST(TypeManager, CircularFwdPtr) {
385 const std::string text = R"(
386 OpCapability Addresses
387 OpCapability Kernel
388 %1 = OpExtInstImport "OpenCL.std"
389 OpMemoryModel Physical64 OpenCL
390 OpEntryPoint Kernel %7 "test"
391 OpSource OpenCL_C 102000
392 OpDecorate %11 FuncParamAttr NoCapture
393 %11 = OpDecorationGroup
394 OpGroupDecorate %11 %8 %9
395 OpTypeForwardPointer %100 CrossWorkgroup
396 OpTypeForwardPointer %200 CrossWorkgroup
397 %void = OpTypeVoid
398 %int = OpTypeInt 32 0
399 %float = OpTypeFloat 32
400 %150 = OpTypeStruct %200 %int
401 %250 = OpTypeStruct %100 %float
402 %100 = OpTypePointer CrossWorkgroup %150
403 %200 = OpTypePointer CrossWorkgroup %250
404 %6 = OpTypeFunction %void %100 %200
405 %7 = OpFunction %void Pure %6
406 %8 = OpFunctionParameter %100
407 %9 = OpFunctionParameter %200
408 %10 = OpLabel
409 OpReturn
410 OpFunctionEnd
411 )";
412
413 std::unique_ptr<IRContext> context =
414 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
415 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
416 TypeManager manager(nullptr, context.get());
417
418 Type* p100 = manager.GetType(100);
419 Type* s150 = manager.GetType(150);
420 Type* p200 = manager.GetType(200);
421 Type* s250 = manager.GetType(250);
422
423 EXPECT_TRUE(p100->AsPointer());
424 EXPECT_EQ(p100->AsPointer()->pointee_type(), s150);
425
426 EXPECT_TRUE(p200->AsPointer());
427 EXPECT_EQ(p200->AsPointer()->pointee_type(), s250);
428
429 EXPECT_TRUE(s150->AsStruct());
430 EXPECT_EQ(s150->AsStruct()->element_types()[0], p200);
431
432 EXPECT_TRUE(s250->AsStruct());
433 EXPECT_EQ(s250->AsStruct()->element_types()[0], p100);
434 }
435
436 TEST(TypeManager, IsomorphicStructWithFwdPtr) {
437 const std::string text = R"(
438 OpCapability Addresses
439 OpCapability Kernel
440 %1 = OpExtInstImport "OpenCL.std"
441 OpMemoryModel Physical64 OpenCL
442 OpEntryPoint Kernel %7 "test"
443 OpSource OpenCL_C 102000
444 OpDecorate %11 FuncParamAttr NoCapture
445 %11 = OpDecorationGroup
446 OpGroupDecorate %11 %8 %9
447 OpTypeForwardPointer %100 CrossWorkgroup
448 OpTypeForwardPointer %200 CrossWorkgroup
449 %void = OpTypeVoid
450 %_struct_1 = OpTypeStruct %100
451 %_struct_2 = OpTypeStruct %200
452 %100 = OpTypePointer CrossWorkgroup %_struct_1
453 %200 = OpTypePointer CrossWorkgroup %_struct_2
454 %6 = OpTypeFunction %void %100 %200
455 %7 = OpFunction %void Pure %6
456 %8 = OpFunctionParameter %100
457 %9 = OpFunctionParameter %200
458 %10 = OpLabel
459 OpReturn
460 OpFunctionEnd
461 )";
462
463 std::unique_ptr<IRContext> context =
464 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
465 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
466 TypeManager manager(nullptr, context.get());
467
468 EXPECT_EQ(manager.GetType(100), manager.GetType(200));
469 }
470
471 TEST(TypeManager, IsomorphicCircularFwdPtr) {
472 const std::string text = R"(
473 OpCapability Addresses
474 OpCapability Kernel
475 %1 = OpExtInstImport "OpenCL.std"
476 OpMemoryModel Physical64 OpenCL
477 OpEntryPoint Kernel %7 "test"
478 OpSource OpenCL_C 102000
479 OpDecorate %11 FuncParamAttr NoCapture
480 %11 = OpDecorationGroup
481 OpGroupDecorate %11 %8 %9
482 OpTypeForwardPointer %100 CrossWorkgroup
483 OpTypeForwardPointer %200 CrossWorkgroup
484 OpTypeForwardPointer %300 CrossWorkgroup
485 OpTypeForwardPointer %400 CrossWorkgroup
486 %void = OpTypeVoid
487 %int = OpTypeInt 32 0
488 %float = OpTypeFloat 32
489 %150 = OpTypeStruct %200 %int
490 %250 = OpTypeStruct %100 %float
491 %350 = OpTypeStruct %400 %int
492 %450 = OpTypeStruct %300 %float
493 %100 = OpTypePointer CrossWorkgroup %150
494 %200 = OpTypePointer CrossWorkgroup %250
495 %300 = OpTypePointer CrossWorkgroup %350
496 %400 = OpTypePointer CrossWorkgroup %450
497 %6 = OpTypeFunction %void %100 %200
498 %7 = OpFunction %void Pure %6
499 %8 = OpFunctionParameter %100
500 %9 = OpFunctionParameter %200
501 %10 = OpLabel
502 OpReturn
503 OpFunctionEnd
504 )";
505
506 std::unique_ptr<IRContext> context =
507 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
508 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
509 TypeManager manager(nullptr, context.get());
510
511 Type* p100 = manager.GetType(100);
512 Type* p300 = manager.GetType(300);
513 EXPECT_EQ(p100, p300);
514 Type* p200 = manager.GetType(200);
515 Type* p400 = manager.GetType(400);
516 EXPECT_EQ(p200, p400);
517
518 Type* p150 = manager.GetType(150);
519 Type* p350 = manager.GetType(350);
520 EXPECT_EQ(p150, p350);
521 Type* p250 = manager.GetType(250);
522 Type* p450 = manager.GetType(450);
523 EXPECT_EQ(p250, p450);
524 }
525
526 TEST(TypeManager, PartialIsomorphicFwdPtr) {
527 const std::string text = R"(
528 OpCapability Addresses
529 OpCapability Kernel
530 %1 = OpExtInstImport "OpenCL.std"
531 OpMemoryModel Physical64 OpenCL
532 OpEntryPoint Kernel %7 "test"
533 OpSource OpenCL_C 102000
534 OpDecorate %11 FuncParamAttr NoCapture
535 %11 = OpDecorationGroup
536 OpGroupDecorate %11 %8 %9
537 OpTypeForwardPointer %100 CrossWorkgroup
538 OpTypeForwardPointer %200 CrossWorkgroup
539 %void = OpTypeVoid
540 %int = OpTypeInt 32 0
541 %float = OpTypeFloat 32
542 %150 = OpTypeStruct %200 %int
543 %250 = OpTypeStruct %200 %int
544 %100 = OpTypePointer CrossWorkgroup %150
545 %200 = OpTypePointer CrossWorkgroup %250
546 %6 = OpTypeFunction %void %100 %200
547 %7 = OpFunction %void Pure %6
548 %8 = OpFunctionParameter %100
549 %9 = OpFunctionParameter %200
550 %10 = OpLabel
551 OpReturn
552 OpFunctionEnd
553 )";
554
555 std::unique_ptr<IRContext> context =
556 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
557 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
558 TypeManager manager(nullptr, context.get());
559
560 Type* p100 = manager.GetType(100);
561 Type* p200 = manager.GetType(200);
562 EXPECT_EQ(p100->AsPointer()->pointee_type(),
563 p200->AsPointer()->pointee_type());
564 }
565
566 TEST(TypeManager, DecorationOnStruct) {
567 const std::string text = R"(
568 OpDecorate %struct1 Block
569 OpDecorate %struct2 Block
570 OpDecorate %struct3 Block
571 OpDecorate %struct4 Block
572
573 %u32 = OpTypeInt 32 0 ; id: 5
574 %f32 = OpTypeFloat 32 ; id: 6
575 %struct1 = OpTypeStruct %u32 %f32 ; base
576 %struct2 = OpTypeStruct %f32 %u32 ; different member order
577 %struct3 = OpTypeStruct %f32 ; different member list
578 %struct4 = OpTypeStruct %u32 %f32 ; the same
579 %struct7 = OpTypeStruct %f32 ; no decoration
580 )";
581 std::unique_ptr<IRContext> context =
582 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
583 TypeManager manager(nullptr, context.get());
584
585 ASSERT_EQ(7u, manager.NumTypes());
586 // Make sure we get ids correct.
587 ASSERT_EQ("uint32", manager.GetType(5)->str());
588 ASSERT_EQ("float32", manager.GetType(6)->str());
589
590 // Try all combinations of pairs. Expect to be the same type only when the
591 // same id or (1, 4).
592 for (const auto id1 : {1, 2, 3, 4, 7}) {
593 for (const auto id2 : {1, 2, 3, 4, 7}) {
594 if (id1 == id2 || (id1 == 1 && id2 == 4) || (id1 == 4 && id2 == 1)) {
595 EXPECT_TRUE(manager.GetType(id1)->IsSame(manager.GetType(id2)))
596 << "%struct" << id1 << " is expected to be the same as %struct"
597 << id2;
598 } else {
599 EXPECT_FALSE(manager.GetType(id1)->IsSame(manager.GetType(id2)))
600 << "%struct" << id1 << " is expected to be different with %struct"
601 << id2;
602 }
603 }
604 }
605 }
606
607 TEST(TypeManager, DecorationOnMember) {
608 const std::string text = R"(
609 OpMemberDecorate %struct1 0 Offset 0
610 OpMemberDecorate %struct2 0 Offset 0
611 OpMemberDecorate %struct3 0 Offset 0
612 OpMemberDecorate %struct4 0 Offset 0
613 OpMemberDecorate %struct5 1 Offset 0
614 OpMemberDecorate %struct6 0 Offset 4
615
616 OpDecorate %struct7 Block
617 OpMemberDecorate %struct7 0 Offset 0
618
619 %u32 = OpTypeInt 32 0 ; id: 8
620 %f32 = OpTypeFloat 32 ; id: 9
621 %struct1 = OpTypeStruct %u32 %f32 ; base
622 %struct2 = OpTypeStruct %f32 %u32 ; different member order
623 %struct3 = OpTypeStruct %f32 ; different member list
624 %struct4 = OpTypeStruct %u32 %f32 ; the same
625 %struct5 = OpTypeStruct %u32 %f32 ; member decorate different field
626 %struct6 = OpTypeStruct %u32 %f32 ; different member decoration parameter
627 %struct7 = OpTypeStruct %u32 %f32 ; extra decoration on the struct
628 %struct10 = OpTypeStruct %u32 %f32 ; no member decoration
629 )";
630 std::unique_ptr<IRContext> context =
631 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
632 TypeManager manager(nullptr, context.get());
633
634 ASSERT_EQ(10u, manager.NumTypes());
635 // Make sure we get ids correct.
636 ASSERT_EQ("uint32", manager.GetType(8)->str());
637 ASSERT_EQ("float32", manager.GetType(9)->str());
638
639 // Try all combinations of pairs. Expect to be the same type only when the
640 // same id or (1, 4).
641 for (const auto id1 : {1, 2, 3, 4, 5, 6, 7, 10}) {
642 for (const auto id2 : {1, 2, 3, 4, 5, 6, 7, 10}) {
643 if (id1 == id2 || (id1 == 1 && id2 == 4) || (id1 == 4 && id2 == 1)) {
644 EXPECT_TRUE(manager.GetType(id1)->IsSame(manager.GetType(id2)))
645 << "%struct" << id1 << " is expected to be the same as %struct"
646 << id2;
647 } else {
648 EXPECT_FALSE(manager.GetType(id1)->IsSame(manager.GetType(id2)))
649 << "%struct" << id1 << " is expected to be different with %struct"
650 << id2;
651 }
652 }
653 }
654 }
655
656 TEST(TypeManager, DecorationEmpty) {
657 const std::string text = R"(
658 OpDecorate %struct1 Block
659 OpMemberDecorate %struct2 0 Offset 0
660
661 %u32 = OpTypeInt 32 0 ; id: 3
662 %f32 = OpTypeFloat 32 ; id: 4
663 %struct1 = OpTypeStruct %u32 %f32
664 %struct2 = OpTypeStruct %f32 %u32
665 %struct5 = OpTypeStruct %f32
666 )";
667 std::unique_ptr<IRContext> context =
668 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
669 TypeManager manager(nullptr, context.get());
670
671 ASSERT_EQ(5u, manager.NumTypes());
672 // Make sure we get ids correct.
673 ASSERT_EQ("uint32", manager.GetType(3)->str());
674 ASSERT_EQ("float32", manager.GetType(4)->str());
675
676 // %struct1 with decoration on itself
677 EXPECT_FALSE(manager.GetType(1)->decoration_empty());
678 // %struct2 with decoration on its member
679 EXPECT_FALSE(manager.GetType(2)->decoration_empty());
680 EXPECT_TRUE(manager.GetType(3)->decoration_empty());
681 EXPECT_TRUE(manager.GetType(4)->decoration_empty());
682 // %struct5 has no decorations
683 EXPECT_TRUE(manager.GetType(5)->decoration_empty());
684 }
685
686 TEST(TypeManager, BeginEndForEmptyModule) {
687 const std::string text = "";
688 std::unique_ptr<IRContext> context =
689 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
690 TypeManager manager(nullptr, context.get());
691 ASSERT_EQ(0u, manager.NumTypes());
692
693 EXPECT_EQ(manager.begin(), manager.end());
694 }
695
696 TEST(TypeManager, BeginEnd) {
697 const std::string text = R"(
698 %void1 = OpTypeVoid
699 %void2 = OpTypeVoid
700 %bool = OpTypeBool
701 %u32 = OpTypeInt 32 0
702 %f64 = OpTypeFloat 64
703 )";
704 std::unique_ptr<IRContext> context =
705 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
706 TypeManager manager(nullptr, context.get());
707 ASSERT_EQ(5u, manager.NumTypes());
708
709 EXPECT_NE(manager.begin(), manager.end());
710 for (const auto& t : manager) {
711 switch (t.first) {
712 case 1:
713 case 2:
714 EXPECT_EQ("void", t.second->str());
715 break;
716 case 3:
717 EXPECT_EQ("bool", t.second->str());
718 break;
719 case 4:
720 EXPECT_EQ("uint32", t.second->str());
721 break;
722 case 5:
723 EXPECT_EQ("float64", t.second->str());
724 break;
725 default:
726 EXPECT_TRUE(false && "unreachable");
727 break;
728 }
729 }
730 }
731
732 TEST(TypeManager, LookupType) {
733 const std::string text = R"(
734 %void = OpTypeVoid
735 %uint = OpTypeInt 32 0
736 %int = OpTypeInt 32 1
737 %vec2 = OpTypeVector %int 2
738 )";
739
740 std::unique_ptr<IRContext> context =
741 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
742 EXPECT_NE(context, nullptr);
743 TypeManager manager(nullptr, context.get());
744
745 Void voidTy;
746 EXPECT_EQ(manager.GetId(&voidTy), 1u);
747
748 Integer uintTy(32, false);
749 EXPECT_EQ(manager.GetId(&uintTy), 2u);
750
751 Integer intTy(32, true);
752 EXPECT_EQ(manager.GetId(&intTy), 3u);
753
754 Integer intTy2(32, true);
755 Vector vecTy(&intTy2, 2u);
756 EXPECT_EQ(manager.GetId(&vecTy), 4u);
757 }
758
759 TEST(TypeManager, RemoveId) {
760 const std::string text = R"(
761 OpCapability Shader
762 OpCapability Linkage
763 OpMemoryModel Logical GLSL450
764 %1 = OpTypeInt 32 0
765 %2 = OpTypeInt 32 1
766 )";
767
768 std::unique_ptr<IRContext> context =
769 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
770 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
771 EXPECT_NE(context, nullptr);
772
773 context->get_type_mgr()->RemoveId(1u);
774 ASSERT_EQ(context->get_type_mgr()->GetType(1u), nullptr);
775 ASSERT_NE(context->get_type_mgr()->GetType(2u), nullptr);
776
777 context->get_type_mgr()->RemoveId(2u);
778 ASSERT_EQ(context->get_type_mgr()->GetType(1u), nullptr);
779 ASSERT_EQ(context->get_type_mgr()->GetType(2u), nullptr);
780 }
781
782 TEST(TypeManager, RemoveIdNonDuplicateAmbiguousType) {
783 const std::string text = R"(
784 OpCapability Shader
785 OpCapability Linkage
786 OpMemoryModel Logical GLSL450
787 %1 = OpTypeInt 32 0
788 %2 = OpTypeStruct %1
789 )";
790
791 std::unique_ptr<IRContext> context =
792 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
793 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
794 EXPECT_NE(context, nullptr);
795
796 Integer u32(32, false);
797 Struct st({&u32});
798 ASSERT_EQ(context->get_type_mgr()->GetId(&st), 2u);
799 context->get_type_mgr()->RemoveId(2u);
800 ASSERT_EQ(context->get_type_mgr()->GetType(2u), nullptr);
801 ASSERT_EQ(context->get_type_mgr()->GetId(&st), 0u);
802 }
803
804 TEST(TypeManager, RemoveIdDuplicateAmbiguousType) {
805 const std::string text = R"(
806 OpCapability Shader
807 OpCapability Linkage
808 OpMemoryModel Logical GLSL450
809 %1 = OpTypeInt 32 0
810 %2 = OpTypeStruct %1
811 %3 = OpTypeStruct %1
812 )";
813
814 std::unique_ptr<IRContext> context =
815 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
816 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
817 EXPECT_NE(context, nullptr);
818
819 Integer u32(32, false);
820 Struct st({&u32});
821 uint32_t id = context->get_type_mgr()->GetId(&st);
822 ASSERT_NE(id, 0u);
823 uint32_t toRemove = id == 2u ? 2u : 3u;
824 uint32_t toStay = id == 2u ? 3u : 2u;
825 context->get_type_mgr()->RemoveId(toRemove);
826 ASSERT_EQ(context->get_type_mgr()->GetType(toRemove), nullptr);
827 ASSERT_EQ(context->get_type_mgr()->GetId(&st), toStay);
828 }
829
830 TEST(TypeManager, RemoveIdDoesntUnmapOtherTypes) {
831 const std::string text = R"(
832 OpCapability Shader
833 OpMemoryModel Logical GLSL450
834 %1 = OpTypeInt 32 0
835 %2 = OpTypeStruct %1
836 %3 = OpTypeStruct %1
837 )";
838
839 std::unique_ptr<IRContext> context =
840 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
841 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
842 EXPECT_NE(context, nullptr);
843
844 Integer u32(32, false);
845 Struct st({&u32});
846
847 EXPECT_EQ(1u, context->get_type_mgr()->GetId(&u32));
848 uint32_t id = context->get_type_mgr()->GetId(&st);
849 ASSERT_NE(id, 0u);
850 uint32_t toRemove = id == 2u ? 3u : 2u;
851 uint32_t toStay = id == 2u ? 2u : 3u;
852 context->get_type_mgr()->RemoveId(toRemove);
853 ASSERT_EQ(context->get_type_mgr()->GetType(toRemove), nullptr);
854 ASSERT_EQ(context->get_type_mgr()->GetId(&st), toStay);
855 }
856
857 TEST(TypeManager, GetTypeAndPointerType) {
858 const std::string text = R"(
859 OpCapability Shader
860 OpCapability Linkage
861 OpMemoryModel Logical GLSL450
862 %1 = OpTypeInt 32 0
863 %2 = OpTypeStruct %1
864 )";
865
866 std::unique_ptr<IRContext> context =
867 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
868 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
869 EXPECT_NE(context, nullptr);
870
871 Integer u32(32, false);
872 Pointer u32Ptr(&u32, spv::StorageClass::Function);
873 Struct st({&u32});
874 Pointer stPtr(&st, spv::StorageClass::Input);
875
876 auto pair = context->get_type_mgr()->GetTypeAndPointerType(
877 3u, spv::StorageClass::Function);
878 ASSERT_EQ(nullptr, pair.first);
879 ASSERT_EQ(nullptr, pair.second);
880
881 pair = context->get_type_mgr()->GetTypeAndPointerType(
882 1u, spv::StorageClass::Function);
883 ASSERT_TRUE(pair.first->IsSame(&u32));
884 ASSERT_TRUE(pair.second->IsSame(&u32Ptr));
885
886 pair = context->get_type_mgr()->GetTypeAndPointerType(
887 2u, spv::StorageClass::Input);
888 ASSERT_TRUE(pair.first->IsSame(&st));
889 ASSERT_TRUE(pair.second->IsSame(&stPtr));
890 }
891
892 TEST(TypeManager, DuplicateType) {
893 const std::string text = R"(
894 OpCapability Shader
895 OpMemoryModel Logical GLSL450
896 %1 = OpTypeInt 32 0
897 %2 = OpTypeInt 32 0
898 )";
899
900 std::unique_ptr<IRContext> context =
901 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
902 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
903 EXPECT_NE(context, nullptr);
904
905 const Type* type1 = context->get_type_mgr()->GetType(1u);
906 const Type* type2 = context->get_type_mgr()->GetType(2u);
907 EXPECT_NE(type1, nullptr);
908 EXPECT_NE(type2, nullptr);
909 EXPECT_EQ(*type1, *type2);
910 }
911
912 TEST(TypeManager, MultipleStructs) {
913 const std::string text = R"(
914 OpCapability Shader
915 OpMemoryModel Logical GLSL450
916 OpDecorate %3 Constant
917 %1 = OpTypeInt 32 0
918 %2 = OpTypeStruct %1
919 %3 = OpTypeStruct %1
920 )";
921
922 std::unique_ptr<IRContext> context =
923 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
924 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
925 EXPECT_NE(context, nullptr);
926
927 const Type* type1 = context->get_type_mgr()->GetType(2u);
928 const Type* type2 = context->get_type_mgr()->GetType(3u);
929 EXPECT_NE(type1, nullptr);
930 EXPECT_NE(type2, nullptr);
931 EXPECT_FALSE(type1->IsSame(type2));
932 }
933
934 TEST(TypeManager, RemovingIdAvoidsUseAfterFree) {
935 const std::string text = R"(
936 OpCapability Shader
937 OpMemoryModel Logical GLSL450
938 %1 = OpTypeInt 32 0
939 %2 = OpTypeStruct %1
940 )";
941
942 std::unique_ptr<IRContext> context =
943 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
944 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
945 EXPECT_NE(context, nullptr);
946
947 Integer u32(32, false);
948 Struct st({&u32});
949 const Type* type = context->get_type_mgr()->GetType(2u);
950 EXPECT_NE(type, nullptr);
951 context->get_type_mgr()->RemoveId(1u);
952 EXPECT_TRUE(type->IsSame(&st));
953 }
954
955 TEST(TypeManager, RegisterAndRemoveId) {
956 const std::string text = R"(
957 OpCapability Shader
958 OpMemoryModel Logical GLSL450
959 %1 = OpTypeInt 32 0
960 )";
961
962 std::unique_ptr<IRContext> context =
963 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
964 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
965 EXPECT_NE(context, nullptr);
966
967 uint32_t id = 2u;
968 {
969 // Ensure that u32 goes out of scope.
970 Integer u32(32, false);
971 Struct st({&u32});
972 context->get_type_mgr()->RegisterType(id, st);
973 }
974
975 context->get_type_mgr()->RemoveId(id);
976 EXPECT_EQ(nullptr, context->get_type_mgr()->GetType(id));
977 }
978
979 TEST(TypeManager, RegisterAndRemoveIdAllTypes) {
980 const std::string text = R"(
981 OpCapability Shader
982 OpMemoryModel Logical GLSL450
983 )";
984
985 std::unique_ptr<IRContext> context =
986 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
987 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
988 EXPECT_NE(context, nullptr);
989
990 std::vector<std::unique_ptr<Type>> types = GenerateAllTypes();
991 uint32_t id = 0u;
992 for (auto& t : types) {
993 context->get_type_mgr()->RegisterType(++id, *t);
994 EXPECT_EQ(*t, *context->get_type_mgr()->GetType(id));
995 EXPECT_EQ(id, context->get_type_mgr()->GetId(t.get()));
996 }
997 types.clear();
998
999 for (; id > 0; --id) {
1000 context->get_type_mgr()->RemoveId(id);
1001 EXPECT_EQ(nullptr, context->get_type_mgr()->GetType(id));
1002 }
1003 }
1004
1005 TEST(TypeManager, RegisterAndRemoveIdWithDecorations) {
1006 const std::string text = R"(
1007 OpCapability Shader
1008 OpMemoryModel Logical GLSL450
1009 %1 = OpTypeInt 32 0
1010 )";
1011
1012 std::unique_ptr<IRContext> context =
1013 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
1014 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1015 EXPECT_NE(context, nullptr);
1016
1017 uint32_t id = 2u;
1018 {
1019 Integer u32(32, false);
1020 Struct st({&u32, &u32});
1021 st.AddDecoration({10});
1022 st.AddDecoration({11});
1023 st.AddMemberDecoration(0, {{35, 4}});
1024 st.AddMemberDecoration(1, {{35, 4}});
1025 st.AddMemberDecoration(1, {{36, 5}});
1026 context->get_type_mgr()->RegisterType(id, st);
1027 EXPECT_EQ(st, *context->get_type_mgr()->GetType(id));
1028 }
1029
1030 context->get_type_mgr()->RemoveId(id);
1031 EXPECT_EQ(nullptr, context->get_type_mgr()->GetType(id));
1032 }
1033
1034 TEST(TypeManager, GetTypeInstructionInt) {
1035 const std::string text = R"(
1036 ; CHECK: OpTypeInt 32 0
1037 ; CHECK: OpTypeInt 16 1
1038 OpCapability Shader
1039 OpCapability Int16
1040 OpCapability Linkage
1041 OpMemoryModel Logical GLSL450
1042 )";
1043
1044 std::unique_ptr<IRContext> context =
1045 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
1046 EXPECT_NE(context, nullptr);
1047
1048 Integer uint_32(32, false);
1049 context->get_type_mgr()->GetTypeInstruction(&uint_32);
1050
1051 Integer int_16(16, true);
1052 context->get_type_mgr()->GetTypeInstruction(&int_16);
1053
1054 Match(text, context.get());
1055 }
1056
1057 TEST(TypeManager, GetTypeInstructionDuplicateInts) {
1058 const std::string text = R"(
1059 ; CHECK: OpTypeInt 32 0
1060 ; CHECK-NOT: OpType
1061 OpCapability Shader
1062 OpCapability Linkage
1063 OpMemoryModel Logical GLSL450
1064 )";
1065
1066 std::unique_ptr<IRContext> context =
1067 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
1068 EXPECT_NE(context, nullptr);
1069
1070 Integer uint_32(32, false);
1071 uint32_t id = context->get_type_mgr()->GetTypeInstruction(&uint_32);
1072
1073 Integer other(32, false);
1074 EXPECT_EQ(context->get_type_mgr()->GetTypeInstruction(&other), id);
1075
1076 Match(text, context.get());
1077 }
1078
1079 TEST(TypeManager, GetTypeInstructionAllTypes) {
1080 const std::string text = R"(
1081 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1082 ; CHECK: [[input_ptr:%\w+]] = OpTypePointer Input [[uint]]
1083 ; CHECK: [[uniform_ptr:%\w+]] = OpTypePointer Uniform [[uint]]
1084 ; CHECK: [[uint2:%\w+]] = OpConstant [[uint]] 2
1085 ; CHECK: [[uint8:%\w+]] = OpConstant [[uint]] 8
1086 ; CHECK: [[uint4:%\w+]] = OpConstant [[uint]] 4
1087 ; CHECK: [[uint_arr4:%\w+]] = OpTypeArray [[uint]] [[uint4]]
1088 ; CHECK: [[uint24:%\w+]] = OpConstant [[uint]] 24
1089 ; CHECK: [[uint42:%\w+]] = OpConstant [[uint]] 42
1090 ; CHECK: [[uint_arr4_44:%\w+]] = OpConstantComposite [[uint_arr4]] [[uint4]] [[uint4]] [[uint4]] [[uint4]]
1091 ; CHECK: [[uint100:%\w+]] = OpConstant [[uint]] 100
1092 ; CHECK: [[void:%\w+]] = OpTypeVoid
1093 ; CHECK: [[bool:%\w+]] = OpTypeBool
1094 ; CHECK: [[s32:%\w+]] = OpTypeInt 32 1
1095 ; CHECK: OpTypeInt 64 1
1096 ; CHECK: [[u64:%\w+]] = OpTypeInt 64 0
1097 ; CHECK: [[f32:%\w+]] = OpTypeFloat 32
1098 ; CHECK: OpTypeFloat 64
1099 ; CHECK: OpTypeVector [[s32]] 2
1100 ; CHECK: [[v3s32:%\w+]] = OpTypeVector [[s32]] 3
1101 ; CHECK: OpTypeVector [[u64]] 4
1102 ; CHECK: [[v3f32:%\w+]] = OpTypeVector [[f32]] 3
1103 ; CHECK: OpTypeMatrix [[v3s32]] 3
1104 ; CHECK: OpTypeMatrix [[v3s32]] 4
1105 ; CHECK: OpTypeMatrix [[v3f32]] 4
1106 ; CHECK: [[image1:%\w+]] = OpTypeImage [[s32]] 2D 0 0 0 0 Rg8 ReadOnly
1107 ; CHECK: OpTypeImage [[s32]] 2D 0 1 0 0 Rg8 ReadOnly
1108 ; CHECK: OpTypeImage [[s32]] 3D 0 1 0 0 Rg8 ReadOnly
1109 ; CHECK: [[image2:%\w+]] = OpTypeImage [[void]] 3D 0 1 0 1 Rg8 ReadWrite
1110 ; CHECK: OpTypeSampler
1111 ; CHECK: OpTypeSampledImage [[image1]]
1112 ; CHECK: OpTypeSampledImage [[image2]]
1113 ; CHECK: OpTypeArray [[f32]] [[uint100]]
1114 ; CHECK: [[a42f32:%\w+]] = OpTypeArray [[f32]] [[uint42]]
1115 ; CHECK: OpTypeArray [[u64]] [[uint24]]
1116 ; CHECK: OpTypeRuntimeArray [[v3f32]]
1117 ; CHECK: [[rav3s32:%\w+]] = OpTypeRuntimeArray [[v3s32]]
1118 ; CHECK: OpTypeStruct [[s32]]
1119 ; CHECK: [[sts32f32:%\w+]] = OpTypeStruct [[s32]] [[f32]]
1120 ; CHECK: OpTypeStruct [[u64]] [[a42f32]] [[rav3s32]]
1121 ; CHECK: OpTypeOpaque ""
1122 ; CHECK: OpTypeOpaque "hello"
1123 ; CHECK: OpTypeOpaque "world"
1124 ; CHECK: OpTypePointer Input [[f32]]
1125 ; CHECK: OpTypePointer Function [[sts32f32]]
1126 ; CHECK: OpTypePointer Function [[a42f32]]
1127 ; CHECK: OpTypeFunction [[void]]
1128 ; CHECK: OpTypeFunction [[void]] [[bool]]
1129 ; CHECK: OpTypeFunction [[void]] [[bool]] [[s32]]
1130 ; CHECK: OpTypeFunction [[s32]] [[bool]] [[s32]]
1131 ; CHECK: OpTypeEvent
1132 ; CHECK: OpTypeDeviceEvent
1133 ; CHECK: OpTypeReserveId
1134 ; CHECK: OpTypeQueue
1135 ; CHECK: OpTypePipe ReadWrite
1136 ; CHECK: OpTypePipe ReadOnly
1137 ; CHECK: OpTypeForwardPointer [[input_ptr]] Input
1138 ; CHECK: OpTypeForwardPointer [[uniform_ptr]] Input
1139 ; CHECK: OpTypeForwardPointer [[uniform_ptr]] Uniform
1140 ; CHECK: OpTypePipeStorage
1141 ; CHECK: OpTypeNamedBarrier
1142 ; CHECK: OpTypeAccelerationStructureKHR
1143 ; CHECK: OpTypeCooperativeMatrixNV [[f32]] [[uint24]] [[uint24]] [[uint24]]
1144 ; CHECK: OpTypeCooperativeMatrixKHR [[f32]] [[uint8]] [[uint8]] [[uint8]] [[uint2]]
1145 ; CHECK: OpTypeRayQueryKHR
1146 ; CHECK: OpTypeHitObjectNV
1147 ; CHECK: [[tensor_f32:%\w+]] = OpTypeTensorARM [[f32]]
1148 ; CHECK: [[tensor_f32_ranked:%\w+]] = OpTypeTensorARM [[f32]] [[uint4]]
1149 ; CHECK: [[tensor_f32_shaped:%\w+]] = OpTypeTensorARM [[f32]] [[uint4]] [[uint_arr4_44]]
1150 ; CHECK: OpTypeGraphARM 0 [[tensor_f32]]
1151 ; CHECK: OpTypeGraphARM 1 [[tensor_f32_ranked]] [[tensor_f32_ranked]]
1152 ; CHECK: OpTypeGraphARM 1 [[tensor_f32_shaped]] [[tensor_f32_shaped]]
1153 OpCapability Shader
1154 OpCapability Int64
1155 OpCapability Linkage
1156 OpMemoryModel Logical GLSL450
1157 %uint = OpTypeInt 32 0
1158 %1 = OpTypePointer Input %uint
1159 %2 = OpTypePointer Uniform %uint
1160 %1000 = OpConstant %uint 0
1161 %1001 = OpConstant %uint 1
1162 %1002 = OpConstant %uint 2
1163 %8 = OpConstant %uint 8
1164 %4 = OpConstant %uint 4
1165 %5 = OpTypeArray %uint %4
1166 %24 = OpConstant %uint 24
1167 %42 = OpConstant %uint 42
1168 %44 = OpConstantComposite %5 %4 %4 %4 %4
1169 %100 = OpConstant %uint 100
1170 %1003 = OpConstantFalse %bool
1171 )";
1172
1173 std::unique_ptr<IRContext> context =
1174 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
1175 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1176 EXPECT_NE(context, nullptr);
1177
1178 std::vector<std::unique_ptr<Type>> types = GenerateAllTypes();
1179 for (auto& t : types) {
1180 context->get_type_mgr()->GetTypeInstruction(t.get());
1181 }
1182
1183 Match(text, context.get(), false);
1184 }
1185
1186 TEST(TypeManager, GetTypeInstructionWithDecorations) {
1187 const std::string text = R"(
1188 ; CHECK: OpDecorate [[struct:%\w+]] CPacked
1189 ; CHECK: OpMemberDecorate [[struct]] 1 Offset 4
1190 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1191 ; CHECK: [[struct]] = OpTypeStruct [[uint]] [[uint]]
1192 OpCapability Shader
1193 OpCapability Kernel
1194 OpCapability Linkage
1195 OpMemoryModel Logical GLSL450
1196 %uint = OpTypeInt 32 0
1197 )";
1198
1199 std::unique_ptr<IRContext> context =
1200 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
1201 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1202 EXPECT_NE(context, nullptr);
1203
1204 Integer u32(32, false);
1205 Struct st({&u32, &u32});
1206 st.AddDecoration({10});
1207 st.AddMemberDecoration(1, {{35, 4}});
1208 (void)context->get_def_use_mgr();
1209 context->get_type_mgr()->GetTypeInstruction(&st);
1210
1211 Match(text, context.get());
1212 }
1213
1214 TEST(TypeManager, GetPointerToAmbiguousType1) {
1215 const std::string text = R"(
1216 ; CHECK: [[struct1:%\w+]] = OpTypeStruct
1217 ; CHECK: [[struct2:%\w+]] = OpTypeStruct
1218 ; CHECK: OpTypePointer Function [[struct2]]
1219 ; CHECK: OpTypePointer Function [[struct1]]
1220 OpCapability Shader
1221 OpCapability Kernel
1222 OpCapability Linkage
1223 OpMemoryModel Logical GLSL450
1224 %uint = OpTypeInt 32 0
1225 %1 = OpTypeStruct %uint
1226 %2 = OpTypeStruct %uint
1227 %3 = OpTypePointer Function %2
1228 )";
1229
1230 std::unique_ptr<IRContext> context =
1231 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
1232 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1233 EXPECT_NE(context, nullptr);
1234
1235 context->get_type_mgr()->FindPointerToType(1, spv::StorageClass::Function);
1236 Match(text, context.get());
1237 }
1238
1239 TEST(TypeManager, GetPointerToAmbiguousType2) {
1240 const std::string text = R"(
1241 ; CHECK: [[struct1:%\w+]] = OpTypeStruct
1242 ; CHECK: [[struct2:%\w+]] = OpTypeStruct
1243 ; CHECK: OpTypePointer Function [[struct1]]
1244 ; CHECK: OpTypePointer Function [[struct2]]
1245 OpCapability Shader
1246 OpCapability Kernel
1247 OpCapability Linkage
1248 OpMemoryModel Logical GLSL450
1249 %uint = OpTypeInt 32 0
1250 %1 = OpTypeStruct %uint
1251 %2 = OpTypeStruct %uint
1252 %3 = OpTypePointer Function %1
1253 )";
1254
1255 std::unique_ptr<IRContext> context =
1256 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
1257 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1258 EXPECT_NE(context, nullptr);
1259
1260 context->get_type_mgr()->FindPointerToType(2, spv::StorageClass::Function);
1261 Match(text, context.get());
1262 }
1263
1264 // Structures containing circular type references
1265 // (from https://github.com/KhronosGroup/SPIRV-Tools/issues/5623).
1266 TEST(TypeManager, CircularPointerToStruct) {
1267 const std::string text = R"(
1268 OpCapability VariablePointers
1269 OpCapability PhysicalStorageBufferAddresses
1270 OpCapability Int64
1271 OpCapability Shader
1272 OpExtension "SPV_KHR_variable_pointers"
1273 OpExtension "SPV_KHR_physical_storage_buffer"
1274 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1275 OpEntryPoint Fragment %1 "main"
1276 OpExecutionMode %1 OriginUpperLeft
1277 OpExecutionMode %1 DepthReplacing
1278 OpDecorate %1200 ArrayStride 24
1279 OpMemberDecorate %600 0 Offset 0
1280 OpMemberDecorate %800 0 Offset 0
1281 OpMemberDecorate %120 0 Offset 16
1282 OpTypeForwardPointer %1200 PhysicalStorageBuffer
1283 %600 = OpTypeStruct %1200
1284 %800 = OpTypeStruct %1200
1285 %120 = OpTypeStruct %800
1286 %1200 = OpTypePointer PhysicalStorageBuffer %120
1287 )";
1288
1289 std::unique_ptr<IRContext> context =
1290 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
1291 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1292 TypeManager manager(nullptr, context.get());
1293 uint32_t id = manager.FindPointerToType(600, spv::StorageClass::Function);
1294 EXPECT_EQ(id, 1201);
1295 }
1296
1297 TEST(TypeManager, AttachLinkageDecoration) {
1298 const std::string text = R"(
1299 OpCapability Shader
1300 OpCapability Linkage
1301 OpMemoryModel Logical GLSL450
1302 OpDecorate %1000 LinkageAttributes "_1000" Export
1303 %800 = OpTypeInt 32 0
1304 %1000 = OpTypeStruct %800
1305 %1200 = OpTypeStruct %800
1306 )";
1307
1308 std::unique_ptr<IRContext> context =
1309 BuildModule(SPV_ENV_UNIVERSAL_1_5, nullptr, text,
1310 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1311 TypeManager manager(nullptr, context.get());
1312
1313 constexpr uint32_t source_id = 1000u;
1314 constexpr uint32_t target_id = 1200u;
1315 std::vector<Instruction*> decorations =
1316 context->get_decoration_mgr()->GetDecorationsFor(source_id, true);
1317 Type* type = context->get_type_mgr()->GetType(target_id);
1318 for (auto dec : decorations) {
1319 manager.AttachDecoration(*dec, type);
1320 }
1321 EXPECT_FALSE(type->decoration_empty());
1322 EXPECT_TRUE(
1323 type->HasSameDecorations(context->get_type_mgr()->GetType(source_id)));
1324 }
1325
1326 } // namespace
1327 } // namespace analysis
1328 } // namespace opt
1329 } // namespace spvtools