Debian Codegraph @

test/opt/types_test.cpp

raw
1 // Copyright (c) 2016 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 #include "source/opt/types.h"
16
17 #include <memory>
18 #include <utility>
19 #include <vector>
20
21 #include "gtest/gtest.h"
22 #include "source/util/make_unique.h"
23
24 namespace spvtools {
25 namespace opt {
26 namespace analysis {
27 namespace {
28
29 // Fixture class providing some element types.
30 class SameTypeTest : public ::testing::Test {
31 protected:
32 void SetUp() override {
33 void_t_ = MakeUnique<Void>();
34 u32_t_ = MakeUnique<Integer>(32, false);
35 f64_t_ = MakeUnique<Float>(64);
36 v3u32_t_ = MakeUnique<Vector>(u32_t_.get(), 3);
37 image_t_ = MakeUnique<Image>(f64_t_.get(), spv::Dim::Dim2D, 1, 1, 0, 0,
38 spv::ImageFormat::R16,
39 spv::AccessQualifier::ReadWrite);
40 }
41
42 // Element types to be used for constructing other types for testing.
43 std::unique_ptr<Type> void_t_;
44 std::unique_ptr<Type> u32_t_;
45 std::unique_ptr<Type> f64_t_;
46 std::unique_ptr<Type> v3u32_t_;
47 std::unique_ptr<Type> image_t_;
48 };
49
50 #define TestMultipleInstancesOfTheSameTypeQualified(ty, name, ...) \
51 TEST_F(SameTypeTest, MultiSame##ty##name) { \
52 std::vector<std::unique_ptr<Type>> types; \
53 for (int i = 0; i < 10; ++i) types.emplace_back(new ty(__VA_ARGS__)); \
54 for (size_t i = 0; i < types.size(); ++i) { \
55 for (size_t j = 0; j < types.size(); ++j) { \
56 EXPECT_TRUE(types[i]->IsSame(types[j].get())) \
57 << "expected '" << types[i]->str() << "' is the same as '" \
58 << types[j]->str() << "'"; \
59 EXPECT_TRUE(*types[i] == *types[j]) \
60 << "expected '" << types[i]->str() << "' is the same as '" \
61 << types[j]->str() << "'"; \
62 } \
63 } \
64 }
65 #define TestMultipleInstancesOfTheSameType(ty, ...) \
66 TestMultipleInstancesOfTheSameTypeQualified(ty, Simple, __VA_ARGS__)
67
68 // clang-format off
69 TestMultipleInstancesOfTheSameType(Void)
70 TestMultipleInstancesOfTheSameType(Bool)
71 TestMultipleInstancesOfTheSameType(Integer, 32, true)
72 TestMultipleInstancesOfTheSameType(Float, 64)
73 TestMultipleInstancesOfTheSameType(Vector, u32_t_.get(), 3)
74 TestMultipleInstancesOfTheSameType(Matrix, v3u32_t_.get(), 4)
75 TestMultipleInstancesOfTheSameType(Image, f64_t_.get(), spv::Dim::Cube, 0, 0, 1, 1,
76 spv::ImageFormat::Rgb10A2,
77 spv::AccessQualifier::WriteOnly)
78 TestMultipleInstancesOfTheSameType(Sampler)
79 TestMultipleInstancesOfTheSameType(SampledImage, image_t_.get())
80 // There are three classes of arrays, based on the kinds of length information
81 // they have.
82 // 1. Array length is a constant or spec constant without spec ID, with literals
83 // for the constant value.
84 TestMultipleInstancesOfTheSameTypeQualified(Array, LenConstant, u32_t_.get(),
85 Array::LengthInfo{42,
86 {
87 0,
88 9999,
89 }})
90 // 2. Array length is a spec constant with a given spec id.
91 TestMultipleInstancesOfTheSameTypeQualified(Array, LenSpecId, u32_t_.get(),
92 Array::LengthInfo{42, {1, 99}})
93 // 3. Array length is an OpSpecConstantOp expression
94 TestMultipleInstancesOfTheSameTypeQualified(Array, LenDefiningId, u32_t_.get(),
95 Array::LengthInfo{42, {2, 42}})
96
97 TestMultipleInstancesOfTheSameType(RuntimeArray, u32_t_.get())
98 TestMultipleInstancesOfTheSameType(Struct, std::vector<const Type*>{
99 u32_t_.get(), f64_t_.get()})
100 TestMultipleInstancesOfTheSameType(Opaque, "testing rocks")
101 TestMultipleInstancesOfTheSameType(Pointer, u32_t_.get(), spv::StorageClass::Input)
102 TestMultipleInstancesOfTheSameType(Function, u32_t_.get(),
103 {f64_t_.get(), f64_t_.get()})
104 TestMultipleInstancesOfTheSameType(Event)
105 TestMultipleInstancesOfTheSameType(DeviceEvent)
106 TestMultipleInstancesOfTheSameType(ReserveId)
107 TestMultipleInstancesOfTheSameType(Queue)
108 TestMultipleInstancesOfTheSameType(Pipe, spv::AccessQualifier::ReadWrite)
109 TestMultipleInstancesOfTheSameType(ForwardPointer, 10, spv::StorageClass::Uniform)
110 TestMultipleInstancesOfTheSameType(PipeStorage)
111 TestMultipleInstancesOfTheSameType(NamedBarrier)
112 TestMultipleInstancesOfTheSameType(AccelerationStructureNV)
113 #undef TestMultipleInstanceOfTheSameType
114 #undef TestMultipleInstanceOfTheSameTypeQual
115
116 std::vector<std::unique_ptr<Type>> GenerateAllTypes() {
117 // clang-format on
118 // Types in this test case are only equal to themselves, nothing else.
119 std::vector<std::unique_ptr<Type>> types;
120
121 // Forward Pointer
122 types.emplace_back(new ForwardPointer(10000, spv::StorageClass::Input));
123 types.emplace_back(new ForwardPointer(20000, spv::StorageClass::Input));
124
125 // Void, Bool
126 types.emplace_back(new Void());
127 auto* voidt = types.back().get();
128 types.emplace_back(new Bool());
129 auto* boolt = types.back().get();
130
131 // Integer
132 types.emplace_back(new Integer(32, true));
133 auto* s32 = types.back().get();
134 types.emplace_back(new Integer(32, false));
135 types.emplace_back(new Integer(64, true));
136 types.emplace_back(new Integer(64, false));
137 auto* u64 = types.back().get();
138
139 // Float
140 types.emplace_back(new Float(32));
141 auto* f32 = types.back().get();
142 types.emplace_back(new Float(64));
143
144 // Vector
145 types.emplace_back(new Vector(s32, 2));
146 types.emplace_back(new Vector(s32, 3));
147 auto* v3s32 = types.back().get();
148 types.emplace_back(new Vector(u64, 4));
149 types.emplace_back(new Vector(f32, 3));
150 auto* v3f32 = types.back().get();
151
152 // Matrix
153 types.emplace_back(new Matrix(v3s32, 3));
154 types.emplace_back(new Matrix(v3s32, 4));
155 types.emplace_back(new Matrix(v3f32, 4));
156
157 // Images
158 types.emplace_back(new Image(s32, spv::Dim::Dim2D, 0, 0, 0, 0,
159 spv::ImageFormat::Rg8,
160 spv::AccessQualifier::ReadOnly));
161 auto* image1 = types.back().get();
162 types.emplace_back(new Image(s32, spv::Dim::Dim2D, 0, 1, 0, 0,
163 spv::ImageFormat::Rg8,
164 spv::AccessQualifier::ReadOnly));
165 types.emplace_back(new Image(s32, spv::Dim::Dim3D, 0, 1, 0, 0,
166 spv::ImageFormat::Rg8,
167 spv::AccessQualifier::ReadOnly));
168 types.emplace_back(new Image(voidt, spv::Dim::Dim3D, 0, 1, 0, 1,
169 spv::ImageFormat::Rg8,
170 spv::AccessQualifier::ReadWrite));
171 auto* image2 = types.back().get();
172
173 // Sampler
174 types.emplace_back(new Sampler());
175
176 // Sampled Image
177 types.emplace_back(new SampledImage(image1));
178 types.emplace_back(new SampledImage(image2));
179
180 // Array
181 // Length is constant with integer bit representation of 42.
182 types.emplace_back(new Array(f32, Array::LengthInfo{99u, {0, 42u}}));
183 auto* a42f32 = types.back().get();
184 // Differs from previous in length value only.
185 types.emplace_back(new Array(f32, Array::LengthInfo{99u, {0, 44u}}));
186 // Length is 64-bit constant integer value 42.
187 types.emplace_back(new Array(u64, Array::LengthInfo{100u, {0, 42u, 0u}}));
188 // Differs from previous in length value only.
189 types.emplace_back(new Array(u64, Array::LengthInfo{100u, {0, 44u, 0u}}));
190
191 // Length is spec constant with spec id 18 and default value 44.
192 types.emplace_back(new Array(f32, Array::LengthInfo{99u,
193 {
194 1,
195 18u,
196 44u,
197 }}));
198 // Differs from previous in spec id only.
199 types.emplace_back(new Array(f32, Array::LengthInfo{99u, {1, 19u, 44u}}));
200 // Differs from previous in literal value only.
201 types.emplace_back(new Array(f32, Array::LengthInfo{99u, {1, 19u, 48u}}));
202 // Length is spec constant op with id 42.
203 types.emplace_back(new Array(f32, Array::LengthInfo{42u, {2, 42}}));
204 // Differs from previous in result id only.
205 types.emplace_back(new Array(f32, Array::LengthInfo{43u, {2, 43}}));
206
207 // RuntimeArray
208 types.emplace_back(new RuntimeArray(v3f32));
209 types.emplace_back(new RuntimeArray(v3s32));
210 auto* rav3s32 = types.back().get();
211
212 // Struct
213 types.emplace_back(new Struct(std::vector<const Type*>{s32}));
214 types.emplace_back(new Struct(std::vector<const Type*>{s32, f32}));
215 auto* sts32f32 = types.back().get();
216 types.emplace_back(
217 new Struct(std::vector<const Type*>{u64, a42f32, rav3s32}));
218
219 // Opaque
220 types.emplace_back(new Opaque(""));
221 types.emplace_back(new Opaque("hello"));
222 types.emplace_back(new Opaque("world"));
223
224 // Pointer
225 types.emplace_back(new Pointer(f32, spv::StorageClass::Input));
226 types.emplace_back(new Pointer(sts32f32, spv::StorageClass::Function));
227 types.emplace_back(new Pointer(a42f32, spv::StorageClass::Function));
228 types.emplace_back(new Pointer(voidt, spv::StorageClass::Function));
229 types.emplace_back(new Pointer(nullptr, spv::StorageClass::Uniform));
230
231 // Function
232 types.emplace_back(new Function(voidt, {}));
233 types.emplace_back(new Function(voidt, {boolt}));
234 types.emplace_back(new Function(voidt, {boolt, s32}));
235 types.emplace_back(new Function(s32, {boolt, s32}));
236
237 // Event, Device Event, Reserve Id, Queue,
238 types.emplace_back(new Event());
239 types.emplace_back(new DeviceEvent());
240 types.emplace_back(new ReserveId());
241 types.emplace_back(new Queue());
242
243 // Pipe, Forward Pointer, PipeStorage, NamedBarrier
244 types.emplace_back(new Pipe(spv::AccessQualifier::ReadWrite));
245 types.emplace_back(new Pipe(spv::AccessQualifier::ReadOnly));
246 types.emplace_back(new ForwardPointer(1, spv::StorageClass::Input));
247 types.emplace_back(new ForwardPointer(2, spv::StorageClass::Input));
248 types.emplace_back(new ForwardPointer(2, spv::StorageClass::Uniform));
249 types.emplace_back(new PipeStorage());
250 types.emplace_back(new NamedBarrier());
251
252 return types;
253 }
254
255 TEST(Types, AllTypes) {
256 // Types in this test case are only equal to themselves, nothing else.
257 std::vector<std::unique_ptr<Type>> types = GenerateAllTypes();
258
259 for (size_t i = 0; i < types.size(); ++i) {
260 for (size_t j = 0; j < types.size(); ++j) {
261 if (i == j) {
262 EXPECT_TRUE(types[i]->IsSame(types[j].get()))
263 << "expected '" << types[i]->str() << "' is the same as '"
264 << types[j]->str() << "'";
265 } else {
266 EXPECT_FALSE(types[i]->IsSame(types[j].get()))
267 << "entry (" << i << "," << j << ") expected '" << types[i]->str()
268 << "' is different to '" << types[j]->str() << "'";
269 }
270 }
271 }
272 }
273
274 TEST(Types, TestNumberOfComponentsOnArrays) {
275 Float f32(32);
276 EXPECT_EQ(f32.NumberOfComponents(), 0);
277
278 Array array_size_42(
279 &f32, Array::LengthInfo{99u, {Array::LengthInfo::kConstant, 42u}});
280 EXPECT_EQ(array_size_42.NumberOfComponents(), 42);
281
282 Array array_size_0xDEADBEEF00C0FFEE(
283 &f32, Array::LengthInfo{
284 99u, {Array::LengthInfo::kConstant, 0xC0FFEE, 0xDEADBEEF}});
285 EXPECT_EQ(array_size_0xDEADBEEF00C0FFEE.NumberOfComponents(),
286 0xDEADBEEF00C0FFEEull);
287
288 Array array_size_unknown(
289 &f32,
290 Array::LengthInfo{99u, {Array::LengthInfo::kConstantWithSpecId, 10}});
291 EXPECT_EQ(array_size_unknown.NumberOfComponents(), UINT64_MAX);
292
293 RuntimeArray runtime_array(&f32);
294 EXPECT_EQ(runtime_array.NumberOfComponents(), UINT64_MAX);
295 }
296
297 TEST(Types, TestNumberOfComponentsOnVectors) {
298 Float f32(32);
299 EXPECT_EQ(f32.NumberOfComponents(), 0);
300
301 for (uint32_t vector_size = 1; vector_size < 4; ++vector_size) {
302 Vector vector(&f32, vector_size);
303 EXPECT_EQ(vector.NumberOfComponents(), vector_size);
304 }
305 }
306
307 TEST(Types, TestNumberOfComponentsOnMatrices) {
308 Float f32(32);
309 Vector vector(&f32, 2);
310
311 for (uint32_t number_of_columns = 1; number_of_columns < 4;
312 ++number_of_columns) {
313 Matrix matrix(&vector, number_of_columns);
314 EXPECT_EQ(matrix.NumberOfComponents(), number_of_columns);
315 }
316 }
317
318 TEST(Types, TestNumberOfComponentsOnStructs) {
319 Float f32(32);
320 Vector vector(&f32, 2);
321
322 Struct empty_struct({});
323 EXPECT_EQ(empty_struct.NumberOfComponents(), 0);
324
325 Struct struct_f32({&f32});
326 EXPECT_EQ(struct_f32.NumberOfComponents(), 1);
327
328 Struct struct_f32_vec({&f32, &vector});
329 EXPECT_EQ(struct_f32_vec.NumberOfComponents(), 2);
330
331 Struct struct_100xf32(std::vector<const Type*>(100, &f32));
332 EXPECT_EQ(struct_100xf32.NumberOfComponents(), 100);
333 }
334
335 TEST(Types, IntSignedness) {
336 std::vector<bool> signednesses = {true, false, false, true};
337 std::vector<std::unique_ptr<Integer>> types;
338 for (bool s : signednesses) {
339 types.emplace_back(new Integer(32, s));
340 }
341 for (size_t i = 0; i < signednesses.size(); i++) {
342 EXPECT_EQ(signednesses[i], types[i]->IsSigned());
343 }
344 }
345
346 TEST(Types, IntWidth) {
347 std::vector<uint32_t> widths = {1, 2, 4, 8, 16, 32, 48, 64, 128};
348 std::vector<std::unique_ptr<Integer>> types;
349 for (uint32_t w : widths) {
350 types.emplace_back(new Integer(w, true));
351 }
352 for (size_t i = 0; i < widths.size(); i++) {
353 EXPECT_EQ(widths[i], types[i]->width());
354 }
355 }
356
357 TEST(Types, FloatWidth) {
358 std::vector<uint32_t> widths = {1, 2, 4, 8, 16, 32, 48, 64, 128};
359 std::vector<std::unique_ptr<Float>> types;
360 for (uint32_t w : widths) {
361 types.emplace_back(new Float(w));
362 }
363 for (size_t i = 0; i < widths.size(); i++) {
364 EXPECT_EQ(widths[i], types[i]->width());
365 }
366 }
367
368 TEST(Types, FloatFPEncoding) {
369 std::vector<spv::FPEncoding> encodings = {
370 spv::FPEncoding::BFloat16KHR,
371 spv::FPEncoding::Max,
372 };
373 std::vector<std::unique_ptr<Float>> types;
374 for (spv::FPEncoding encoding : encodings) {
375 types.emplace_back(new Float(16, encoding));
376 }
377 for (size_t i = 0; i < encodings.size(); i++) {
378 EXPECT_EQ(encodings[i], types[i]->encoding());
379 }
380 }
381
382 TEST(Types, VectorElementCount) {
383 auto s32 = MakeUnique<Integer>(32, true);
384 for (uint32_t c : {2, 3, 4}) {
385 auto s32v = MakeUnique<Vector>(s32.get(), c);
386 EXPECT_EQ(c, s32v->element_count());
387 }
388 }
389
390 TEST(Types, MatrixElementCount) {
391 auto s32 = MakeUnique<Integer>(32, true);
392 auto s32v4 = MakeUnique<Vector>(s32.get(), 4);
393 for (uint32_t c : {1, 2, 3, 4, 10, 100}) {
394 auto s32m = MakeUnique<Matrix>(s32v4.get(), c);
395 EXPECT_EQ(c, s32m->element_count());
396 }
397 }
398
399 TEST(Types, IsUniqueType) {
400 std::vector<std::unique_ptr<Type>> types = GenerateAllTypes();
401
402 for (auto& t : types) {
403 bool expectation = true;
404 // Disallowing variable pointers.
405 switch (t->kind()) {
406 case Type::kArray:
407 case Type::kRuntimeArray:
408 case Type::kStruct:
409 case Type::kPointer:
410 expectation = false;
411 break;
412 default:
413 break;
414 }
415 EXPECT_EQ(t->IsUniqueType(), expectation)
416 << "expected '" << t->str() << "' to be a "
417 << (expectation ? "" : "non-") << "unique type";
418 }
419 }
420
421 std::vector<std::unique_ptr<Type>> GenerateAllTypesWithDecorations() {
422 std::vector<std::unique_ptr<Type>> types = GenerateAllTypes();
423 uint32_t elems = 1;
424 uint32_t decs = 1;
425 for (auto& t : types) {
426 for (uint32_t i = 0; i < (decs % 10); ++i) {
427 std::vector<uint32_t> decoration;
428 for (uint32_t j = 0; j < (elems % 4) + 1; ++j) {
429 decoration.push_back(j);
430 }
431 t->AddDecoration(std::move(decoration));
432 ++elems;
433 ++decs;
434 }
435 }
436
437 return types;
438 }
439
440 TEST(Types, Clone) {
441 std::vector<std::unique_ptr<Type>> types = GenerateAllTypesWithDecorations();
442 for (auto& t : types) {
443 auto clone = t->Clone();
444 EXPECT_TRUE(*t == *clone);
445 EXPECT_TRUE(t->HasSameDecorations(clone.get()));
446 EXPECT_NE(clone.get(), t.get());
447 }
448 }
449
450 TEST(Types, RemoveDecorations) {
451 std::vector<std::unique_ptr<Type>> types = GenerateAllTypesWithDecorations();
452 for (auto& t : types) {
453 auto decorationless = t->RemoveDecorations();
454 EXPECT_EQ(*t == *decorationless, t->decoration_empty());
455 EXPECT_EQ(t->HasSameDecorations(decorationless.get()),
456 t->decoration_empty());
457 EXPECT_NE(t.get(), decorationless.get());
458 }
459 }
460
461 TEST(Types, UntypedPointer) {
462 std::unique_ptr<Type> type(new Pointer(nullptr, spv::StorageClass::Uniform));
463 const auto untyped = type->AsPointer();
464 EXPECT_NE(untyped, nullptr);
465 EXPECT_TRUE(untyped->is_untyped());
466 EXPECT_EQ(untyped->pointee_type(), nullptr);
467 EXPECT_EQ(untyped->storage_class(), spv::StorageClass::Uniform);
468 }
469
470 } // namespace
471 } // namespace analysis
472 } // namespace opt
473 } // namespace spvtools