Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 "src/tint/resolver/resolver.h" |
| 16 | |
| 17 | #include "gmock/gmock.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | #include "src/tint/resolver/resolver_test_helper.h" |
| 19 | #include "src/tint/sem/struct.h" |
| 20 | |
dan sinclair | d209379 | 2022-04-07 17:45:45 +0000 | [diff] [blame] | 21 | namespace tint::resolver { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
dan sinclair | f9b831c | 2022-08-29 21:13:00 +0000 | [diff] [blame] | 24 | using namespace tint::number_suffixes; // NOLINT |
| 25 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 26 | using ResolverHostShareableValidationTest = ResolverTest; |
| 27 | |
| 28 | TEST_F(ResolverHostShareableValidationTest, BoolMember) { |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 29 | auto* s = |
| 30 | Structure("S", utils::Vector{Member(Source{{56, 78}}, "x", ty.bool_(Source{{12, 34}}))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 32 | GlobalVar(Source{{90, 12}}, "g", ty.Of(s), builtin::AddressSpace::kStorage, |
| 33 | builtin::Access::kRead, Binding(0_a), Group(0_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 35 | ASSERT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 36 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 37 | EXPECT_EQ( |
| 38 | r()->error(), |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 39 | R"(12:34 error: Type 'bool' cannot be used in address space 'storage' as it is non-host-shareable |
| 40 | 56:78 note: while analyzing structure member S.x |
| 41 | 90:12 note: while instantiating 'var' g)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST_F(ResolverHostShareableValidationTest, BoolVectorMember) { |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 45 | auto* s = Structure( |
| 46 | "S", utils::Vector{Member(Source{{56, 78}}, "x", ty.vec3<bool>(Source{{12, 34}}))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 48 | GlobalVar(Source{{90, 12}}, "g", ty.Of(s), builtin::AddressSpace::kStorage, |
| 49 | builtin::Access::kRead, Binding(0_a), Group(0_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | ASSERT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 52 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 53 | EXPECT_EQ( |
| 54 | r()->error(), |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 55 | R"(12:34 error: Type 'vec3<bool>' cannot be used in address space 'storage' as it is non-host-shareable |
| 56 | 56:78 note: while analyzing structure member S.x |
| 57 | 90:12 note: while instantiating 'var' g)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST_F(ResolverHostShareableValidationTest, Aliases) { |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 61 | Alias("a1", ty.bool_()); |
Ben Clayton | 2117f80 | 2023-02-03 14:01:43 +0000 | [diff] [blame] | 62 | auto* s = |
| 63 | Structure("S", utils::Vector{Member(Source{{56, 78}}, "x", ty(Source{{12, 34}}, "a1"))}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 64 | auto* a2 = Alias("a2", ty.Of(s)); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 65 | GlobalVar(Source{{90, 12}}, "g", ty.Of(a2), builtin::AddressSpace::kStorage, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 66 | builtin::Access::kRead, Binding(0_a), Group(0_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 67 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 68 | ASSERT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 69 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | EXPECT_EQ( |
| 71 | r()->error(), |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 72 | R"(12:34 error: Type 'bool' cannot be used in address space 'storage' as it is non-host-shareable |
| 73 | 56:78 note: while analyzing structure member S.x |
| 74 | 90:12 note: while instantiating 'var' g)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(ResolverHostShareableValidationTest, NestedStructures) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 78 | auto* i1 = Structure("I1", utils::Vector{Member(Source{{1, 2}}, "x", ty.bool_())}); |
| 79 | auto* i2 = Structure("I2", utils::Vector{Member(Source{{3, 4}}, "y", ty.Of(i1))}); |
| 80 | auto* i3 = Structure("I3", utils::Vector{Member(Source{{5, 6}}, "z", ty.Of(i2))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 81 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 82 | auto* s = Structure("S", utils::Vector{Member(Source{{7, 8}}, "m", ty.Of(i3))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 84 | GlobalVar(Source{{9, 10}}, "g", ty.Of(s), builtin::AddressSpace::kStorage, |
| 85 | builtin::Access::kRead, Binding(0_a), Group(0_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 86 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | ASSERT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 88 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 89 | EXPECT_EQ( |
| 90 | r()->error(), |
Ben Clayton | d205b71 | 2022-11-30 01:13:35 +0000 | [diff] [blame] | 91 | R"(error: Type 'bool' cannot be used in address space 'storage' as it is non-host-shareable |
Ben Clayton | 4fe330f | 2022-10-13 13:33:25 +0000 | [diff] [blame] | 92 | 1:2 note: while analyzing structure member I1.x |
| 93 | 3:4 note: while analyzing structure member I2.y |
| 94 | 5:6 note: while analyzing structure member I3.z |
| 95 | 7:8 note: while analyzing structure member S.m |
Ben Clayton | ee49b1e | 2022-06-20 15:30:41 +0000 | [diff] [blame] | 96 | 9:10 note: while instantiating 'var' g)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(ResolverHostShareableValidationTest, NoError) { |
dan sinclair | e4039c7 | 2023-02-17 21:58:59 +0000 | [diff] [blame] | 100 | Enable(builtin::Extension::kF16); |
Zhaoming Jiang | 6058882 | 2022-06-28 14:03:36 +0000 | [diff] [blame] | 101 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 102 | auto* i1 = Structure("I1", utils::Vector{ |
Zhaoming Jiang | 9c71174 | 2022-07-08 19:35:05 +0000 | [diff] [blame] | 103 | Member(Source{{1, 1}}, "w1", ty.f32()), |
Zhaoming Jiang | 6058882 | 2022-06-28 14:03:36 +0000 | [diff] [blame] | 104 | Member(Source{{2, 1}}, "x1", ty.f32()), |
| 105 | Member(Source{{3, 1}}, "y1", ty.vec3<f32>()), |
| 106 | Member(Source{{4, 1}}, "z1", ty.array<i32, 4>()), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 107 | }); |
| 108 | auto* a1 = Alias("a1", ty.Of(i1)); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 109 | auto* i2 = Structure("I2", utils::Vector{ |
Zhaoming Jiang | 6058882 | 2022-06-28 14:03:36 +0000 | [diff] [blame] | 110 | Member(Source{{5, 1}}, "x2", ty.mat2x2<f32>()), |
Zhaoming Jiang | 9c71174 | 2022-07-08 19:35:05 +0000 | [diff] [blame] | 111 | Member(Source{{6, 1}}, "w2", ty.mat3x4<f32>()), |
Zhaoming Jiang | 6058882 | 2022-06-28 14:03:36 +0000 | [diff] [blame] | 112 | Member(Source{{7, 1}}, "z2", ty.Of(i1)), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 113 | }); |
| 114 | auto* a2 = Alias("a2", ty.Of(i2)); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 115 | auto* i3 = Structure("I3", utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 116 | Member(Source{{4, 1}}, "x3", ty.Of(a1)), |
| 117 | Member(Source{{5, 1}}, "y3", ty.Of(i2)), |
| 118 | Member(Source{{6, 1}}, "z3", ty.Of(a2)), |
| 119 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 120 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 121 | auto* s = Structure("S", utils::Vector{Member(Source{{7, 8}}, "m", ty.Of(i3))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 122 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 123 | GlobalVar(Source{{9, 10}}, "g", ty.Of(s), builtin::AddressSpace::kStorage, |
| 124 | builtin::Access::kRead, Binding(0_a), Group(0_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 125 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 126 | ASSERT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // namespace |
dan sinclair | d209379 | 2022-04-07 17:45:45 +0000 | [diff] [blame] | 130 | } // namespace tint::resolver |