Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 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 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 15 | #include "src/resolver/resolver.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 16 | |
Antonio Maiorano | fe2b417 | 2021-03-01 20:01:39 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 18 | #include <cmath> |
| 19 | #include <iomanip> |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 20 | #include <utility> |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 21 | |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 22 | #include "src/ast/alias.h" |
| 23 | #include "src/ast/array.h" |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 24 | #include "src/ast/assignment_statement.h" |
dan sinclair | a7d498e | 2020-09-22 22:07:13 +0000 | [diff] [blame] | 25 | #include "src/ast/bitcast_expression.h" |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 26 | #include "src/ast/break_statement.h" |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 27 | #include "src/ast/call_statement.h" |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 28 | #include "src/ast/continue_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 29 | #include "src/ast/depth_texture.h" |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 30 | #include "src/ast/disable_validation_decoration.h" |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 31 | #include "src/ast/discard_statement.h" |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 32 | #include "src/ast/fallthrough_statement.h" |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 33 | #include "src/ast/for_loop_statement.h" |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 34 | #include "src/ast/if_statement.h" |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 35 | #include "src/ast/internal_decoration.h" |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 36 | #include "src/ast/interpolate_decoration.h" |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 37 | #include "src/ast/loop_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 38 | #include "src/ast/matrix.h" |
James Price | e87ded8 | 2021-04-30 17:14:19 +0000 | [diff] [blame] | 39 | #include "src/ast/override_decoration.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 40 | #include "src/ast/pointer.h" |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 41 | #include "src/ast/return_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 42 | #include "src/ast/sampled_texture.h" |
| 43 | #include "src/ast/sampler.h" |
| 44 | #include "src/ast/storage_texture.h" |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 45 | #include "src/ast/struct_block_decoration.h" |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 46 | #include "src/ast/switch_statement.h" |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 47 | #include "src/ast/type_name.h" |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 48 | #include "src/ast/unary_op_expression.h" |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 49 | #include "src/ast/variable_decl_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 50 | #include "src/ast/vector.h" |
Antonio Maiorano | bfc9794 | 2021-04-06 20:41:07 +0000 | [diff] [blame] | 51 | #include "src/ast/workgroup_decoration.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 52 | #include "src/sem/array.h" |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 53 | #include "src/sem/atomic_type.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 54 | #include "src/sem/call.h" |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 55 | #include "src/sem/depth_multisampled_texture_type.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 56 | #include "src/sem/depth_texture_type.h" |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 57 | #include "src/sem/for_loop_statement.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 58 | #include "src/sem/function.h" |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 59 | #include "src/sem/if_statement.h" |
| 60 | #include "src/sem/loop_statement.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 61 | #include "src/sem/member_accessor_expression.h" |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 62 | #include "src/sem/multisampled_texture_type.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 63 | #include "src/sem/pointer_type.h" |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 64 | #include "src/sem/reference_type.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 65 | #include "src/sem/sampled_texture_type.h" |
| 66 | #include "src/sem/sampler_type.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 67 | #include "src/sem/statement.h" |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 68 | #include "src/sem/storage_texture_type.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 69 | #include "src/sem/struct.h" |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 70 | #include "src/sem/switch_statement.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 71 | #include "src/sem/variable.h" |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 72 | #include "src/utils/defer.h" |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 73 | #include "src/utils/get_or_create.h" |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 74 | #include "src/utils/math.h" |
Ben Clayton | b350500 | 2021-05-19 16:11:04 +0000 | [diff] [blame] | 75 | #include "src/utils/scoped_assignment.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 76 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 77 | namespace tint { |
Ben Clayton | c7dcbae | 2021-03-09 15:08:48 +0000 | [diff] [blame] | 78 | namespace resolver { |
Ben Clayton | 052ab89 | 2021-02-08 19:53:42 +0000 | [diff] [blame] | 79 | namespace { |
| 80 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 81 | using IntrinsicType = tint::sem::IntrinsicType; |
Ben Clayton | 052ab89 | 2021-02-08 19:53:42 +0000 | [diff] [blame] | 82 | |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 83 | bool IsValidStorageTextureDimension(ast::TextureDimension dim) { |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 84 | switch (dim) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 85 | case ast::TextureDimension::k1d: |
| 86 | case ast::TextureDimension::k2d: |
| 87 | case ast::TextureDimension::k2dArray: |
| 88 | case ast::TextureDimension::k3d: |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 89 | return true; |
| 90 | default: |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 95 | bool IsValidStorageTextureImageFormat(ast::ImageFormat format) { |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 96 | switch (format) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 97 | case ast::ImageFormat::kR32Uint: |
| 98 | case ast::ImageFormat::kR32Sint: |
| 99 | case ast::ImageFormat::kR32Float: |
| 100 | case ast::ImageFormat::kRg32Uint: |
| 101 | case ast::ImageFormat::kRg32Sint: |
| 102 | case ast::ImageFormat::kRg32Float: |
| 103 | case ast::ImageFormat::kRgba8Unorm: |
| 104 | case ast::ImageFormat::kRgba8Snorm: |
| 105 | case ast::ImageFormat::kRgba8Uint: |
| 106 | case ast::ImageFormat::kRgba8Sint: |
| 107 | case ast::ImageFormat::kRgba16Uint: |
| 108 | case ast::ImageFormat::kRgba16Sint: |
| 109 | case ast::ImageFormat::kRgba16Float: |
| 110 | case ast::ImageFormat::kRgba32Uint: |
| 111 | case ast::ImageFormat::kRgba32Sint: |
| 112 | case ast::ImageFormat::kRgba32Float: |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 113 | return true; |
| 114 | default: |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 119 | /// @returns true if the decoration list contains a |
| 120 | /// ast::DisableValidationDecoration with the validation mode equal to |
| 121 | /// `validation` |
| 122 | bool IsValidationDisabled(const ast::DecorationList& decorations, |
| 123 | ast::DisabledValidation validation) { |
| 124 | for (auto* decoration : decorations) { |
| 125 | if (auto* dv = decoration->As<ast::DisableValidationDecoration>()) { |
| 126 | if (dv->Validation() == validation) { |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 134 | /// @returns true if the decoration list does not contains a |
| 135 | /// ast::DisableValidationDecoration with the validation mode equal to |
| 136 | /// `validation` |
| 137 | bool IsValidationEnabled(const ast::DecorationList& decorations, |
| 138 | ast::DisabledValidation validation) { |
| 139 | return !IsValidationDisabled(decorations, validation); |
| 140 | } |
| 141 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 142 | // Helper to stringify a pipeline IO decoration. |
| 143 | std::string deco_to_str(const ast::Decoration* deco) { |
| 144 | std::stringstream str; |
| 145 | if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { |
| 146 | str << "builtin(" << builtin->value() << ")"; |
| 147 | } else if (auto* location = deco->As<ast::LocationDecoration>()) { |
| 148 | str << "location(" << location->value() << ")"; |
| 149 | } |
| 150 | return str.str(); |
| 151 | } |
Ben Clayton | 052ab89 | 2021-02-08 19:53:42 +0000 | [diff] [blame] | 152 | } // namespace |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 153 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 154 | Resolver::Resolver(ProgramBuilder* builder) |
Ben Clayton | b82acac | 2021-05-06 16:04:03 +0000 | [diff] [blame] | 155 | : builder_(builder), |
| 156 | diagnostics_(builder->Diagnostics()), |
Ben Clayton | b29a59d | 2021-06-01 19:06:31 +0000 | [diff] [blame] | 157 | intrinsic_table_(IntrinsicTable::Create(*builder)) {} |
Ben Clayton | d59cedb | 2021-01-25 18:14:08 +0000 | [diff] [blame] | 158 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 159 | Resolver::~Resolver() = default; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 160 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 161 | void Resolver::set_referenced_from_function_if_needed(VariableInfo* var, |
| 162 | bool local) { |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 163 | if (current_function_ == nullptr) { |
| 164 | return; |
| 165 | } |
Ryan Harrison | 3832b8e | 2021-06-09 20:45:09 +0000 | [diff] [blame] | 166 | |
| 167 | if (var->kind != VariableKind::kGlobal) { |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
Ben Clayton | f81df12 | 2021-02-17 13:10:49 +0000 | [diff] [blame] | 171 | current_function_->referenced_module_vars.add(var); |
Enrico Galli | 3d449d2 | 2020-12-08 21:07:24 +0000 | [diff] [blame] | 172 | if (local) { |
Ben Clayton | f81df12 | 2021-02-17 13:10:49 +0000 | [diff] [blame] | 173 | current_function_->local_referenced_module_vars.add(var); |
Enrico Galli | 3d449d2 | 2020-12-08 21:07:24 +0000 | [diff] [blame] | 174 | } |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 177 | bool Resolver::Resolve() { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 178 | if (builder_->Diagnostics().contains_errors()) { |
| 179 | return false; |
| 180 | } |
| 181 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 182 | bool result = ResolveInternal(); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 183 | |
Ben Clayton | b82acac | 2021-05-06 16:04:03 +0000 | [diff] [blame] | 184 | if (!result && !diagnostics_.contains_errors()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 185 | TINT_ICE(Resolver, diagnostics_) |
| 186 | << "resolving failed, but no error was raised"; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 190 | // Even if resolving failed, create all the semantic nodes for information we |
| 191 | // did generate. |
| 192 | CreateSemanticNodes(); |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 197 | // https://gpuweb.github.io/gpuweb/wgsl/#plain-types-section |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 198 | bool Resolver::IsPlain(const sem::Type* type) const { |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 199 | return type->is_scalar() || type->Is<sem::Atomic>() || |
| 200 | type->Is<sem::Vector>() || type->Is<sem::Matrix>() || |
| 201 | type->Is<sem::Array>() || type->Is<sem::Struct>(); |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // https://gpuweb.github.io/gpuweb/wgsl.html#storable-types |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 205 | bool Resolver::IsStorable(const sem::Type* type) const { |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 206 | return IsPlain(type) || type->Is<sem::Texture>() || type->Is<sem::Sampler>(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 209 | // https://gpuweb.github.io/gpuweb/wgsl.html#host-shareable-types |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 210 | bool Resolver::IsHostShareable(const sem::Type* type) const { |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 211 | if (type->IsAnyOf<sem::I32, sem::U32, sem::F32>()) { |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 212 | return true; |
| 213 | } |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 214 | if (auto* vec = type->As<sem::Vector>()) { |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 215 | return IsHostShareable(vec->type()); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 216 | } |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 217 | if (auto* mat = type->As<sem::Matrix>()) { |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 218 | return IsHostShareable(mat->type()); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 219 | } |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 220 | if (auto* arr = type->As<sem::Array>()) { |
| 221 | return IsHostShareable(arr->ElemType()); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 222 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 223 | if (auto* str = type->As<sem::Struct>()) { |
| 224 | for (auto* member : str->Members()) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 225 | if (!IsHostShareable(member->Type())) { |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | return true; |
| 230 | } |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 231 | if (auto* atomic = type->As<sem::Atomic>()) { |
| 232 | return IsHostShareable(atomic->Type()); |
| 233 | } |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 237 | bool Resolver::ResolveInternal() { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 238 | Mark(&builder_->AST()); |
| 239 | |
Antonio Maiorano | bb5617f | 2021-03-19 18:45:30 +0000 | [diff] [blame] | 240 | // Process everything else in the order they appear in the module. This is |
| 241 | // necessary for validation of use-before-declaration. |
| 242 | for (auto* decl : builder_->AST().GlobalDeclarations()) { |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 243 | if (auto* td = decl->As<ast::TypeDecl>()) { |
| 244 | Mark(td); |
| 245 | if (!TypeDecl(td)) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 246 | return false; |
Antonio Maiorano | bb5617f | 2021-03-19 18:45:30 +0000 | [diff] [blame] | 247 | } |
| 248 | } else if (auto* func = decl->As<ast::Function>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 249 | Mark(func); |
Antonio Maiorano | bb5617f | 2021-03-19 18:45:30 +0000 | [diff] [blame] | 250 | if (!Function(func)) { |
| 251 | return false; |
| 252 | } |
| 253 | } else if (auto* var = decl->As<ast::Variable>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 254 | Mark(var); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 255 | if (!GlobalVariable(var)) { |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 258 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 259 | TINT_UNREACHABLE(Resolver, diagnostics_) |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 260 | << "unhandled global declaration: " << decl->TypeInfo().name; |
| 261 | return false; |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 262 | } |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 265 | if (!ValidatePipelineStages()) { |
| 266 | return false; |
| 267 | } |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 268 | if (!ValidateAtomicUses()) { |
| 269 | return false; |
| 270 | } |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 271 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 272 | bool result = true; |
| 273 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 274 | for (auto* node : builder_->ASTNodes().Objects()) { |
| 275 | if (marked_.count(node) == 0) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 276 | TINT_ICE(Resolver, diagnostics_) |
| 277 | << "AST node '" << node->TypeInfo().name |
| 278 | << "' was not reached by the resolver\n" |
| 279 | << "At: " << node->source() << "\n" |
| 280 | << "Content: " << builder_->str(node) << "\n" |
| 281 | << "Pointer: " << node; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 282 | result = false; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 286 | return result; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 289 | sem::Type* Resolver::Type(const ast::Type* ty) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 290 | Mark(ty); |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 291 | auto* s = [&]() -> sem::Type* { |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 292 | if (ty->Is<ast::Void>()) { |
| 293 | return builder_->create<sem::Void>(); |
| 294 | } |
| 295 | if (ty->Is<ast::Bool>()) { |
| 296 | return builder_->create<sem::Bool>(); |
| 297 | } |
| 298 | if (ty->Is<ast::I32>()) { |
| 299 | return builder_->create<sem::I32>(); |
| 300 | } |
| 301 | if (ty->Is<ast::U32>()) { |
| 302 | return builder_->create<sem::U32>(); |
| 303 | } |
| 304 | if (ty->Is<ast::F32>()) { |
| 305 | return builder_->create<sem::F32>(); |
| 306 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 307 | if (auto* t = ty->As<ast::Vector>()) { |
| 308 | if (auto* el = Type(t->type())) { |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 309 | if (auto* vector = builder_->create<sem::Vector>( |
| 310 | const_cast<sem::Type*>(el), t->size())) { |
| 311 | if (ValidateVector(vector, t->source())) { |
| 312 | return vector; |
| 313 | } |
| 314 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 315 | } |
| 316 | return nullptr; |
| 317 | } |
| 318 | if (auto* t = ty->As<ast::Matrix>()) { |
| 319 | if (auto* el = Type(t->type())) { |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 320 | if (auto* column_type = builder_->create<sem::Vector>( |
| 321 | const_cast<sem::Type*>(el), t->rows())) { |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 322 | if (auto* matrix = |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 323 | builder_->create<sem::Matrix>(column_type, t->columns())) { |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 324 | if (ValidateMatrix(matrix, t->source())) { |
| 325 | return matrix; |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 329 | } |
| 330 | return nullptr; |
| 331 | } |
| 332 | if (auto* t = ty->As<ast::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 333 | return Array(t); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 334 | } |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 335 | if (auto* t = ty->As<ast::Atomic>()) { |
| 336 | if (auto* el = Type(t->type())) { |
| 337 | auto* a = builder_->create<sem::Atomic>(const_cast<sem::Type*>(el)); |
| 338 | if (!ValidateAtomic(t, a)) { |
| 339 | return nullptr; |
| 340 | } |
| 341 | return a; |
| 342 | } |
| 343 | return nullptr; |
| 344 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 345 | if (auto* t = ty->As<ast::Pointer>()) { |
| 346 | if (auto* el = Type(t->type())) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 347 | auto access = t->access(); |
| 348 | if (access == ast::kUndefined) { |
| 349 | access = DefaultAccessForStorageClass(t->storage_class()); |
| 350 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 351 | return builder_->create<sem::Pointer>(const_cast<sem::Type*>(el), |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 352 | t->storage_class(), access); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 353 | } |
| 354 | return nullptr; |
| 355 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 356 | if (auto* t = ty->As<ast::Sampler>()) { |
| 357 | return builder_->create<sem::Sampler>(t->kind()); |
| 358 | } |
| 359 | if (auto* t = ty->As<ast::SampledTexture>()) { |
| 360 | if (auto* el = Type(t->type())) { |
| 361 | return builder_->create<sem::SampledTexture>( |
| 362 | t->dim(), const_cast<sem::Type*>(el)); |
| 363 | } |
| 364 | return nullptr; |
| 365 | } |
| 366 | if (auto* t = ty->As<ast::MultisampledTexture>()) { |
| 367 | if (auto* el = Type(t->type())) { |
| 368 | return builder_->create<sem::MultisampledTexture>( |
| 369 | t->dim(), const_cast<sem::Type*>(el)); |
| 370 | } |
| 371 | return nullptr; |
| 372 | } |
| 373 | if (auto* t = ty->As<ast::DepthTexture>()) { |
| 374 | return builder_->create<sem::DepthTexture>(t->dim()); |
| 375 | } |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 376 | if (auto* t = ty->As<ast::DepthMultisampledTexture>()) { |
| 377 | return builder_->create<sem::DepthMultisampledTexture>(t->dim()); |
| 378 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 379 | if (auto* t = ty->As<ast::StorageTexture>()) { |
| 380 | if (auto* el = Type(t->type())) { |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 381 | if (!ValidateStorageTexture(t)) { |
Antonio Maiorano | 6cf7f2e | 2021-05-21 14:32:38 +0000 | [diff] [blame] | 382 | return nullptr; |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 383 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 384 | return builder_->create<sem::StorageTexture>( |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 385 | t->dim(), t->image_format(), t->access(), |
Antonio Maiorano | 6cf7f2e | 2021-05-21 14:32:38 +0000 | [diff] [blame] | 386 | const_cast<sem::Type*>(el)); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 387 | } |
| 388 | return nullptr; |
| 389 | } |
Ryan Harrison | 0aeb77c | 2021-05-06 08:20:52 +0000 | [diff] [blame] | 390 | if (ty->As<ast::ExternalTexture>()) { |
Ben Clayton | 58f93c9 | 2021-05-05 18:00:52 +0000 | [diff] [blame] | 391 | return builder_->create<sem::ExternalTexture>(); |
| 392 | } |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 393 | if (auto* t = ty->As<ast::TypeName>()) { |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 394 | auto it = named_type_info_.find(t->name()); |
| 395 | if (it == named_type_info_.end()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 396 | AddError( |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 397 | "unknown type '" + builder_->Symbols().NameFor(t->name()) + "'", |
| 398 | t->source()); |
| 399 | return nullptr; |
| 400 | } |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 401 | return it->second.sem; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 402 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 403 | TINT_UNREACHABLE(Resolver, diagnostics_) |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 404 | << "Unhandled ast::Type: " << ty->TypeInfo().name; |
| 405 | return nullptr; |
| 406 | }(); |
| 407 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 408 | if (s) { |
| 409 | builder_->Sem().Add(ty, s); |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 410 | } |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 411 | return s; |
| 412 | } |
| 413 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 414 | bool Resolver::ValidateAtomic(const ast::Atomic* a, const sem::Atomic* s) { |
| 415 | // https://gpuweb.github.io/gpuweb/wgsl/#atomic-types |
| 416 | // T must be either u32 or i32. |
| 417 | if (!s->Type()->IsAnyOf<sem::U32, sem::I32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 418 | AddError("atomic only supports i32 or u32 types", a->type()->source()); |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | bool Resolver::ValidateAtomicUses() { |
| 425 | // https://gpuweb.github.io/gpuweb/wgsl/#atomic-types |
| 426 | // Atomic types may only be instantiated by variables in the workgroup storage |
| 427 | // class or by storage buffer variables with a read_write access mode. |
| 428 | for (auto sm : atomic_members_) { |
| 429 | auto* structure = sm.structure; |
| 430 | for (auto usage : structure->StorageClassUsage()) { |
| 431 | if (usage == ast::StorageClass::kWorkgroup) { |
| 432 | continue; |
| 433 | } |
| 434 | if (usage != ast::StorageClass::kStorage) { |
| 435 | // TODO(crbug.com/tint/901): Validate that the access mode is |
| 436 | // read_write. |
| 437 | auto* member = structure->Members()[sm.index]; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 438 | AddError( |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 439 | "atomic types can only be used in storage classes workgroup or " |
| 440 | "storage, but was used by storage class " + |
| 441 | std::string(ast::str(usage)), |
| 442 | member->Declaration()->type()->source()); |
| 443 | // TODO(crbug.com/tint/901): Add note pointing at where the usage came |
| 444 | // from. |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 452 | bool Resolver::ValidateStorageTexture(const ast::StorageTexture* t) { |
| 453 | switch (t->access()) { |
| 454 | case ast::Access::kUndefined: |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 455 | AddError("storage textures must have access control", t->source()); |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 456 | return false; |
| 457 | case ast::Access::kReadWrite: |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 458 | AddError("storage textures only support read-only and write-only access", |
| 459 | t->source()); |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 460 | return false; |
| 461 | |
| 462 | case ast::Access::kRead: |
| 463 | case ast::Access::kWrite: |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | if (!IsValidStorageTextureDimension(t->dim())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 468 | AddError("cube dimensions for storage textures are not supported", |
| 469 | t->source()); |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
| 473 | if (!IsValidStorageTextureImageFormat(t->image_format())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 474 | AddError( |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 475 | "image format must be one of the texel formats specified for storage " |
| 476 | "textues in https://gpuweb.github.io/gpuweb/wgsl/#texel-formats", |
| 477 | t->source()); |
| 478 | return false; |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 483 | Resolver::VariableInfo* Resolver::Variable(ast::Variable* var, |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 484 | VariableKind kind, |
| 485 | uint32_t index /* = 0 */) { |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 486 | if (variable_to_info_.count(var)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 487 | TINT_ICE(Resolver, diagnostics_) |
| 488 | << "Variable " << builder_->Symbols().NameFor(var->symbol()) |
| 489 | << " already resolved"; |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 490 | return nullptr; |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 493 | std::string type_name; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 494 | const sem::Type* storage_type = nullptr; |
| 495 | |
| 496 | // If the variable has a declared type, resolve it. |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 497 | if (auto* ty = var->type()) { |
| 498 | type_name = ty->FriendlyName(builder_->Symbols()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 499 | storage_type = Type(ty); |
| 500 | if (!storage_type) { |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 501 | return nullptr; |
| 502 | } |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 503 | } |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 504 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 505 | std::string rhs_type_name; |
| 506 | const sem::Type* rhs_type = nullptr; |
| 507 | |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 508 | // Does the variable have a constructor? |
| 509 | if (auto* ctor = var->constructor()) { |
| 510 | Mark(var->constructor()); |
| 511 | if (!Expression(var->constructor())) { |
| 512 | return nullptr; |
| 513 | } |
| 514 | |
| 515 | // Fetch the constructor's type |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 516 | rhs_type_name = TypeNameOf(ctor); |
| 517 | rhs_type = TypeOf(ctor); |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 518 | if (!rhs_type) { |
| 519 | return nullptr; |
| 520 | } |
| 521 | |
| 522 | // If the variable has no declared type, infer it from the RHS |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 523 | if (!storage_type) { |
Antonio Maiorano | 9834fef | 2021-06-04 15:28:47 +0000 | [diff] [blame] | 524 | if (!var->is_const() && kind == VariableKind::kGlobal) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 525 | AddError("global var declaration must specify a type", var->source()); |
Antonio Maiorano | 9834fef | 2021-06-04 15:28:47 +0000 | [diff] [blame] | 526 | return nullptr; |
| 527 | } |
| 528 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 529 | type_name = rhs_type_name; |
| 530 | storage_type = rhs_type->UnwrapRef(); // Implicit load of RHS |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 531 | } |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 532 | } else if (var->is_const() && kind != VariableKind::kParameter && |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 533 | !ast::HasDecoration<ast::OverrideDecoration>(var->decorations())) { |
Ben Clayton | b291cfc | 2021-07-19 19:50:52 +0000 | [diff] [blame] | 534 | AddError("let declaration must have an initializer", var->source()); |
| 535 | return nullptr; |
| 536 | } else if (!var->type()) { |
| 537 | AddError( |
| 538 | (kind == VariableKind::kGlobal) |
| 539 | ? "module scope var declaration requires a type and initializer" |
| 540 | : "function scope var declaration requires a type or initializer", |
| 541 | var->source()); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 542 | return nullptr; |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 545 | if (!storage_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 546 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 547 | << "failed to determine storage type for variable '" + |
| 548 | builder_->Symbols().NameFor(var->symbol()) + "'\n" |
| 549 | << "Source: " << var->source(); |
| 550 | return nullptr; |
| 551 | } |
| 552 | |
| 553 | auto storage_class = var->declared_storage_class(); |
| 554 | if (storage_class == ast::StorageClass::kNone) { |
| 555 | if (storage_type->UnwrapRef()->is_handle()) { |
| 556 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
| 557 | // If the store type is a texture type or a sampler type, then the |
| 558 | // variable declaration must not have a storage class decoration. The |
| 559 | // storage class will always be handle. |
| 560 | storage_class = ast::StorageClass::kUniformConstant; |
| 561 | } else if (kind == VariableKind::kLocal && !var->is_const()) { |
| 562 | storage_class = ast::StorageClass::kFunction; |
| 563 | } |
| 564 | } |
| 565 | |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 566 | auto access = var->declared_access(); |
| 567 | if (access == ast::Access::kUndefined) { |
| 568 | access = DefaultAccessForStorageClass(storage_class); |
| 569 | } |
| 570 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 571 | auto* type = storage_type; |
| 572 | if (!var->is_const()) { |
| 573 | // Variable declaration. Unlike `let`, `var` has storage. |
| 574 | // Variables are always of a reference type to the declared storage type. |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 575 | type = |
| 576 | builder_->create<sem::Reference>(storage_type, storage_class, access); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (rhs_type && !ValidateVariableConstructor(var, storage_type, type_name, |
| 580 | rhs_type, rhs_type_name)) { |
| 581 | return nullptr; |
| 582 | } |
| 583 | |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 584 | auto* info = |
| 585 | variable_infos_.Create(var, const_cast<sem::Type*>(type), type_name, |
| 586 | storage_class, access, kind, index); |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 587 | variable_to_info_.emplace(var, info); |
| 588 | |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 589 | return info; |
| 590 | } |
| 591 | |
Ben Clayton | 9545fb7 | 2021-07-06 10:20:19 +0000 | [diff] [blame] | 592 | ast::Access Resolver::DefaultAccessForStorageClass( |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 593 | ast::StorageClass storage_class) { |
| 594 | // https://gpuweb.github.io/gpuweb/wgsl/#storage-class |
| 595 | switch (storage_class) { |
| 596 | case ast::StorageClass::kStorage: |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 597 | case ast::StorageClass::kUniform: |
| 598 | case ast::StorageClass::kUniformConstant: |
| 599 | return ast::Access::kRead; |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | return ast::Access::kReadWrite; |
| 604 | } |
| 605 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 606 | bool Resolver::ValidateVariableConstructor(const ast::Variable* var, |
| 607 | const sem::Type* storage_type, |
| 608 | const std::string& type_name, |
| 609 | const sem::Type* rhs_type, |
| 610 | const std::string& rhs_type_name) { |
| 611 | auto* value_type = rhs_type->UnwrapRef(); // Implicit load of RHS |
| 612 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 613 | // Value type has to match storage type |
Antonio Maiorano | be607c0 | 2021-05-18 15:26:40 +0000 | [diff] [blame] | 614 | if (storage_type != value_type) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 615 | std::string decl = var->is_const() ? "let" : "var"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 616 | AddError("cannot initialize " + decl + " of type '" + type_name + |
| 617 | "' with value of type '" + rhs_type_name + "'", |
| 618 | var->source()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 619 | return false; |
| 620 | } |
| 621 | return true; |
| 622 | } |
| 623 | |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 624 | bool Resolver::GlobalVariable(ast::Variable* var) { |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 625 | if (!ValidateNoDuplicateDefinition(var->symbol(), var->source(), |
| 626 | /* check_global_scope_only */ true)) { |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 630 | auto* info = Variable(var, VariableKind::kGlobal); |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 631 | if (!info) { |
| 632 | return false; |
| 633 | } |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 634 | variable_stack_.set_global(var->symbol(), info); |
| 635 | |
| 636 | if (!var->is_const() && info->storage_class == ast::StorageClass::kNone) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 637 | AddError("global variables must have a storage class", var->source()); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | if (var->is_const() && !(info->storage_class == ast::StorageClass::kNone)) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 641 | AddError("global constants shouldn't have a storage class", var->source()); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | |
| 645 | for (auto* deco : var->decorations()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 646 | Mark(deco); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 647 | |
| 648 | if (auto* override_deco = deco->As<ast::OverrideDecoration>()) { |
| 649 | // Track the constant IDs that are specified in the shader. |
| 650 | if (override_deco->HasValue()) { |
| 651 | constant_ids_.emplace(override_deco->value(), info); |
| 652 | } |
| 653 | } |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 656 | if (!ValidateNoDuplicateDecorations(var->decorations())) { |
| 657 | return false; |
| 658 | } |
| 659 | |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 660 | if (auto bp = var->binding_point()) { |
| 661 | info->binding_point = {bp.group->value(), bp.binding->value()}; |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 664 | if (!ValidateGlobalVariable(info)) { |
| 665 | return false; |
| 666 | } |
| 667 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 668 | if (!ApplyStorageClassUsageToType( |
| 669 | info->storage_class, const_cast<sem::Type*>(info->type->UnwrapRef()), |
| 670 | var->source())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 671 | AddNote("while instantiating variable " + |
| 672 | builder_->Symbols().NameFor(var->symbol()), |
| 673 | var->source()); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 677 | // TODO(bclayton): Call this at the end of resolve on all uniform and storage |
| 678 | // referenced structs |
| 679 | if (!ValidateStorageClassLayout(info)) { |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | bool Resolver::ValidateStorageClassLayout(const sem::Struct* str, |
| 687 | ast::StorageClass sc) { |
| 688 | // https://gpuweb.github.io/gpuweb/wgsl/#storage-class-layout-constraints |
| 689 | |
| 690 | auto is_uniform_struct_or_array = [sc](const sem::Type* ty) { |
| 691 | return sc == ast::StorageClass::kUniform && |
| 692 | ty->IsAnyOf<sem::Array, sem::Struct>(); |
| 693 | }; |
| 694 | |
| 695 | auto is_uniform_struct = [sc](const sem::Type* ty) { |
| 696 | return sc == ast::StorageClass::kUniform && ty->Is<sem::Struct>(); |
| 697 | }; |
| 698 | |
| 699 | auto required_alignment_of = [&](const sem::Type* ty) { |
Ben Clayton | fced350 | 2021-07-22 18:56:54 +0000 | [diff] [blame] | 700 | uint32_t actual_align = ty->Align(); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 701 | uint32_t required_align = actual_align; |
| 702 | if (is_uniform_struct_or_array(ty)) { |
| 703 | required_align = utils::RoundUp(16u, actual_align); |
| 704 | } |
| 705 | return required_align; |
| 706 | }; |
| 707 | |
| 708 | auto member_name_of = [this](const sem::StructMember* sm) { |
| 709 | return builder_->Symbols().NameFor(sm->Declaration()->symbol()); |
| 710 | }; |
| 711 | |
| 712 | auto type_name_of = [this](const sem::StructMember* sm) { |
| 713 | return sm->Declaration()->type()->FriendlyName(builder_->Symbols()); |
| 714 | }; |
| 715 | |
| 716 | // TODO(amaiorano): Output struct and member decorations so that this output |
| 717 | // can be copied verbatim back into source |
| 718 | auto get_struct_layout_string = [&](const sem::Struct* st) -> std::string { |
| 719 | std::stringstream ss; |
| 720 | |
| 721 | if (st->Members().empty()) { |
| 722 | TINT_ICE(Resolver, diagnostics_) << "Validation should have ensured that " |
| 723 | "structs have at least one member"; |
| 724 | return {}; |
| 725 | } |
| 726 | const auto* const last_member = st->Members().back(); |
| 727 | const uint32_t last_member_struct_padding_offset = |
| 728 | last_member->Offset() + last_member->Size(); |
| 729 | |
| 730 | // Compute max widths to align output |
| 731 | const auto offset_w = |
| 732 | static_cast<int>(::log10(last_member_struct_padding_offset)) + 1; |
| 733 | const auto size_w = static_cast<int>(::log10(st->Size())) + 1; |
| 734 | const auto align_w = static_cast<int>(::log10(st->Align())) + 1; |
| 735 | |
| 736 | auto print_struct_begin_line = [&](size_t align, size_t size, |
| 737 | std::string struct_name) { |
| 738 | ss << "/* " << std::setw(offset_w) << " " |
| 739 | << "align(" << std::setw(align_w) << align << ") size(" |
| 740 | << std::setw(size_w) << size << ") */ struct " << struct_name |
| 741 | << " {\n"; |
| 742 | }; |
| 743 | |
| 744 | auto print_struct_end_line = [&]() { |
| 745 | ss << "/* " |
| 746 | << std::setw(offset_w + size_w + align_w) << " " |
| 747 | << "*/ };"; |
| 748 | }; |
| 749 | |
| 750 | auto print_member_line = [&](size_t offset, size_t align, size_t size, |
| 751 | std::string s) { |
| 752 | ss << "/* offset(" << std::setw(offset_w) << offset << ") align(" |
| 753 | << std::setw(align_w) << align << ") size(" << std::setw(size_w) |
| 754 | << size << ") */ " << s << ";\n"; |
| 755 | }; |
| 756 | |
| 757 | print_struct_begin_line(st->Align(), st->Size(), |
| 758 | st->FriendlyName(builder_->Symbols())); |
| 759 | |
| 760 | for (size_t i = 0; i < st->Members().size(); ++i) { |
| 761 | auto* const m = st->Members()[i]; |
| 762 | |
| 763 | // Output field alignment padding, if any |
| 764 | auto* const prev_member = (i == 0) ? nullptr : str->Members()[i - 1]; |
| 765 | if (prev_member) { |
| 766 | uint32_t padding = |
| 767 | m->Offset() - (prev_member->Offset() + prev_member->Size()); |
| 768 | if (padding > 0) { |
| 769 | size_t padding_offset = m->Offset() - padding; |
| 770 | print_member_line(padding_offset, 1, padding, |
| 771 | "// -- implicit field alignment padding --"); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | // Output member |
| 776 | std::string member_name = member_name_of(m); |
| 777 | print_member_line(m->Offset(), m->Align(), m->Size(), |
| 778 | member_name_of(m) + " : " + type_name_of(m)); |
| 779 | } |
| 780 | |
| 781 | // Output struct size padding, if any |
| 782 | uint32_t struct_padding = st->Size() - last_member_struct_padding_offset; |
| 783 | if (struct_padding > 0) { |
| 784 | print_member_line(last_member_struct_padding_offset, 1, struct_padding, |
| 785 | "// -- implicit struct size padding --"); |
| 786 | } |
| 787 | |
| 788 | print_struct_end_line(); |
| 789 | |
| 790 | return ss.str(); |
| 791 | }; |
| 792 | |
| 793 | if (!ast::IsHostShareable(sc)) { |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | for (size_t i = 0; i < str->Members().size(); ++i) { |
| 798 | auto* const m = str->Members()[i]; |
| 799 | uint32_t required_align = required_alignment_of(m->Type()); |
| 800 | |
| 801 | // Validate that member is at a valid byte offset |
| 802 | if (m->Offset() % required_align != 0) { |
| 803 | AddError("the offset of a struct member of type '" + type_name_of(m) + |
| 804 | "' in storage class '" + ast::str(sc) + |
| 805 | "' must be a multiple of " + std::to_string(required_align) + |
| 806 | " bytes, but '" + member_name_of(m) + |
| 807 | "' is currently at offset " + std::to_string(m->Offset()) + |
| 808 | ". Consider setting [[align(" + |
| 809 | std::to_string(required_align) + ")]] on this member", |
| 810 | m->Declaration()->source()); |
| 811 | |
| 812 | AddNote("see layout of struct:\n" + get_struct_layout_string(str), |
| 813 | str->Declaration()->source()); |
| 814 | |
| 815 | if (auto* member_str = m->Type()->As<sem::Struct>()) { |
| 816 | AddNote("and layout of struct member:\n" + |
| 817 | get_struct_layout_string(member_str), |
| 818 | member_str->Declaration()->source()); |
| 819 | } |
| 820 | |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | // For uniform buffers, validate that the number of bytes between the |
| 825 | // previous member of type struct and the current is a multiple of 16 bytes. |
| 826 | auto* const prev_member = (i == 0) ? nullptr : str->Members()[i - 1]; |
| 827 | if (prev_member && is_uniform_struct(prev_member->Type())) { |
| 828 | const uint32_t prev_to_curr_offset = m->Offset() - prev_member->Offset(); |
| 829 | if (prev_to_curr_offset % 16 != 0) { |
| 830 | AddError( |
| 831 | "uniform storage requires that the number of bytes between the " |
| 832 | "start of the previous member of type struct and the current " |
| 833 | "member be a multiple of 16 bytes, but there are currently " + |
| 834 | std::to_string(prev_to_curr_offset) + " bytes between '" + |
| 835 | member_name_of(prev_member) + "' and '" + member_name_of(m) + |
| 836 | "'. Consider setting [[align(16)]] on this member", |
| 837 | m->Declaration()->source()); |
| 838 | |
| 839 | AddNote("see layout of struct:\n" + get_struct_layout_string(str), |
| 840 | str->Declaration()->source()); |
| 841 | |
| 842 | auto* prev_member_str = prev_member->Type()->As<sem::Struct>(); |
| 843 | AddNote("and layout of previous member struct:\n" + |
| 844 | get_struct_layout_string(prev_member_str), |
| 845 | prev_member_str->Declaration()->source()); |
| 846 | return false; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | // For uniform buffer array members, validate that array elements are |
| 851 | // aligned to 16 bytes |
| 852 | if (auto* arr = m->Type()->As<sem::Array>()) { |
| 853 | if (sc == ast::StorageClass::kUniform) { |
| 854 | // We already validated that this array member is itself aligned to 16 |
| 855 | // bytes above, so we only need to validate that stride is a multiple of |
| 856 | // 16 bytes. |
| 857 | if (arr->Stride() % 16 != 0) { |
| 858 | AddError( |
| 859 | "uniform storage requires that array elements be aligned to 16 " |
| 860 | "bytes, but array stride of '" + |
| 861 | member_name_of(m) + "' is currently " + |
| 862 | std::to_string(arr->Stride()) + |
| 863 | ". Consider setting [[stride(" + |
| 864 | std::to_string( |
| 865 | utils::RoundUp(required_align, arr->Stride())) + |
| 866 | ")]] on the array type", |
| 867 | m->Declaration()->type()->source()); |
| 868 | AddNote("see layout of struct:\n" + get_struct_layout_string(str), |
| 869 | str->Declaration()->source()); |
| 870 | return false; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // If member is struct, recurse |
| 876 | if (auto* str_member = m->Type()->As<sem::Struct>()) { |
| 877 | // Cache result of struct + storage class pair |
| 878 | if (valid_struct_storage_layouts_.emplace(str_member, sc).second) { |
| 879 | if (!ValidateStorageClassLayout(str_member, sc)) { |
| 880 | return false; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | bool Resolver::ValidateStorageClassLayout(const VariableInfo* info) { |
| 890 | if (auto* str = info->type->UnwrapRef()->As<sem::Struct>()) { |
| 891 | if (!ValidateStorageClassLayout(str, info->storage_class)) { |
| 892 | AddNote("see declaration of variable", info->declaration->source()); |
| 893 | return false; |
| 894 | } |
| 895 | } |
| 896 | |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 897 | return true; |
| 898 | } |
| 899 | |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 900 | bool Resolver::ValidateGlobalVariable(const VariableInfo* info) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 901 | if (!ValidateNoDuplicateDecorations(info->declaration->decorations())) { |
| 902 | return false; |
| 903 | } |
| 904 | |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 905 | for (auto* deco : info->declaration->decorations()) { |
| 906 | if (info->declaration->is_const()) { |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 907 | if (auto* override_deco = deco->As<ast::OverrideDecoration>()) { |
| 908 | if (override_deco->HasValue()) { |
| 909 | uint32_t id = override_deco->value(); |
| 910 | auto itr = constant_ids_.find(id); |
| 911 | if (itr != constant_ids_.end() && itr->second != info) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 912 | AddError("pipeline constant IDs must be unique", deco->source()); |
| 913 | AddNote("a pipeline constant with an ID of " + std::to_string(id) + |
| 914 | " was previously declared " |
| 915 | "here:", |
| 916 | ast::GetDecoration<ast::OverrideDecoration>( |
| 917 | itr->second->declaration->decorations()) |
| 918 | ->source()); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | if (id > 65535) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 922 | AddError("pipeline constant IDs must be between 0 and 65535", |
| 923 | deco->source()); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 924 | return false; |
| 925 | } |
| 926 | } |
| 927 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 928 | AddError("decoration is not valid for constants", deco->source()); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 929 | return false; |
| 930 | } |
| 931 | } else { |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 932 | bool is_shader_io_decoration = |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 933 | deco->IsAnyOf<ast::BuiltinDecoration, ast::InterpolateDecoration, |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 934 | ast::InvariantDecoration, ast::LocationDecoration>(); |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 935 | bool has_io_storage_class = |
| 936 | info->storage_class == ast::StorageClass::kInput || |
| 937 | info->storage_class == ast::StorageClass::kOutput; |
| 938 | if (!(deco->IsAnyOf<ast::BindingDecoration, ast::GroupDecoration, |
| 939 | ast::InternalDecoration>()) && |
| 940 | (!is_shader_io_decoration || !has_io_storage_class)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 941 | AddError("decoration is not valid for variables", deco->source()); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 942 | return false; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | auto binding_point = info->declaration->binding_point(); |
| 948 | switch (info->storage_class) { |
| 949 | case ast::StorageClass::kUniform: |
| 950 | case ast::StorageClass::kStorage: |
| 951 | case ast::StorageClass::kUniformConstant: { |
| 952 | // https://gpuweb.github.io/gpuweb/wgsl/#resource-interface |
| 953 | // Each resource variable must be declared with both group and binding |
| 954 | // attributes. |
| 955 | if (!binding_point) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 956 | AddError( |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 957 | "resource variables require [[group]] and [[binding]] " |
| 958 | "decorations", |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 959 | info->declaration->source()); |
| 960 | return false; |
| 961 | } |
| 962 | break; |
| 963 | } |
| 964 | default: |
| 965 | if (binding_point.binding || binding_point.group) { |
| 966 | // https://gpuweb.github.io/gpuweb/wgsl/#attribute-binding |
| 967 | // Must only be applied to a resource variable |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 968 | AddError( |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 969 | "non-resource variables must not have [[group]] or [[binding]] " |
| 970 | "decorations", |
| 971 | info->declaration->source()); |
| 972 | return false; |
| 973 | } |
| 974 | } |
| 975 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 976 | // https://gpuweb.github.io/gpuweb/wgsl/#variable-declaration |
| 977 | // The access mode always has a default, and except for variables in the |
| 978 | // storage storage class, must not be written. |
| 979 | if (info->storage_class != ast::StorageClass::kStorage && |
| 980 | info->declaration->declared_access() != ast::Access::kUndefined) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 981 | AddError( |
Sarah | 085dcbb | 2021-07-28 09:59:35 +0000 | [diff] [blame] | 982 | "variables not in <storage> storage class must not declare an access " |
| 983 | "mode", |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 984 | info->declaration->source()); |
| 985 | return false; |
| 986 | } |
| 987 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 988 | switch (info->storage_class) { |
| 989 | case ast::StorageClass::kStorage: { |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 990 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
| 991 | // A variable in the storage storage class is a storage buffer variable. |
| 992 | // Its store type must be a host-shareable structure type with block |
| 993 | // attribute, satisfying the storage class constraints. |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 994 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 995 | auto* str = info->type->UnwrapRef()->As<sem::Struct>(); |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 996 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 997 | if (!str) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 998 | AddError( |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 999 | "variables declared in the <storage> storage class must be of a " |
| 1000 | "structure type", |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1001 | info->declaration->source()); |
| 1002 | return false; |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1003 | } |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (!str->IsBlockDecorated()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1006 | AddError( |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1007 | "structure used as a storage buffer must be declared with the " |
| 1008 | "[[block]] decoration", |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1009 | str->Declaration()->source()); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1010 | if (info->declaration->source().range.begin.line) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1011 | AddNote("structure used as storage buffer here", |
| 1012 | info->declaration->source()); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1013 | } |
| 1014 | return false; |
| 1015 | } |
| 1016 | break; |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1017 | } |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1018 | case ast::StorageClass::kUniform: { |
| 1019 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
| 1020 | // A variable in the uniform storage class is a uniform buffer variable. |
| 1021 | // Its store type must be a host-shareable structure type with block |
| 1022 | // attribute, satisfying the storage class constraints. |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1023 | auto* str = info->type->UnwrapRef()->As<sem::Struct>(); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1024 | if (!str) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1025 | AddError( |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1026 | "variables declared in the <uniform> storage class must be of a " |
| 1027 | "structure type", |
| 1028 | info->declaration->source()); |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | if (!str->IsBlockDecorated()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1033 | AddError( |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1034 | "structure used as a uniform buffer must be declared with the " |
| 1035 | "[[block]] decoration", |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1036 | str->Declaration()->source()); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1037 | if (info->declaration->source().range.begin.line) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1038 | AddNote("structure used as uniform buffer here", |
| 1039 | info->declaration->source()); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1040 | } |
| 1041 | return false; |
| 1042 | } |
Sarah | 52b6a00 | 2021-06-16 19:40:13 +0000 | [diff] [blame] | 1043 | |
| 1044 | for (auto* member : str->Members()) { |
| 1045 | if (auto* arr = member->Type()->As<sem::Array>()) { |
| 1046 | if (arr->IsRuntimeSized()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1047 | AddError( |
Sarah | 52b6a00 | 2021-06-16 19:40:13 +0000 | [diff] [blame] | 1048 | "structure containing a runtime sized array " |
| 1049 | "cannot be used as a uniform buffer", |
| 1050 | info->declaration->source()); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1051 | AddNote("structure is declared here", str->Declaration()->source()); |
Sarah | 52b6a00 | 2021-06-16 19:40:13 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | } |
| 1059 | default: |
| 1060 | break; |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1063 | return ValidateVariable(info); |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1066 | bool Resolver::ValidateVariable(const VariableInfo* info) { |
| 1067 | auto* var = info->declaration; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1068 | auto* storage_type = info->type->UnwrapRef(); |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (!var->is_const() && !IsStorable(storage_type)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1071 | AddError(storage_type->FriendlyName(builder_->Symbols()) + |
| 1072 | " cannot be used as the type of a var", |
| 1073 | var->source()); |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 1074 | return false; |
| 1075 | } |
| 1076 | |
James Price | d12379a | 2021-07-26 22:11:58 +0000 | [diff] [blame] | 1077 | if (var->is_const() && info->kind != VariableKind::kParameter && |
| 1078 | !(storage_type->IsConstructible() || storage_type->Is<sem::Pointer>())) { |
| 1079 | AddError(storage_type->FriendlyName(builder_->Symbols()) + |
| 1080 | " cannot be used as the type of a let", |
| 1081 | var->source()); |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1085 | if (auto* r = storage_type->As<sem::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 1086 | if (r->IsRuntimeSized()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 1087 | AddError("runtime arrays may only appear as the last member of a struct", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1088 | var->source()); |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1089 | return false; |
| 1090 | } |
| 1091 | } |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 1092 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1093 | if (auto* r = storage_type->As<sem::MultisampledTexture>()) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 1094 | if (r->dim() != ast::TextureDimension::k2d) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1095 | AddError("only 2d multisampled textures are supported", var->source()); |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 1096 | return false; |
| 1097 | } |
| 1098 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1099 | if (!r->type()->UnwrapRef()->is_numeric_scalar()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1100 | AddError("texture_multisampled_2d<type>: type must be f32, i32 or u32", |
| 1101 | var->source()); |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 1102 | return false; |
| 1103 | } |
| 1104 | } |
| 1105 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1106 | if (storage_type->is_handle() && |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1107 | var->declared_storage_class() != ast::StorageClass::kNone) { |
| 1108 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1109 | // If the store type is a texture type or a sampler type, then the |
| 1110 | // variable declaration must not have a storage class decoration. The |
| 1111 | // storage class will always be handle. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1112 | AddError("variables of type '" + info->type_name + |
| 1113 | "' must not have a storage class", |
| 1114 | var->source()); |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1115 | return false; |
| 1116 | } |
| 1117 | |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1118 | if (IsValidationEnabled(var->decorations(), |
| 1119 | ast::DisabledValidation::kIgnoreStorageClass) && |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 1120 | (var->declared_storage_class() == ast::StorageClass::kInput || |
| 1121 | var->declared_storage_class() == ast::StorageClass::kOutput)) { |
| 1122 | AddError("invalid use of input/output storage class", var->source()); |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 1126 | // https://gpuweb.github.io/gpuweb/wgsl/#atomic-types |
| 1127 | // Atomic types may only be instantiated by variables in the workgroup storage |
| 1128 | // class or by storage buffer variables with a read_write access mode. |
Ben Clayton | f2ec7f3 | 2021-06-29 11:53:15 +0000 | [diff] [blame] | 1129 | if (info->type->UnwrapRef()->Is<sem::Atomic>() && |
| 1130 | info->storage_class != ast::StorageClass::kWorkgroup) { |
| 1131 | // Storage buffers require a structure, so just check for workgroup |
| 1132 | // storage here. |
| 1133 | AddError("atomic var requires workgroup storage", |
| 1134 | info->declaration->type()->source()); |
| 1135 | return false; |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1138 | return true; |
| 1139 | } |
| 1140 | |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1141 | bool Resolver::ValidateFunctionParameter(const ast::Function* func, |
| 1142 | const VariableInfo* info) { |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1143 | if (!ValidateVariable(info)) { |
| 1144 | return false; |
| 1145 | } |
Sarah | ac5dbd2 | 2021-06-28 14:05:25 +0000 | [diff] [blame] | 1146 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1147 | for (auto* deco : info->declaration->decorations()) { |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1148 | if (!func->IsEntryPoint() && !deco->Is<ast::InternalDecoration>()) { |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1149 | AddError( |
| 1150 | "decoration is not valid for non-entry point function parameters", |
| 1151 | deco->source()); |
James Price | c3f1908 | 2021-06-24 16:20:16 +0000 | [diff] [blame] | 1152 | return false; |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 1153 | } else if (!deco->IsAnyOf<ast::BuiltinDecoration, ast::InvariantDecoration, |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1154 | ast::LocationDecoration, |
| 1155 | ast::InterpolateDecoration, |
James Price | c3f1908 | 2021-06-24 16:20:16 +0000 | [diff] [blame] | 1156 | ast::InternalDecoration>() && |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1157 | (IsValidationEnabled( |
| 1158 | info->declaration->decorations(), |
| 1159 | ast::DisabledValidation::kEntryPointParameter) && |
| 1160 | IsValidationEnabled( |
| 1161 | info->declaration->decorations(), |
Ryan Harrison | 2f258d1 | 2021-07-07 20:41:00 +0000 | [diff] [blame] | 1162 | ast::DisabledValidation:: |
| 1163 | kIgnoreConstructibleFunctionParameter))) { |
James Price | c3f1908 | 2021-06-24 16:20:16 +0000 | [diff] [blame] | 1164 | AddError("decoration is not valid for function parameters", |
| 1165 | deco->source()); |
| 1166 | return false; |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1169 | |
| 1170 | if (auto* ref = info->type->As<sem::Pointer>()) { |
| 1171 | auto sc = ref->StorageClass(); |
| 1172 | if (!(sc == ast::StorageClass::kFunction || |
| 1173 | sc == ast::StorageClass::kPrivate || |
| 1174 | sc == ast::StorageClass::kWorkgroup)) { |
| 1175 | std::stringstream ss; |
| 1176 | ss << "function parameter of pointer type cannot be in '" << sc |
| 1177 | << "' storage class"; |
| 1178 | AddError(ss.str(), info->declaration->source()); |
| 1179 | return false; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | if (IsPlain(info->type)) { |
Antonio Maiorano | 68a6dd0 | 2021-07-20 18:28:31 +0000 | [diff] [blame] | 1184 | if (!info->type->IsConstructible() && |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1185 | IsValidationEnabled( |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1186 | info->declaration->decorations(), |
Ryan Harrison | 2f258d1 | 2021-07-07 20:41:00 +0000 | [diff] [blame] | 1187 | ast::DisabledValidation::kIgnoreConstructibleFunctionParameter)) { |
| 1188 | AddError("store type of function parameter must be a constructible type", |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1189 | info->declaration->source()); |
| 1190 | return false; |
| 1191 | } |
| 1192 | } else if (!info->type->IsAnyOf<sem::Texture, sem::Sampler, sem::Pointer>()) { |
| 1193 | AddError("store type of function parameter cannot be " + |
| 1194 | info->type->FriendlyName(builder_->Symbols()), |
| 1195 | info->declaration->source()); |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1199 | return true; |
| 1200 | } |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 1201 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1202 | bool Resolver::ValidateBuiltinDecoration(const ast::BuiltinDecoration* deco, |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1203 | const sem::Type* storage_type, |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1204 | const bool is_input, |
| 1205 | const bool is_struct_member) { |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1206 | auto* type = storage_type->UnwrapRef(); |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1207 | const auto stage = current_function_ |
| 1208 | ? current_function_->declaration->pipeline_stage() |
| 1209 | : ast::PipelineStage::kNone; |
| 1210 | std::stringstream stage_name; |
| 1211 | stage_name << stage; |
| 1212 | bool is_stage_mismatch = false; |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1213 | bool is_output = !is_input; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1214 | switch (deco->value()) { |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1215 | case ast::Builtin::kPosition: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1216 | if (stage != ast::PipelineStage::kNone && |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1217 | !((is_input && stage == ast::PipelineStage::kFragment) || |
| 1218 | (is_output && stage == ast::PipelineStage::kVertex))) { |
| 1219 | is_stage_mismatch = true; |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1220 | } |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 1221 | if (!(type->is_float_vector() && type->As<sem::Vector>()->Width() == 4)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1222 | AddError("store type of " + deco_to_str(deco) + " must be 'vec4<f32>'", |
| 1223 | deco->source()); |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1224 | return false; |
| 1225 | } |
| 1226 | break; |
| 1227 | case ast::Builtin::kGlobalInvocationId: |
| 1228 | case ast::Builtin::kLocalInvocationId: |
| 1229 | case ast::Builtin::kWorkgroupId: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1230 | if (stage != ast::PipelineStage::kNone && |
| 1231 | !(stage == ast::PipelineStage::kCompute && is_input)) { |
| 1232 | is_stage_mismatch = true; |
| 1233 | } |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1234 | if (!(type->is_unsigned_integer_vector() && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 1235 | type->As<sem::Vector>()->Width() == 3)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1236 | AddError("store type of " + deco_to_str(deco) + " must be 'vec3<u32>'", |
| 1237 | deco->source()); |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1238 | return false; |
| 1239 | } |
| 1240 | break; |
| 1241 | case ast::Builtin::kFragDepth: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1242 | if (stage != ast::PipelineStage::kNone && |
| 1243 | !(stage == ast::PipelineStage::kFragment && !is_input)) { |
| 1244 | is_stage_mismatch = true; |
| 1245 | } |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1246 | if (!type->Is<sem::F32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1247 | AddError("store type of " + deco_to_str(deco) + " must be 'f32'", |
| 1248 | deco->source()); |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | break; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1252 | case ast::Builtin::kFrontFacing: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1253 | if (stage != ast::PipelineStage::kNone && |
| 1254 | !(stage == ast::PipelineStage::kFragment && is_input)) { |
| 1255 | is_stage_mismatch = true; |
| 1256 | } |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1257 | if (!type->Is<sem::Bool>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1258 | AddError("store type of " + deco_to_str(deco) + " must be 'bool'", |
| 1259 | deco->source()); |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| 1262 | break; |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1263 | case ast::Builtin::kLocalInvocationIndex: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1264 | if (stage != ast::PipelineStage::kNone && |
| 1265 | !(stage == ast::PipelineStage::kCompute && is_input)) { |
| 1266 | is_stage_mismatch = true; |
| 1267 | } |
| 1268 | if (!type->Is<sem::U32>()) { |
| 1269 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
| 1270 | deco->source()); |
| 1271 | return false; |
| 1272 | } |
| 1273 | break; |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1274 | case ast::Builtin::kVertexIndex: |
| 1275 | case ast::Builtin::kInstanceIndex: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1276 | if (stage != ast::PipelineStage::kNone && |
| 1277 | !(stage == ast::PipelineStage::kVertex && is_input)) { |
| 1278 | is_stage_mismatch = true; |
| 1279 | } |
| 1280 | if (!type->Is<sem::U32>()) { |
| 1281 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
| 1282 | deco->source()); |
| 1283 | return false; |
| 1284 | } |
| 1285 | break; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1286 | case ast::Builtin::kSampleMask: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1287 | if (stage != ast::PipelineStage::kNone && |
| 1288 | !(stage == ast::PipelineStage::kFragment)) { |
| 1289 | is_stage_mismatch = true; |
| 1290 | } |
| 1291 | if (!type->Is<sem::U32>()) { |
| 1292 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
| 1293 | deco->source()); |
| 1294 | return false; |
| 1295 | } |
| 1296 | break; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1297 | case ast::Builtin::kSampleIndex: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1298 | if (stage != ast::PipelineStage::kNone && |
| 1299 | !(stage == ast::PipelineStage::kFragment && is_input)) { |
| 1300 | is_stage_mismatch = true; |
| 1301 | } |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1302 | if (!type->Is<sem::U32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1303 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
| 1304 | deco->source()); |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1305 | return false; |
| 1306 | } |
| 1307 | break; |
James Price | c41d940 | 2021-07-19 21:32:27 +0000 | [diff] [blame] | 1308 | case ast::Builtin::kNumWorkgroups: |
| 1309 | // TODO(crbug.com/tint/752): Backend support (needs extra work for HLSL). |
| 1310 | AddError("num_workgroups builtin is not yet implemented", deco->source()); |
| 1311 | return false; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1312 | default: |
| 1313 | break; |
| 1314 | } |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1315 | |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1316 | // ignore builtin attribute on struct members to facillate data movement |
| 1317 | // between stages |
| 1318 | if (!is_struct_member) { |
| 1319 | if (is_stage_mismatch) { |
| 1320 | AddError(deco_to_str(deco) + " cannot be used in " + |
| 1321 | (is_input ? "input of " : "output of ") + stage_name.str() + |
| 1322 | " pipeline stage", |
| 1323 | deco->source()); |
| 1324 | return false; |
| 1325 | } |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1328 | return true; |
Antonio Maiorano | 09356cc | 2021-04-06 14:07:07 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 1331 | bool Resolver::ValidateInterpolateDecoration( |
| 1332 | const ast::InterpolateDecoration* deco, |
| 1333 | const sem::Type* storage_type) { |
| 1334 | auto* type = storage_type->UnwrapRef(); |
| 1335 | |
| 1336 | if (!type->is_float_scalar_or_vector()) { |
| 1337 | AddError( |
| 1338 | "store type of interpolate attribute must be floating point scalar or " |
| 1339 | "vector", |
| 1340 | deco->source()); |
| 1341 | return false; |
| 1342 | } |
| 1343 | |
| 1344 | if (deco->type() == ast::InterpolationType::kFlat && |
| 1345 | deco->sampling() != ast::InterpolationSampling::kNone) { |
| 1346 | AddError("flat interpolation attribute must not have a sampling parameter", |
| 1347 | deco->source()); |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1354 | bool Resolver::ValidateFunction(const ast::Function* func, |
| 1355 | const FunctionInfo* info) { |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 1356 | if (!ValidateNoDuplicateDefinition(func->symbol(), func->source(), |
| 1357 | /* check_global_scope_only */ true)) { |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1358 | return false; |
| 1359 | } |
| 1360 | |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1361 | auto workgroup_deco_count = 0; |
| 1362 | for (auto* deco : func->decorations()) { |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1363 | if (deco->Is<ast::WorkgroupDecoration>()) { |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1364 | workgroup_deco_count++; |
| 1365 | if (func->pipeline_stage() != ast::PipelineStage::kCompute) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1366 | AddError( |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1367 | "the workgroup_size attribute is only valid for compute stages", |
| 1368 | deco->source()); |
| 1369 | return false; |
| 1370 | } |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1371 | } else if (!deco->IsAnyOf<ast::StageDecoration, |
| 1372 | ast::InternalDecoration>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1373 | AddError("decoration is not valid for functions", deco->source()); |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1374 | return false; |
| 1375 | } |
| 1376 | } |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1377 | |
Ben Clayton | 725159c | 2021-07-17 17:45:25 +0000 | [diff] [blame] | 1378 | if (func->params().size() > 255) { |
| 1379 | AddError("functions may declare at most 255 parameters", func->source()); |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1383 | for (auto* param : func->params()) { |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1384 | if (!ValidateFunctionParameter(func, variable_to_info_.at(param))) { |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1385 | return false; |
| 1386 | } |
| 1387 | } |
| 1388 | |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1389 | if (!info->return_type->Is<sem::Void>()) { |
Antonio Maiorano | 68a6dd0 | 2021-07-20 18:28:31 +0000 | [diff] [blame] | 1390 | if (!info->return_type->IsConstructible()) { |
Ryan Harrison | 2f258d1 | 2021-07-07 20:41:00 +0000 | [diff] [blame] | 1391 | AddError("function return type must be a constructible type", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1392 | func->return_type()->source()); |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 1393 | return false; |
| 1394 | } |
| 1395 | |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1396 | if (func->body()) { |
| 1397 | if (!func->get_last_statement() || |
| 1398 | !func->get_last_statement()->Is<ast::ReturnStatement>()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 1399 | AddError("non-void function must end with a return statement", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1400 | func->source()); |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1401 | return false; |
| 1402 | } |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1403 | } else if (IsValidationEnabled( |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 1404 | func->decorations(), |
| 1405 | ast::DisabledValidation::kFunctionHasNoBody)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1406 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1407 | << "Function " << builder_->Symbols().NameFor(func->symbol()) |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 1408 | << " has no body"; |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | for (auto* deco : func->return_type_decorations()) { |
James Price | 1b1d963 | 2021-06-28 09:53:37 +0000 | [diff] [blame] | 1412 | if (!func->IsEntryPoint()) { |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1413 | AddError( |
| 1414 | "decoration is not valid for non-entry point function return types", |
| 1415 | deco->source()); |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1416 | return false; |
| 1417 | } |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1418 | if (!deco->IsAnyOf<ast::BuiltinDecoration, ast::InternalDecoration, |
| 1419 | ast::LocationDecoration, ast::InterpolateDecoration, |
| 1420 | ast::InvariantDecoration>() && |
| 1421 | (IsValidationEnabled(info->declaration->decorations(), |
| 1422 | ast::DisabledValidation::kEntryPointParameter) && |
| 1423 | IsValidationEnabled(info->declaration->decorations(), |
| 1424 | ast::DisabledValidation:: |
| 1425 | kIgnoreConstructibleFunctionParameter))) { |
James Price | 1b1d963 | 2021-06-28 09:53:37 +0000 | [diff] [blame] | 1426 | AddError("decoration is not valid for entry point return types", |
| 1427 | deco->source()); |
| 1428 | return false; |
| 1429 | } |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1433 | if (func->IsEntryPoint()) { |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1434 | if (!ValidateEntryPoint(func, info)) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1435 | return false; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | return true; |
| 1440 | } |
| 1441 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1442 | bool Resolver::ValidateEntryPoint(const ast::Function* func, |
| 1443 | const FunctionInfo* info) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1444 | // Use a lambda to validate the entry point decorations for a type. |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1445 | // Persistent state is used to track which builtins and locations have |
| 1446 | // already been seen, in order to catch conflicts. |
| 1447 | // TODO(jrprice): This state could be stored in FunctionInfo instead, and |
| 1448 | // then passed to sem::Function since it would be useful there too. |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1449 | std::unordered_set<ast::Builtin> builtins; |
| 1450 | std::unordered_set<uint32_t> locations; |
| 1451 | enum class ParamOrRetType { |
| 1452 | kParameter, |
| 1453 | kReturnType, |
| 1454 | }; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1455 | |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1456 | // Inner lambda that is applied to a type and all of its members. |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1457 | auto validate_entry_point_decorations_inner = [&](const ast::DecorationList& |
| 1458 | decos, |
| 1459 | sem::Type* ty, |
| 1460 | Source source, |
| 1461 | ParamOrRetType param_or_ret, |
| 1462 | bool is_struct_member) { |
| 1463 | // Scan decorations for pipeline IO attributes. |
| 1464 | // Check for overlap with attributes that have been seen previously. |
| 1465 | ast::Decoration* pipeline_io_attribute = nullptr; |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 1466 | ast::InvariantDecoration* invariant_attribute = nullptr; |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1467 | for (auto* deco : decos) { |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1468 | auto is_invalid_compute_shader_decoration = false; |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1469 | if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { |
| 1470 | if (pipeline_io_attribute) { |
| 1471 | AddError("multiple entry point IO attributes", deco->source()); |
| 1472 | AddNote("previously consumed " + deco_to_str(pipeline_io_attribute), |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1473 | pipeline_io_attribute->source()); |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1474 | return false; |
| 1475 | } |
| 1476 | pipeline_io_attribute = deco; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1477 | |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1478 | if (builtins.count(builtin->value())) { |
| 1479 | AddError(deco_to_str(builtin) + |
| 1480 | " attribute appears multiple times as pipeline " + |
| 1481 | (param_or_ret == ParamOrRetType::kParameter ? "input" |
| 1482 | : "output"), |
| 1483 | func->source()); |
| 1484 | return false; |
| 1485 | } |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1486 | |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1487 | if (!ValidateBuiltinDecoration( |
| 1488 | builtin, ty, |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1489 | /* is_input */ param_or_ret == ParamOrRetType::kParameter, |
| 1490 | /* is_struct_member */ is_struct_member)) { |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1491 | return false; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1492 | } |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1493 | builtins.emplace(builtin->value()); |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1494 | } else if (auto* location = deco->As<ast::LocationDecoration>()) { |
| 1495 | if (pipeline_io_attribute) { |
| 1496 | AddError("multiple entry point IO attributes", deco->source()); |
| 1497 | AddNote("previously consumed " + deco_to_str(pipeline_io_attribute), |
| 1498 | pipeline_io_attribute->source()); |
| 1499 | return false; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1500 | } |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1501 | pipeline_io_attribute = deco; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1502 | |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1503 | if (locations.count(location->value())) { |
| 1504 | AddError(deco_to_str(location) + |
| 1505 | " attribute appears multiple times as pipeline " + |
| 1506 | (param_or_ret == ParamOrRetType::kParameter ? "input" |
| 1507 | : "output"), |
| 1508 | func->source()); |
| 1509 | return false; |
| 1510 | } |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1511 | |
| 1512 | if (func->pipeline_stage() == ast::PipelineStage::kCompute) { |
| 1513 | is_invalid_compute_shader_decoration = true; |
| 1514 | } |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1515 | locations.emplace(location->value()); |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1516 | } else if (auto* interpolate = deco->As<ast::InterpolateDecoration>()) { |
| 1517 | if (func->pipeline_stage() == ast::PipelineStage::kCompute) { |
| 1518 | is_invalid_compute_shader_decoration = true; |
| 1519 | } else if (!ValidateInterpolateDecoration(interpolate, ty)) { |
| 1520 | return false; |
| 1521 | } |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 1522 | } else if (auto* invariant = deco->As<ast::InvariantDecoration>()) { |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1523 | if (func->pipeline_stage() == ast::PipelineStage::kCompute) { |
| 1524 | is_invalid_compute_shader_decoration = true; |
| 1525 | } |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 1526 | invariant_attribute = invariant; |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1527 | } |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1528 | if (is_invalid_compute_shader_decoration) { |
| 1529 | std::string input_or_output = |
| 1530 | param_or_ret == ParamOrRetType::kParameter ? "inputs" : "output"; |
| 1531 | AddError( |
| 1532 | "decoration is not valid for compute shader " + input_or_output, |
| 1533 | deco->source()); |
| 1534 | return false; |
| 1535 | } |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | // Check that we saw a pipeline IO attribute iff we need one. |
| 1539 | if (ty->Is<sem::Struct>()) { |
| 1540 | if (pipeline_io_attribute) { |
| 1541 | AddError("entry point IO attributes must not be used on structure " + |
| 1542 | std::string(param_or_ret == ParamOrRetType::kParameter |
| 1543 | ? "parameters" |
| 1544 | : "return types"), |
| 1545 | pipeline_io_attribute->source()); |
| 1546 | return false; |
| 1547 | } |
| 1548 | } else if (IsValidationEnabled( |
| 1549 | decos, ast::DisabledValidation::kEntryPointParameter)) { |
| 1550 | if (!pipeline_io_attribute) { |
| 1551 | std::string err = "missing entry point IO attribute"; |
| 1552 | if (!is_struct_member) { |
| 1553 | err += |
| 1554 | (param_or_ret == ParamOrRetType::kParameter ? " on parameter" |
| 1555 | : " on return type"); |
| 1556 | } |
| 1557 | AddError(err, source); |
| 1558 | return false; |
| 1559 | } |
| 1560 | |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 1561 | auto* builtin = pipeline_io_attribute->As<ast::BuiltinDecoration>(); |
| 1562 | if (invariant_attribute && |
| 1563 | !(builtin && builtin->value() == ast::Builtin::kPosition)) { |
| 1564 | AddError( |
| 1565 | "invariant attribute must only be applied to a position builtin", |
| 1566 | invariant_attribute->source()); |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1570 | // Check that all user defined attributes are numeric scalars, vectors |
| 1571 | // of numeric scalars. |
| 1572 | // Testing for being a struct is handled by the if portion above. |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 1573 | if (!builtin) { |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1574 | if (!ty->is_numeric_scalar_or_vector()) { |
| 1575 | AddError( |
| 1576 | "User defined entry point IO types must be a numeric scalar, " |
| 1577 | "a numeric vector, or a structure", |
| 1578 | source); |
| 1579 | return false; |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | return true; |
| 1585 | }; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Outer lambda for validating the entry point decorations for a type. |
| 1588 | auto validate_entry_point_decorations = [&](const ast::DecorationList& decos, |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1589 | sem::Type* ty, Source source, |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1590 | ParamOrRetType param_or_ret) { |
| 1591 | // Validate the decorations for the type. |
| 1592 | if (!validate_entry_point_decorations_inner(decos, ty, source, param_or_ret, |
| 1593 | false)) { |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 1597 | if (auto* str = ty->As<sem::Struct>()) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1598 | // Validate the decorations for each struct members, and also check for |
| 1599 | // invalid member types. |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1600 | for (auto* member : str->Members()) { |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 1601 | if (member->Type()->Is<sem::Struct>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1602 | AddError("entry point IO types cannot contain nested structures", |
| 1603 | member->Declaration()->source()); |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1604 | AddNote("while analysing entry point '" + |
| 1605 | builder_->Symbols().NameFor(func->symbol()) + "'", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1606 | func->source()); |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1607 | return false; |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | if (auto* arr = member->Type()->As<sem::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 1611 | if (arr->IsRuntimeSized()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1612 | AddError("entry point IO types cannot contain runtime sized arrays", |
| 1613 | member->Declaration()->source()); |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1614 | AddNote("while analysing entry point '" + |
| 1615 | builder_->Symbols().NameFor(func->symbol()) + "'", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1616 | func->source()); |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1617 | return false; |
| 1618 | } |
| 1619 | } |
| 1620 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1621 | if (!validate_entry_point_decorations_inner( |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 1622 | member->Declaration()->decorations(), member->Type(), |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1623 | member->Declaration()->source(), param_or_ret, true)) { |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1624 | AddNote("while analysing entry point '" + |
| 1625 | builder_->Symbols().NameFor(func->symbol()) + "'", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1626 | func->source()); |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1627 | return false; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return true; |
| 1633 | }; |
| 1634 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1635 | for (auto* param : info->parameters) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1636 | if (!validate_entry_point_decorations( |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1637 | param->declaration->decorations(), param->type, |
| 1638 | param->declaration->source(), ParamOrRetType::kParameter)) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | } |
| 1642 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1643 | // Clear IO sets after parameter validation. Builtin and location attributes |
| 1644 | // in return types should be validated independently from those used in |
| 1645 | // parameters. |
| 1646 | builtins.clear(); |
| 1647 | locations.clear(); |
| 1648 | |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1649 | if (!info->return_type->Is<sem::Void>()) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1650 | if (!validate_entry_point_decorations(func->return_type_decorations(), |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1651 | info->return_type, func->source(), |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1652 | ParamOrRetType::kReturnType)) { |
| 1653 | return false; |
| 1654 | } |
| 1655 | } |
| 1656 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1657 | if (func->pipeline_stage() == ast::PipelineStage::kVertex && |
| 1658 | builtins.count(ast::Builtin::kPosition) == 0) { |
| 1659 | // Check module-scope variables, as the SPIR-V sanitizer generates these. |
| 1660 | bool found = false; |
| 1661 | for (auto* var : info->referenced_module_vars) { |
| 1662 | if (auto* builtin = ast::GetDecoration<ast::BuiltinDecoration>( |
| 1663 | var->declaration->decorations())) { |
| 1664 | if (builtin->value() == ast::Builtin::kPosition) { |
| 1665 | found = true; |
| 1666 | break; |
| 1667 | } |
| 1668 | } |
| 1669 | } |
Ben Clayton | 677437d | 2021-06-16 09:50:11 +0000 | [diff] [blame] | 1670 | if (!found) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1671 | AddError( |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1672 | "a vertex shader must include the 'position' builtin in its return " |
| 1673 | "type", |
| 1674 | func->source()); |
| 1675 | return false; |
| 1676 | } |
| 1677 | } |
| 1678 | |
Sarah | e6cb51e | 2021-06-29 18:39:44 +0000 | [diff] [blame] | 1679 | if (func->pipeline_stage() == ast::PipelineStage::kCompute) { |
| 1680 | if (!ast::HasDecoration<ast::WorkgroupDecoration>(func->decorations())) { |
| 1681 | AddError( |
| 1682 | "a compute shader must include 'workgroup_size' in its " |
| 1683 | "attributes", |
| 1684 | func->source()); |
| 1685 | return false; |
| 1686 | } |
| 1687 | } |
| 1688 | |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1689 | // Validate there are no resource variable binding collisions |
| 1690 | std::unordered_map<sem::BindingPoint, const ast::Variable*> binding_points; |
| 1691 | for (auto* var_info : info->referenced_module_vars) { |
| 1692 | if (!var_info->declaration->binding_point()) { |
| 1693 | continue; |
| 1694 | } |
| 1695 | auto bp = var_info->binding_point; |
| 1696 | auto res = binding_points.emplace(bp, var_info->declaration); |
Ben Clayton | 3103a1f | 2021-05-12 13:30:51 +0000 | [diff] [blame] | 1697 | if (!res.second && |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1698 | IsValidationEnabled(var_info->declaration->decorations(), |
| 1699 | ast::DisabledValidation::kBindingPointCollision) && |
| 1700 | IsValidationEnabled(res.first->second->decorations(), |
| 1701 | ast::DisabledValidation::kBindingPointCollision)) { |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1702 | // https://gpuweb.github.io/gpuweb/wgsl/#resource-interface |
| 1703 | // Bindings must not alias within a shader stage: two different |
| 1704 | // variables in the resource interface of a given shader must not have |
| 1705 | // the same group and binding values, when considered as a pair of |
| 1706 | // values. |
| 1707 | auto func_name = builder_->Symbols().NameFor(info->declaration->symbol()); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1708 | AddError("entry point '" + func_name + |
| 1709 | "' references multiple variables that use the " |
| 1710 | "same resource binding [[group(" + |
| 1711 | std::to_string(bp.group) + "), binding(" + |
| 1712 | std::to_string(bp.binding) + ")]]", |
| 1713 | var_info->declaration->source()); |
| 1714 | AddNote("first resource binding usage declared here", |
| 1715 | res.first->second->source()); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1716 | return false; |
| 1717 | } |
| 1718 | } |
| 1719 | |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1720 | return true; |
| 1721 | } |
| 1722 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1723 | bool Resolver::Function(ast::Function* func) { |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1724 | auto* info = function_infos_.Create<FunctionInfo>(func); |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 1725 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 1726 | if (func->IsEntryPoint()) { |
| 1727 | entry_points_.emplace_back(info); |
| 1728 | } |
| 1729 | |
Ben Clayton | b350500 | 2021-05-19 16:11:04 +0000 | [diff] [blame] | 1730 | TINT_SCOPED_ASSIGNMENT(current_function_, info); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1731 | |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 1732 | variable_stack_.push_scope(); |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 1733 | uint32_t parameter_index = 0; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 1734 | for (auto* param : func->params()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1735 | Mark(param); |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 1736 | auto* param_info = |
| 1737 | Variable(param, VariableKind::kParameter, parameter_index++); |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 1738 | if (!param_info) { |
| 1739 | return false; |
| 1740 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1741 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1742 | for (auto* deco : param->decorations()) { |
| 1743 | Mark(deco); |
| 1744 | } |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 1745 | if (!ValidateNoDuplicateDecorations(param->decorations())) { |
| 1746 | return false; |
| 1747 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1748 | |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 1749 | variable_stack_.set(param->symbol(), param_info); |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1750 | info->parameters.emplace_back(param_info); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1751 | |
Ben Clayton | c87ff9c | 2021-03-31 17:23:07 +0000 | [diff] [blame] | 1752 | if (!ApplyStorageClassUsageToType(param->declared_storage_class(), |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1753 | param_info->type, param->source())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1754 | AddNote("while instantiating parameter " + |
| 1755 | builder_->Symbols().NameFor(param->symbol()), |
| 1756 | param->source()); |
Ben Clayton | c87ff9c | 2021-03-31 17:23:07 +0000 | [diff] [blame] | 1757 | return false; |
| 1758 | } |
| 1759 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1760 | if (auto* str = param_info->type->As<sem::Struct>()) { |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1761 | switch (func->pipeline_stage()) { |
| 1762 | case ast::PipelineStage::kVertex: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1763 | str->AddUsage(sem::PipelineStageUsage::kVertexInput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1764 | break; |
| 1765 | case ast::PipelineStage::kFragment: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1766 | str->AddUsage(sem::PipelineStageUsage::kFragmentInput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1767 | break; |
| 1768 | case ast::PipelineStage::kCompute: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1769 | str->AddUsage(sem::PipelineStageUsage::kComputeInput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1770 | break; |
| 1771 | case ast::PipelineStage::kNone: |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | } |
| 1776 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1777 | if (auto* ty = func->return_type()) { |
| 1778 | info->return_type = Type(ty); |
| 1779 | info->return_type_name = ty->FriendlyName(builder_->Symbols()); |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1780 | if (!info->return_type) { |
| 1781 | return false; |
| 1782 | } |
| 1783 | } else { |
| 1784 | info->return_type = builder_->create<sem::Void>(); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1785 | info->return_type_name = |
| 1786 | info->return_type->FriendlyName(builder_->Symbols()); |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1789 | if (auto* str = info->return_type->As<sem::Struct>()) { |
James Price | 5c523f3 | 2021-03-31 15:44:07 +0000 | [diff] [blame] | 1790 | if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, str, |
| 1791 | func->source())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1792 | AddNote("while instantiating return type for " + |
| 1793 | builder_->Symbols().NameFor(func->symbol()), |
| 1794 | func->source()); |
James Price | 5c523f3 | 2021-03-31 15:44:07 +0000 | [diff] [blame] | 1795 | return false; |
| 1796 | } |
| 1797 | |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1798 | switch (func->pipeline_stage()) { |
| 1799 | case ast::PipelineStage::kVertex: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1800 | str->AddUsage(sem::PipelineStageUsage::kVertexOutput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | case ast::PipelineStage::kFragment: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1803 | str->AddUsage(sem::PipelineStageUsage::kFragmentOutput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1804 | break; |
| 1805 | case ast::PipelineStage::kCompute: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1806 | str->AddUsage(sem::PipelineStageUsage::kComputeOutput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | case ast::PipelineStage::kNone: |
| 1809 | break; |
| 1810 | } |
dan sinclair | 011aed9 | 2020-06-22 20:18:17 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1813 | if (func->body()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1814 | Mark(func->body()); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 1815 | if (current_compound_statement_) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1816 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 1817 | << "Resolver::Function() called with a current compound statement"; |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 1818 | return false; |
| 1819 | } |
Ben Clayton | 22d891c | 2021-05-20 15:17:08 +0000 | [diff] [blame] | 1820 | auto* sem_block = builder_->create<sem::FunctionBlockStatement>(func); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 1821 | builder_->Sem().Add(func->body(), sem_block); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 1822 | if (!Scope(sem_block, [&] { return Statements(func->body()->list()); })) { |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1823 | return false; |
| 1824 | } |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 1825 | } |
| 1826 | variable_stack_.pop_scope(); |
| 1827 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1828 | for (auto* deco : func->decorations()) { |
| 1829 | Mark(deco); |
| 1830 | } |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 1831 | if (!ValidateNoDuplicateDecorations(func->decorations())) { |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1835 | for (auto* deco : func->return_type_decorations()) { |
| 1836 | Mark(deco); |
| 1837 | } |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 1838 | if (!ValidateNoDuplicateDecorations(func->return_type_decorations())) { |
| 1839 | return false; |
| 1840 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1841 | |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 1842 | // Set work-group size defaults. |
| 1843 | for (int i = 0; i < 3; i++) { |
| 1844 | info->workgroup_size[i].value = 1; |
| 1845 | info->workgroup_size[i].overridable_const = nullptr; |
| 1846 | } |
| 1847 | |
| 1848 | if (auto* workgroup = |
| 1849 | ast::GetDecoration<ast::WorkgroupDecoration>(func->decorations())) { |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1850 | auto values = workgroup->values(); |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1851 | auto is_i32 = false; |
| 1852 | auto is_less_than_one = true; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1853 | for (int i = 0; i < 3; i++) { |
| 1854 | // Each argument to this decoration can either be a literal, an |
| 1855 | // identifier for a module-scope constants, or nullptr if not specified. |
| 1856 | |
| 1857 | if (!values[i]) { |
| 1858 | // Not specified, just use the default. |
| 1859 | continue; |
| 1860 | } |
| 1861 | |
| 1862 | Mark(values[i]); |
| 1863 | |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1864 | uint32_t value = 0; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1865 | if (auto* ident = values[i]->As<ast::IdentifierExpression>()) { |
| 1866 | // We have an identifier of a module-scope constant. |
| 1867 | if (!Identifier(ident)) { |
| 1868 | return false; |
| 1869 | } |
| 1870 | |
| 1871 | VariableInfo* var; |
| 1872 | if (!variable_stack_.get(ident->symbol(), &var) || |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1873 | !(var->declaration->is_const() && var->type->is_integer_scalar())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1874 | AddError( |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1875 | "workgroup_size parameter must be either literal or module-scope " |
| 1876 | "constant of type i32 or u32", |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1877 | values[i]->source()); |
| 1878 | return false; |
| 1879 | } |
| 1880 | |
| 1881 | // Capture the constant if an [[override]] attribute is present. |
| 1882 | if (ast::HasDecoration<ast::OverrideDecoration>( |
| 1883 | var->declaration->decorations())) { |
| 1884 | info->workgroup_size[i].overridable_const = var->declaration; |
| 1885 | } |
| 1886 | |
| 1887 | auto* constructor = var->declaration->constructor(); |
| 1888 | if (constructor) { |
| 1889 | // Resolve the constructor expression to use as the default value. |
Ben Clayton | af6fc5f | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 1890 | auto val = ConstantValueOf(constructor); |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1891 | if (!val.IsValid() || !val.Type()->is_integer_scalar()) { |
Ben Clayton | af6fc5f | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 1892 | TINT_ICE(Resolver, diagnostics_) |
| 1893 | << "failed to resolve workgroup_size constant value"; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1894 | return false; |
| 1895 | } |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1896 | |
| 1897 | if (i == 0) { |
| 1898 | is_i32 = val.Type()->Is<sem::I32>(); |
| 1899 | } else { |
| 1900 | if (is_i32 != val.Type()->Is<sem::I32>()) { |
| 1901 | AddError( |
| 1902 | "workgroup_size parameters must be of the same type, " |
| 1903 | "either i32 or u32", |
| 1904 | values[i]->source()); |
| 1905 | return false; |
| 1906 | } |
| 1907 | } |
| 1908 | is_less_than_one = |
| 1909 | is_i32 ? val.Elements()[0].i32 < 1 : val.Elements()[0].u32 < 1; |
| 1910 | |
| 1911 | value = is_i32 ? static_cast<uint32_t>(val.Elements()[0].i32) |
| 1912 | : val.Elements()[0].u32; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1913 | } else { |
| 1914 | // No constructor means this value must be overriden by the user. |
| 1915 | info->workgroup_size[i].value = 0; |
| 1916 | continue; |
| 1917 | } |
| 1918 | } else if (auto* scalar = |
| 1919 | values[i]->As<ast::ScalarConstructorExpression>()) { |
| 1920 | // We have a literal. |
| 1921 | Mark(scalar->literal()); |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1922 | auto* literal = scalar->literal()->As<ast::IntLiteral>(); |
| 1923 | if (!literal) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1924 | AddError( |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1925 | "workgroup_size parameter must be either literal or module-scope " |
| 1926 | "constant of type i32 or u32", |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1927 | values[i]->source()); |
| 1928 | return false; |
| 1929 | } |
| 1930 | |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1931 | if (i == 0) { |
| 1932 | is_i32 = literal->Is<ast::SintLiteral>(); |
| 1933 | } else { |
| 1934 | if (literal->Is<ast::SintLiteral>() != is_i32) { |
| 1935 | AddError( |
| 1936 | "workgroup_size parameters must be of the same type, " |
| 1937 | "either i32 or u32", |
| 1938 | values[i]->source()); |
| 1939 | return false; |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | is_less_than_one = |
| 1944 | is_i32 ? literal->value_as_i32() < 1 : literal->value_as_u32() < 1; |
| 1945 | value = is_i32 ? static_cast<uint32_t>(literal->value_as_i32()) |
| 1946 | : literal->value_as_u32(); |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | // Validate and set the default value for this dimension. |
Sarah | 58bbe32 | 2021-07-14 23:33:24 +0000 | [diff] [blame] | 1950 | if (is_less_than_one) { |
| 1951 | AddError("workgroup_size parameter must be at least 1", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1952 | values[i]->source()); |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1953 | return false; |
| 1954 | } |
| 1955 | info->workgroup_size[i].value = value; |
| 1956 | } |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1959 | if (!ValidateFunction(func, info)) { |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 1960 | return false; |
| 1961 | } |
| 1962 | |
dan sinclair | f8fa6cf | 2021-02-24 22:11:34 +0000 | [diff] [blame] | 1963 | // Register the function information _after_ processing the statements. This |
| 1964 | // allows us to catch a function calling itself when determining the call |
| 1965 | // information as this function doesn't exist until it's finished. |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1966 | symbol_to_function_[func->symbol()] = info; |
| 1967 | function_to_info_.emplace(func, info); |
dan sinclair | f8fa6cf | 2021-02-24 22:11:34 +0000 | [diff] [blame] | 1968 | |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 1969 | return true; |
| 1970 | } |
| 1971 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1972 | bool Resolver::Statements(const ast::StatementList& stmts) { |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1973 | for (auto* stmt : stmts) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1974 | Mark(stmt); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1975 | if (!Statement(stmt)) { |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 1976 | return false; |
| 1977 | } |
| 1978 | } |
Sarah | c3f8fdf | 2021-06-21 17:53:56 +0000 | [diff] [blame] | 1979 | if (!ValidateStatements(stmts)) { |
| 1980 | return false; |
| 1981 | } |
| 1982 | |
| 1983 | return true; |
| 1984 | } |
| 1985 | |
| 1986 | bool Resolver::ValidateStatements(const ast::StatementList& stmts) { |
| 1987 | auto next_stmt = stmts.begin(); |
| 1988 | for (auto* stmt : stmts) { |
| 1989 | next_stmt++; |
| 1990 | if (stmt->IsAnyOf<ast::ReturnStatement, ast::BreakStatement, |
| 1991 | ast::ContinueStatement>()) { |
| 1992 | if (stmt != stmts.back()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1993 | AddError("code is unreachable", (*next_stmt)->source()); |
Sarah | c3f8fdf | 2021-06-21 17:53:56 +0000 | [diff] [blame] | 1994 | return false; |
| 1995 | } |
| 1996 | } |
| 1997 | } |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 1998 | return true; |
| 1999 | } |
| 2000 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2001 | bool Resolver::Statement(ast::Statement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2002 | if (stmt->Is<ast::CaseStatement>()) { |
| 2003 | AddError("case statement can only be used inside a switch statement", |
| 2004 | stmt->source()); |
| 2005 | return false; |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2006 | } |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2007 | if (stmt->Is<ast::ElseStatement>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2008 | TINT_ICE(Resolver, diagnostics_) |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2009 | << "Resolver::Statement() encountered an Else statement. Else " |
| 2010 | "statements are embedded in If statements, so should never be " |
| 2011 | "encountered as top-level statements"; |
| 2012 | return false; |
| 2013 | } |
| 2014 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2015 | // Compound statements. These create their own sem::CompoundStatement |
| 2016 | // bindings. |
| 2017 | if (auto* b = stmt->As<ast::BlockStatement>()) { |
| 2018 | return BlockStatement(b); |
| 2019 | } |
| 2020 | if (auto* l = stmt->As<ast::ForLoopStatement>()) { |
| 2021 | return ForLoopStatement(l); |
| 2022 | } |
| 2023 | if (auto* l = stmt->As<ast::LoopStatement>()) { |
| 2024 | return LoopStatement(l); |
| 2025 | } |
| 2026 | if (auto* i = stmt->As<ast::IfStatement>()) { |
| 2027 | return IfStatement(i); |
| 2028 | } |
| 2029 | if (auto* s = stmt->As<ast::SwitchStatement>()) { |
| 2030 | return SwitchStatement(s); |
| 2031 | } |
| 2032 | |
| 2033 | // Non-Compound statements |
| 2034 | sem::Statement* sem_statement = |
| 2035 | builder_->create<sem::Statement>(stmt, current_compound_statement_); |
| 2036 | builder_->Sem().Add(stmt, sem_statement); |
| 2037 | TINT_SCOPED_ASSIGNMENT(current_statement_, sem_statement); |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2038 | if (auto* a = stmt->As<ast::AssignmentStatement>()) { |
Antonio Maiorano | e09989a | 2021-03-31 13:26:43 +0000 | [diff] [blame] | 2039 | return Assignment(a); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 2040 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2041 | if (stmt->Is<ast::BreakStatement>()) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2042 | if (!sem_statement->FindFirstParent<sem::LoopBlockStatement>() && |
| 2043 | !sem_statement->FindFirstParent<sem::SwitchCaseBlockStatement>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2044 | AddError("break statement must be in a loop or switch case", |
| 2045 | stmt->source()); |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 2046 | return false; |
| 2047 | } |
dan sinclair | 6bd7061 | 2020-06-03 16:11:28 +0000 | [diff] [blame] | 2048 | return true; |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 2049 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2050 | if (auto* c = stmt->As<ast::CallStatement>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2051 | Mark(c->expr()); |
Ben Clayton | 7fe0106 | 2021-06-11 13:22:27 +0000 | [diff] [blame] | 2052 | if (!Expression(c->expr())) { |
| 2053 | return false; |
| 2054 | } |
| 2055 | if (!ValidateCallStatement(c)) { |
| 2056 | return false; |
| 2057 | } |
| 2058 | return true; |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 2059 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2060 | if (stmt->Is<ast::ContinueStatement>()) { |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2061 | // Set if we've hit the first continue statement in our parent loop |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2062 | if (auto* block = |
| 2063 | current_block_->FindFirstParent< |
| 2064 | sem::LoopBlockStatement, sem::LoopContinuingBlockStatement>()) { |
| 2065 | if (auto* loop_block = block->As<sem::LoopBlockStatement>()) { |
| 2066 | if (loop_block->FirstContinue() == size_t(~0)) { |
| 2067 | const_cast<sem::LoopBlockStatement*>(loop_block) |
| 2068 | ->SetFirstContinue(loop_block->Decls().size()); |
| 2069 | } |
| 2070 | } else { |
| 2071 | AddError("continuing blocks must not contain a continue statement", |
| 2072 | stmt->source()); |
| 2073 | return false; |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2074 | } |
| 2075 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2076 | AddError("continue statement must be in a loop", stmt->source()); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2077 | return false; |
| 2078 | } |
| 2079 | |
dan sinclair | 6bd7061 | 2020-06-03 16:11:28 +0000 | [diff] [blame] | 2080 | return true; |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 2081 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2082 | if (stmt->Is<ast::DiscardStatement>()) { |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2083 | if (auto* continuing = |
| 2084 | sem_statement |
| 2085 | ->FindFirstParent<sem::LoopContinuingBlockStatement>()) { |
| 2086 | AddError("continuing blocks must not contain a discard statement", |
| 2087 | stmt->source()); |
| 2088 | if (continuing != sem_statement->Parent()) { |
| 2089 | AddNote("see continuing block here", |
| 2090 | continuing->Declaration()->source()); |
| 2091 | } |
| 2092 | return false; |
| 2093 | } |
dan sinclair | 8f3c635 | 2020-07-25 14:33:50 +0000 | [diff] [blame] | 2094 | return true; |
| 2095 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2096 | if (stmt->Is<ast::FallthroughStatement>()) { |
dan sinclair | 1913fc9 | 2020-04-07 12:54:59 +0000 | [diff] [blame] | 2097 | return true; |
| 2098 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2099 | if (auto* r = stmt->As<ast::ReturnStatement>()) { |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 2100 | return Return(r); |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 2101 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2102 | if (auto* v = stmt->As<ast::VariableDeclStatement>()) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 2103 | return VariableDeclStatement(v); |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 2104 | } |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 2105 | |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2106 | AddError( |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 2107 | "unknown statement type for type determination: " + builder_->str(stmt), |
| 2108 | stmt->source()); |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 2109 | return false; |
| 2110 | } |
| 2111 | |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 2112 | bool Resolver::CaseStatement(ast::CaseStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2113 | auto* sem = builder_->create<sem::SwitchCaseBlockStatement>( |
| 2114 | stmt->body(), current_compound_statement_); |
| 2115 | builder_->Sem().Add(stmt, sem); |
| 2116 | builder_->Sem().Add(stmt->body(), sem); |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2117 | Mark(stmt->body()); |
| 2118 | for (auto* sel : stmt->selectors()) { |
| 2119 | Mark(sel); |
| 2120 | } |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2121 | return Scope(sem, [&] { return Statements(stmt->body()->list()); }); |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Ben Clayton | 5fb87dd | 2021-03-09 15:17:28 +0000 | [diff] [blame] | 2124 | bool Resolver::IfStatement(ast::IfStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2125 | auto* sem = |
| 2126 | builder_->create<sem::IfStatement>(stmt, current_compound_statement_); |
| 2127 | builder_->Sem().Add(stmt, sem); |
| 2128 | return Scope(sem, [&] { |
| 2129 | Mark(stmt->condition()); |
| 2130 | if (!Expression(stmt->condition())) { |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2131 | return false; |
| 2132 | } |
Ben Clayton | 5fb87dd | 2021-03-09 15:17:28 +0000 | [diff] [blame] | 2133 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2134 | auto* cond_type = TypeOf(stmt->condition())->UnwrapRef(); |
| 2135 | if (!cond_type->Is<sem::Bool>()) { |
| 2136 | AddError("if statement condition must be bool, got " + |
| 2137 | cond_type->FriendlyName(builder_->Symbols()), |
| 2138 | stmt->condition()->source()); |
| 2139 | return false; |
| 2140 | } |
| 2141 | |
| 2142 | Mark(stmt->body()); |
| 2143 | auto* body = builder_->create<sem::BlockStatement>( |
| 2144 | stmt->body(), current_compound_statement_); |
| 2145 | builder_->Sem().Add(stmt->body(), body); |
| 2146 | if (!Scope(body, [&] { return Statements(stmt->body()->list()); })) { |
| 2147 | return false; |
| 2148 | } |
| 2149 | |
| 2150 | for (auto* else_stmt : stmt->else_statements()) { |
| 2151 | Mark(else_stmt); |
| 2152 | if (!ElseStatement(else_stmt)) { |
| 2153 | return false; |
| 2154 | } |
| 2155 | } |
| 2156 | return true; |
| 2157 | }); |
| 2158 | } |
| 2159 | |
| 2160 | bool Resolver::ElseStatement(ast::ElseStatement* stmt) { |
| 2161 | auto* sem = |
| 2162 | builder_->create<sem::ElseStatement>(stmt, current_compound_statement_); |
| 2163 | builder_->Sem().Add(stmt, sem); |
| 2164 | return Scope(sem, [&] { |
| 2165 | if (auto* cond = stmt->condition()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2166 | Mark(cond); |
| 2167 | if (!Expression(cond)) { |
| 2168 | return false; |
| 2169 | } |
Antonio Maiorano | 9fdfa1e | 2021-05-10 13:46:06 +0000 | [diff] [blame] | 2170 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2171 | auto* else_cond_type = TypeOf(cond)->UnwrapRef(); |
Ben Clayton | c03d3bd | 2021-05-20 14:22:28 +0000 | [diff] [blame] | 2172 | if (!else_cond_type->Is<sem::Bool>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2173 | AddError("else statement condition must be bool, got " + |
| 2174 | else_cond_type->FriendlyName(builder_->Symbols()), |
| 2175 | cond->source()); |
Antonio Maiorano | 9fdfa1e | 2021-05-10 13:46:06 +0000 | [diff] [blame] | 2176 | return false; |
| 2177 | } |
Ben Clayton | dba65b7 | 2021-03-31 20:35:46 +0000 | [diff] [blame] | 2178 | } |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2179 | |
| 2180 | Mark(stmt->body()); |
| 2181 | auto* body = builder_->create<sem::BlockStatement>( |
| 2182 | stmt->body(), current_compound_statement_); |
| 2183 | builder_->Sem().Add(stmt->body(), body); |
| 2184 | return Scope(body, [&] { return Statements(stmt->body()->list()); }); |
| 2185 | }); |
| 2186 | } |
| 2187 | |
| 2188 | bool Resolver::BlockStatement(ast::BlockStatement* stmt) { |
| 2189 | auto* sem = builder_->create<sem::BlockStatement>( |
| 2190 | stmt->As<ast::BlockStatement>(), current_compound_statement_); |
| 2191 | builder_->Sem().Add(stmt, sem); |
| 2192 | return Scope(sem, [&] { return Statements(stmt->list()); }); |
Ben Clayton | 5fb87dd | 2021-03-09 15:17:28 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2195 | bool Resolver::LoopStatement(ast::LoopStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2196 | auto* sem = |
| 2197 | builder_->create<sem::LoopStatement>(stmt, current_compound_statement_); |
| 2198 | builder_->Sem().Add(stmt, sem); |
| 2199 | return Scope(sem, [&] { |
| 2200 | Mark(stmt->body()); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2201 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2202 | auto* body = builder_->create<sem::LoopBlockStatement>( |
| 2203 | stmt->body(), current_compound_statement_); |
| 2204 | builder_->Sem().Add(stmt->body(), body); |
| 2205 | return Scope(body, [&] { |
| 2206 | if (!Statements(stmt->body()->list())) { |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2207 | return false; |
| 2208 | } |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2209 | if (stmt->continuing()) { // has_continuing() also checks for empty() |
| 2210 | Mark(stmt->continuing()); |
| 2211 | } |
| 2212 | if (stmt->has_continuing()) { |
| 2213 | auto* continuing = builder_->create<sem::LoopContinuingBlockStatement>( |
| 2214 | stmt->continuing(), current_compound_statement_); |
| 2215 | builder_->Sem().Add(stmt->continuing(), continuing); |
| 2216 | if (!Scope(continuing, |
| 2217 | [&] { return Statements(stmt->continuing()->list()); })) { |
| 2218 | return false; |
| 2219 | } |
| 2220 | } |
| 2221 | return true; |
| 2222 | }); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2223 | }); |
| 2224 | } |
| 2225 | |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2226 | bool Resolver::ForLoopStatement(ast::ForLoopStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2227 | auto* sem = builder_->create<sem::ForLoopStatement>( |
| 2228 | stmt, current_compound_statement_); |
| 2229 | builder_->Sem().Add(stmt, sem); |
| 2230 | return Scope(sem, [&] { |
| 2231 | if (auto* initializer = stmt->initializer()) { |
| 2232 | Mark(initializer); |
| 2233 | if (!Statement(initializer)) { |
| 2234 | return false; |
| 2235 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2238 | if (auto* condition = stmt->condition()) { |
| 2239 | Mark(condition); |
| 2240 | if (!Expression(condition)) { |
| 2241 | return false; |
| 2242 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2243 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2244 | if (!TypeOf(condition)->Is<sem::Bool>()) { |
| 2245 | AddError( |
| 2246 | "for-loop condition must be bool, got " + TypeNameOf(condition), |
| 2247 | condition->source()); |
| 2248 | return false; |
| 2249 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2250 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2251 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2252 | if (auto* continuing = stmt->continuing()) { |
| 2253 | Mark(continuing); |
| 2254 | if (!Statement(continuing)) { |
| 2255 | return false; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | Mark(stmt->body()); |
| 2260 | |
| 2261 | auto* body = builder_->create<sem::LoopBlockStatement>( |
| 2262 | stmt->body(), current_compound_statement_); |
| 2263 | builder_->Sem().Add(stmt->body(), body); |
| 2264 | return Scope(body, [&] { return Statements(stmt->body()->statements()); }); |
| 2265 | }); |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2268 | bool Resolver::Expressions(const ast::ExpressionList& list) { |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 2269 | for (auto* expr : list) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2270 | Mark(expr); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2271 | if (!Expression(expr)) { |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2272 | return false; |
| 2273 | } |
| 2274 | } |
| 2275 | return true; |
| 2276 | } |
| 2277 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2278 | bool Resolver::Expression(ast::Expression* expr) { |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 2279 | if (TypeOf(expr)) { |
| 2280 | return true; // Already resolved |
| 2281 | } |
| 2282 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 2283 | bool ok = false; |
| 2284 | if (auto* array = expr->As<ast::ArrayAccessorExpression>()) { |
| 2285 | ok = ArrayAccessor(array); |
| 2286 | } else if (auto* bin_op = expr->As<ast::BinaryExpression>()) { |
| 2287 | ok = Binary(bin_op); |
| 2288 | } else if (auto* bitcast = expr->As<ast::BitcastExpression>()) { |
| 2289 | ok = Bitcast(bitcast); |
| 2290 | } else if (auto* call = expr->As<ast::CallExpression>()) { |
| 2291 | ok = Call(call); |
| 2292 | } else if (auto* ctor = expr->As<ast::ConstructorExpression>()) { |
| 2293 | ok = Constructor(ctor); |
| 2294 | } else if (auto* ident = expr->As<ast::IdentifierExpression>()) { |
| 2295 | ok = Identifier(ident); |
| 2296 | } else if (auto* member = expr->As<ast::MemberAccessorExpression>()) { |
| 2297 | ok = MemberAccessor(member); |
| 2298 | } else if (auto* unary = expr->As<ast::UnaryOpExpression>()) { |
| 2299 | ok = UnaryOp(unary); |
| 2300 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2301 | AddError("unknown expression for type determination", expr->source()); |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 2302 | } |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 2303 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 2304 | if (!ok) { |
| 2305 | return false; |
| 2306 | } |
| 2307 | |
| 2308 | auto* ty = TypeOf(expr); |
| 2309 | if (ty->Is<sem::Atomic>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2310 | AddError("an expression must not evaluate to an atomic type", |
| 2311 | expr->source()); |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 2312 | return false; |
| 2313 | } |
| 2314 | |
| 2315 | return true; |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2318 | bool Resolver::ArrayAccessor(ast::ArrayAccessorExpression* expr) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2319 | Mark(expr->array()); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2320 | if (!Expression(expr->array())) { |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2321 | return false; |
| 2322 | } |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2323 | auto* idx = expr->idx_expr(); |
| 2324 | Mark(idx); |
| 2325 | if (!Expression(idx)) { |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 2326 | return false; |
| 2327 | } |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2328 | |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 2329 | auto* res = TypeOf(expr->array()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2330 | auto* parent_type = res->UnwrapRef(); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 2331 | const sem::Type* ret = nullptr; |
| 2332 | if (auto* arr = parent_type->As<sem::Array>()) { |
| 2333 | ret = arr->ElemType(); |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2334 | } else if (auto* vec = parent_type->As<sem::Vector>()) { |
Ben Clayton | 1b6a8ce | 2020-12-01 21:07:27 +0000 | [diff] [blame] | 2335 | ret = vec->type(); |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2336 | } else if (auto* mat = parent_type->As<sem::Matrix>()) { |
| 2337 | ret = builder_->create<sem::Vector>(mat->type(), mat->rows()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2338 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2339 | AddError("invalid parent type (" + parent_type->type_name() + |
| 2340 | ") in array accessor", |
| 2341 | expr->source()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2342 | return false; |
| 2343 | } |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2344 | |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2345 | if (!TypeOf(idx)->UnwrapRef()->IsAnyOf<sem::I32, sem::U32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2346 | AddError("index must be of type 'i32' or 'u32', found: '" + |
| 2347 | TypeNameOf(idx) + "'", |
| 2348 | idx->source()); |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2349 | return false; |
| 2350 | } |
| 2351 | |
| 2352 | if (parent_type->Is<sem::Array>() || parent_type->Is<sem::Matrix>()) { |
| 2353 | if (!res->Is<sem::Reference>()) { |
| 2354 | // TODO(bclayton): expand this to allow any const_expr expression |
| 2355 | // https://github.com/gpuweb/gpuweb/issues/1272 |
| 2356 | auto* scalar = idx->As<ast::ScalarConstructorExpression>(); |
| 2357 | if (!scalar || !scalar->literal()->As<ast::IntLiteral>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2358 | AddError("index must be signed or unsigned integer literal", |
| 2359 | idx->source()); |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2360 | return false; |
| 2361 | } |
| 2362 | } |
| 2363 | } |
| 2364 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2365 | // If we're extracting from a reference, we return a reference. |
| 2366 | if (auto* ref = res->As<sem::Reference>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 2367 | ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), |
| 2368 | ref->Access()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2369 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2370 | SetExprInfo(expr, ret); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2371 | |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2372 | return true; |
| 2373 | } |
| 2374 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2375 | bool Resolver::Bitcast(ast::BitcastExpression* expr) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2376 | Mark(expr->expr()); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2377 | if (!Expression(expr->expr())) { |
dan sinclair | 8b48b26 | 2020-06-18 18:02:46 +0000 | [diff] [blame] | 2378 | return false; |
| 2379 | } |
Ben Clayton | 0e66b40 | 2021-06-03 08:17:44 +0000 | [diff] [blame] | 2380 | auto* ty = Type(expr->type()); |
| 2381 | if (!ty) { |
| 2382 | return false; |
| 2383 | } |
Sarah | 4b1c9de | 2021-06-16 17:42:13 +0000 | [diff] [blame] | 2384 | if (ty->Is<sem::Pointer>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2385 | AddError("cannot cast to a pointer", expr->source()); |
Sarah | 4b1c9de | 2021-06-16 17:42:13 +0000 | [diff] [blame] | 2386 | return false; |
| 2387 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2388 | SetExprInfo(expr, ty, expr->type()->FriendlyName(builder_->Symbols())); |
dan sinclair | a01777c | 2020-04-07 12:57:52 +0000 | [diff] [blame] | 2389 | return true; |
| 2390 | } |
| 2391 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2392 | bool Resolver::Call(ast::CallExpression* call) { |
| 2393 | if (!Expressions(call->params())) { |
dan sinclair | fd5d4ca | 2020-04-20 15:46:18 +0000 | [diff] [blame] | 2394 | return false; |
dan sinclair | ccb52dc | 2020-04-20 14:18:54 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2397 | Mark(call->func()); |
James Price | 28ec968 | 2021-05-22 12:42:14 +0000 | [diff] [blame] | 2398 | auto* ident = call->func(); |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2399 | auto name = builder_->Symbols().NameFor(ident->symbol()); |
| 2400 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 2401 | auto intrinsic_type = sem::ParseIntrinsicType(name); |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 2402 | if (intrinsic_type != IntrinsicType::kNone) { |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2403 | if (!IntrinsicCall(call, intrinsic_type)) { |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2404 | return false; |
| 2405 | } |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 2406 | } else { |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2407 | if (!FunctionCall(call)) { |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2408 | return false; |
| 2409 | } |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 2412 | return true; |
| 2413 | } |
| 2414 | |
Ben Clayton | 7fe0106 | 2021-06-11 13:22:27 +0000 | [diff] [blame] | 2415 | bool Resolver::ValidateCallStatement(ast::CallStatement* stmt) { |
| 2416 | const sem::Type* return_type = nullptr; |
| 2417 | // A function call is made to either a user declared function or an intrinsic. |
| 2418 | // function_calls_ only maps CallExpression to user declared functions |
| 2419 | auto it = function_calls_.find(stmt->expr()); |
| 2420 | if (it != function_calls_.end()) { |
| 2421 | return_type = it->second.function->return_type; |
| 2422 | } else { |
| 2423 | // Must be an intrinsic call |
| 2424 | auto* target = builder_->Sem().Get(stmt->expr())->Target(); |
| 2425 | if (auto* intrinsic = target->As<sem::Intrinsic>()) { |
| 2426 | return_type = intrinsic->ReturnType(); |
| 2427 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2428 | TINT_ICE(Resolver, diagnostics_) |
| 2429 | << "call target was not an intrinsic, but a " |
| 2430 | << intrinsic->TypeInfo().name; |
Ben Clayton | 7fe0106 | 2021-06-11 13:22:27 +0000 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | if (!return_type->Is<sem::Void>()) { |
| 2435 | // https://gpuweb.github.io/gpuweb/wgsl/#function-call-statement |
| 2436 | // A function call statement executes a function call where the called |
| 2437 | // function does not return a value. If the called function returns a value, |
| 2438 | // that value must be consumed either through assignment, evaluation in |
| 2439 | // another expression or through use of the ignore built-in function (see |
| 2440 | // § 16.13 Value-steering functions). |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2441 | AddError( |
Ben Clayton | 7fe0106 | 2021-06-11 13:22:27 +0000 | [diff] [blame] | 2442 | "result of called function was not used. If this was intentional wrap " |
| 2443 | "the function call in ignore()", |
| 2444 | stmt->source()); |
| 2445 | return false; |
| 2446 | } |
| 2447 | return true; |
| 2448 | } |
| 2449 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2450 | bool Resolver::IntrinsicCall(ast::CallExpression* call, |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 2451 | sem::IntrinsicType intrinsic_type) { |
Ben Clayton | 12ed13d | 2021-04-28 12:38:13 +0000 | [diff] [blame] | 2452 | std::vector<const sem::Type*> arg_tys; |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 2453 | arg_tys.reserve(call->params().size()); |
| 2454 | for (auto* expr : call->params()) { |
| 2455 | arg_tys.emplace_back(TypeOf(expr)); |
| 2456 | } |
| 2457 | |
Ben Clayton | b29a59d | 2021-06-01 19:06:31 +0000 | [diff] [blame] | 2458 | auto* result = |
| 2459 | intrinsic_table_->Lookup(intrinsic_type, arg_tys, call->source()); |
| 2460 | if (!result) { |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2461 | return false; |
| 2462 | } |
| 2463 | |
Ben Clayton | 3e59eb0 | 2021-06-10 17:31:54 +0000 | [diff] [blame] | 2464 | if (result->IsDeprecated()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2465 | AddWarning("use of deprecated intrinsic", call->source()); |
Ben Clayton | 3e59eb0 | 2021-06-10 17:31:54 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2468 | auto* out = builder_->create<sem::Call>(call, result, current_statement_); |
| 2469 | builder_->Sem().Add(call, out); |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2470 | SetExprInfo(call, result->ReturnType()); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 2471 | |
| 2472 | current_function_->intrinsic_calls.emplace_back( |
| 2473 | IntrinsicCallInfo{call, result}); |
| 2474 | |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2475 | if (IsTextureIntrinsic(intrinsic_type) && |
| 2476 | !ValidateTextureIntrinsicFunction(call, out)) { |
| 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | return true; |
| 2481 | } |
| 2482 | |
| 2483 | bool Resolver::ValidateTextureIntrinsicFunction( |
| 2484 | const ast::CallExpression* ast_call, |
| 2485 | const sem::Call* sem_call) { |
| 2486 | auto* intrinsic = sem_call->Target()->As<sem::Intrinsic>(); |
| 2487 | if (!intrinsic) { |
| 2488 | return false; |
| 2489 | } |
| 2490 | std::string func_name = intrinsic->str(); |
| 2491 | auto index = |
| 2492 | sem::IndexOf(intrinsic->Parameters(), sem::ParameterUsage::kOffset); |
| 2493 | if (index > -1) { |
| 2494 | auto* param = ast_call->params()[index]; |
| 2495 | if (param->Is<ast::TypeConstructorExpression>()) { |
| 2496 | auto values = ConstantValueOf(param); |
| 2497 | if (!values.IsValid()) { |
Sarah | 36b49e8 | 2021-07-21 03:34:34 +0000 | [diff] [blame] | 2498 | AddError( |
| 2499 | "'" + func_name + "' offset parameter must be a const_expression", |
| 2500 | param->source()); |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2501 | return false; |
| 2502 | } |
| 2503 | if (!values.Type()->Is<sem::Vector>() || |
| 2504 | !values.ElementType()->Is<sem::I32>()) { |
| 2505 | TINT_ICE(Resolver, diagnostics_) |
| 2506 | << "failed to resolve '" + func_name + "' offset parameter type"; |
| 2507 | return false; |
| 2508 | } |
| 2509 | for (auto offset : values.Elements()) { |
| 2510 | auto offset_value = offset.i32; |
| 2511 | if (offset_value < -8 || offset_value > 7) { |
| 2512 | AddError("each offset component of '" + func_name + |
| 2513 | "' must be at least -8 and at most 7. " |
| 2514 | "found: '" + |
| 2515 | std::to_string(offset_value) + "'", |
| 2516 | param->source()); |
| 2517 | return false; |
| 2518 | } |
| 2519 | } |
| 2520 | } else { |
Sarah | 36b49e8 | 2021-07-21 03:34:34 +0000 | [diff] [blame] | 2521 | AddError( |
| 2522 | "'" + func_name + "' offset parameter must be a const_expression", |
| 2523 | param->source()); |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2524 | return false; |
| 2525 | } |
| 2526 | } |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2527 | return true; |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2530 | bool Resolver::FunctionCall(const ast::CallExpression* call) { |
| 2531 | auto* ident = call->func(); |
| 2532 | auto name = builder_->Symbols().NameFor(ident->symbol()); |
| 2533 | |
| 2534 | auto callee_func_it = symbol_to_function_.find(ident->symbol()); |
| 2535 | if (callee_func_it == symbol_to_function_.end()) { |
| 2536 | if (current_function_ && |
| 2537 | current_function_->declaration->symbol() == ident->symbol()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 2538 | AddError("recursion is not permitted. '" + name + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2539 | "' attempted to call itself.", |
| 2540 | call->source()); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2541 | } else { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 2542 | AddError("unable to find called function: " + name, call->source()); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2543 | } |
| 2544 | return false; |
| 2545 | } |
| 2546 | auto* callee_func = callee_func_it->second; |
| 2547 | |
| 2548 | if (current_function_) { |
| 2549 | callee_func->callsites.push_back(call); |
| 2550 | |
| 2551 | // Note: Requires called functions to be resolved first. |
| 2552 | // This is currently guaranteed as functions must be declared before |
| 2553 | // use. |
| 2554 | current_function_->transitive_calls.add(callee_func); |
| 2555 | for (auto* transitive_call : callee_func->transitive_calls) { |
| 2556 | current_function_->transitive_calls.add(transitive_call); |
| 2557 | } |
| 2558 | |
| 2559 | // We inherit any referenced variables from the callee. |
| 2560 | for (auto* var : callee_func->referenced_module_vars) { |
| 2561 | set_referenced_from_function_if_needed(var, false); |
| 2562 | } |
| 2563 | } |
| 2564 | |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2565 | function_calls_.emplace(call, |
| 2566 | FunctionCallInfo{callee_func, current_statement_}); |
| 2567 | SetExprInfo(call, callee_func->return_type, callee_func->return_type_name); |
| 2568 | |
| 2569 | if (!ValidateFunctionCall(call, callee_func)) { |
| 2570 | return false; |
| 2571 | } |
| 2572 | return true; |
| 2573 | } |
| 2574 | |
| 2575 | bool Resolver::ValidateFunctionCall(const ast::CallExpression* call, |
| 2576 | const FunctionInfo* callee_func) { |
| 2577 | auto* ident = call->func(); |
| 2578 | auto name = builder_->Symbols().NameFor(ident->symbol()); |
| 2579 | |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2580 | if (call->params().size() != callee_func->parameters.size()) { |
| 2581 | bool more = call->params().size() > callee_func->parameters.size(); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2582 | AddError("too " + (more ? std::string("many") : std::string("few")) + |
| 2583 | " arguments in call to '" + name + "', expected " + |
| 2584 | std::to_string(callee_func->parameters.size()) + ", got " + |
| 2585 | std::to_string(call->params().size()), |
| 2586 | call->source()); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2587 | return false; |
| 2588 | } |
| 2589 | |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2590 | for (size_t i = 0; i < call->params().size(); ++i) { |
| 2591 | const VariableInfo* param = callee_func->parameters[i]; |
| 2592 | const ast::Expression* arg_expr = call->params()[i]; |
| 2593 | auto* arg_type = TypeOf(arg_expr)->UnwrapRef(); |
| 2594 | |
| 2595 | if (param->type != arg_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2596 | AddError("type mismatch for argument " + std::to_string(i + 1) + |
| 2597 | " in call to '" + name + "', expected '" + |
| 2598 | param->type->FriendlyName(builder_->Symbols()) + "', got '" + |
| 2599 | arg_type->FriendlyName(builder_->Symbols()) + "'", |
| 2600 | arg_expr->source()); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2601 | return false; |
| 2602 | } |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2603 | |
| 2604 | if (param->declaration->type()->Is<ast::Pointer>()) { |
| 2605 | auto is_valid = false; |
| 2606 | if (auto* ident_expr = arg_expr->As<ast::IdentifierExpression>()) { |
| 2607 | VariableInfo* var; |
| 2608 | if (!variable_stack_.get(ident_expr->symbol(), &var)) { |
| 2609 | TINT_ICE(Resolver, diagnostics_) << "failed to resolve identifier"; |
| 2610 | return false; |
| 2611 | } |
| 2612 | if (var->kind == VariableKind::kParameter) { |
| 2613 | is_valid = true; |
| 2614 | } |
| 2615 | } else if (auto* unary = arg_expr->As<ast::UnaryOpExpression>()) { |
| 2616 | if (unary->op() == ast::UnaryOp::kAddressOf) { |
| 2617 | if (auto* ident_unary = |
| 2618 | unary->expr()->As<ast::IdentifierExpression>()) { |
| 2619 | VariableInfo* var; |
| 2620 | if (!variable_stack_.get(ident_unary->symbol(), &var)) { |
| 2621 | TINT_ICE(Resolver, diagnostics_) |
| 2622 | << "failed to resolve identifier"; |
| 2623 | return false; |
| 2624 | } |
| 2625 | if (var->declaration->is_const()) { |
| 2626 | TINT_ICE(Resolver, diagnostics_) |
| 2627 | << "Resolver::FunctionCall() encountered an address-of " |
| 2628 | "expression of a constant identifier expression"; |
| 2629 | return false; |
| 2630 | } |
| 2631 | is_valid = true; |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | if (!is_valid) { |
| 2637 | AddError( |
| 2638 | "expected an address-of expression of a variable identifier " |
| 2639 | "expression or a function parameter", |
| 2640 | arg_expr->source()); |
| 2641 | return false; |
| 2642 | } |
| 2643 | } |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2644 | } |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2645 | return true; |
| 2646 | } |
| 2647 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2648 | bool Resolver::Constructor(ast::ConstructorExpression* expr) { |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2649 | if (auto* type_ctor = expr->As<ast::TypeConstructorExpression>()) { |
| 2650 | for (auto* value : type_ctor->values()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2651 | Mark(value); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2652 | if (!Expression(value)) { |
dan sinclair | 361e457 | 2020-04-24 00:41:12 +0000 | [diff] [blame] | 2653 | return false; |
| 2654 | } |
| 2655 | } |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2656 | |
Ben Clayton | 0e66b40 | 2021-06-03 08:17:44 +0000 | [diff] [blame] | 2657 | auto* type = Type(type_ctor->type()); |
| 2658 | if (!type) { |
| 2659 | return false; |
| 2660 | } |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2661 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2662 | auto type_name = type_ctor->type()->FriendlyName(builder_->Symbols()); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 2663 | |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2664 | // Now that the argument types have been determined, make sure that they |
| 2665 | // obey the constructor type rules laid out in |
| 2666 | // https://gpuweb.github.io/gpuweb/wgsl.html#type-constructor-expr. |
Sarah | 4b1c9de | 2021-06-16 17:42:13 +0000 | [diff] [blame] | 2667 | if (type->Is<sem::Pointer>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2668 | AddError("cannot cast to a pointer", expr->source()); |
Sarah | 4b1c9de | 2021-06-16 17:42:13 +0000 | [diff] [blame] | 2669 | return false; |
| 2670 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2671 | |
| 2672 | bool ok = true; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 2673 | if (auto* vec_type = type->As<sem::Vector>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2674 | ok = ValidateVectorConstructor(type_ctor, vec_type, type_name); |
| 2675 | } else if (auto* mat_type = type->As<sem::Matrix>()) { |
| 2676 | ok = ValidateMatrixConstructor(type_ctor, mat_type, type_name); |
| 2677 | } else if (type->is_scalar()) { |
| 2678 | ok = ValidateScalarConstructor(type_ctor, type, type_name); |
| 2679 | } else if (auto* arr_type = type->As<sem::Array>()) { |
| 2680 | ok = ValidateArrayConstructor(type_ctor, arr_type); |
| 2681 | } else if (auto* struct_type = type->As<sem::Struct>()) { |
| 2682 | ok = ValidateStructureConstructor(type_ctor, struct_type); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2683 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2684 | if (!ok) { |
| 2685 | return false; |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2686 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2687 | SetExprInfo(expr, type, type_name); |
| 2688 | return true; |
| 2689 | } |
| 2690 | |
| 2691 | if (auto* scalar_ctor = expr->As<ast::ScalarConstructorExpression>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2692 | Mark(scalar_ctor->literal()); |
Ben Clayton | 0e66b40 | 2021-06-03 08:17:44 +0000 | [diff] [blame] | 2693 | auto* type = TypeOf(scalar_ctor->literal()); |
| 2694 | if (!type) { |
| 2695 | return false; |
| 2696 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2697 | SetExprInfo(expr, type); |
| 2698 | return true; |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2699 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2700 | |
| 2701 | TINT_ICE(Resolver, diagnostics_) << "unexpected constructor expression type"; |
| 2702 | return false; |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Sarah | e285911 | 2021-07-09 16:32:00 +0000 | [diff] [blame] | 2705 | bool Resolver::ValidateStructureConstructor( |
| 2706 | const ast::TypeConstructorExpression* ctor, |
| 2707 | const sem::Struct* struct_type) { |
| 2708 | if (ctor->values().size() > 0) { |
| 2709 | if (ctor->values().size() != struct_type->Members().size()) { |
| 2710 | std::string fm = ctor->values().size() < struct_type->Members().size() |
| 2711 | ? "few" |
| 2712 | : "many"; |
| 2713 | AddError("struct constructor has too " + fm + " inputs: expected " + |
| 2714 | std::to_string(struct_type->Members().size()) + ", found " + |
| 2715 | std::to_string(ctor->values().size()), |
| 2716 | ctor->source()); |
| 2717 | return false; |
| 2718 | } |
| 2719 | for (auto* member : struct_type->Members()) { |
| 2720 | auto* value = ctor->values()[member->Index()]; |
| 2721 | if (member->Type() != TypeOf(value)->UnwrapRef()) { |
| 2722 | AddError( |
| 2723 | "type in struct constructor does not match struct member type: " |
| 2724 | "expected '" + |
| 2725 | member->Type()->FriendlyName(builder_->Symbols()) + |
| 2726 | "', found '" + TypeNameOf(value) + "'", |
| 2727 | value->source()); |
| 2728 | return false; |
| 2729 | } |
| 2730 | } |
| 2731 | } |
| 2732 | return true; |
| 2733 | } |
| 2734 | |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2735 | bool Resolver::ValidateArrayConstructor( |
| 2736 | const ast::TypeConstructorExpression* ctor, |
| 2737 | const sem::Array* array_type) { |
| 2738 | auto& values = ctor->values(); |
| 2739 | auto* elem_type = array_type->ElemType(); |
| 2740 | for (auto* value : values) { |
| 2741 | auto* value_type = TypeOf(value)->UnwrapRef(); |
| 2742 | if (value_type != elem_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2743 | AddError( |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2744 | "type in array constructor does not match array type: " |
| 2745 | "expected '" + |
| 2746 | elem_type->FriendlyName(builder_->Symbols()) + "', found '" + |
| 2747 | TypeNameOf(value) + "'", |
| 2748 | value->source()); |
| 2749 | return false; |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | if (array_type->IsRuntimeSized()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2754 | AddError("cannot init a runtime-sized array", ctor->source()); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2755 | return false; |
| 2756 | } else if (!values.empty() && (values.size() != array_type->Count())) { |
| 2757 | std::string fm = values.size() < array_type->Count() ? "few" : "many"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2758 | AddError("array constructor has too " + fm + " elements: expected " + |
| 2759 | std::to_string(array_type->Count()) + ", found " + |
| 2760 | std::to_string(values.size()), |
| 2761 | ctor->source()); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2762 | return false; |
| 2763 | } else if (values.size() > array_type->Count()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2764 | AddError("array constructor has too many elements: expected " + |
| 2765 | std::to_string(array_type->Count()) + ", found " + |
| 2766 | std::to_string(values.size()), |
| 2767 | ctor->source()); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2768 | return false; |
| 2769 | } |
| 2770 | return true; |
| 2771 | } |
| 2772 | |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2773 | bool Resolver::ValidateVectorConstructor( |
| 2774 | const ast::TypeConstructorExpression* ctor, |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2775 | const sem::Vector* vec_type, |
| 2776 | const std::string& type_name) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 2777 | auto& values = ctor->values(); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2778 | auto* elem_type = vec_type->type(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2779 | size_t value_cardinality_sum = 0; |
| 2780 | for (auto* value : values) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2781 | auto* value_type = TypeOf(value)->UnwrapRef(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2782 | if (value_type->is_scalar()) { |
| 2783 | if (elem_type != value_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2784 | AddError( |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2785 | "type in vector constructor does not match vector type: " |
| 2786 | "expected '" + |
| 2787 | elem_type->FriendlyName(builder_->Symbols()) + "', found '" + |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2788 | TypeNameOf(value) + "'", |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2789 | value->source()); |
| 2790 | return false; |
| 2791 | } |
| 2792 | |
| 2793 | value_cardinality_sum++; |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2794 | } else if (auto* value_vec = value_type->As<sem::Vector>()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2795 | auto* value_elem_type = value_vec->type(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2796 | // A mismatch of vector type parameter T is only an error if multiple |
| 2797 | // arguments are present. A single argument constructor constitutes a |
| 2798 | // type conversion expression. |
James Price | 1fa28ac | 2021-07-19 17:35:39 +0000 | [diff] [blame] | 2799 | if (elem_type != value_elem_type && values.size() > 1u) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2800 | AddError( |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2801 | "type in vector constructor does not match vector type: " |
| 2802 | "expected '" + |
| 2803 | elem_type->FriendlyName(builder_->Symbols()) + "', found '" + |
| 2804 | value_elem_type->FriendlyName(builder_->Symbols()) + "'", |
| 2805 | value->source()); |
| 2806 | return false; |
| 2807 | } |
| 2808 | |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 2809 | value_cardinality_sum += value_vec->Width(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2810 | } else { |
| 2811 | // A vector constructor can only accept vectors and scalars. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2812 | AddError("expected vector or scalar type in vector constructor; found: " + |
| 2813 | value_type->FriendlyName(builder_->Symbols()), |
| 2814 | value->source()); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2815 | return false; |
| 2816 | } |
| 2817 | } |
| 2818 | |
Antonio Maiorano | b5508fd | 2021-06-10 13:23:31 +0000 | [diff] [blame] | 2819 | // A correct vector constructor must either be a zero-value expression, |
| 2820 | // a single-value initializer (splat) expression, or the number of components |
| 2821 | // of all constructor arguments must add up to the vector cardinality. |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 2822 | if (value_cardinality_sum > 1 && value_cardinality_sum != vec_type->Width()) { |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2823 | if (values.empty()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2824 | TINT_ICE(Resolver, diagnostics_) |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2825 | << "constructor arguments expected to be non-empty!"; |
| 2826 | } |
| 2827 | const Source& values_start = values[0]->source(); |
| 2828 | const Source& values_end = values[values.size() - 1]->source(); |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2829 | AddError("attempted to construct '" + type_name + "' with " + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2830 | std::to_string(value_cardinality_sum) + " component(s)", |
| 2831 | Source::Combine(values_start, values_end)); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2832 | return false; |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 2833 | } |
| 2834 | return true; |
| 2835 | } |
| 2836 | |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 2837 | bool Resolver::ValidateVector(const sem::Vector* ty, const Source& source) { |
| 2838 | if (!ty->type()->is_scalar()) { |
| 2839 | AddError("vector element type must be 'bool', 'f32', 'i32' or 'u32'", |
| 2840 | source); |
| 2841 | return false; |
| 2842 | } |
| 2843 | return true; |
| 2844 | } |
| 2845 | |
| 2846 | bool Resolver::ValidateMatrix(const sem::Matrix* ty, const Source& source) { |
| 2847 | if (!ty->is_float_matrix()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2848 | AddError("matrix element type must be 'f32'", source); |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 2849 | return false; |
| 2850 | } |
| 2851 | return true; |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 2852 | } |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 2853 | |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2854 | bool Resolver::ValidateMatrixConstructor( |
| 2855 | const ast::TypeConstructorExpression* ctor, |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2856 | const sem::Matrix* matrix_type, |
| 2857 | const std::string& type_name) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 2858 | auto& values = ctor->values(); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2859 | // Zero Value expression |
| 2860 | if (values.empty()) { |
| 2861 | return true; |
| 2862 | } |
| 2863 | |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 2864 | if (!ValidateMatrix(matrix_type, ctor->source())) { |
| 2865 | return false; |
| 2866 | } |
| 2867 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2868 | auto* elem_type = matrix_type->type(); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2869 | if (matrix_type->columns() != values.size()) { |
| 2870 | const Source& values_start = values[0]->source(); |
| 2871 | const Source& values_end = values[values.size() - 1]->source(); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2872 | AddError("expected " + std::to_string(matrix_type->columns()) + " '" + |
| 2873 | VectorPretty(matrix_type->rows(), elem_type) + |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2874 | "' arguments in '" + type_name + "' constructor, found " + |
| 2875 | std::to_string(values.size()), |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2876 | Source::Combine(values_start, values_end)); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2877 | return false; |
| 2878 | } |
| 2879 | |
| 2880 | for (auto* value : values) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2881 | auto* value_type = TypeOf(value)->UnwrapRef(); |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2882 | auto* value_vec = value_type->As<sem::Vector>(); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2883 | |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 2884 | if (!value_vec || value_vec->Width() != matrix_type->rows() || |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2885 | elem_type != value_vec->type()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2886 | AddError("expected argument type '" + |
| 2887 | VectorPretty(matrix_type->rows(), elem_type) + "' in '" + |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2888 | type_name + "' constructor, found '" + TypeNameOf(value) + |
| 2889 | "'", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2890 | value->source()); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2891 | return false; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | return true; |
| 2896 | } |
| 2897 | |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2898 | bool Resolver::ValidateScalarConstructor( |
| 2899 | const ast::TypeConstructorExpression* ctor, |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2900 | const sem::Type* type, |
| 2901 | const std::string& type_name) { |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2902 | if (ctor->values().size() == 0) { |
| 2903 | return true; |
| 2904 | } |
| 2905 | if (ctor->values().size() > 1) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2906 | AddError("expected zero or one value in constructor, got " + |
| 2907 | std::to_string(ctor->values().size()), |
| 2908 | ctor->source()); |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2909 | return false; |
| 2910 | } |
| 2911 | |
| 2912 | // Validate constructor |
| 2913 | auto* value = ctor->values()[0]; |
| 2914 | auto* value_type = TypeOf(value)->UnwrapRef(); |
| 2915 | |
| 2916 | using Bool = sem::Bool; |
| 2917 | using I32 = sem::I32; |
| 2918 | using U32 = sem::U32; |
| 2919 | using F32 = sem::F32; |
| 2920 | |
James Price | 1fa28ac | 2021-07-19 17:35:39 +0000 | [diff] [blame] | 2921 | const bool is_valid = (type->Is<Bool>() && value_type->is_scalar()) || |
| 2922 | (type->Is<I32>() && value_type->is_scalar()) || |
| 2923 | (type->Is<U32>() && value_type->is_scalar()) || |
| 2924 | (type->Is<F32>() && value_type->is_scalar()); |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2925 | if (!is_valid) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2926 | AddError("cannot construct '" + type_name + "' with a value of type '" + |
| 2927 | TypeNameOf(value) + "'", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2928 | ctor->source()); |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2929 | |
| 2930 | return false; |
| 2931 | } |
| 2932 | |
| 2933 | return true; |
| 2934 | } |
| 2935 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2936 | bool Resolver::Identifier(ast::IdentifierExpression* expr) { |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 2937 | auto symbol = expr->symbol(); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 2938 | VariableInfo* var; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 2939 | if (variable_stack_.get(symbol, &var)) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2940 | SetExprInfo(expr, var->type, var->type_name); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 2941 | |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 2942 | var->users.push_back(expr); |
Enrico Galli | 3d449d2 | 2020-12-08 21:07:24 +0000 | [diff] [blame] | 2943 | set_referenced_from_function_if_needed(var, true); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2944 | |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2945 | if (current_block_) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2946 | // If identifier is part of a loop continuing block, make sure it |
| 2947 | // doesn't refer to a variable that is bypassed by a continue statement |
| 2948 | // in the loop's body block. |
Ben Clayton | 9a3ba02 | 2021-05-19 21:22:07 +0000 | [diff] [blame] | 2949 | if (auto* continuing_block = |
| 2950 | current_block_ |
| 2951 | ->FindFirstParent<sem::LoopContinuingBlockStatement>()) { |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2952 | auto* loop_block = |
Ben Clayton | 9a3ba02 | 2021-05-19 21:22:07 +0000 | [diff] [blame] | 2953 | continuing_block->FindFirstParent<sem::LoopBlockStatement>(); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2954 | if (loop_block->FirstContinue() != size_t(~0)) { |
| 2955 | auto& decls = loop_block->Decls(); |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2956 | // If our identifier is in loop_block->decls, make sure its index is |
| 2957 | // less than first_continue |
| 2958 | auto iter = std::find_if( |
| 2959 | decls.begin(), decls.end(), |
| 2960 | [&symbol](auto* v) { return v->symbol() == symbol; }); |
| 2961 | if (iter != decls.end()) { |
| 2962 | auto var_decl_index = |
| 2963 | static_cast<size_t>(std::distance(decls.begin(), iter)); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2964 | if (var_decl_index >= loop_block->FirstContinue()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2965 | AddError("continue statement bypasses declaration of '" + |
| 2966 | builder_->Symbols().NameFor(symbol) + |
| 2967 | "' in continuing block", |
| 2968 | expr->source()); |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2969 | return false; |
| 2970 | } |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2971 | } |
| 2972 | } |
| 2973 | } |
| 2974 | } |
| 2975 | |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 2976 | return true; |
| 2977 | } |
| 2978 | |
Ben Clayton | f6866a2 | 2021-01-11 22:02:42 +0000 | [diff] [blame] | 2979 | auto iter = symbol_to_function_.find(symbol); |
dan sinclair | 4ac6568 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 2980 | if (iter != symbol_to_function_.end()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2981 | AddError("missing '(' for function call", expr->source().End()); |
Ben Clayton | 33a8cdd | 2021-02-24 13:31:22 +0000 | [diff] [blame] | 2982 | return false; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2985 | std::string name = builder_->Symbols().NameFor(symbol); |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 2986 | if (sem::ParseIntrinsicType(name) != IntrinsicType::kNone) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2987 | AddError("missing '(' for intrinsic call", expr->source().End()); |
Ben Clayton | 33a8cdd | 2021-02-24 13:31:22 +0000 | [diff] [blame] | 2988 | return false; |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 2989 | } |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2990 | |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 2991 | AddError("identifier must be declared before use: " + name, expr->source()); |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2992 | return false; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2995 | bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2996 | Mark(expr->structure()); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2997 | if (!Expression(expr->structure())) { |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 2998 | return false; |
| 2999 | } |
| 3000 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3001 | auto* structure = TypeOf(expr->structure()); |
| 3002 | auto* storage_type = structure->UnwrapRef(); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 3003 | |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 3004 | sem::Type* ret = nullptr; |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3005 | std::vector<uint32_t> swizzle; |
Ben Clayton | c1052a4 | 2021-02-03 23:55:56 +0000 | [diff] [blame] | 3006 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3007 | if (auto* str = storage_type->As<sem::Struct>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3008 | Mark(expr->member()); |
dan sinclair | eb737c2 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 3009 | auto symbol = expr->member()->symbol(); |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3010 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3011 | const sem::StructMember* member = nullptr; |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3012 | for (auto* m : str->Members()) { |
Ben Clayton | 053559d | 2021-07-23 16:43:01 +0000 | [diff] [blame] | 3013 | if (m->Name() == symbol) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 3014 | ret = m->Type(); |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3015 | member = m; |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3016 | break; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3017 | } |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3020 | if (ret == nullptr) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3021 | AddError( |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 3022 | "struct member " + builder_->Symbols().NameFor(symbol) + " not found", |
| 3023 | expr->source()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3024 | return false; |
| 3025 | } |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 3026 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3027 | // If we're extracting from a reference, we return a reference. |
| 3028 | if (auto* ref = structure->As<sem::Reference>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3029 | ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), |
| 3030 | ref->Access()); |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 3031 | } |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3032 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3033 | builder_->Sem().Add(expr, builder_->create<sem::StructMemberAccess>( |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3034 | expr, ret, current_statement_, member)); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3035 | } else if (auto* vec = storage_type->As<sem::Vector>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3036 | Mark(expr->member()); |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3037 | std::string s = builder_->Symbols().NameFor(expr->member()->symbol()); |
| 3038 | auto size = s.size(); |
| 3039 | swizzle.reserve(s.size()); |
dan sinclair | eb737c2 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 3040 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3041 | for (auto c : s) { |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3042 | switch (c) { |
| 3043 | case 'x': |
| 3044 | case 'r': |
| 3045 | swizzle.emplace_back(0); |
| 3046 | break; |
| 3047 | case 'y': |
| 3048 | case 'g': |
| 3049 | swizzle.emplace_back(1); |
| 3050 | break; |
| 3051 | case 'z': |
| 3052 | case 'b': |
| 3053 | swizzle.emplace_back(2); |
| 3054 | break; |
| 3055 | case 'w': |
| 3056 | case 'a': |
| 3057 | swizzle.emplace_back(3); |
| 3058 | break; |
| 3059 | default: |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3060 | AddError("invalid vector swizzle character", |
| 3061 | expr->member()->source().Begin() + swizzle.size()); |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3062 | return false; |
| 3063 | } |
Antonio Maiorano | 39c05a9 | 2021-06-01 00:37:40 +0000 | [diff] [blame] | 3064 | |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3065 | if (swizzle.back() >= vec->Width()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3066 | AddError("invalid vector swizzle member", expr->member()->source()); |
Antonio Maiorano | 39c05a9 | 2021-06-01 00:37:40 +0000 | [diff] [blame] | 3067 | return false; |
| 3068 | } |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | if (size < 1 || size > 4) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3072 | AddError("invalid vector swizzle size", expr->member()->source()); |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3073 | return false; |
| 3074 | } |
| 3075 | |
Antonio Maiorano | fe2b417 | 2021-03-01 20:01:39 +0000 | [diff] [blame] | 3076 | // All characters are valid, check if they're being mixed |
| 3077 | auto is_rgba = [](char c) { |
| 3078 | return c == 'r' || c == 'g' || c == 'b' || c == 'a'; |
| 3079 | }; |
| 3080 | auto is_xyzw = [](char c) { |
| 3081 | return c == 'x' || c == 'y' || c == 'z' || c == 'w'; |
| 3082 | }; |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3083 | if (!std::all_of(s.begin(), s.end(), is_rgba) && |
| 3084 | !std::all_of(s.begin(), s.end(), is_xyzw)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3085 | AddError("invalid mixing of vector swizzle characters rgba with xyzw", |
| 3086 | expr->member()->source()); |
Antonio Maiorano | fe2b417 | 2021-03-01 20:01:39 +0000 | [diff] [blame] | 3087 | return false; |
| 3088 | } |
| 3089 | |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 3090 | if (size == 1) { |
| 3091 | // A single element swizzle is just the type of the vector. |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3092 | ret = vec->type(); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3093 | // If we're extracting from a reference, we return a reference. |
| 3094 | if (auto* ref = structure->As<sem::Reference>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3095 | ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), |
| 3096 | ref->Access()); |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 3097 | } |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 3098 | } else { |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3099 | // The vector will have a number of components equal to the length of |
Antonio Maiorano | d15391e | 2021-04-08 14:08:47 +0000 | [diff] [blame] | 3100 | // the swizzle. |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 3101 | ret = builder_->create<sem::Vector>(vec->type(), |
| 3102 | static_cast<uint32_t>(size)); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 3103 | } |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3104 | builder_->Sem().Add( |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3105 | expr, builder_->create<sem::Swizzle>(expr, ret, current_statement_, |
| 3106 | std::move(swizzle))); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3107 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3108 | AddError("invalid use of member accessor on a non-vector/non-struct " + |
| 3109 | TypeNameOf(expr->structure()), |
| 3110 | expr->source()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3111 | return false; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3114 | SetExprInfo(expr, ret); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3115 | |
| 3116 | return true; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3119 | bool Resolver::Binary(ast::BinaryExpression* expr) { |
| 3120 | Mark(expr->lhs()); |
| 3121 | Mark(expr->rhs()); |
| 3122 | |
| 3123 | if (!Expression(expr->lhs()) || !Expression(expr->rhs())) { |
| 3124 | return false; |
| 3125 | } |
| 3126 | |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 3127 | using Bool = sem::Bool; |
| 3128 | using F32 = sem::F32; |
| 3129 | using I32 = sem::I32; |
| 3130 | using U32 = sem::U32; |
| 3131 | using Matrix = sem::Matrix; |
| 3132 | using Vector = sem::Vector; |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3133 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3134 | auto* lhs_type = const_cast<sem::Type*>(TypeOf(expr->lhs())->UnwrapRef()); |
| 3135 | auto* rhs_type = const_cast<sem::Type*>(TypeOf(expr->rhs())->UnwrapRef()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3136 | |
| 3137 | auto* lhs_vec = lhs_type->As<Vector>(); |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 3138 | auto* lhs_vec_elem_type = lhs_vec ? lhs_vec->type() : nullptr; |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3139 | auto* rhs_vec = rhs_type->As<Vector>(); |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 3140 | auto* rhs_vec_elem_type = rhs_vec ? rhs_vec->type() : nullptr; |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3141 | |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3142 | const bool matching_vec_elem_types = |
| 3143 | lhs_vec_elem_type && rhs_vec_elem_type && |
| 3144 | (lhs_vec_elem_type == rhs_vec_elem_type) && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3145 | (lhs_vec->Width() == rhs_vec->Width()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3146 | |
Antonio Maiorano | 15c6ed0 | 2021-04-01 14:59:27 +0000 | [diff] [blame] | 3147 | const bool matching_types = matching_vec_elem_types || (lhs_type == rhs_type); |
| 3148 | |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3149 | // Binary logical expressions |
| 3150 | if (expr->IsLogicalAnd() || expr->IsLogicalOr()) { |
| 3151 | if (matching_types && lhs_type->Is<Bool>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3152 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3153 | return true; |
| 3154 | } |
| 3155 | } |
| 3156 | if (expr->IsOr() || expr->IsAnd()) { |
| 3157 | if (matching_types && lhs_type->Is<Bool>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3158 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3159 | return true; |
| 3160 | } |
| 3161 | if (matching_types && lhs_vec_elem_type && lhs_vec_elem_type->Is<Bool>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3162 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3163 | return true; |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | // Arithmetic expressions |
| 3168 | if (expr->IsArithmetic()) { |
| 3169 | // Binary arithmetic expressions over scalars |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3170 | if (matching_types && lhs_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3171 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3172 | return true; |
| 3173 | } |
| 3174 | |
| 3175 | // Binary arithmetic expressions over vectors |
| 3176 | if (matching_types && lhs_vec_elem_type && |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3177 | lhs_vec_elem_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3178 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3179 | return true; |
| 3180 | } |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3181 | |
| 3182 | // Binary arithmetic expressions with mixed scalar and vector operands |
| 3183 | if (lhs_vec_elem_type && (lhs_vec_elem_type == rhs_type)) { |
| 3184 | if (expr->IsModulo()) { |
| 3185 | if (rhs_type->is_integer_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3186 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3187 | return true; |
| 3188 | } |
| 3189 | } else if (rhs_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3190 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3191 | return true; |
| 3192 | } |
| 3193 | } |
| 3194 | if (rhs_vec_elem_type && (rhs_vec_elem_type == lhs_type)) { |
| 3195 | if (expr->IsModulo()) { |
| 3196 | if (lhs_type->is_integer_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3197 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3198 | return true; |
| 3199 | } |
| 3200 | } else if (lhs_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3201 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3202 | return true; |
| 3203 | } |
| 3204 | } |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3207 | // Matrix arithmetic |
Antonio Maiorano | c91f8f2 | 2021-05-31 15:53:20 +0000 | [diff] [blame] | 3208 | auto* lhs_mat = lhs_type->As<Matrix>(); |
| 3209 | auto* lhs_mat_elem_type = lhs_mat ? lhs_mat->type() : nullptr; |
| 3210 | auto* rhs_mat = rhs_type->As<Matrix>(); |
| 3211 | auto* rhs_mat_elem_type = rhs_mat ? rhs_mat->type() : nullptr; |
| 3212 | // Addition and subtraction of float matrices |
| 3213 | if ((expr->IsAdd() || expr->IsSubtract()) && lhs_mat_elem_type && |
| 3214 | lhs_mat_elem_type->Is<F32>() && rhs_mat_elem_type && |
| 3215 | rhs_mat_elem_type->Is<F32>() && |
| 3216 | (lhs_mat->columns() == rhs_mat->columns()) && |
| 3217 | (lhs_mat->rows() == rhs_mat->rows())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3218 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | c91f8f2 | 2021-05-31 15:53:20 +0000 | [diff] [blame] | 3219 | return true; |
| 3220 | } |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3221 | if (expr->IsMultiply()) { |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3222 | // Multiplication of a matrix and a scalar |
| 3223 | if (lhs_type->Is<F32>() && rhs_mat_elem_type && |
| 3224 | rhs_mat_elem_type->Is<F32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3225 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3226 | return true; |
| 3227 | } |
| 3228 | if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() && |
| 3229 | rhs_type->Is<F32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3230 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3231 | return true; |
| 3232 | } |
| 3233 | |
| 3234 | // Vector times matrix |
| 3235 | if (lhs_vec_elem_type && lhs_vec_elem_type->Is<F32>() && |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3236 | rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>() && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3237 | (lhs_vec->Width() == rhs_mat->rows())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3238 | SetExprInfo(expr, builder_->create<sem::Vector>(lhs_vec->type(), |
| 3239 | rhs_mat->columns())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3240 | return true; |
| 3241 | } |
| 3242 | |
| 3243 | // Matrix times vector |
| 3244 | if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() && |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3245 | rhs_vec_elem_type && rhs_vec_elem_type->Is<F32>() && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3246 | (lhs_mat->columns() == rhs_vec->Width())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3247 | SetExprInfo(expr, builder_->create<sem::Vector>(rhs_vec->type(), |
| 3248 | lhs_mat->rows())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3249 | return true; |
| 3250 | } |
| 3251 | |
| 3252 | // Matrix times matrix |
| 3253 | if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() && |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3254 | rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>() && |
| 3255 | (lhs_mat->columns() == rhs_mat->rows())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3256 | SetExprInfo(expr, builder_->create<sem::Matrix>( |
| 3257 | builder_->create<sem::Vector>(lhs_mat_elem_type, |
| 3258 | lhs_mat->rows()), |
| 3259 | rhs_mat->columns())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3260 | return true; |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | // Comparison expressions |
| 3265 | if (expr->IsComparison()) { |
| 3266 | if (matching_types) { |
| 3267 | // Special case for bools: only == and != |
| 3268 | if (lhs_type->Is<Bool>() && (expr->IsEqual() || expr->IsNotEqual())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3269 | SetExprInfo(expr, builder_->create<sem::Bool>()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3270 | return true; |
| 3271 | } |
| 3272 | |
| 3273 | // For the rest, we can compare i32, u32, and f32 |
| 3274 | if (lhs_type->IsAnyOf<I32, U32, F32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3275 | SetExprInfo(expr, builder_->create<sem::Bool>()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3276 | return true; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | // Same for vectors |
| 3281 | if (matching_vec_elem_types) { |
| 3282 | if (lhs_vec_elem_type->Is<Bool>() && |
| 3283 | (expr->IsEqual() || expr->IsNotEqual())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3284 | SetExprInfo(expr, builder_->create<sem::Vector>( |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3285 | builder_->create<sem::Bool>(), lhs_vec->Width())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3286 | return true; |
| 3287 | } |
| 3288 | |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3289 | if (lhs_vec_elem_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3290 | SetExprInfo(expr, builder_->create<sem::Vector>( |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3291 | builder_->create<sem::Bool>(), lhs_vec->Width())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3292 | return true; |
| 3293 | } |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | // Binary bitwise operations |
| 3298 | if (expr->IsBitwise()) { |
Antonio Maiorano | 0895c23 | 2021-06-01 17:42:41 +0000 | [diff] [blame] | 3299 | if (matching_types && lhs_type->is_integer_scalar_or_vector()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3300 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3301 | return true; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | // Bit shift expressions |
| 3306 | if (expr->IsBitshift()) { |
| 3307 | // Type validation rules are the same for left or right shift, despite |
| 3308 | // differences in computation rules (i.e. right shift can be arithmetic or |
| 3309 | // logical depending on lhs type). |
| 3310 | |
| 3311 | if (lhs_type->IsAnyOf<I32, U32>() && rhs_type->Is<U32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3312 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3313 | return true; |
| 3314 | } |
| 3315 | |
| 3316 | if (lhs_vec_elem_type && lhs_vec_elem_type->IsAnyOf<I32, U32>() && |
| 3317 | rhs_vec_elem_type && rhs_vec_elem_type->Is<U32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3318 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3319 | return true; |
| 3320 | } |
| 3321 | } |
| 3322 | |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3323 | AddError("Binary expression operand types are invalid for this operation: " + |
| 3324 | lhs_type->FriendlyName(builder_->Symbols()) + " " + |
| 3325 | FriendlyName(expr->op()) + " " + |
| 3326 | rhs_type->FriendlyName(builder_->Symbols()), |
| 3327 | expr->source()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3328 | return false; |
| 3329 | } |
| 3330 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3331 | bool Resolver::UnaryOp(ast::UnaryOpExpression* unary) { |
| 3332 | Mark(unary->expr()); |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3333 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3334 | // Resolve the inner expression |
| 3335 | if (!Expression(unary->expr())) { |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 3336 | return false; |
| 3337 | } |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 3338 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3339 | auto* expr_type = TypeOf(unary->expr()); |
| 3340 | if (!expr_type) { |
| 3341 | return false; |
| 3342 | } |
| 3343 | |
| 3344 | std::string type_name; |
| 3345 | const sem::Type* type = nullptr; |
| 3346 | |
| 3347 | switch (unary->op()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3348 | case ast::UnaryOp::kNot: |
| 3349 | // Result type matches the deref'd inner type. |
| 3350 | type_name = TypeNameOf(unary->expr()); |
| 3351 | type = expr_type->UnwrapRef(); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3352 | if (!type->Is<sem::Bool>() && !type->is_bool_vector()) { |
| 3353 | AddError("cannot logical negate expression of type '" + |
| 3354 | TypeNameOf(unary->expr()), |
| 3355 | unary->expr()->source()); |
| 3356 | return false; |
| 3357 | } |
| 3358 | break; |
| 3359 | |
| 3360 | case ast::UnaryOp::kComplement: |
| 3361 | // Result type matches the deref'd inner type. |
| 3362 | type_name = TypeNameOf(unary->expr()); |
| 3363 | type = expr_type->UnwrapRef(); |
| 3364 | if (!type->is_integer_scalar_or_vector()) { |
| 3365 | AddError("cannot bitwise complement expression of type '" + |
| 3366 | TypeNameOf(unary->expr()), |
| 3367 | unary->expr()->source()); |
| 3368 | return false; |
| 3369 | } |
| 3370 | break; |
| 3371 | |
| 3372 | case ast::UnaryOp::kNegation: |
| 3373 | // Result type matches the deref'd inner type. |
| 3374 | type_name = TypeNameOf(unary->expr()); |
| 3375 | type = expr_type->UnwrapRef(); |
| 3376 | if (!(type->IsAnyOf<sem::F32, sem::I32>() || |
| 3377 | type->is_signed_integer_vector() || type->is_float_vector())) { |
| 3378 | AddError( |
| 3379 | "cannot negate expression of type '" + TypeNameOf(unary->expr()), |
| 3380 | unary->expr()->source()); |
| 3381 | return false; |
| 3382 | } |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3383 | break; |
| 3384 | |
| 3385 | case ast::UnaryOp::kAddressOf: |
| 3386 | if (auto* ref = expr_type->As<sem::Reference>()) { |
Sarah | edecbb1 | 2021-07-28 03:57:22 +0000 | [diff] [blame] | 3387 | if (ref->StoreType()->UnwrapRef()->is_handle()) { |
| 3388 | AddError( |
| 3389 | "cannot take the address of expression in handle storage class", |
| 3390 | unary->expr()->source()); |
| 3391 | return false; |
| 3392 | } |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3393 | type = builder_->create<sem::Pointer>( |
| 3394 | ref->StoreType(), ref->StorageClass(), ref->Access()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3395 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3396 | AddError("cannot take the address of expression", |
| 3397 | unary->expr()->source()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3398 | return false; |
| 3399 | } |
| 3400 | break; |
| 3401 | |
| 3402 | case ast::UnaryOp::kIndirection: |
| 3403 | if (auto* ptr = expr_type->As<sem::Pointer>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3404 | type = builder_->create<sem::Reference>( |
| 3405 | ptr->StoreType(), ptr->StorageClass(), ptr->Access()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3406 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3407 | AddError("cannot dereference expression of type '" + |
| 3408 | TypeNameOf(unary->expr()) + "'", |
| 3409 | unary->expr()->source()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3410 | return false; |
| 3411 | } |
| 3412 | break; |
| 3413 | } |
| 3414 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3415 | SetExprInfo(unary, type); |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 3416 | return true; |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 3419 | bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) { |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 3420 | ast::Variable* var = stmt->variable(); |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3421 | Mark(var); |
| 3422 | |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 3423 | if (!ValidateNoDuplicateDefinition(var->symbol(), var->source())) { |
Antonio Maiorano | 09356cc | 2021-04-06 14:07:07 +0000 | [diff] [blame] | 3424 | return false; |
| 3425 | } |
| 3426 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3427 | auto* info = Variable(var, VariableKind::kLocal); |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 3428 | if (!info) { |
| 3429 | return false; |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3432 | for (auto* deco : var->decorations()) { |
| 3433 | // TODO(bclayton): Validate decorations |
| 3434 | Mark(deco); |
| 3435 | } |
| 3436 | |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3437 | variable_stack_.set(var->symbol(), info); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 3438 | if (current_block_) { // Not all statements are inside a block |
| 3439 | current_block_->AddDecl(var); |
| 3440 | } |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3441 | |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 3442 | if (!ValidateVariable(info)) { |
Antonio Maiorano | 09356cc | 2021-04-06 14:07:07 +0000 | [diff] [blame] | 3443 | return false; |
| 3444 | } |
| 3445 | |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3446 | if (!var->is_const()) { |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 3447 | if (info->storage_class != ast::StorageClass::kFunction && |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 3448 | IsValidationEnabled(var->decorations(), |
| 3449 | ast::DisabledValidation::kIgnoreStorageClass)) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3450 | if (info->storage_class != ast::StorageClass::kNone) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3451 | AddError("function variable has a non-function storage class", |
| 3452 | stmt->source()); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3453 | return false; |
| 3454 | } |
| 3455 | info->storage_class = ast::StorageClass::kFunction; |
| 3456 | } |
| 3457 | } |
| 3458 | |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 3459 | if (!ApplyStorageClassUsageToType(info->storage_class, info->type, |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 3460 | var->source())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3461 | AddNote("while instantiating variable " + |
| 3462 | builder_->Symbols().NameFor(var->symbol()), |
| 3463 | var->source()); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3464 | return false; |
Antonio Maiorano | 6ce2edf | 2021-02-26 18:25:56 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | return true; |
| 3468 | } |
| 3469 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3470 | sem::Type* Resolver::TypeDecl(const ast::TypeDecl* named_type) { |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3471 | sem::Type* result = nullptr; |
| 3472 | if (auto* alias = named_type->As<ast::Alias>()) { |
| 3473 | result = Type(alias->type()); |
| 3474 | } else if (auto* str = named_type->As<ast::Struct>()) { |
| 3475 | result = Structure(str); |
| 3476 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3477 | TINT_UNREACHABLE(Resolver, diagnostics_) << "Unhandled TypeDecl"; |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | if (!result) { |
| 3481 | return nullptr; |
| 3482 | } |
| 3483 | |
| 3484 | named_type_info_.emplace(named_type->name(), |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3485 | TypeDeclInfo{named_type, result}); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3486 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3487 | if (!ValidateTypeDecl(named_type)) { |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3488 | return nullptr; |
| 3489 | } |
| 3490 | |
| 3491 | builder_->Sem().Add(named_type, result); |
| 3492 | return result; |
| 3493 | } |
| 3494 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3495 | bool Resolver::ValidateTypeDecl(const ast::TypeDecl* named_type) const { |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3496 | auto iter = named_type_info_.find(named_type->name()); |
| 3497 | if (iter == named_type_info_.end()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3498 | TINT_ICE(Resolver, diagnostics_) |
| 3499 | << "ValidateTypeDecl called() before TypeDecl()"; |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3500 | } |
| 3501 | if (iter->second.ast != named_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3502 | AddError("type with the name '" + |
| 3503 | builder_->Symbols().NameFor(named_type->name()) + |
| 3504 | "' was already declared", |
| 3505 | named_type->source()); |
| 3506 | AddNote("first declared here", iter->second.ast->source()); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3507 | return false; |
| 3508 | } |
| 3509 | return true; |
| 3510 | } |
| 3511 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3512 | sem::Type* Resolver::TypeOf(const ast::Expression* expr) { |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3513 | auto it = expr_info_.find(expr); |
| 3514 | if (it != expr_info_.end()) { |
Ben Clayton | 19d3205 | 2021-05-20 15:10:48 +0000 | [diff] [blame] | 3515 | return const_cast<sem::Type*>(it->second.type); |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3516 | } |
| 3517 | return nullptr; |
| 3518 | } |
| 3519 | |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 3520 | std::string Resolver::TypeNameOf(const ast::Expression* expr) { |
| 3521 | auto it = expr_info_.find(expr); |
| 3522 | if (it != expr_info_.end()) { |
| 3523 | return it->second.type_name; |
| 3524 | } |
| 3525 | return ""; |
| 3526 | } |
| 3527 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3528 | sem::Type* Resolver::TypeOf(const ast::Literal* lit) { |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 3529 | if (lit->Is<ast::SintLiteral>()) { |
| 3530 | return builder_->create<sem::I32>(); |
| 3531 | } |
| 3532 | if (lit->Is<ast::UintLiteral>()) { |
| 3533 | return builder_->create<sem::U32>(); |
| 3534 | } |
| 3535 | if (lit->Is<ast::FloatLiteral>()) { |
| 3536 | return builder_->create<sem::F32>(); |
| 3537 | } |
| 3538 | if (lit->Is<ast::BoolLiteral>()) { |
| 3539 | return builder_->create<sem::Bool>(); |
| 3540 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3541 | TINT_UNREACHABLE(Resolver, diagnostics_) |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 3542 | << "Unhandled literal type: " << lit->TypeInfo().name; |
| 3543 | return nullptr; |
| 3544 | } |
| 3545 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3546 | void Resolver::SetExprInfo(const ast::Expression* expr, |
| 3547 | const sem::Type* type, |
| 3548 | std::string type_name) { |
Ben Clayton | 90f43cf | 2021-03-31 20:43:26 +0000 | [diff] [blame] | 3549 | if (expr_info_.count(expr)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3550 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3551 | << "SetExprInfo() called twice for the same expression"; |
Ben Clayton | 90f43cf | 2021-03-31 20:43:26 +0000 | [diff] [blame] | 3552 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3553 | if (type_name.empty()) { |
| 3554 | type_name = type->FriendlyName(builder_->Symbols()); |
| 3555 | } |
| 3556 | auto constant_value = EvaluateConstantValue(expr, type); |
| 3557 | expr_info_.emplace( |
| 3558 | expr, ExpressionInfo{type, std::move(type_name), current_statement_, |
| 3559 | std::move(constant_value)}); |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3562 | bool Resolver::ValidatePipelineStages() { |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3563 | auto check_workgroup_storage = [&](FunctionInfo* func, |
| 3564 | FunctionInfo* entry_point) { |
| 3565 | auto stage = entry_point->declaration->pipeline_stage(); |
| 3566 | if (stage != ast::PipelineStage::kCompute) { |
| 3567 | for (auto* var : func->local_referenced_module_vars) { |
| 3568 | if (var->storage_class == ast::StorageClass::kWorkgroup) { |
| 3569 | std::stringstream stage_name; |
| 3570 | stage_name << stage; |
| 3571 | for (auto* user : var->users) { |
| 3572 | auto it = expr_info_.find(user->As<ast::Expression>()); |
| 3573 | if (it != expr_info_.end()) { |
| 3574 | if (func->declaration->symbol() == |
| 3575 | it->second.statement->Function()->symbol()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3576 | AddError("workgroup memory cannot be used by " + |
| 3577 | stage_name.str() + " pipeline stage", |
| 3578 | user->source()); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | } |
| 3582 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3583 | AddNote("variable is declared here", var->declaration->source()); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3584 | if (func != entry_point) { |
| 3585 | TraverseCallChain(entry_point, func, [&](FunctionInfo* f) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3586 | AddNote( |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3587 | "called by function '" + |
| 3588 | builder_->Symbols().NameFor(f->declaration->symbol()) + |
| 3589 | "'", |
| 3590 | f->declaration->source()); |
| 3591 | }); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3592 | AddNote("called by entry point '" + |
| 3593 | builder_->Symbols().NameFor( |
| 3594 | entry_point->declaration->symbol()) + |
| 3595 | "'", |
| 3596 | entry_point->declaration->source()); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3597 | } |
| 3598 | return false; |
| 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | return true; |
| 3603 | }; |
| 3604 | |
| 3605 | for (auto* entry_point : entry_points_) { |
| 3606 | if (!check_workgroup_storage(entry_point, entry_point)) { |
| 3607 | return false; |
| 3608 | } |
| 3609 | for (auto* func : entry_point->transitive_calls) { |
| 3610 | if (!check_workgroup_storage(func, entry_point)) { |
| 3611 | return false; |
| 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3616 | auto check_intrinsic_calls = [&](FunctionInfo* func, |
| 3617 | FunctionInfo* entry_point) { |
| 3618 | auto stage = entry_point->declaration->pipeline_stage(); |
| 3619 | for (auto& call : func->intrinsic_calls) { |
| 3620 | if (!call.intrinsic->SupportedStages().Contains(stage)) { |
| 3621 | std::stringstream err; |
| 3622 | err << "built-in cannot be used by " << stage << " pipeline stage"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3623 | AddError(err.str(), call.call->source()); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3624 | if (func != entry_point) { |
| 3625 | TraverseCallChain(entry_point, func, [&](FunctionInfo* f) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3626 | AddNote("called by function '" + |
| 3627 | builder_->Symbols().NameFor(f->declaration->symbol()) + |
| 3628 | "'", |
| 3629 | f->declaration->source()); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3630 | }); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3631 | AddNote("called by entry point '" + |
| 3632 | builder_->Symbols().NameFor( |
| 3633 | entry_point->declaration->symbol()) + |
| 3634 | "'", |
| 3635 | entry_point->declaration->source()); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3636 | } |
| 3637 | return false; |
| 3638 | } |
| 3639 | } |
| 3640 | return true; |
| 3641 | }; |
| 3642 | |
| 3643 | for (auto* entry_point : entry_points_) { |
| 3644 | if (!check_intrinsic_calls(entry_point, entry_point)) { |
| 3645 | return false; |
| 3646 | } |
| 3647 | for (auto* func : entry_point->transitive_calls) { |
| 3648 | if (!check_intrinsic_calls(func, entry_point)) { |
| 3649 | return false; |
| 3650 | } |
| 3651 | } |
| 3652 | } |
| 3653 | return true; |
| 3654 | } |
| 3655 | |
| 3656 | template <typename CALLBACK> |
| 3657 | void Resolver::TraverseCallChain(FunctionInfo* from, |
| 3658 | FunctionInfo* to, |
| 3659 | CALLBACK&& callback) const { |
| 3660 | for (auto* f : from->transitive_calls) { |
| 3661 | if (f == to) { |
| 3662 | callback(f); |
| 3663 | return; |
| 3664 | } |
| 3665 | if (f->transitive_calls.contains(to)) { |
| 3666 | TraverseCallChain(f, to, callback); |
| 3667 | callback(f); |
| 3668 | return; |
| 3669 | } |
| 3670 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3671 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3672 | << "TraverseCallChain() 'from' does not transitively call 'to'"; |
| 3673 | } |
| 3674 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 3675 | void Resolver::CreateSemanticNodes() const { |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3676 | auto& sem = builder_->Sem(); |
| 3677 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3678 | // Collate all the 'ancestor_entry_points' - this is a map of function |
| 3679 | // symbol to all the entry points that transitively call the function. |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3680 | std::unordered_map<Symbol, std::vector<Symbol>> ancestor_entry_points; |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3681 | for (auto* entry_point : entry_points_) { |
| 3682 | for (auto* call : entry_point->transitive_calls) { |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3683 | auto& vec = ancestor_entry_points[call->declaration->symbol()]; |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3684 | vec.emplace_back(entry_point->declaration->symbol()); |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3685 | } |
| 3686 | } |
| 3687 | |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3688 | // The next pipeline constant ID to try to allocate. |
| 3689 | uint16_t next_constant_id = 0; |
| 3690 | |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3691 | // Create semantic nodes for all ast::Variables |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3692 | std::unordered_map<const tint::ast::Variable*, sem::Parameter*> sem_params; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3693 | for (auto it : variable_to_info_) { |
| 3694 | auto* var = it.first; |
| 3695 | auto* info = it.second; |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3696 | |
| 3697 | sem::Variable* sem_var = nullptr; |
| 3698 | |
| 3699 | if (auto* override_deco = |
| 3700 | ast::GetDecoration<ast::OverrideDecoration>(var->decorations())) { |
| 3701 | // Create a pipeline overridable constant. |
| 3702 | uint16_t constant_id; |
| 3703 | if (override_deco->HasValue()) { |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 3704 | constant_id = static_cast<uint16_t>(override_deco->value()); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3705 | } else { |
| 3706 | // No ID was specified, so allocate the next available ID. |
| 3707 | constant_id = next_constant_id; |
| 3708 | while (constant_ids_.count(constant_id)) { |
| 3709 | if (constant_id == UINT16_MAX) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3710 | TINT_ICE(Resolver, builder_->Diagnostics()) |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3711 | << "no more pipeline constant IDs available"; |
| 3712 | return; |
| 3713 | } |
| 3714 | constant_id++; |
| 3715 | } |
| 3716 | next_constant_id = constant_id + 1; |
| 3717 | } |
| 3718 | |
James Price | e55e210 | 2021-06-18 09:27:13 +0000 | [diff] [blame] | 3719 | sem_var = |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3720 | builder_->create<sem::GlobalVariable>(var, info->type, constant_id); |
| 3721 | } else { |
| 3722 | switch (info->kind) { |
| 3723 | case VariableKind::kGlobal: |
| 3724 | sem_var = builder_->create<sem::GlobalVariable>( |
| 3725 | var, info->type, info->storage_class, info->access, |
| 3726 | info->binding_point); |
| 3727 | break; |
| 3728 | case VariableKind::kLocal: |
| 3729 | sem_var = builder_->create<sem::LocalVariable>( |
| 3730 | var, info->type, info->storage_class, info->access); |
| 3731 | break; |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3732 | case VariableKind::kParameter: { |
| 3733 | auto* param = builder_->create<sem::Parameter>( |
| 3734 | var, info->index, info->type, info->storage_class, info->access); |
| 3735 | sem_var = param; |
| 3736 | sem_params.emplace(var, param); |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3737 | break; |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3738 | } |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3739 | } |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3742 | std::vector<const sem::VariableUser*> users; |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 3743 | for (auto* user : info->users) { |
| 3744 | // Create semantic node for the identifier expression if necessary |
| 3745 | auto* sem_expr = sem.Get(user); |
| 3746 | if (sem_expr == nullptr) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3747 | auto& expr_info = expr_info_.at(user); |
| 3748 | auto* type = expr_info.type; |
| 3749 | auto* stmt = expr_info.statement; |
| 3750 | auto* sem_user = builder_->create<sem::VariableUser>( |
| 3751 | user, type, stmt, sem_var, expr_info.constant_value); |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3752 | sem_var->AddUser(sem_user); |
| 3753 | sem.Add(user, sem_user); |
| 3754 | } else { |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3755 | auto* sem_user = sem_expr->As<sem::VariableUser>(); |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3756 | if (!sem_user) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3757 | TINT_ICE(Resolver, diagnostics_) << "expected sem::VariableUser, got " |
| 3758 | << sem_expr->TypeInfo().name; |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3759 | } |
| 3760 | sem_var->AddUser(sem_user); |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 3761 | } |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 3762 | } |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3763 | sem.Add(var, sem_var); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) { |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3767 | std::vector<const sem::Variable*> out; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3768 | out.reserve(in.size()); |
| 3769 | for (auto* info : in) { |
| 3770 | out.emplace_back(sem.Get(info->declaration)); |
| 3771 | } |
| 3772 | return out; |
| 3773 | }; |
| 3774 | |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3775 | // Create semantic nodes for all ast::Functions |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3776 | std::unordered_map<FunctionInfo*, sem::Function*> func_info_to_sem_func; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 3777 | for (auto it : function_to_info_) { |
| 3778 | auto* func = it.first; |
| 3779 | auto* info = it.second; |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3780 | |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3781 | std::vector<sem::Parameter*> parameters; |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3782 | parameters.reserve(info->parameters.size()); |
| 3783 | for (auto* p : info->parameters) { |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3784 | parameters.emplace_back(sem_params.at(p->declaration)); |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3787 | auto* sem_func = builder_->create<sem::Function>( |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 3788 | info->declaration, const_cast<sem::Type*>(info->return_type), |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3789 | parameters, remap_vars(info->referenced_module_vars), |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 3790 | remap_vars(info->local_referenced_module_vars), info->return_statements, |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 3791 | info->callsites, ancestor_entry_points[func->symbol()], |
| 3792 | info->workgroup_size); |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 3793 | func_info_to_sem_func.emplace(info, sem_func); |
| 3794 | sem.Add(func, sem_func); |
| 3795 | } |
| 3796 | |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3797 | // Create semantic nodes for all ast::CallExpressions |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 3798 | for (auto it : function_calls_) { |
| 3799 | auto* call = it.first; |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3800 | auto info = it.second; |
| 3801 | auto* sem_func = func_info_to_sem_func.at(info.function); |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3802 | sem.Add(call, builder_->create<sem::Call>(call, sem_func, info.statement)); |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | // Create semantic nodes for all remaining expression types |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3806 | for (auto it : expr_info_) { |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3807 | auto* expr = it.first; |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3808 | auto& info = it.second; |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3809 | if (sem.Get(expr)) { |
| 3810 | // Expression has already been assigned a semantic node |
| 3811 | continue; |
| 3812 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3813 | sem.Add(expr, builder_->create<sem::Expression>( |
| 3814 | const_cast<ast::Expression*>(expr), info.type, |
| 3815 | info.statement, info.constant_value)); |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 3816 | } |
| 3817 | } |
| 3818 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3819 | sem::Array* Resolver::Array(const ast::Array* arr) { |
| 3820 | auto source = arr->source(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3821 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3822 | auto* el_ty = Type(arr->type()); |
| 3823 | if (!el_ty) { |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3824 | return nullptr; |
| 3825 | } |
| 3826 | |
Ken Rockot | ac9db20 | 2021-07-19 20:30:09 +0000 | [diff] [blame] | 3827 | if (!IsPlain(el_ty)) { // Check must come before GetDefaultAlignAndSize() |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3828 | AddError(el_ty->FriendlyName(builder_->Symbols()) + |
| 3829 | " cannot be used as an element type of an array", |
| 3830 | source); |
Ben Clayton | efb741f | 2021-06-03 08:24:55 +0000 | [diff] [blame] | 3831 | return nullptr; |
| 3832 | } |
| 3833 | |
Ben Clayton | fced350 | 2021-07-22 18:56:54 +0000 | [diff] [blame] | 3834 | uint32_t el_align = el_ty->Align(); |
| 3835 | uint32_t el_size = el_ty->Size(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3836 | |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 3837 | if (!ValidateNoDuplicateDecorations(arr->decorations())) { |
| 3838 | return nullptr; |
| 3839 | } |
| 3840 | |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3841 | // Look for explicit stride via [[stride(n)]] decoration |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3842 | uint32_t explicit_stride = 0; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3843 | for (auto* deco : arr->decorations()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3844 | Mark(deco); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3845 | if (auto* sd = deco->As<ast::StrideDecoration>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3846 | explicit_stride = sd->stride(); |
| 3847 | if (!ValidateArrayStrideDecoration(sd, el_size, el_align, source)) { |
Antonio Maiorano | fc03a46 | 2021-04-16 02:36:44 +0000 | [diff] [blame] | 3848 | return nullptr; |
| 3849 | } |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3850 | continue; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3851 | } |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3852 | |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3853 | AddError("decoration is not valid for array types", deco->source()); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3854 | return nullptr; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3855 | } |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3856 | |
| 3857 | // Calculate implicit stride |
Antonio Maiorano | fc03a46 | 2021-04-16 02:36:44 +0000 | [diff] [blame] | 3858 | auto implicit_stride = utils::RoundUp(el_align, el_size); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3859 | |
| 3860 | auto stride = explicit_stride ? explicit_stride : implicit_stride; |
| 3861 | |
| 3862 | // WebGPU requires runtime arrays have at least one element, but the AST |
| 3863 | // records an element count of 0 for it. |
| 3864 | auto size = std::max<uint32_t>(arr->size(), 1) * stride; |
| 3865 | auto* sem = builder_->create<sem::Array>(el_ty, arr->size(), el_align, size, |
Ben Clayton | 31936f3 | 2021-06-16 09:50:11 +0000 | [diff] [blame] | 3866 | stride, implicit_stride); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3867 | |
| 3868 | if (!ValidateArray(sem, source)) { |
| 3869 | return nullptr; |
| 3870 | } |
| 3871 | |
| 3872 | return sem; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3875 | bool Resolver::ValidateArray(const sem::Array* arr, const Source& source) { |
| 3876 | auto* el_ty = arr->ElemType(); |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3877 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3878 | if (auto* el_str = el_ty->As<sem::Struct>()) { |
| 3879 | if (el_str->IsBlockDecorated()) { |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3880 | // https://gpuweb.github.io/gpuweb/wgsl/#attributes |
| 3881 | // A structure type with the block attribute must not be: |
| 3882 | // * the element type of an array type |
| 3883 | // * the member type in another structure |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3884 | AddError( |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3885 | "A structure type with a [[block]] decoration cannot be used as an " |
| 3886 | "element of an array", |
| 3887 | source); |
| 3888 | return false; |
| 3889 | } |
| 3890 | } |
| 3891 | return true; |
| 3892 | } |
| 3893 | |
| 3894 | bool Resolver::ValidateArrayStrideDecoration(const ast::StrideDecoration* deco, |
| 3895 | uint32_t el_size, |
| 3896 | uint32_t el_align, |
| 3897 | const Source& source) { |
| 3898 | auto stride = deco->stride(); |
| 3899 | bool is_valid_stride = |
| 3900 | (stride >= el_size) && (stride >= el_align) && (stride % el_align == 0); |
| 3901 | if (!is_valid_stride) { |
| 3902 | // https://gpuweb.github.io/gpuweb/wgsl/#array-layout-rules |
| 3903 | // Arrays decorated with the stride attribute must have a stride that is |
| 3904 | // at least the size of the element type, and be a multiple of the |
| 3905 | // element type's alignment value. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3906 | AddError( |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3907 | "arrays decorated with the stride attribute must have a stride " |
| 3908 | "that is at least the size of the element type, and be a multiple " |
| 3909 | "of the element type's alignment value.", |
| 3910 | source); |
| 3911 | return false; |
| 3912 | } |
| 3913 | return true; |
| 3914 | } |
| 3915 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3916 | bool Resolver::ValidateStructure(const sem::Struct* str) { |
James Price | 37cabbb | 2021-07-01 08:13:41 +0000 | [diff] [blame] | 3917 | if (str->Members().empty()) { |
| 3918 | AddError("structures must have at least one member", |
| 3919 | str->Declaration()->source()); |
| 3920 | return false; |
| 3921 | } |
| 3922 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3923 | for (auto* member : str->Members()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3924 | if (auto* r = member->Type()->As<sem::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3925 | if (r->IsRuntimeSized()) { |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3926 | if (member != str->Members().back()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3927 | AddError( |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 3928 | "runtime arrays may only appear as the last member of a struct", |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 3929 | member->Declaration()->source()); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 3930 | return false; |
| 3931 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3932 | if (!str->IsBlockDecorated()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 3933 | AddError( |
| 3934 | "a struct containing a runtime-sized array " |
| 3935 | "requires the [[block]] attribute: '" + |
| 3936 | builder_->Symbols().NameFor(str->Declaration()->name()) + "'", |
| 3937 | member->Declaration()->source()); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 3938 | return false; |
| 3939 | } |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 3940 | } |
| 3941 | } |
| 3942 | |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 3943 | auto has_position = false; |
| 3944 | ast::InvariantDecoration* invariant_attribute = nullptr; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 3945 | for (auto* deco : member->Declaration()->decorations()) { |
Ben Clayton | 97668c8 | 2021-07-27 08:17:29 +0000 | [diff] [blame] | 3946 | if (!deco->IsAnyOf<ast::BuiltinDecoration, // |
| 3947 | ast::InternalDecoration, // |
| 3948 | ast::InterpolateDecoration, // |
| 3949 | ast::InvariantDecoration, // |
| 3950 | ast::LocationDecoration, // |
| 3951 | ast::StructMemberOffsetDecoration, // |
| 3952 | ast::StructMemberSizeDecoration, // |
| 3953 | ast::StructMemberAlignDecoration>()) { |
| 3954 | if (deco->Is<ast::StrideDecoration>() && |
| 3955 | IsValidationDisabled( |
| 3956 | member->Declaration()->decorations(), |
| 3957 | ast::DisabledValidation::kIgnoreStrideDecoration)) { |
| 3958 | continue; |
| 3959 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3960 | AddError("decoration is not valid for structure members", |
| 3961 | deco->source()); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 3962 | return false; |
| 3963 | } |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 3964 | if (auto* invariant = deco->As<ast::InvariantDecoration>()) { |
| 3965 | invariant_attribute = invariant; |
| 3966 | } |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 3967 | if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 3968 | if (!ValidateBuiltinDecoration(builtin, member->Type(), |
| 3969 | /* is_input */ false, |
| 3970 | /* is_struct_member */ true)) { |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 3971 | return false; |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 3972 | } |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 3973 | if (builtin->value() == ast::Builtin::kPosition) { |
| 3974 | has_position = true; |
| 3975 | } |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 3976 | } else if (auto* interpolate = deco->As<ast::InterpolateDecoration>()) { |
| 3977 | if (!ValidateInterpolateDecoration(interpolate, member->Type())) { |
| 3978 | return false; |
| 3979 | } |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 3980 | } |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 3981 | } |
Antonio Maiorano | 8b2be2d | 2021-06-18 19:57:57 +0000 | [diff] [blame] | 3982 | |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 3983 | if (invariant_attribute && !has_position) { |
| 3984 | AddError("invariant attribute must only be applied to a position builtin", |
| 3985 | invariant_attribute->source()); |
| 3986 | return false; |
| 3987 | } |
| 3988 | |
Antonio Maiorano | 8b2be2d | 2021-06-18 19:57:57 +0000 | [diff] [blame] | 3989 | if (auto* member_struct_type = member->Type()->As<sem::Struct>()) { |
| 3990 | if (auto* member_struct_type_block_decoration = |
| 3991 | ast::GetDecoration<ast::StructBlockDecoration>( |
| 3992 | member_struct_type->Declaration()->decorations())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3993 | AddError("structs must not contain [[block]] decorated struct members", |
| 3994 | member->Declaration()->source()); |
| 3995 | AddNote("see member's struct decoration here", |
| 3996 | member_struct_type_block_decoration->source()); |
Antonio Maiorano | 8b2be2d | 2021-06-18 19:57:57 +0000 | [diff] [blame] | 3997 | return false; |
| 3998 | } |
| 3999 | } |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4002 | for (auto* deco : str->Declaration()->decorations()) { |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4003 | if (!(deco->Is<ast::StructBlockDecoration>())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4004 | AddError("decoration is not valid for struct declarations", |
| 4005 | deco->source()); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4006 | return false; |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | return true; |
| 4011 | } |
| 4012 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4013 | sem::Struct* Resolver::Structure(const ast::Struct* str) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4014 | if (!ValidateNoDuplicateDecorations(str->decorations())) { |
| 4015 | return nullptr; |
| 4016 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4017 | for (auto* deco : str->decorations()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4018 | Mark(deco); |
| 4019 | } |
| 4020 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 4021 | sem::StructMemberList sem_members; |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4022 | sem_members.reserve(str->members().size()); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4023 | |
| 4024 | // Calculate the effective size and alignment of each field, and the overall |
| 4025 | // size of the structure. |
| 4026 | // For size, use the size attribute if provided, otherwise use the default |
| 4027 | // size for the type. |
| 4028 | // For alignment, use the alignment attribute if provided, otherwise use the |
| 4029 | // default alignment for the member type. |
| 4030 | // Diagnostic errors are raised if a basic rule is violated. |
| 4031 | // Validation of storage-class rules requires analysing the actual variable |
| 4032 | // usage of the structure, and so is performed as part of the variable |
| 4033 | // validation. |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4034 | uint32_t struct_size = 0; |
| 4035 | uint32_t struct_align = 1; |
Sarah | 0bbe24d | 2021-07-19 17:38:20 +0000 | [diff] [blame] | 4036 | std::unordered_map<Symbol, ast::StructMember*> member_map; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4037 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4038 | for (auto* member : str->members()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4039 | Mark(member); |
Sarah | 0bbe24d | 2021-07-19 17:38:20 +0000 | [diff] [blame] | 4040 | auto result = member_map.emplace(member->symbol(), member); |
| 4041 | if (!result.second) { |
| 4042 | AddError("redefinition of '" + |
| 4043 | builder_->Symbols().NameFor(member->symbol()) + "'", |
| 4044 | member->source()); |
| 4045 | AddNote("previous definition is here", result.first->second->source()); |
| 4046 | return nullptr; |
| 4047 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4048 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4049 | // Resolve member type |
| 4050 | auto* type = Type(member->type()); |
| 4051 | if (!type) { |
| 4052 | return nullptr; |
| 4053 | } |
Ben Clayton | 2ac55fe | 2021-04-28 14:13:13 +0000 | [diff] [blame] | 4054 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4055 | // Validate member type |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 4056 | if (!IsPlain(type)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4057 | AddError(type->FriendlyName(builder_->Symbols()) + |
| 4058 | " cannot be used as the type of a structure member", |
| 4059 | member->source()); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4060 | return nullptr; |
| 4061 | } |
| 4062 | |
| 4063 | uint32_t offset = struct_size; |
Ben Clayton | fced350 | 2021-07-22 18:56:54 +0000 | [diff] [blame] | 4064 | uint32_t align = type->Align(); |
| 4065 | uint32_t size = type->Size(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4066 | |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4067 | if (!ValidateNoDuplicateDecorations(member->decorations())) { |
| 4068 | return nullptr; |
| 4069 | } |
| 4070 | |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4071 | bool has_offset_deco = false; |
| 4072 | bool has_align_deco = false; |
| 4073 | bool has_size_deco = false; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4074 | for (auto* deco : member->decorations()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4075 | Mark(deco); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4076 | if (auto* o = deco->As<ast::StructMemberOffsetDecoration>()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4077 | // Offset decorations are not part of the WGSL spec, but are emitted |
| 4078 | // by the SPIR-V reader. |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4079 | if (o->offset() < struct_size) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4080 | AddError("offsets must be in ascending order", o->source()); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4081 | return nullptr; |
| 4082 | } |
| 4083 | offset = o->offset(); |
| 4084 | align = 1; |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4085 | has_offset_deco = true; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4086 | } else if (auto* a = deco->As<ast::StructMemberAlignDecoration>()) { |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 4087 | if (a->align() <= 0 || !utils::IsPowerOfTwo(a->align())) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4088 | AddError("align value must be a positive, power-of-two integer", |
| 4089 | a->source()); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4090 | return nullptr; |
| 4091 | } |
| 4092 | align = a->align(); |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4093 | has_align_deco = true; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4094 | } else if (auto* s = deco->As<ast::StructMemberSizeDecoration>()) { |
| 4095 | if (s->size() < size) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4096 | AddError("size must be at least as big as the type's size (" + |
| 4097 | std::to_string(size) + ")", |
| 4098 | s->source()); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4099 | return nullptr; |
| 4100 | } |
| 4101 | size = s->size(); |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4102 | has_size_deco = true; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4106 | if (has_offset_deco && (has_align_deco || has_size_deco)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4107 | AddError( |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4108 | "offset decorations cannot be used with align or size decorations", |
| 4109 | member->source()); |
| 4110 | return nullptr; |
| 4111 | } |
| 4112 | |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 4113 | offset = utils::RoundUp(align, offset); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4114 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4115 | auto* sem_member = builder_->create<sem::StructMember>( |
Ben Clayton | 053559d | 2021-07-23 16:43:01 +0000 | [diff] [blame] | 4116 | member, member->symbol(), const_cast<sem::Type*>(type), |
Ben Clayton | 2e1a284 | 2021-05-07 21:05:54 +0000 | [diff] [blame] | 4117 | static_cast<uint32_t>(sem_members.size()), offset, align, size); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4118 | builder_->Sem().Add(member, sem_member); |
| 4119 | sem_members.emplace_back(sem_member); |
| 4120 | |
| 4121 | struct_size = offset + size; |
| 4122 | struct_align = std::max(struct_align, align); |
| 4123 | } |
| 4124 | |
Ben Clayton | ad97343 | 2021-03-17 21:54:13 +0000 | [diff] [blame] | 4125 | auto size_no_padding = struct_size; |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 4126 | struct_size = utils::RoundUp(struct_align, struct_size); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4127 | |
Ben Clayton | 053559d | 2021-07-23 16:43:01 +0000 | [diff] [blame] | 4128 | auto* out = |
| 4129 | builder_->create<sem::Struct>(str, str->name(), sem_members, struct_align, |
| 4130 | struct_size, size_no_padding); |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 4131 | |
| 4132 | // Keep track of atomic members for validation after all usages have been |
| 4133 | // determined. |
| 4134 | for (size_t i = 0; i < sem_members.size(); i++) { |
| 4135 | if (sem_members[i]->Type()->Is<sem::Atomic>()) { |
| 4136 | atomic_members_.emplace_back(StructMember{out, i}); |
| 4137 | } |
| 4138 | } |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4139 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4140 | if (!ValidateStructure(out)) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4141 | return nullptr; |
| 4142 | } |
| 4143 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4144 | return out; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4147 | bool Resolver::ValidateReturn(const ast::ReturnStatement* ret) { |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 4148 | auto* func_type = current_function_->return_type; |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4149 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4150 | auto* ret_type = ret->has_value() ? TypeOf(ret->value())->UnwrapRef() |
Ben Clayton | cc98b4f | 2021-05-20 14:31:48 +0000 | [diff] [blame] | 4151 | : builder_->create<sem::Void>(); |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4152 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4153 | if (func_type->UnwrapRef() != ret_type) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4154 | AddError( |
| 4155 | "return statement type must match its function " |
| 4156 | "return type, returned '" + |
| 4157 | ret_type->FriendlyName(builder_->Symbols()) + "', expected '" + |
| 4158 | current_function_->return_type_name + "'", |
| 4159 | ret->source()); |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4160 | return false; |
| 4161 | } |
| 4162 | |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 4163 | auto* sem = builder_->Sem().Get(ret); |
| 4164 | if (auto* continuing = |
| 4165 | sem->FindFirstParent<sem::LoopContinuingBlockStatement>()) { |
| 4166 | AddError("continuing blocks must not contain a return statement", |
| 4167 | ret->source()); |
| 4168 | if (continuing != sem->Parent()) { |
| 4169 | AddNote("see continuing block here", continuing->Declaration()->source()); |
| 4170 | } |
| 4171 | return false; |
| 4172 | } |
| 4173 | |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4174 | return true; |
| 4175 | } |
| 4176 | |
| 4177 | bool Resolver::Return(ast::ReturnStatement* ret) { |
| 4178 | current_function_->return_statements.push_back(ret); |
| 4179 | |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4180 | if (auto* value = ret->value()) { |
| 4181 | Mark(value); |
James Price | 7e22196 | 2021-06-15 15:00:57 +0000 | [diff] [blame] | 4182 | if (!Expression(value)) { |
| 4183 | return false; |
| 4184 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
James Price | 7e22196 | 2021-06-15 15:00:57 +0000 | [diff] [blame] | 4187 | // Validate after processing the return value expression so that its type is |
| 4188 | // available for validation. |
| 4189 | return ValidateReturn(ret); |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4192 | bool Resolver::ValidateSwitch(const ast::SwitchStatement* s) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4193 | auto* cond_type = TypeOf(s->condition())->UnwrapRef(); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4194 | if (!cond_type->is_integer_scalar()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4195 | AddError( |
| 4196 | "switch statement selector expression must be of a " |
| 4197 | "scalar integer type", |
| 4198 | s->condition()->source()); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4199 | return false; |
| 4200 | } |
| 4201 | |
| 4202 | bool has_default = false; |
| 4203 | std::unordered_set<uint32_t> selector_set; |
| 4204 | |
| 4205 | for (auto* case_stmt : s->body()) { |
| 4206 | if (case_stmt->IsDefault()) { |
| 4207 | if (has_default) { |
| 4208 | // More than one default clause |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4209 | AddError("switch statement must have exactly one default clause", |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4210 | case_stmt->source()); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4211 | return false; |
| 4212 | } |
| 4213 | has_default = true; |
| 4214 | } |
| 4215 | |
| 4216 | for (auto* selector : case_stmt->selectors()) { |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 4217 | if (cond_type != TypeOf(selector)) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4218 | AddError( |
| 4219 | "the case selector values must have the same " |
| 4220 | "type as the selector expression.", |
| 4221 | case_stmt->source()); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4222 | return false; |
| 4223 | } |
| 4224 | |
| 4225 | auto v = selector->value_as_u32(); |
| 4226 | if (selector_set.find(v) != selector_set.end()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4227 | AddError( |
| 4228 | "a literal value must not appear more than once in " |
| 4229 | "the case selectors for a switch statement: '" + |
| 4230 | builder_->str(selector) + "'", |
| 4231 | case_stmt->source()); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4232 | return false; |
| 4233 | } |
| 4234 | selector_set.emplace(v); |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | if (!has_default) { |
| 4239 | // No default clause |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4240 | AddError("switch statement must have a default clause", s->source()); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4241 | return false; |
| 4242 | } |
| 4243 | |
| 4244 | if (!s->body().empty()) { |
| 4245 | auto* last_clause = s->body().back()->As<ast::CaseStatement>(); |
| 4246 | auto* last_stmt = last_clause->body()->last(); |
| 4247 | if (last_stmt && last_stmt->Is<ast::FallthroughStatement>()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4248 | AddError( |
| 4249 | "a fallthrough statement must not appear as " |
| 4250 | "the last statement in last clause of a switch", |
| 4251 | last_stmt->source()); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4252 | return false; |
| 4253 | } |
| 4254 | } |
| 4255 | |
| 4256 | return true; |
| 4257 | } |
| 4258 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4259 | bool Resolver::SwitchStatement(ast::SwitchStatement* stmt) { |
| 4260 | auto* sem = |
| 4261 | builder_->create<sem::SwitchStatement>(stmt, current_compound_statement_); |
| 4262 | builder_->Sem().Add(stmt, sem); |
| 4263 | return Scope(sem, [&] { |
| 4264 | Mark(stmt->condition()); |
| 4265 | if (!Expression(stmt->condition())) { |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4266 | return false; |
| 4267 | } |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4268 | for (auto* case_stmt : stmt->body()) { |
| 4269 | Mark(case_stmt); |
| 4270 | if (!CaseStatement(case_stmt)) { |
| 4271 | return false; |
| 4272 | } |
| 4273 | } |
| 4274 | if (!ValidateSwitch(stmt)) { |
| 4275 | return false; |
| 4276 | } |
| 4277 | return true; |
| 4278 | }); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Antonio Maiorano | e09989a | 2021-03-31 13:26:43 +0000 | [diff] [blame] | 4281 | bool Resolver::Assignment(ast::AssignmentStatement* a) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4282 | Mark(a->lhs()); |
| 4283 | Mark(a->rhs()); |
| 4284 | |
Antonio Maiorano | e09989a | 2021-03-31 13:26:43 +0000 | [diff] [blame] | 4285 | if (!Expression(a->lhs()) || !Expression(a->rhs())) { |
| 4286 | return false; |
| 4287 | } |
| 4288 | return ValidateAssignment(a); |
| 4289 | } |
| 4290 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4291 | bool Resolver::ValidateAssignment(const ast::AssignmentStatement* a) { |
| 4292 | // https://gpuweb.github.io/gpuweb/wgsl/#assignment-statement |
| 4293 | auto const* lhs_type = TypeOf(a->lhs()); |
| 4294 | auto const* rhs_type = TypeOf(a->rhs()); |
| 4295 | |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4296 | if (auto* ident = a->lhs()->As<ast::IdentifierExpression>()) { |
| 4297 | VariableInfo* var; |
| 4298 | if (variable_stack_.get(ident->symbol(), &var)) { |
| 4299 | if (var->kind == VariableKind::kParameter) { |
| 4300 | AddError("cannot assign to function parameter", a->lhs()->source()); |
| 4301 | AddNote("'" + builder_->Symbols().NameFor(ident->symbol()) + |
| 4302 | "' is declared here:", |
| 4303 | var->declaration->source()); |
| 4304 | return false; |
| 4305 | } |
| 4306 | if (var->declaration->is_const()) { |
| 4307 | AddError("cannot assign to const", a->lhs()->source()); |
| 4308 | AddNote("'" + builder_->Symbols().NameFor(ident->symbol()) + |
| 4309 | "' is declared here:", |
| 4310 | var->declaration->source()); |
| 4311 | return false; |
| 4312 | } |
| 4313 | } |
| 4314 | } |
| 4315 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4316 | auto* lhs_ref = lhs_type->As<sem::Reference>(); |
| 4317 | if (!lhs_ref) { |
| 4318 | // LHS is not a reference, so it has no storage. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4319 | AddError("cannot assign to value of type '" + TypeNameOf(a->lhs()) + "'", |
| 4320 | a->lhs()->source()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4321 | return false; |
| 4322 | } |
| 4323 | |
Antonio Maiorano | be607c0 | 2021-05-18 15:26:40 +0000 | [diff] [blame] | 4324 | auto* storage_type = lhs_ref->StoreType(); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4325 | auto* value_type = rhs_type->UnwrapRef(); // Implicit load of RHS |
| 4326 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4327 | // Value type has to match storage type |
| 4328 | if (storage_type != value_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4329 | AddError("cannot assign '" + TypeNameOf(a->rhs()) + "' to '" + |
| 4330 | TypeNameOf(a->lhs()) + "'", |
| 4331 | a->source()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4332 | return false; |
| 4333 | } |
Sarah | ebaa4b4 | 2021-06-15 22:39:19 +0000 | [diff] [blame] | 4334 | if (lhs_ref->Access() == ast::Access::kRead) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4335 | AddError( |
Sarah | ebaa4b4 | 2021-06-15 22:39:19 +0000 | [diff] [blame] | 4336 | "cannot store into a read-only type '" + TypeNameOf(a->lhs()) + "'", |
| 4337 | a->source()); |
| 4338 | return false; |
| 4339 | } |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4340 | return true; |
| 4341 | } |
| 4342 | |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 4343 | bool Resolver::ValidateNoDuplicateDefinition(Symbol sym, |
| 4344 | const Source& source, |
| 4345 | bool check_global_scope_only) { |
| 4346 | if (check_global_scope_only) { |
| 4347 | bool is_global = false; |
| 4348 | VariableInfo* var; |
| 4349 | if (variable_stack_.get(sym, &var, &is_global)) { |
| 4350 | if (is_global) { |
| 4351 | AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'", |
| 4352 | source); |
| 4353 | AddNote("previous definition is here", var->declaration->source()); |
| 4354 | return false; |
| 4355 | } |
| 4356 | } |
| 4357 | auto it = symbol_to_function_.find(sym); |
| 4358 | if (it != symbol_to_function_.end()) { |
| 4359 | AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'", |
| 4360 | source); |
| 4361 | AddNote("previous definition is here", it->second->declaration->source()); |
| 4362 | return false; |
| 4363 | } |
| 4364 | } else { |
| 4365 | VariableInfo* var; |
| 4366 | if (variable_stack_.get(sym, &var)) { |
| 4367 | AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'", |
| 4368 | source); |
| 4369 | AddNote("previous definition is here", var->declaration->source()); |
| 4370 | return false; |
| 4371 | } |
| 4372 | } |
| 4373 | return true; |
| 4374 | } |
| 4375 | |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4376 | bool Resolver::ValidateNoDuplicateDecorations( |
| 4377 | const ast::DecorationList& decorations) { |
| 4378 | std::unordered_map<const TypeInfo*, Source> seen; |
| 4379 | for (auto* d : decorations) { |
| 4380 | auto res = seen.emplace(&d->TypeInfo(), d->source()); |
James Price | 4daac0e | 2021-06-15 16:33:57 +0000 | [diff] [blame] | 4381 | if (!res.second && !d->Is<ast::InternalDecoration>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4382 | AddError("duplicate " + d->name() + " decoration", d->source()); |
| 4383 | AddNote("first decoration declared here", res.first->second); |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4384 | return false; |
| 4385 | } |
| 4386 | } |
| 4387 | return true; |
| 4388 | } |
| 4389 | |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4390 | bool Resolver::ApplyStorageClassUsageToType(ast::StorageClass sc, |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4391 | sem::Type* ty, |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 4392 | const Source& usage) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4393 | ty = const_cast<sem::Type*>(ty->UnwrapRef()); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4394 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4395 | if (auto* str = ty->As<sem::Struct>()) { |
| 4396 | if (str->StorageClassUsage().count(sc)) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4397 | return true; // Already applied |
| 4398 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4399 | |
| 4400 | str->AddUsage(sc); |
| 4401 | |
| 4402 | for (auto* member : str->Members()) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4403 | if (!ApplyStorageClassUsageToType(sc, member->Type(), usage)) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4404 | std::stringstream err; |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4405 | err << "while analysing structure member " |
| 4406 | << str->FriendlyName(builder_->Symbols()) << "." |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4407 | << builder_->Symbols().NameFor(member->Declaration()->symbol()); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4408 | AddNote(err.str(), member->Declaration()->source()); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4409 | return false; |
| 4410 | } |
| 4411 | } |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4412 | return true; |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 4415 | if (auto* arr = ty->As<sem::Array>()) { |
| 4416 | return ApplyStorageClassUsageToType( |
| 4417 | sc, const_cast<sem::Type*>(arr->ElemType()), usage); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 4420 | if (ast::IsHostShareable(sc) && !IsHostShareable(ty)) { |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4421 | std::stringstream err; |
| 4422 | err << "Type '" << ty->FriendlyName(builder_->Symbols()) |
| 4423 | << "' cannot be used in storage class '" << sc |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 4424 | << "' as it is non-host-shareable"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4425 | AddError(err.str(), usage); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4426 | return false; |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | return true; |
| 4430 | } |
| 4431 | |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 4432 | template <typename F> |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4433 | bool Resolver::Scope(sem::CompoundStatement* stmt, F&& callback) { |
| 4434 | auto* prev_current_statement = current_statement_; |
| 4435 | auto* prev_current_compound_statement = current_compound_statement_; |
| 4436 | auto* prev_current_block = current_block_; |
| 4437 | current_statement_ = stmt; |
| 4438 | current_compound_statement_ = stmt; |
| 4439 | current_block_ = stmt->As<sem::BlockStatement>(); |
Antonio Maiorano | 4682e3f | 2021-03-22 17:38:45 +0000 | [diff] [blame] | 4440 | variable_stack_.push_scope(); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4441 | |
| 4442 | TINT_DEFER({ |
| 4443 | TINT_DEFER(variable_stack_.pop_scope()); |
| 4444 | current_block_ = prev_current_block; |
| 4445 | current_compound_statement_ = prev_current_compound_statement; |
| 4446 | current_statement_ = prev_current_statement; |
| 4447 | }); |
| 4448 | |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 4449 | return callback(); |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 4452 | std::string Resolver::VectorPretty(uint32_t size, |
| 4453 | const sem::Type* element_type) { |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 4454 | sem::Vector vec_type(element_type, size); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 4455 | return vec_type.FriendlyName(builder_->Symbols()); |
| 4456 | } |
| 4457 | |
Ben Clayton | 12ed13d | 2021-04-28 12:38:13 +0000 | [diff] [blame] | 4458 | void Resolver::Mark(const ast::Node* node) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4459 | if (node == nullptr) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4460 | TINT_ICE(Resolver, diagnostics_) << "Resolver::Mark() called with nullptr"; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4461 | } |
| 4462 | if (marked_.emplace(node).second) { |
| 4463 | return; |
| 4464 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4465 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4466 | << "AST node '" << node->TypeInfo().name |
| 4467 | << "' was encountered twice in the same AST of a Program\n" |
Ben Clayton | f0756e3 | 2021-04-29 20:57:55 +0000 | [diff] [blame] | 4468 | << "At: " << node->source() << "\n" |
| 4469 | << "Content: " << builder_->str(node) << "\n" |
| 4470 | << "Pointer: " << node; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4473 | void Resolver::AddError(const std::string& msg, const Source& source) const { |
| 4474 | diagnostics_.add_error(diag::System::Resolver, msg, source); |
| 4475 | } |
| 4476 | |
| 4477 | void Resolver::AddWarning(const std::string& msg, const Source& source) const { |
| 4478 | diagnostics_.add_warning(diag::System::Resolver, msg, source); |
| 4479 | } |
| 4480 | |
| 4481 | void Resolver::AddNote(const std::string& msg, const Source& source) const { |
| 4482 | diagnostics_.add_note(diag::System::Resolver, msg, source); |
| 4483 | } |
| 4484 | |
Ben Clayton | 12ed13d | 2021-04-28 12:38:13 +0000 | [diff] [blame] | 4485 | Resolver::VariableInfo::VariableInfo(const ast::Variable* decl, |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4486 | sem::Type* ty, |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 4487 | const std::string& tn, |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4488 | ast::StorageClass sc, |
Ryan Harrison | 3832b8e | 2021-06-09 20:45:09 +0000 | [diff] [blame] | 4489 | ast::Access ac, |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 4490 | VariableKind k, |
| 4491 | uint32_t idx) |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 4492 | : declaration(decl), |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4493 | type(ty), |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 4494 | type_name(tn), |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4495 | storage_class(sc), |
Ryan Harrison | 3832b8e | 2021-06-09 20:45:09 +0000 | [diff] [blame] | 4496 | access(ac), |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 4497 | kind(k), |
| 4498 | index(idx) {} |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 4499 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 4500 | Resolver::VariableInfo::~VariableInfo() = default; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 4501 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 4502 | Resolver::FunctionInfo::FunctionInfo(ast::Function* decl) : declaration(decl) {} |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 4503 | Resolver::FunctionInfo::~FunctionInfo() = default; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 4504 | |
Ben Clayton | c7dcbae | 2021-03-09 15:08:48 +0000 | [diff] [blame] | 4505 | } // namespace resolver |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 4506 | } // namespace tint |