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 | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 20 | #include <limits> |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 21 | #include <utility> |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 22 | |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 23 | #include "src/ast/alias.h" |
| 24 | #include "src/ast/array.h" |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 25 | #include "src/ast/assignment_statement.h" |
dan sinclair | a7d498e | 2020-09-22 22:07:13 +0000 | [diff] [blame] | 26 | #include "src/ast/bitcast_expression.h" |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 27 | #include "src/ast/break_statement.h" |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 28 | #include "src/ast/call_statement.h" |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 29 | #include "src/ast/continue_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 30 | #include "src/ast/depth_texture.h" |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 31 | #include "src/ast/disable_validation_decoration.h" |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 32 | #include "src/ast/discard_statement.h" |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 33 | #include "src/ast/fallthrough_statement.h" |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 34 | #include "src/ast/for_loop_statement.h" |
dan sinclair | 91c44a5 | 2020-04-07 12:55:25 +0000 | [diff] [blame] | 35 | #include "src/ast/if_statement.h" |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 36 | #include "src/ast/internal_decoration.h" |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 37 | #include "src/ast/interpolate_decoration.h" |
dan sinclair | bc71eda | 2020-04-07 12:55:51 +0000 | [diff] [blame] | 38 | #include "src/ast/loop_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 39 | #include "src/ast/matrix.h" |
James Price | e87ded8 | 2021-04-30 17:14:19 +0000 | [diff] [blame] | 40 | #include "src/ast/override_decoration.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 41 | #include "src/ast/pointer.h" |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 42 | #include "src/ast/return_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 43 | #include "src/ast/sampled_texture.h" |
| 44 | #include "src/ast/sampler.h" |
| 45 | #include "src/ast/storage_texture.h" |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 46 | #include "src/ast/struct_block_decoration.h" |
dan sinclair | 18b3285 | 2020-04-07 12:56:45 +0000 | [diff] [blame] | 47 | #include "src/ast/switch_statement.h" |
Ben Clayton | f164a4a | 2021-10-21 20:38:54 +0000 | [diff] [blame] | 48 | #include "src/ast/traverse_expressions.h" |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 49 | #include "src/ast/type_name.h" |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 50 | #include "src/ast/unary_op_expression.h" |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 51 | #include "src/ast/variable_decl_statement.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 52 | #include "src/ast/vector.h" |
Antonio Maiorano | bfc9794 | 2021-04-06 20:41:07 +0000 | [diff] [blame] | 53 | #include "src/ast/workgroup_decoration.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 54 | #include "src/sem/array.h" |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 55 | #include "src/sem/atomic_type.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 56 | #include "src/sem/call.h" |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 57 | #include "src/sem/depth_multisampled_texture_type.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 58 | #include "src/sem/depth_texture_type.h" |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 59 | #include "src/sem/for_loop_statement.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 60 | #include "src/sem/function.h" |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 61 | #include "src/sem/if_statement.h" |
| 62 | #include "src/sem/loop_statement.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 63 | #include "src/sem/member_accessor_expression.h" |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 64 | #include "src/sem/multisampled_texture_type.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 65 | #include "src/sem/pointer_type.h" |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 66 | #include "src/sem/reference_type.h" |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 67 | #include "src/sem/sampled_texture_type.h" |
| 68 | #include "src/sem/sampler_type.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 69 | #include "src/sem/statement.h" |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 70 | #include "src/sem/storage_texture_type.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 71 | #include "src/sem/struct.h" |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 72 | #include "src/sem/switch_statement.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 73 | #include "src/sem/variable.h" |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 74 | #include "src/utils/defer.h" |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 75 | #include "src/utils/get_or_create.h" |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 76 | #include "src/utils/math.h" |
Ben Clayton | b7bcbf0 | 2021-09-08 15:18:36 +0000 | [diff] [blame] | 77 | #include "src/utils/reverse.h" |
Ben Clayton | b350500 | 2021-05-19 16:11:04 +0000 | [diff] [blame] | 78 | #include "src/utils/scoped_assignment.h" |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 79 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 80 | namespace tint { |
Ben Clayton | c7dcbae | 2021-03-09 15:08:48 +0000 | [diff] [blame] | 81 | namespace resolver { |
Ben Clayton | 052ab89 | 2021-02-08 19:53:42 +0000 | [diff] [blame] | 82 | namespace { |
| 83 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 84 | using IntrinsicType = tint::sem::IntrinsicType; |
Ben Clayton | 052ab89 | 2021-02-08 19:53:42 +0000 | [diff] [blame] | 85 | |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 86 | bool IsValidStorageTextureDimension(ast::TextureDimension dim) { |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 87 | switch (dim) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 88 | case ast::TextureDimension::k1d: |
| 89 | case ast::TextureDimension::k2d: |
| 90 | case ast::TextureDimension::k2dArray: |
| 91 | case ast::TextureDimension::k3d: |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 92 | return true; |
| 93 | default: |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 98 | bool IsValidStorageTextureImageFormat(ast::ImageFormat format) { |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 99 | switch (format) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 100 | case ast::ImageFormat::kR32Uint: |
| 101 | case ast::ImageFormat::kR32Sint: |
| 102 | case ast::ImageFormat::kR32Float: |
| 103 | case ast::ImageFormat::kRg32Uint: |
| 104 | case ast::ImageFormat::kRg32Sint: |
| 105 | case ast::ImageFormat::kRg32Float: |
| 106 | case ast::ImageFormat::kRgba8Unorm: |
| 107 | case ast::ImageFormat::kRgba8Snorm: |
| 108 | case ast::ImageFormat::kRgba8Uint: |
| 109 | case ast::ImageFormat::kRgba8Sint: |
| 110 | case ast::ImageFormat::kRgba16Uint: |
| 111 | case ast::ImageFormat::kRgba16Sint: |
| 112 | case ast::ImageFormat::kRgba16Float: |
| 113 | case ast::ImageFormat::kRgba32Uint: |
| 114 | case ast::ImageFormat::kRgba32Sint: |
| 115 | case ast::ImageFormat::kRgba32Float: |
Ryan Harrison | de8f133 | 2021-04-20 20:47:51 +0000 | [diff] [blame] | 116 | return true; |
| 117 | default: |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 122 | /// @returns true if the decoration list contains a |
| 123 | /// ast::DisableValidationDecoration with the validation mode equal to |
| 124 | /// `validation` |
| 125 | bool IsValidationDisabled(const ast::DecorationList& decorations, |
| 126 | ast::DisabledValidation validation) { |
| 127 | for (auto* decoration : decorations) { |
| 128 | if (auto* dv = decoration->As<ast::DisableValidationDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 129 | if (dv->validation == validation) { |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 137 | /// @returns true if the decoration list does not contains a |
| 138 | /// ast::DisableValidationDecoration with the validation mode equal to |
| 139 | /// `validation` |
| 140 | bool IsValidationEnabled(const ast::DecorationList& decorations, |
| 141 | ast::DisabledValidation validation) { |
| 142 | return !IsValidationDisabled(decorations, validation); |
| 143 | } |
| 144 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 145 | // Helper to stringify a pipeline IO decoration. |
| 146 | std::string deco_to_str(const ast::Decoration* deco) { |
| 147 | std::stringstream str; |
| 148 | if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 149 | str << "builtin(" << builtin->builtin << ")"; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 150 | } else if (auto* location = deco->As<ast::LocationDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 151 | str << "location(" << location->value << ")"; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 152 | } |
| 153 | return str.str(); |
| 154 | } |
Ben Clayton | 052ab89 | 2021-02-08 19:53:42 +0000 | [diff] [blame] | 155 | } // namespace |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 156 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 157 | Resolver::Resolver(ProgramBuilder* builder) |
Ben Clayton | b82acac | 2021-05-06 16:04:03 +0000 | [diff] [blame] | 158 | : builder_(builder), |
| 159 | diagnostics_(builder->Diagnostics()), |
Ben Clayton | b29a59d | 2021-06-01 19:06:31 +0000 | [diff] [blame] | 160 | intrinsic_table_(IntrinsicTable::Create(*builder)) {} |
Ben Clayton | d59cedb | 2021-01-25 18:14:08 +0000 | [diff] [blame] | 161 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 162 | Resolver::~Resolver() = default; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 163 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 164 | void Resolver::set_referenced_from_function_if_needed(VariableInfo* var, |
| 165 | bool local) { |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 166 | if (current_function_ == nullptr) { |
| 167 | return; |
| 168 | } |
Ryan Harrison | 3832b8e | 2021-06-09 20:45:09 +0000 | [diff] [blame] | 169 | |
| 170 | if (var->kind != VariableKind::kGlobal) { |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Ben Clayton | f81df12 | 2021-02-17 13:10:49 +0000 | [diff] [blame] | 174 | current_function_->referenced_module_vars.add(var); |
Enrico Galli | 3d449d2 | 2020-12-08 21:07:24 +0000 | [diff] [blame] | 175 | if (local) { |
Ben Clayton | f81df12 | 2021-02-17 13:10:49 +0000 | [diff] [blame] | 176 | current_function_->local_referenced_module_vars.add(var); |
Enrico Galli | 3d449d2 | 2020-12-08 21:07:24 +0000 | [diff] [blame] | 177 | } |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 180 | bool Resolver::Resolve() { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 181 | if (builder_->Diagnostics().contains_errors()) { |
| 182 | return false; |
| 183 | } |
| 184 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 185 | bool result = ResolveInternal(); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 186 | |
Ben Clayton | b82acac | 2021-05-06 16:04:03 +0000 | [diff] [blame] | 187 | if (!result && !diagnostics_.contains_errors()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 188 | TINT_ICE(Resolver, diagnostics_) |
| 189 | << "resolving failed, but no error was raised"; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 193 | // Even if resolving failed, create all the semantic nodes for information we |
| 194 | // did generate. |
| 195 | CreateSemanticNodes(); |
| 196 | |
| 197 | return result; |
| 198 | } |
| 199 | |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 200 | // https://gpuweb.github.io/gpuweb/wgsl/#plain-types-section |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 201 | bool Resolver::IsPlain(const sem::Type* type) const { |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 202 | return type->is_scalar() || type->Is<sem::Atomic>() || |
| 203 | type->Is<sem::Vector>() || type->Is<sem::Matrix>() || |
| 204 | type->Is<sem::Array>() || type->Is<sem::Struct>(); |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // https://gpuweb.github.io/gpuweb/wgsl.html#storable-types |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 208 | bool Resolver::IsStorable(const sem::Type* type) const { |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 209 | return IsPlain(type) || type->Is<sem::Texture>() || type->Is<sem::Sampler>(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 212 | // https://gpuweb.github.io/gpuweb/wgsl.html#host-shareable-types |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 213 | bool Resolver::IsHostShareable(const sem::Type* type) const { |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 214 | if (type->IsAnyOf<sem::I32, sem::U32, sem::F32>()) { |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 215 | return true; |
| 216 | } |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 217 | if (auto* vec = type->As<sem::Vector>()) { |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 218 | return IsHostShareable(vec->type()); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 219 | } |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 220 | if (auto* mat = type->As<sem::Matrix>()) { |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 221 | return IsHostShareable(mat->type()); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 222 | } |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 223 | if (auto* arr = type->As<sem::Array>()) { |
| 224 | return IsHostShareable(arr->ElemType()); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 225 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 226 | if (auto* str = type->As<sem::Struct>()) { |
| 227 | for (auto* member : str->Members()) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 228 | if (!IsHostShareable(member->Type())) { |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | return true; |
| 233 | } |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 234 | if (auto* atomic = type->As<sem::Atomic>()) { |
| 235 | return IsHostShareable(atomic->Type()); |
| 236 | } |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 240 | bool Resolver::ResolveInternal() { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 241 | Mark(&builder_->AST()); |
| 242 | |
Antonio Maiorano | bb5617f | 2021-03-19 18:45:30 +0000 | [diff] [blame] | 243 | // Process everything else in the order they appear in the module. This is |
| 244 | // necessary for validation of use-before-declaration. |
| 245 | for (auto* decl : builder_->AST().GlobalDeclarations()) { |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 246 | if (auto* td = decl->As<ast::TypeDecl>()) { |
| 247 | Mark(td); |
| 248 | if (!TypeDecl(td)) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 249 | return false; |
Antonio Maiorano | bb5617f | 2021-03-19 18:45:30 +0000 | [diff] [blame] | 250 | } |
| 251 | } else if (auto* func = decl->As<ast::Function>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 252 | Mark(func); |
Antonio Maiorano | bb5617f | 2021-03-19 18:45:30 +0000 | [diff] [blame] | 253 | if (!Function(func)) { |
| 254 | return false; |
| 255 | } |
| 256 | } else if (auto* var = decl->As<ast::Variable>()) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 257 | Mark(var); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 258 | if (!GlobalVariable(var)) { |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 259 | return false; |
| 260 | } |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 261 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 262 | TINT_UNREACHABLE(Resolver, diagnostics_) |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 263 | << "unhandled global declaration: " << decl->TypeInfo().name; |
| 264 | return false; |
dan sinclair | 7be237a | 2020-06-15 20:55:09 +0000 | [diff] [blame] | 265 | } |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 266 | } |
| 267 | |
James Price | ee23c17 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 268 | AllocateOverridableConstantIds(); |
| 269 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 270 | if (!ValidatePipelineStages()) { |
| 271 | return false; |
| 272 | } |
| 273 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 274 | bool result = true; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 275 | for (auto* node : builder_->ASTNodes().Objects()) { |
| 276 | if (marked_.count(node) == 0) { |
Ben Clayton | 11ed0db | 2021-10-14 20:30:29 +0000 | [diff] [blame] | 277 | TINT_ICE(Resolver, diagnostics_) << "AST node '" << node->TypeInfo().name |
| 278 | << "' was not reached by the resolver\n" |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 279 | << "At: " << node->source << "\n" |
Ben Clayton | 11ed0db | 2021-10-14 20:30:29 +0000 | [diff] [blame] | 280 | << "Pointer: " << node; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 281 | result = false; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 285 | return result; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 288 | sem::Type* Resolver::Type(const ast::Type* ty) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 289 | Mark(ty); |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 290 | auto* s = [&]() -> sem::Type* { |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 291 | if (ty->Is<ast::Void>()) { |
| 292 | return builder_->create<sem::Void>(); |
| 293 | } |
| 294 | if (ty->Is<ast::Bool>()) { |
| 295 | return builder_->create<sem::Bool>(); |
| 296 | } |
| 297 | if (ty->Is<ast::I32>()) { |
| 298 | return builder_->create<sem::I32>(); |
| 299 | } |
| 300 | if (ty->Is<ast::U32>()) { |
| 301 | return builder_->create<sem::U32>(); |
| 302 | } |
| 303 | if (ty->Is<ast::F32>()) { |
| 304 | return builder_->create<sem::F32>(); |
| 305 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 306 | if (auto* t = ty->As<ast::Vector>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 307 | if (auto* el = Type(t->type)) { |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 308 | if (auto* vector = builder_->create<sem::Vector>(el, t->width)) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 309 | if (ValidateVector(vector, t->source)) { |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 310 | return vector; |
| 311 | } |
| 312 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 313 | } |
| 314 | return nullptr; |
| 315 | } |
| 316 | if (auto* t = ty->As<ast::Matrix>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 317 | if (auto* el = Type(t->type)) { |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 318 | if (auto* column_type = builder_->create<sem::Vector>(el, t->rows)) { |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 319 | if (auto* matrix = |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 320 | builder_->create<sem::Matrix>(column_type, t->columns)) { |
| 321 | if (ValidateMatrix(matrix, t->source)) { |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 322 | return matrix; |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 326 | } |
| 327 | return nullptr; |
| 328 | } |
| 329 | if (auto* t = ty->As<ast::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 330 | return Array(t); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 331 | } |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 332 | if (auto* t = ty->As<ast::Atomic>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 333 | if (auto* el = Type(t->type)) { |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 334 | auto* a = builder_->create<sem::Atomic>(el); |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 335 | if (!ValidateAtomic(t, a)) { |
| 336 | return nullptr; |
| 337 | } |
| 338 | return a; |
| 339 | } |
| 340 | return nullptr; |
| 341 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 342 | if (auto* t = ty->As<ast::Pointer>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 343 | if (auto* el = Type(t->type)) { |
| 344 | auto access = t->access; |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 345 | if (access == ast::kUndefined) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 346 | access = DefaultAccessForStorageClass(t->storage_class); |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 347 | } |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 348 | return builder_->create<sem::Pointer>(el, t->storage_class, access); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 349 | } |
| 350 | return nullptr; |
| 351 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 352 | if (auto* t = ty->As<ast::Sampler>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 353 | return builder_->create<sem::Sampler>(t->kind); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 354 | } |
| 355 | if (auto* t = ty->As<ast::SampledTexture>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 356 | if (auto* el = Type(t->type)) { |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 357 | return builder_->create<sem::SampledTexture>(t->dim, el); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 358 | } |
| 359 | return nullptr; |
| 360 | } |
| 361 | if (auto* t = ty->As<ast::MultisampledTexture>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 362 | if (auto* el = Type(t->type)) { |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 363 | return builder_->create<sem::MultisampledTexture>(t->dim, el); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 364 | } |
| 365 | return nullptr; |
| 366 | } |
| 367 | if (auto* t = ty->As<ast::DepthTexture>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 368 | return builder_->create<sem::DepthTexture>(t->dim); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 369 | } |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 370 | if (auto* t = ty->As<ast::DepthMultisampledTexture>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 371 | return builder_->create<sem::DepthMultisampledTexture>(t->dim); |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 372 | } |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 373 | if (auto* t = ty->As<ast::StorageTexture>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 374 | if (auto* el = Type(t->type)) { |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 375 | if (!ValidateStorageTexture(t)) { |
Antonio Maiorano | 6cf7f2e | 2021-05-21 14:32:38 +0000 | [diff] [blame] | 376 | return nullptr; |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 377 | } |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 378 | return builder_->create<sem::StorageTexture>(t->dim, t->format, |
| 379 | t->access, el); |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 380 | } |
| 381 | return nullptr; |
| 382 | } |
Ryan Harrison | 0aeb77c | 2021-05-06 08:20:52 +0000 | [diff] [blame] | 383 | if (ty->As<ast::ExternalTexture>()) { |
Ben Clayton | 58f93c9 | 2021-05-05 18:00:52 +0000 | [diff] [blame] | 384 | return builder_->create<sem::ExternalTexture>(); |
| 385 | } |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 386 | if (auto* t = ty->As<ast::TypeName>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 387 | auto it = named_type_info_.find(t->name); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 388 | if (it == named_type_info_.end()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 389 | AddError("unknown type '" + builder_->Symbols().NameFor(t->name) + "'", |
| 390 | t->source); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 391 | return nullptr; |
| 392 | } |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 393 | return it->second.sem; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 394 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 395 | TINT_UNREACHABLE(Resolver, diagnostics_) |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 396 | << "Unhandled ast::Type: " << ty->TypeInfo().name; |
| 397 | return nullptr; |
| 398 | }(); |
| 399 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 400 | if (s) { |
| 401 | builder_->Sem().Add(ty, s); |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 402 | } |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 403 | return s; |
| 404 | } |
| 405 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 406 | bool Resolver::ValidateAtomic(const ast::Atomic* a, const sem::Atomic* s) { |
| 407 | // https://gpuweb.github.io/gpuweb/wgsl/#atomic-types |
| 408 | // T must be either u32 or i32. |
| 409 | if (!s->Type()->IsAnyOf<sem::U32, sem::I32>()) { |
Ryan Harrison | 3bcbfc78 | 2021-08-11 19:59:44 +0000 | [diff] [blame] | 410 | AddError("atomic only supports i32 or u32 types", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 411 | a->type ? a->type->source : a->source); |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | return true; |
| 415 | } |
| 416 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 417 | bool Resolver::ValidateStorageTexture(const ast::StorageTexture* t) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 418 | switch (t->access) { |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 419 | case ast::Access::kWrite: |
| 420 | break; |
Ben Clayton | f3f2d0a | 2021-10-14 10:10:45 +0000 | [diff] [blame] | 421 | case ast::Access::kUndefined: |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 422 | AddError("storage texture missing access control", t->source); |
Ben Clayton | f3f2d0a | 2021-10-14 10:10:45 +0000 | [diff] [blame] | 423 | return false; |
| 424 | default: |
| 425 | AddError("storage textures currently only support 'write' access control", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 426 | t->source); |
Ben Clayton | f3f2d0a | 2021-10-14 10:10:45 +0000 | [diff] [blame] | 427 | return false; |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 430 | if (!IsValidStorageTextureDimension(t->dim)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 431 | AddError("cube dimensions for storage textures are not supported", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 432 | t->source); |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 436 | if (!IsValidStorageTextureImageFormat(t->format)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 437 | AddError( |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 438 | "image format must be one of the texel formats specified for storage " |
| 439 | "textues in https://gpuweb.github.io/gpuweb/wgsl/#texel-formats", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 440 | t->source); |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 446 | Resolver::VariableInfo* Resolver::Variable(const ast::Variable* var, |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 447 | VariableKind kind, |
| 448 | uint32_t index /* = 0 */) { |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 449 | if (variable_to_info_.count(var)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 450 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 451 | << "Variable " << builder_->Symbols().NameFor(var->symbol) |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 452 | << " already resolved"; |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 453 | return nullptr; |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 456 | std::string type_name; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 457 | const sem::Type* storage_type = nullptr; |
| 458 | |
| 459 | // If the variable has a declared type, resolve it. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 460 | if (auto* ty = var->type) { |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 461 | type_name = ty->FriendlyName(builder_->Symbols()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 462 | storage_type = Type(ty); |
| 463 | if (!storage_type) { |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 464 | return nullptr; |
| 465 | } |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 466 | } |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 467 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 468 | std::string rhs_type_name; |
| 469 | const sem::Type* rhs_type = nullptr; |
| 470 | |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 471 | // Does the variable have a constructor? |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 472 | if (auto* ctor = var->constructor) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 473 | if (!Expression(var->constructor)) { |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 474 | return nullptr; |
| 475 | } |
| 476 | |
| 477 | // Fetch the constructor's type |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 478 | rhs_type_name = TypeNameOf(ctor); |
| 479 | rhs_type = TypeOf(ctor); |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 480 | if (!rhs_type) { |
| 481 | return nullptr; |
| 482 | } |
| 483 | |
| 484 | // If the variable has no declared type, infer it from the RHS |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 485 | if (!storage_type) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 486 | if (!var->is_const && kind == VariableKind::kGlobal) { |
| 487 | AddError("global var declaration must specify a type", var->source); |
Antonio Maiorano | 9834fef | 2021-06-04 15:28:47 +0000 | [diff] [blame] | 488 | return nullptr; |
| 489 | } |
| 490 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 491 | type_name = rhs_type_name; |
| 492 | storage_type = rhs_type->UnwrapRef(); // Implicit load of RHS |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 493 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 494 | } else if (var->is_const && kind != VariableKind::kParameter && |
| 495 | !ast::HasDecoration<ast::OverrideDecoration>(var->decorations)) { |
| 496 | AddError("let declaration must have an initializer", var->source); |
Ben Clayton | b291cfc | 2021-07-19 19:50:52 +0000 | [diff] [blame] | 497 | return nullptr; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 498 | } else if (!var->type) { |
Ben Clayton | b291cfc | 2021-07-19 19:50:52 +0000 | [diff] [blame] | 499 | AddError( |
| 500 | (kind == VariableKind::kGlobal) |
| 501 | ? "module scope var declaration requires a type and initializer" |
| 502 | : "function scope var declaration requires a type or initializer", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 503 | var->source); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 504 | return nullptr; |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 507 | if (!storage_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 508 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 509 | << "failed to determine storage type for variable '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 510 | builder_->Symbols().NameFor(var->symbol) + "'\n" |
| 511 | << "Source: " << var->source; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 512 | return nullptr; |
| 513 | } |
| 514 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 515 | auto storage_class = var->declared_storage_class; |
| 516 | if (storage_class == ast::StorageClass::kNone && !var->is_const) { |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 517 | // No declared storage class. Infer from usage / type. |
| 518 | if (kind == VariableKind::kLocal) { |
| 519 | storage_class = ast::StorageClass::kFunction; |
| 520 | } else if (storage_type->UnwrapRef()->is_handle()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 521 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
| 522 | // If the store type is a texture type or a sampler type, then the |
| 523 | // variable declaration must not have a storage class decoration. The |
| 524 | // storage class will always be handle. |
| 525 | storage_class = ast::StorageClass::kUniformConstant; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 529 | auto access = var->declared_access; |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 530 | if (access == ast::Access::kUndefined) { |
| 531 | access = DefaultAccessForStorageClass(storage_class); |
| 532 | } |
| 533 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 534 | auto* type = storage_type; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 535 | if (!var->is_const) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 536 | // Variable declaration. Unlike `let`, `var` has storage. |
| 537 | // Variables are always of a reference type to the declared storage type. |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 538 | type = |
| 539 | builder_->create<sem::Reference>(storage_type, storage_class, access); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 542 | if (rhs_type && |
| 543 | !ValidateVariableConstructor(var, storage_class, storage_type, type_name, |
| 544 | rhs_type, rhs_type_name)) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 545 | return nullptr; |
| 546 | } |
| 547 | |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 548 | auto* info = |
| 549 | variable_infos_.Create(var, const_cast<sem::Type*>(type), type_name, |
| 550 | storage_class, access, kind, index); |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 551 | variable_to_info_.emplace(var, info); |
| 552 | |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 553 | return info; |
| 554 | } |
| 555 | |
Ben Clayton | 9545fb7 | 2021-07-06 10:20:19 +0000 | [diff] [blame] | 556 | ast::Access Resolver::DefaultAccessForStorageClass( |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 557 | ast::StorageClass storage_class) { |
| 558 | // https://gpuweb.github.io/gpuweb/wgsl/#storage-class |
| 559 | switch (storage_class) { |
| 560 | case ast::StorageClass::kStorage: |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 561 | case ast::StorageClass::kUniform: |
| 562 | case ast::StorageClass::kUniformConstant: |
| 563 | return ast::Access::kRead; |
| 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | return ast::Access::kReadWrite; |
| 568 | } |
| 569 | |
James Price | ee23c17 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 570 | void Resolver::AllocateOverridableConstantIds() { |
| 571 | // The next pipeline constant ID to try to allocate. |
| 572 | uint16_t next_constant_id = 0; |
| 573 | |
| 574 | // Allocate constant IDs in global declaration order, so that they are |
| 575 | // deterministic. |
| 576 | // TODO(crbug.com/tint/1192): If a transform changes the order or removes an |
| 577 | // unused constant, the allocation may change on the next Resolver pass. |
| 578 | for (auto* decl : builder_->AST().GlobalDeclarations()) { |
| 579 | auto* var = decl->As<ast::Variable>(); |
| 580 | if (!var) { |
| 581 | continue; |
| 582 | } |
| 583 | auto* override_deco = |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 584 | ast::GetDecoration<ast::OverrideDecoration>(var->decorations); |
James Price | ee23c17 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 585 | if (!override_deco) { |
| 586 | continue; |
| 587 | } |
| 588 | |
| 589 | uint16_t constant_id; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 590 | if (override_deco->has_value) { |
| 591 | constant_id = static_cast<uint16_t>(override_deco->value); |
James Price | ee23c17 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 592 | } else { |
| 593 | // No ID was specified, so allocate the next available ID. |
| 594 | constant_id = next_constant_id; |
| 595 | while (constant_ids_.count(constant_id)) { |
| 596 | if (constant_id == UINT16_MAX) { |
| 597 | TINT_ICE(Resolver, builder_->Diagnostics()) |
| 598 | << "no more pipeline constant IDs available"; |
| 599 | return; |
| 600 | } |
| 601 | constant_id++; |
| 602 | } |
| 603 | next_constant_id = constant_id + 1; |
| 604 | } |
| 605 | |
| 606 | variable_to_info_[var]->constant_id = constant_id; |
| 607 | } |
| 608 | } |
| 609 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 610 | bool Resolver::ValidateVariableConstructor(const ast::Variable* var, |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 611 | ast::StorageClass storage_class, |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 612 | const sem::Type* storage_type, |
| 613 | const std::string& type_name, |
| 614 | const sem::Type* rhs_type, |
| 615 | const std::string& rhs_type_name) { |
| 616 | auto* value_type = rhs_type->UnwrapRef(); // Implicit load of RHS |
| 617 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 618 | // Value type has to match storage type |
Antonio Maiorano | be607c0 | 2021-05-18 15:26:40 +0000 | [diff] [blame] | 619 | if (storage_type != value_type) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 620 | std::string decl = var->is_const ? "let" : "var"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 621 | AddError("cannot initialize " + decl + " of type '" + type_name + |
| 622 | "' with value of type '" + rhs_type_name + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 623 | var->source); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 624 | return false; |
| 625 | } |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 626 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 627 | if (!var->is_const) { |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 628 | switch (storage_class) { |
| 629 | case ast::StorageClass::kPrivate: |
| 630 | case ast::StorageClass::kFunction: |
| 631 | break; // Allowed an initializer |
| 632 | default: |
| 633 | // https://gpuweb.github.io/gpuweb/wgsl/#var-and-let |
| 634 | // Optionally has an initializer expression, if the variable is in the |
| 635 | // private or function storage classes. |
| 636 | AddError("var of storage class '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 637 | std::string(ast::ToString(storage_class)) + |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 638 | "' cannot have an initializer. var initializers are only " |
| 639 | "supported for the storage classes " |
| 640 | "'private' and 'function'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 641 | var->source); |
Ben Clayton | 231648c | 2021-08-27 14:54:47 +0000 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | } |
| 645 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 646 | return true; |
| 647 | } |
| 648 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 649 | bool Resolver::GlobalVariable(const ast::Variable* var) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 650 | if (!ValidateNoDuplicateDefinition(var->symbol, var->source, |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 651 | /* check_global_scope_only */ true)) { |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 652 | return false; |
| 653 | } |
| 654 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 655 | auto* info = Variable(var, VariableKind::kGlobal); |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 656 | if (!info) { |
| 657 | return false; |
| 658 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 659 | variable_stack_.set_global(var->symbol, info); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 660 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 661 | if (!var->is_const && info->storage_class == ast::StorageClass::kNone) { |
| 662 | AddError("global variables must have a storage class", var->source); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 663 | return false; |
| 664 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 665 | if (var->is_const && !(info->storage_class == ast::StorageClass::kNone)) { |
| 666 | AddError("global constants shouldn't have a storage class", var->source); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 667 | return false; |
| 668 | } |
| 669 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 670 | for (auto* deco : var->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 671 | Mark(deco); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 672 | |
| 673 | if (auto* override_deco = deco->As<ast::OverrideDecoration>()) { |
| 674 | // Track the constant IDs that are specified in the shader. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 675 | if (override_deco->has_value) { |
| 676 | constant_ids_.emplace(override_deco->value, info); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 681 | if (!ValidateNoDuplicateDecorations(var->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 682 | return false; |
| 683 | } |
| 684 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 685 | if (auto bp = var->BindingPoint()) { |
| 686 | info->binding_point = {bp.group->value, bp.binding->value}; |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 689 | if (!ValidateGlobalVariable(info)) { |
| 690 | return false; |
| 691 | } |
| 692 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 693 | if (!ApplyStorageClassUsageToType( |
| 694 | info->storage_class, const_cast<sem::Type*>(info->type->UnwrapRef()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 695 | var->source)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 696 | AddNote("while instantiating variable " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 697 | builder_->Symbols().NameFor(var->symbol), |
| 698 | var->source); |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 699 | return false; |
| 700 | } |
| 701 | |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 702 | // TODO(bclayton): Call this at the end of resolve on all uniform and storage |
| 703 | // referenced structs |
| 704 | if (!ValidateStorageClassLayout(info)) { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | return true; |
| 709 | } |
| 710 | |
| 711 | bool Resolver::ValidateStorageClassLayout(const sem::Struct* str, |
| 712 | ast::StorageClass sc) { |
| 713 | // https://gpuweb.github.io/gpuweb/wgsl/#storage-class-layout-constraints |
| 714 | |
| 715 | auto is_uniform_struct_or_array = [sc](const sem::Type* ty) { |
| 716 | return sc == ast::StorageClass::kUniform && |
| 717 | ty->IsAnyOf<sem::Array, sem::Struct>(); |
| 718 | }; |
| 719 | |
| 720 | auto is_uniform_struct = [sc](const sem::Type* ty) { |
| 721 | return sc == ast::StorageClass::kUniform && ty->Is<sem::Struct>(); |
| 722 | }; |
| 723 | |
| 724 | auto required_alignment_of = [&](const sem::Type* ty) { |
Ben Clayton | fced350 | 2021-07-22 18:56:54 +0000 | [diff] [blame] | 725 | uint32_t actual_align = ty->Align(); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 726 | uint32_t required_align = actual_align; |
| 727 | if (is_uniform_struct_or_array(ty)) { |
| 728 | required_align = utils::RoundUp(16u, actual_align); |
| 729 | } |
| 730 | return required_align; |
| 731 | }; |
| 732 | |
| 733 | auto member_name_of = [this](const sem::StructMember* sm) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 734 | return builder_->Symbols().NameFor(sm->Declaration()->symbol); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | auto type_name_of = [this](const sem::StructMember* sm) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 738 | return sm->Declaration()->type->FriendlyName(builder_->Symbols()); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | // TODO(amaiorano): Output struct and member decorations so that this output |
| 742 | // can be copied verbatim back into source |
| 743 | auto get_struct_layout_string = [&](const sem::Struct* st) -> std::string { |
| 744 | std::stringstream ss; |
| 745 | |
| 746 | if (st->Members().empty()) { |
| 747 | TINT_ICE(Resolver, diagnostics_) << "Validation should have ensured that " |
| 748 | "structs have at least one member"; |
| 749 | return {}; |
| 750 | } |
| 751 | const auto* const last_member = st->Members().back(); |
| 752 | const uint32_t last_member_struct_padding_offset = |
| 753 | last_member->Offset() + last_member->Size(); |
| 754 | |
| 755 | // Compute max widths to align output |
| 756 | const auto offset_w = |
| 757 | static_cast<int>(::log10(last_member_struct_padding_offset)) + 1; |
| 758 | const auto size_w = static_cast<int>(::log10(st->Size())) + 1; |
| 759 | const auto align_w = static_cast<int>(::log10(st->Align())) + 1; |
| 760 | |
| 761 | auto print_struct_begin_line = [&](size_t align, size_t size, |
| 762 | std::string struct_name) { |
| 763 | ss << "/* " << std::setw(offset_w) << " " |
| 764 | << "align(" << std::setw(align_w) << align << ") size(" |
| 765 | << std::setw(size_w) << size << ") */ struct " << struct_name |
| 766 | << " {\n"; |
| 767 | }; |
| 768 | |
| 769 | auto print_struct_end_line = [&]() { |
| 770 | ss << "/* " |
| 771 | << std::setw(offset_w + size_w + align_w) << " " |
| 772 | << "*/ };"; |
| 773 | }; |
| 774 | |
| 775 | auto print_member_line = [&](size_t offset, size_t align, size_t size, |
| 776 | std::string s) { |
| 777 | ss << "/* offset(" << std::setw(offset_w) << offset << ") align(" |
| 778 | << std::setw(align_w) << align << ") size(" << std::setw(size_w) |
| 779 | << size << ") */ " << s << ";\n"; |
| 780 | }; |
| 781 | |
| 782 | print_struct_begin_line(st->Align(), st->Size(), |
| 783 | st->FriendlyName(builder_->Symbols())); |
| 784 | |
| 785 | for (size_t i = 0; i < st->Members().size(); ++i) { |
| 786 | auto* const m = st->Members()[i]; |
| 787 | |
| 788 | // Output field alignment padding, if any |
| 789 | auto* const prev_member = (i == 0) ? nullptr : str->Members()[i - 1]; |
| 790 | if (prev_member) { |
| 791 | uint32_t padding = |
| 792 | m->Offset() - (prev_member->Offset() + prev_member->Size()); |
| 793 | if (padding > 0) { |
| 794 | size_t padding_offset = m->Offset() - padding; |
| 795 | print_member_line(padding_offset, 1, padding, |
| 796 | "// -- implicit field alignment padding --"); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | // Output member |
| 801 | std::string member_name = member_name_of(m); |
| 802 | print_member_line(m->Offset(), m->Align(), m->Size(), |
| 803 | member_name_of(m) + " : " + type_name_of(m)); |
| 804 | } |
| 805 | |
| 806 | // Output struct size padding, if any |
| 807 | uint32_t struct_padding = st->Size() - last_member_struct_padding_offset; |
| 808 | if (struct_padding > 0) { |
| 809 | print_member_line(last_member_struct_padding_offset, 1, struct_padding, |
| 810 | "// -- implicit struct size padding --"); |
| 811 | } |
| 812 | |
| 813 | print_struct_end_line(); |
| 814 | |
| 815 | return ss.str(); |
| 816 | }; |
| 817 | |
| 818 | if (!ast::IsHostShareable(sc)) { |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | for (size_t i = 0; i < str->Members().size(); ++i) { |
| 823 | auto* const m = str->Members()[i]; |
| 824 | uint32_t required_align = required_alignment_of(m->Type()); |
| 825 | |
| 826 | // Validate that member is at a valid byte offset |
| 827 | if (m->Offset() % required_align != 0) { |
| 828 | AddError("the offset of a struct member of type '" + type_name_of(m) + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 829 | "' in storage class '" + ast::ToString(sc) + |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 830 | "' must be a multiple of " + std::to_string(required_align) + |
| 831 | " bytes, but '" + member_name_of(m) + |
| 832 | "' is currently at offset " + std::to_string(m->Offset()) + |
| 833 | ". Consider setting [[align(" + |
| 834 | std::to_string(required_align) + ")]] on this member", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 835 | m->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 836 | |
| 837 | AddNote("see layout of struct:\n" + get_struct_layout_string(str), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 838 | str->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 839 | |
| 840 | if (auto* member_str = m->Type()->As<sem::Struct>()) { |
| 841 | AddNote("and layout of struct member:\n" + |
| 842 | get_struct_layout_string(member_str), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 843 | member_str->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | // For uniform buffers, validate that the number of bytes between the |
| 850 | // previous member of type struct and the current is a multiple of 16 bytes. |
| 851 | auto* const prev_member = (i == 0) ? nullptr : str->Members()[i - 1]; |
| 852 | if (prev_member && is_uniform_struct(prev_member->Type())) { |
| 853 | const uint32_t prev_to_curr_offset = m->Offset() - prev_member->Offset(); |
| 854 | if (prev_to_curr_offset % 16 != 0) { |
| 855 | AddError( |
| 856 | "uniform storage requires that the number of bytes between the " |
| 857 | "start of the previous member of type struct and the current " |
| 858 | "member be a multiple of 16 bytes, but there are currently " + |
| 859 | std::to_string(prev_to_curr_offset) + " bytes between '" + |
| 860 | member_name_of(prev_member) + "' and '" + member_name_of(m) + |
| 861 | "'. Consider setting [[align(16)]] on this member", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 862 | m->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 863 | |
| 864 | AddNote("see layout of struct:\n" + get_struct_layout_string(str), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 865 | str->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 866 | |
| 867 | auto* prev_member_str = prev_member->Type()->As<sem::Struct>(); |
| 868 | AddNote("and layout of previous member struct:\n" + |
| 869 | get_struct_layout_string(prev_member_str), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 870 | prev_member_str->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 871 | return false; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // For uniform buffer array members, validate that array elements are |
| 876 | // aligned to 16 bytes |
| 877 | if (auto* arr = m->Type()->As<sem::Array>()) { |
| 878 | if (sc == ast::StorageClass::kUniform) { |
| 879 | // We already validated that this array member is itself aligned to 16 |
| 880 | // bytes above, so we only need to validate that stride is a multiple of |
| 881 | // 16 bytes. |
| 882 | if (arr->Stride() % 16 != 0) { |
| 883 | AddError( |
| 884 | "uniform storage requires that array elements be aligned to 16 " |
| 885 | "bytes, but array stride of '" + |
| 886 | member_name_of(m) + "' is currently " + |
| 887 | std::to_string(arr->Stride()) + |
| 888 | ". Consider setting [[stride(" + |
| 889 | std::to_string( |
| 890 | utils::RoundUp(required_align, arr->Stride())) + |
| 891 | ")]] on the array type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 892 | m->Declaration()->type->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 893 | AddNote("see layout of struct:\n" + get_struct_layout_string(str), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 894 | str->Declaration()->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 895 | return false; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // If member is struct, recurse |
| 901 | if (auto* str_member = m->Type()->As<sem::Struct>()) { |
| 902 | // Cache result of struct + storage class pair |
| 903 | if (valid_struct_storage_layouts_.emplace(str_member, sc).second) { |
| 904 | if (!ValidateStorageClassLayout(str_member, sc)) { |
| 905 | return false; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | bool Resolver::ValidateStorageClassLayout(const VariableInfo* info) { |
| 915 | if (auto* str = info->type->UnwrapRef()->As<sem::Struct>()) { |
| 916 | if (!ValidateStorageClassLayout(str, info->storage_class)) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 917 | AddNote("see declaration of variable", info->declaration->source); |
Antonio Maiorano | 60dae24 | 2021-07-15 19:09:25 +0000 | [diff] [blame] | 918 | return false; |
| 919 | } |
| 920 | } |
| 921 | |
Antonio Maiorano | bbbb0ed | 2021-04-06 20:18:57 +0000 | [diff] [blame] | 922 | return true; |
| 923 | } |
| 924 | |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 925 | bool Resolver::ValidateGlobalVariable(const VariableInfo* info) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 926 | if (!ValidateNoDuplicateDecorations(info->declaration->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 927 | return false; |
| 928 | } |
| 929 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 930 | for (auto* deco : info->declaration->decorations) { |
| 931 | if (info->declaration->is_const) { |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 932 | if (auto* override_deco = deco->As<ast::OverrideDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 933 | if (override_deco->has_value) { |
| 934 | uint32_t id = override_deco->value; |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 935 | auto itr = constant_ids_.find(id); |
| 936 | if (itr != constant_ids_.end() && itr->second != info) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 937 | AddError("pipeline constant IDs must be unique", deco->source); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 938 | AddNote("a pipeline constant with an ID of " + std::to_string(id) + |
| 939 | " was previously declared " |
| 940 | "here:", |
| 941 | ast::GetDecoration<ast::OverrideDecoration>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 942 | itr->second->declaration->decorations) |
| 943 | ->source); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | if (id > 65535) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 947 | AddError("pipeline constant IDs must be between 0 and 65535", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 948 | deco->source); |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | } else { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 953 | AddError("decoration is not valid for constants", deco->source); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 954 | return false; |
| 955 | } |
| 956 | } else { |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 957 | bool is_shader_io_decoration = |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 958 | deco->IsAnyOf<ast::BuiltinDecoration, ast::InterpolateDecoration, |
James Price | 508d249 | 2021-07-12 12:12:32 +0000 | [diff] [blame] | 959 | ast::InvariantDecoration, ast::LocationDecoration>(); |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 960 | bool has_io_storage_class = |
| 961 | info->storage_class == ast::StorageClass::kInput || |
| 962 | info->storage_class == ast::StorageClass::kOutput; |
| 963 | if (!(deco->IsAnyOf<ast::BindingDecoration, ast::GroupDecoration, |
| 964 | ast::InternalDecoration>()) && |
| 965 | (!is_shader_io_decoration || !has_io_storage_class)) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 966 | AddError("decoration is not valid for variables", deco->source); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 967 | return false; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 972 | auto binding_point = info->declaration->BindingPoint(); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 973 | switch (info->storage_class) { |
| 974 | case ast::StorageClass::kUniform: |
| 975 | case ast::StorageClass::kStorage: |
| 976 | case ast::StorageClass::kUniformConstant: { |
| 977 | // https://gpuweb.github.io/gpuweb/wgsl/#resource-interface |
| 978 | // Each resource variable must be declared with both group and binding |
| 979 | // attributes. |
| 980 | if (!binding_point) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 981 | AddError( |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 982 | "resource variables require [[group]] and [[binding]] " |
| 983 | "decorations", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 984 | info->declaration->source); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 985 | return false; |
| 986 | } |
| 987 | break; |
| 988 | } |
| 989 | default: |
| 990 | if (binding_point.binding || binding_point.group) { |
| 991 | // https://gpuweb.github.io/gpuweb/wgsl/#attribute-binding |
| 992 | // Must only be applied to a resource variable |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 993 | AddError( |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 994 | "non-resource variables must not have [[group]] or [[binding]] " |
| 995 | "decorations", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 996 | info->declaration->source); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 997 | return false; |
| 998 | } |
| 999 | } |
| 1000 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 1001 | // https://gpuweb.github.io/gpuweb/wgsl/#variable-declaration |
| 1002 | // The access mode always has a default, and except for variables in the |
| 1003 | // storage storage class, must not be written. |
| 1004 | if (info->storage_class != ast::StorageClass::kStorage && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1005 | info->declaration->declared_access != ast::Access::kUndefined) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1006 | AddError( |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 1007 | "only variables in <storage> storage class may declare an access mode", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1008 | info->declaration->source); |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 1009 | return false; |
| 1010 | } |
| 1011 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1012 | switch (info->storage_class) { |
| 1013 | case ast::StorageClass::kStorage: { |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1014 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
| 1015 | // A variable in the storage storage class is a storage buffer variable. |
| 1016 | // Its store type must be a host-shareable structure type with block |
| 1017 | // attribute, satisfying the storage class constraints. |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1018 | |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 1019 | auto* str = info->type->UnwrapRef()->As<sem::Struct>(); |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 1020 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1021 | if (!str) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1022 | AddError( |
Ben Clayton | 93e8f52 | 2021-06-04 20:41:47 +0000 | [diff] [blame] | 1023 | "variables declared in the <storage> storage class must be of a " |
| 1024 | "structure type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1025 | info->declaration->source); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1026 | return false; |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1027 | } |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (!str->IsBlockDecorated()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1030 | AddError( |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1031 | "structure used as a storage buffer must be declared with the " |
| 1032 | "[[block]] decoration", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1033 | str->Declaration()->source); |
| 1034 | if (info->declaration->source.range.begin.line) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1035 | AddNote("structure used as storage buffer here", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1036 | info->declaration->source); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1037 | } |
| 1038 | return false; |
| 1039 | } |
| 1040 | break; |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1041 | } |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1042 | case ast::StorageClass::kUniform: { |
| 1043 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
| 1044 | // A variable in the uniform storage class is a uniform buffer variable. |
| 1045 | // Its store type must be a host-shareable structure type with block |
| 1046 | // attribute, satisfying the storage class constraints. |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1047 | auto* str = info->type->UnwrapRef()->As<sem::Struct>(); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1048 | if (!str) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1049 | AddError( |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1050 | "variables declared in the <uniform> storage class must be of a " |
| 1051 | "structure type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1052 | info->declaration->source); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | if (!str->IsBlockDecorated()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1057 | AddError( |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1058 | "structure used as a uniform buffer must be declared with the " |
| 1059 | "[[block]] decoration", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1060 | str->Declaration()->source); |
| 1061 | if (info->declaration->source.range.begin.line) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1062 | AddNote("structure used as uniform buffer here", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1063 | info->declaration->source); |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1064 | } |
| 1065 | return false; |
| 1066 | } |
Sarah | 52b6a00 | 2021-06-16 19:40:13 +0000 | [diff] [blame] | 1067 | |
| 1068 | for (auto* member : str->Members()) { |
| 1069 | if (auto* arr = member->Type()->As<sem::Array>()) { |
| 1070 | if (arr->IsRuntimeSized()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1071 | AddError( |
Sarah | 52b6a00 | 2021-06-16 19:40:13 +0000 | [diff] [blame] | 1072 | "structure containing a runtime sized array " |
| 1073 | "cannot be used as a uniform buffer", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1074 | info->declaration->source); |
| 1075 | AddNote("structure is declared here", str->Declaration()->source); |
Sarah | 52b6a00 | 2021-06-16 19:40:13 +0000 | [diff] [blame] | 1076 | return false; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1081 | break; |
| 1082 | } |
| 1083 | default: |
| 1084 | break; |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1087 | if (!info->declaration->is_const) { |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 1088 | if (!ValidateAtomicVariable(info)) { |
| 1089 | return false; |
| 1090 | } |
| 1091 | } |
| 1092 | |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1093 | return ValidateVariable(info); |
Ben Clayton | 85bfea6 | 2021-04-19 20:20:33 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 1096 | // https://gpuweb.github.io/gpuweb/wgsl/#atomic-types |
| 1097 | // Atomic types may only be instantiated by variables in the workgroup storage |
| 1098 | // class or by storage buffer variables with a read_write access mode. |
| 1099 | bool Resolver::ValidateAtomicVariable(const VariableInfo* info) { |
| 1100 | auto sc = info->storage_class; |
| 1101 | auto access = info->access; |
| 1102 | auto* type = info->type->UnwrapRef(); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1103 | auto source = info->declaration->type ? info->declaration->type->source |
| 1104 | : info->declaration->source; |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 1105 | |
| 1106 | if (type->Is<sem::Atomic>()) { |
| 1107 | if (sc != ast::StorageClass::kWorkgroup) { |
| 1108 | AddError( |
| 1109 | "atomic variables must have <storage> or <workgroup> storage class", |
Ryan Harrison | 3bcbfc78 | 2021-08-11 19:59:44 +0000 | [diff] [blame] | 1110 | source); |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 1111 | return false; |
| 1112 | } |
| 1113 | } else if (type->IsAnyOf<sem::Struct, sem::Array>()) { |
| 1114 | auto found = atomic_composite_info_.find(type); |
| 1115 | if (found != atomic_composite_info_.end()) { |
| 1116 | if (sc != ast::StorageClass::kStorage && |
| 1117 | sc != ast::StorageClass::kWorkgroup) { |
| 1118 | AddError( |
| 1119 | "atomic variables must have <storage> or <workgroup> storage class", |
| 1120 | source); |
| 1121 | AddNote("atomic sub-type of '" + |
| 1122 | type->FriendlyName(builder_->Symbols()) + |
| 1123 | "' is declared here", |
| 1124 | found->second); |
| 1125 | return false; |
| 1126 | } else if (sc == ast::StorageClass::kStorage && |
| 1127 | access != ast::Access::kReadWrite) { |
| 1128 | AddError( |
| 1129 | "atomic variables in <storage> storage class must have read_write " |
| 1130 | "access mode", |
| 1131 | source); |
| 1132 | AddNote("atomic sub-type of '" + |
| 1133 | type->FriendlyName(builder_->Symbols()) + |
| 1134 | "' is declared here", |
| 1135 | found->second); |
| 1136 | return false; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1144 | bool Resolver::ValidateVariable(const VariableInfo* info) { |
| 1145 | auto* var = info->declaration; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1146 | auto* storage_type = info->type->UnwrapRef(); |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 1147 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1148 | if (!var->is_const && !IsStorable(storage_type)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1149 | AddError(storage_type->FriendlyName(builder_->Symbols()) + |
| 1150 | " cannot be used as the type of a var", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1151 | var->source); |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 1152 | return false; |
| 1153 | } |
| 1154 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1155 | if (var->is_const && info->kind != VariableKind::kParameter && |
James Price | d12379a | 2021-07-26 22:11:58 +0000 | [diff] [blame] | 1156 | !(storage_type->IsConstructible() || storage_type->Is<sem::Pointer>())) { |
| 1157 | AddError(storage_type->FriendlyName(builder_->Symbols()) + |
| 1158 | " cannot be used as the type of a let", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1159 | var->source); |
James Price | d12379a | 2021-07-26 22:11:58 +0000 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
| 1162 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1163 | if (auto* r = storage_type->As<sem::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 1164 | if (r->IsRuntimeSized()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 1165 | AddError("runtime arrays may only appear as the last member of a struct", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1166 | var->source); |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | } |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 1170 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1171 | if (auto* r = storage_type->As<sem::MultisampledTexture>()) { |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 1172 | if (r->dim() != ast::TextureDimension::k2d) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1173 | AddError("only 2d multisampled textures are supported", var->source); |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 1174 | return false; |
| 1175 | } |
| 1176 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1177 | if (!r->type()->UnwrapRef()->is_numeric_scalar()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1178 | AddError("texture_multisampled_2d<type>: type must be f32, i32 or u32", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1179 | var->source); |
Ryan Harrison | 83116d2 | 2021-04-20 16:09:21 +0000 | [diff] [blame] | 1180 | return false; |
| 1181 | } |
| 1182 | } |
| 1183 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1184 | if (storage_type->is_handle() && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1185 | var->declared_storage_class != ast::StorageClass::kNone) { |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1186 | // https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1187 | // If the store type is a texture type or a sampler type, then the |
| 1188 | // variable declaration must not have a storage class decoration. The |
| 1189 | // storage class will always be handle. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1190 | AddError("variables of type '" + info->type_name + |
| 1191 | "' must not have a storage class", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1192 | var->source); |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
| 1195 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1196 | if (IsValidationEnabled(var->decorations, |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1197 | ast::DisabledValidation::kIgnoreStorageClass) && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1198 | (var->declared_storage_class == ast::StorageClass::kInput || |
| 1199 | var->declared_storage_class == ast::StorageClass::kOutput)) { |
| 1200 | AddError("invalid use of input/output storage class", var->source); |
James Price | 14c0b8a | 2021-06-24 15:53:26 +0000 | [diff] [blame] | 1201 | return false; |
| 1202 | } |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1203 | return true; |
| 1204 | } |
| 1205 | |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1206 | bool Resolver::ValidateFunctionParameter(const ast::Function* func, |
| 1207 | const VariableInfo* info) { |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1208 | if (!ValidateVariable(info)) { |
| 1209 | return false; |
| 1210 | } |
Sarah | ac5dbd2 | 2021-06-28 14:05:25 +0000 | [diff] [blame] | 1211 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1212 | for (auto* deco : info->declaration->decorations) { |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1213 | if (!func->IsEntryPoint() && !deco->Is<ast::InternalDecoration>()) { |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1214 | AddError( |
| 1215 | "decoration is not valid for non-entry point function parameters", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1216 | deco->source); |
James Price | c3f1908 | 2021-06-24 16:20:16 +0000 | [diff] [blame] | 1217 | return false; |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 1218 | } else if (!deco->IsAnyOf<ast::BuiltinDecoration, ast::InvariantDecoration, |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1219 | ast::LocationDecoration, |
| 1220 | ast::InterpolateDecoration, |
James Price | c3f1908 | 2021-06-24 16:20:16 +0000 | [diff] [blame] | 1221 | ast::InternalDecoration>() && |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1222 | (IsValidationEnabled( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1223 | info->declaration->decorations, |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1224 | ast::DisabledValidation::kEntryPointParameter) && |
| 1225 | IsValidationEnabled( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1226 | info->declaration->decorations, |
Ryan Harrison | 2f258d1 | 2021-07-07 20:41:00 +0000 | [diff] [blame] | 1227 | ast::DisabledValidation:: |
| 1228 | kIgnoreConstructibleFunctionParameter))) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1229 | AddError("decoration is not valid for function parameters", deco->source); |
James Price | c3f1908 | 2021-06-24 16:20:16 +0000 | [diff] [blame] | 1230 | return false; |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 1231 | } |
| 1232 | } |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (auto* ref = info->type->As<sem::Pointer>()) { |
| 1235 | auto sc = ref->StorageClass(); |
| 1236 | if (!(sc == ast::StorageClass::kFunction || |
| 1237 | sc == ast::StorageClass::kPrivate || |
| 1238 | sc == ast::StorageClass::kWorkgroup)) { |
| 1239 | std::stringstream ss; |
| 1240 | ss << "function parameter of pointer type cannot be in '" << sc |
| 1241 | << "' storage class"; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1242 | AddError(ss.str(), info->declaration->source); |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1243 | return false; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (IsPlain(info->type)) { |
Antonio Maiorano | 68a6dd0 | 2021-07-20 18:28:31 +0000 | [diff] [blame] | 1248 | if (!info->type->IsConstructible() && |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1249 | IsValidationEnabled( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1250 | info->declaration->decorations, |
Ryan Harrison | 2f258d1 | 2021-07-07 20:41:00 +0000 | [diff] [blame] | 1251 | ast::DisabledValidation::kIgnoreConstructibleFunctionParameter)) { |
| 1252 | AddError("store type of function parameter must be a constructible type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1253 | info->declaration->source); |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1254 | return false; |
| 1255 | } |
| 1256 | } else if (!info->type->IsAnyOf<sem::Texture, sem::Sampler, sem::Pointer>()) { |
| 1257 | AddError("store type of function parameter cannot be " + |
| 1258 | info->type->FriendlyName(builder_->Symbols()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1259 | info->declaration->source); |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| 1262 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 1265 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1266 | bool Resolver::ValidateBuiltinDecoration(const ast::BuiltinDecoration* deco, |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1267 | const sem::Type* storage_type, |
James Price | ceab140 | 2021-10-05 15:02:17 +0000 | [diff] [blame] | 1268 | const bool is_input) { |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1269 | auto* type = storage_type->UnwrapRef(); |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1270 | const auto stage = current_function_ |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1271 | ? current_function_->declaration->PipelineStage() |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1272 | : ast::PipelineStage::kNone; |
| 1273 | std::stringstream stage_name; |
| 1274 | stage_name << stage; |
| 1275 | bool is_stage_mismatch = false; |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1276 | bool is_output = !is_input; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1277 | switch (deco->builtin) { |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1278 | case ast::Builtin::kPosition: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1279 | if (stage != ast::PipelineStage::kNone && |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1280 | !((is_input && stage == ast::PipelineStage::kFragment) || |
| 1281 | (is_output && stage == ast::PipelineStage::kVertex))) { |
| 1282 | is_stage_mismatch = true; |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1283 | } |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 1284 | if (!(type->is_float_vector() && type->As<sem::Vector>()->Width() == 4)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1285 | AddError("store type of " + deco_to_str(deco) + " must be 'vec4<f32>'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1286 | deco->source); |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1287 | return false; |
| 1288 | } |
| 1289 | break; |
| 1290 | case ast::Builtin::kGlobalInvocationId: |
| 1291 | case ast::Builtin::kLocalInvocationId: |
James Price | 1bb6371 | 2021-09-13 17:11:58 +0000 | [diff] [blame] | 1292 | case ast::Builtin::kNumWorkgroups: |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1293 | case ast::Builtin::kWorkgroupId: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1294 | if (stage != ast::PipelineStage::kNone && |
| 1295 | !(stage == ast::PipelineStage::kCompute && is_input)) { |
| 1296 | is_stage_mismatch = true; |
| 1297 | } |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1298 | if (!(type->is_unsigned_integer_vector() && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 1299 | type->As<sem::Vector>()->Width() == 3)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1300 | AddError("store type of " + deco_to_str(deco) + " must be 'vec3<u32>'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1301 | deco->source); |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1302 | return false; |
| 1303 | } |
| 1304 | break; |
| 1305 | case ast::Builtin::kFragDepth: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1306 | if (stage != ast::PipelineStage::kNone && |
| 1307 | !(stage == ast::PipelineStage::kFragment && !is_input)) { |
| 1308 | is_stage_mismatch = true; |
| 1309 | } |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1310 | if (!type->Is<sem::F32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1311 | AddError("store type of " + deco_to_str(deco) + " must be 'f32'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1312 | deco->source); |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1313 | return false; |
| 1314 | } |
| 1315 | break; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1316 | case ast::Builtin::kFrontFacing: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1317 | if (stage != ast::PipelineStage::kNone && |
| 1318 | !(stage == ast::PipelineStage::kFragment && is_input)) { |
| 1319 | is_stage_mismatch = true; |
| 1320 | } |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1321 | if (!type->Is<sem::Bool>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1322 | AddError("store type of " + deco_to_str(deco) + " must be 'bool'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1323 | deco->source); |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1324 | return false; |
| 1325 | } |
| 1326 | break; |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1327 | case ast::Builtin::kLocalInvocationIndex: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1328 | if (stage != ast::PipelineStage::kNone && |
| 1329 | !(stage == ast::PipelineStage::kCompute && is_input)) { |
| 1330 | is_stage_mismatch = true; |
| 1331 | } |
| 1332 | if (!type->Is<sem::U32>()) { |
| 1333 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1334 | deco->source); |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1335 | return false; |
| 1336 | } |
| 1337 | break; |
Sarah | 57a737b | 2021-06-23 17:35:42 +0000 | [diff] [blame] | 1338 | case ast::Builtin::kVertexIndex: |
| 1339 | case ast::Builtin::kInstanceIndex: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1340 | if (stage != ast::PipelineStage::kNone && |
| 1341 | !(stage == ast::PipelineStage::kVertex && is_input)) { |
| 1342 | is_stage_mismatch = true; |
| 1343 | } |
| 1344 | if (!type->Is<sem::U32>()) { |
| 1345 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1346 | deco->source); |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1347 | return false; |
| 1348 | } |
| 1349 | break; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1350 | case ast::Builtin::kSampleMask: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1351 | if (stage != ast::PipelineStage::kNone && |
| 1352 | !(stage == ast::PipelineStage::kFragment)) { |
| 1353 | is_stage_mismatch = true; |
| 1354 | } |
| 1355 | if (!type->Is<sem::U32>()) { |
| 1356 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1357 | deco->source); |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1358 | return false; |
| 1359 | } |
| 1360 | break; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1361 | case ast::Builtin::kSampleIndex: |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1362 | if (stage != ast::PipelineStage::kNone && |
| 1363 | !(stage == ast::PipelineStage::kFragment && is_input)) { |
| 1364 | is_stage_mismatch = true; |
| 1365 | } |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1366 | if (!type->Is<sem::U32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1367 | AddError("store type of " + deco_to_str(deco) + " must be 'u32'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1368 | deco->source); |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1369 | return false; |
| 1370 | } |
| 1371 | break; |
| 1372 | default: |
| 1373 | break; |
| 1374 | } |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1375 | |
James Price | ceab140 | 2021-10-05 15:02:17 +0000 | [diff] [blame] | 1376 | if (is_stage_mismatch) { |
| 1377 | AddError(deco_to_str(deco) + " cannot be used in " + |
| 1378 | (is_input ? "input of " : "output of ") + stage_name.str() + |
| 1379 | " pipeline stage", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1380 | deco->source); |
James Price | ceab140 | 2021-10-05 15:02:17 +0000 | [diff] [blame] | 1381 | return false; |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1384 | return true; |
Antonio Maiorano | 09356cc | 2021-04-06 14:07:07 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 1387 | bool Resolver::ValidateInterpolateDecoration( |
| 1388 | const ast::InterpolateDecoration* deco, |
| 1389 | const sem::Type* storage_type) { |
| 1390 | auto* type = storage_type->UnwrapRef(); |
| 1391 | |
James Price | a41694e | 2021-10-21 23:08:44 +0000 | [diff] [blame] | 1392 | if (type->is_integer_scalar_or_vector() && |
| 1393 | deco->type != ast::InterpolationType::kFlat) { |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 1394 | AddError( |
James Price | a41694e | 2021-10-21 23:08:44 +0000 | [diff] [blame] | 1395 | "interpolation type must be 'flat' for integral user-defined IO types", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1396 | deco->source); |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 1397 | return false; |
| 1398 | } |
| 1399 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1400 | if (deco->type == ast::InterpolationType::kFlat && |
| 1401 | deco->sampling != ast::InterpolationSampling::kNone) { |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 1402 | AddError("flat interpolation attribute must not have a sampling parameter", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1403 | deco->source); |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1410 | bool Resolver::ValidateFunction(const ast::Function* func, |
| 1411 | const FunctionInfo* info) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1412 | if (!ValidateNoDuplicateDefinition(func->symbol, func->source, |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 1413 | /* check_global_scope_only */ true)) { |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1414 | return false; |
| 1415 | } |
| 1416 | |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1417 | auto workgroup_deco_count = 0; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1418 | for (auto* deco : func->decorations) { |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1419 | if (deco->Is<ast::WorkgroupDecoration>()) { |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1420 | workgroup_deco_count++; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1421 | if (func->PipelineStage() != ast::PipelineStage::kCompute) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1422 | AddError( |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1423 | "the workgroup_size attribute is only valid for compute stages", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1424 | deco->source); |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1425 | return false; |
| 1426 | } |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1427 | } else if (!deco->IsAnyOf<ast::StageDecoration, |
| 1428 | ast::InternalDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1429 | AddError("decoration is not valid for functions", deco->source); |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
| 1432 | } |
James Price | 43f50ea | 2021-05-14 20:41:03 +0000 | [diff] [blame] | 1433 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1434 | if (func->params.size() > 255) { |
| 1435 | AddError("functions may declare at most 255 parameters", func->source); |
Ben Clayton | 725159c | 2021-07-17 17:45:25 +0000 | [diff] [blame] | 1436 | return false; |
| 1437 | } |
| 1438 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1439 | for (auto* param : func->params) { |
Sarah | 9432c97 | 2021-06-29 15:24:04 +0000 | [diff] [blame] | 1440 | if (!ValidateFunctionParameter(func, variable_to_info_.at(param))) { |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1441 | return false; |
| 1442 | } |
| 1443 | } |
| 1444 | |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1445 | if (!info->return_type->Is<sem::Void>()) { |
Antonio Maiorano | 68a6dd0 | 2021-07-20 18:28:31 +0000 | [diff] [blame] | 1446 | if (!info->return_type->IsConstructible()) { |
Ryan Harrison | 2f258d1 | 2021-07-07 20:41:00 +0000 | [diff] [blame] | 1447 | AddError("function return type must be a constructible type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1448 | func->return_type->source); |
Antonio Maiorano | f19e0e4 | 2021-06-21 20:51:16 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1452 | if (func->body) { |
| 1453 | if (!func->body->Last() || |
| 1454 | !func->body->Last()->Is<ast::ReturnStatement>()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 1455 | AddError("non-void function must end with a return statement", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1456 | func->source); |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1457 | return false; |
| 1458 | } |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1459 | } else if (IsValidationEnabled( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1460 | func->decorations, |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 1461 | ast::DisabledValidation::kFunctionHasNoBody)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1462 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1463 | << "Function " << builder_->Symbols().NameFor(func->symbol) |
Ben Clayton | 451f2cc | 2021-05-12 12:54:21 +0000 | [diff] [blame] | 1464 | << " has no body"; |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1467 | for (auto* deco : func->return_type_decorations) { |
James Price | 1b1d963 | 2021-06-28 09:53:37 +0000 | [diff] [blame] | 1468 | if (!func->IsEntryPoint()) { |
Sarah | 99a78ad | 2021-07-08 14:02:56 +0000 | [diff] [blame] | 1469 | AddError( |
| 1470 | "decoration is not valid for non-entry point function return types", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1471 | deco->source); |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1474 | if (!deco->IsAnyOf<ast::BuiltinDecoration, ast::InternalDecoration, |
| 1475 | ast::LocationDecoration, ast::InterpolateDecoration, |
| 1476 | ast::InvariantDecoration>() && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1477 | (IsValidationEnabled(info->declaration->decorations, |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1478 | ast::DisabledValidation::kEntryPointParameter) && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1479 | IsValidationEnabled(info->declaration->decorations, |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1480 | ast::DisabledValidation:: |
| 1481 | kIgnoreConstructibleFunctionParameter))) { |
James Price | 1b1d963 | 2021-06-28 09:53:37 +0000 | [diff] [blame] | 1482 | AddError("decoration is not valid for entry point return types", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1483 | deco->source); |
James Price | 1b1d963 | 2021-06-28 09:53:37 +0000 | [diff] [blame] | 1484 | return false; |
| 1485 | } |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1489 | if (func->IsEntryPoint()) { |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1490 | if (!ValidateEntryPoint(func, info)) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1491 | return false; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1498 | bool Resolver::ValidateEntryPoint(const ast::Function* func, |
| 1499 | const FunctionInfo* info) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1500 | // Use a lambda to validate the entry point decorations for a type. |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 1501 | // Persistent state is used to track which builtins and locations have |
| 1502 | // already been seen, in order to catch conflicts. |
| 1503 | // TODO(jrprice): This state could be stored in FunctionInfo instead, and |
| 1504 | // then passed to sem::Function since it would be useful there too. |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1505 | std::unordered_set<ast::Builtin> builtins; |
| 1506 | std::unordered_set<uint32_t> locations; |
| 1507 | enum class ParamOrRetType { |
| 1508 | kParameter, |
| 1509 | kReturnType, |
| 1510 | }; |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 1511 | |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1512 | // Inner lambda that is applied to a type and all of its members. |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1513 | auto validate_entry_point_decorations_inner = [&](const ast::DecorationList& |
| 1514 | decos, |
| 1515 | sem::Type* ty, |
| 1516 | Source source, |
| 1517 | ParamOrRetType param_or_ret, |
| 1518 | bool is_struct_member) { |
| 1519 | // Scan decorations for pipeline IO attributes. |
| 1520 | // Check for overlap with attributes that have been seen previously. |
| 1521 | const ast::Decoration* pipeline_io_attribute = nullptr; |
James Price | a41694e | 2021-10-21 23:08:44 +0000 | [diff] [blame] | 1522 | const ast::InterpolateDecoration* interpolate_attribute = nullptr; |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1523 | const ast::InvariantDecoration* invariant_attribute = nullptr; |
| 1524 | for (auto* deco : decos) { |
| 1525 | auto is_invalid_compute_shader_decoration = false; |
| 1526 | if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { |
| 1527 | if (pipeline_io_attribute) { |
| 1528 | AddError("multiple entry point IO attributes", deco->source); |
| 1529 | AddNote("previously consumed " + deco_to_str(pipeline_io_attribute), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1530 | pipeline_io_attribute->source); |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
| 1533 | pipeline_io_attribute = deco; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1534 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1535 | if (builtins.count(builtin->builtin)) { |
| 1536 | AddError(deco_to_str(builtin) + |
| 1537 | " attribute appears multiple times as pipeline " + |
| 1538 | (param_or_ret == ParamOrRetType::kParameter ? "input" |
| 1539 | : "output"), |
| 1540 | func->source); |
| 1541 | return false; |
Ben Clayton | ada4864 | 2021-07-29 16:55:27 +0000 | [diff] [blame] | 1542 | } |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1543 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1544 | if (!ValidateBuiltinDecoration( |
| 1545 | builtin, ty, |
| 1546 | /* is_input */ param_or_ret == ParamOrRetType::kParameter)) { |
| 1547 | return false; |
| 1548 | } |
| 1549 | builtins.emplace(builtin->builtin); |
| 1550 | } else if (auto* location = deco->As<ast::LocationDecoration>()) { |
| 1551 | if (pipeline_io_attribute) { |
| 1552 | AddError("multiple entry point IO attributes", deco->source); |
| 1553 | AddNote("previously consumed " + deco_to_str(pipeline_io_attribute), |
| 1554 | pipeline_io_attribute->source); |
| 1555 | return false; |
| 1556 | } |
| 1557 | pipeline_io_attribute = deco; |
James Price | b3e6c0d | 2021-10-15 14:55:39 +0000 | [diff] [blame] | 1558 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1559 | bool is_input = param_or_ret == ParamOrRetType::kParameter; |
| 1560 | if (!ValidateLocationDecoration(location, ty, locations, source, |
| 1561 | is_input)) { |
| 1562 | return false; |
| 1563 | } |
| 1564 | } else if (auto* interpolate = deco->As<ast::InterpolateDecoration>()) { |
| 1565 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 1566 | is_invalid_compute_shader_decoration = true; |
| 1567 | } else if (!ValidateInterpolateDecoration(interpolate, ty)) { |
| 1568 | return false; |
| 1569 | } |
James Price | a41694e | 2021-10-21 23:08:44 +0000 | [diff] [blame] | 1570 | interpolate_attribute = interpolate; |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1571 | } else if (auto* invariant = deco->As<ast::InvariantDecoration>()) { |
| 1572 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 1573 | is_invalid_compute_shader_decoration = true; |
| 1574 | } |
| 1575 | invariant_attribute = invariant; |
| 1576 | } |
| 1577 | if (is_invalid_compute_shader_decoration) { |
| 1578 | std::string input_or_output = |
| 1579 | param_or_ret == ParamOrRetType::kParameter ? "inputs" : "output"; |
| 1580 | AddError( |
| 1581 | "decoration is not valid for compute shader " + input_or_output, |
| 1582 | deco->source); |
| 1583 | return false; |
| 1584 | } |
| 1585 | } |
Ben Clayton | 08b0ab9 | 2021-07-08 15:12:36 +0000 | [diff] [blame] | 1586 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1587 | if (IsValidationEnabled(decos, |
| 1588 | ast::DisabledValidation::kEntryPointParameter)) { |
| 1589 | if (is_struct_member && ty->Is<sem::Struct>()) { |
| 1590 | AddError("nested structures cannot be used for entry point IO", source); |
| 1591 | return false; |
| 1592 | } |
| 1593 | |
| 1594 | if (!ty->Is<sem::Struct>() && !pipeline_io_attribute) { |
| 1595 | std::string err = "missing entry point IO attribute"; |
| 1596 | if (!is_struct_member) { |
| 1597 | err += |
| 1598 | (param_or_ret == ParamOrRetType::kParameter ? " on parameter" |
| 1599 | : " on return type"); |
| 1600 | } |
| 1601 | AddError(err, source); |
| 1602 | return false; |
| 1603 | } |
| 1604 | |
James Price | a41694e | 2021-10-21 23:08:44 +0000 | [diff] [blame] | 1605 | if (pipeline_io_attribute && |
| 1606 | pipeline_io_attribute->Is<ast::LocationDecoration>()) { |
| 1607 | if (ty->is_integer_scalar_or_vector() && !interpolate_attribute) { |
| 1608 | // TODO(crbug.com/tint/1224): Make these errors once downstream |
| 1609 | // usages have caught up (no sooner than M99). |
| 1610 | if (func->PipelineStage() == ast::PipelineStage::kVertex && |
| 1611 | param_or_ret == ParamOrRetType::kReturnType) { |
| 1612 | AddWarning( |
| 1613 | "integral user-defined vertex outputs must have a flat " |
| 1614 | "interpolation attribute", |
| 1615 | source); |
| 1616 | } |
| 1617 | if (func->PipelineStage() == ast::PipelineStage::kFragment && |
| 1618 | param_or_ret == ParamOrRetType::kParameter) { |
| 1619 | AddWarning( |
| 1620 | "integral user-defined fragment inputs must have a flat " |
| 1621 | "interpolation attribute", |
| 1622 | source); |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1627 | if (invariant_attribute) { |
| 1628 | bool has_position = false; |
| 1629 | if (pipeline_io_attribute) { |
| 1630 | if (auto* builtin = |
| 1631 | pipeline_io_attribute->As<ast::BuiltinDecoration>()) { |
| 1632 | has_position = (builtin->builtin == ast::Builtin::kPosition); |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1635 | if (!has_position) { |
| 1636 | AddError( |
| 1637 | "invariant attribute must only be applied to a position " |
| 1638 | "builtin", |
| 1639 | invariant_attribute->source); |
| 1640 | return false; |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | return true; |
| 1645 | }; |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1646 | |
| 1647 | // Outer lambda for validating the entry point decorations for a type. |
| 1648 | auto validate_entry_point_decorations = [&](const ast::DecorationList& decos, |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1649 | sem::Type* ty, Source source, |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1650 | ParamOrRetType param_or_ret) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1651 | if (!validate_entry_point_decorations_inner(decos, ty, source, param_or_ret, |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 1652 | /*is_struct_member*/ false)) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1653 | return false; |
| 1654 | } |
| 1655 | |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 1656 | if (auto* str = ty->As<sem::Struct>()) { |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1657 | for (auto* member : str->Members()) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1658 | if (!validate_entry_point_decorations_inner( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1659 | member->Declaration()->decorations, member->Type(), |
| 1660 | member->Declaration()->source, param_or_ret, |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 1661 | /*is_struct_member*/ true)) { |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 1662 | AddNote("while analysing entry point '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1663 | builder_->Symbols().NameFor(func->symbol) + "'", |
| 1664 | func->source); |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1665 | return false; |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | return true; |
| 1671 | }; |
| 1672 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1673 | for (auto* param : info->parameters) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1674 | if (!validate_entry_point_decorations( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1675 | param->declaration->decorations, param->type, |
| 1676 | param->declaration->source, ParamOrRetType::kParameter)) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1677 | return false; |
| 1678 | } |
| 1679 | } |
| 1680 | |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1681 | // Clear IO sets after parameter validation. Builtin and location attributes |
| 1682 | // in return types should be validated independently from those used in |
| 1683 | // parameters. |
| 1684 | builtins.clear(); |
| 1685 | locations.clear(); |
| 1686 | |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1687 | if (!info->return_type->Is<sem::Void>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1688 | if (!validate_entry_point_decorations(func->return_type_decorations, |
| 1689 | info->return_type, func->source, |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1690 | ParamOrRetType::kReturnType)) { |
| 1691 | return false; |
| 1692 | } |
| 1693 | } |
| 1694 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1695 | if (func->PipelineStage() == ast::PipelineStage::kVertex && |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1696 | builtins.count(ast::Builtin::kPosition) == 0) { |
| 1697 | // Check module-scope variables, as the SPIR-V sanitizer generates these. |
| 1698 | bool found = false; |
| 1699 | for (auto* var : info->referenced_module_vars) { |
| 1700 | if (auto* builtin = ast::GetDecoration<ast::BuiltinDecoration>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1701 | var->declaration->decorations)) { |
| 1702 | if (builtin->builtin == ast::Builtin::kPosition) { |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1703 | found = true; |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | } |
Ben Clayton | 677437d | 2021-06-16 09:50:11 +0000 | [diff] [blame] | 1708 | if (!found) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1709 | AddError( |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1710 | "a vertex shader must include the 'position' builtin in its return " |
| 1711 | "type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1712 | func->source); |
James Price | 2dd3937 | 2021-04-21 16:13:42 +0000 | [diff] [blame] | 1713 | return false; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1717 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 1718 | if (!ast::HasDecoration<ast::WorkgroupDecoration>(func->decorations)) { |
Sarah | e6cb51e | 2021-06-29 18:39:44 +0000 | [diff] [blame] | 1719 | AddError( |
| 1720 | "a compute shader must include 'workgroup_size' in its " |
| 1721 | "attributes", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1722 | func->source); |
Sarah | e6cb51e | 2021-06-29 18:39:44 +0000 | [diff] [blame] | 1723 | return false; |
| 1724 | } |
| 1725 | } |
| 1726 | |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1727 | // Validate there are no resource variable binding collisions |
| 1728 | std::unordered_map<sem::BindingPoint, const ast::Variable*> binding_points; |
| 1729 | for (auto* var_info : info->referenced_module_vars) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1730 | if (!var_info->declaration->BindingPoint()) { |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1731 | continue; |
| 1732 | } |
| 1733 | auto bp = var_info->binding_point; |
| 1734 | auto res = binding_points.emplace(bp, var_info->declaration); |
Ben Clayton | 3103a1f | 2021-05-12 13:30:51 +0000 | [diff] [blame] | 1735 | if (!res.second && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1736 | IsValidationEnabled(var_info->declaration->decorations, |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1737 | ast::DisabledValidation::kBindingPointCollision) && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1738 | IsValidationEnabled(res.first->second->decorations, |
Sarah | a8f58ef | 2021-06-30 19:22:30 +0000 | [diff] [blame] | 1739 | ast::DisabledValidation::kBindingPointCollision)) { |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1740 | // https://gpuweb.github.io/gpuweb/wgsl/#resource-interface |
| 1741 | // Bindings must not alias within a shader stage: two different |
| 1742 | // variables in the resource interface of a given shader must not have |
| 1743 | // the same group and binding values, when considered as a pair of |
| 1744 | // values. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1745 | auto func_name = builder_->Symbols().NameFor(info->declaration->symbol); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1746 | AddError("entry point '" + func_name + |
| 1747 | "' references multiple variables that use the " |
| 1748 | "same resource binding [[group(" + |
| 1749 | std::to_string(bp.group) + "), binding(" + |
| 1750 | std::to_string(bp.binding) + ")]]", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1751 | var_info->declaration->source); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1752 | AddNote("first resource binding usage declared here", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1753 | res.first->second->source); |
Ben Clayton | 3f968e7 | 2021-05-10 19:16:46 +0000 | [diff] [blame] | 1754 | return false; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1758 | return true; |
| 1759 | } |
| 1760 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 1761 | bool Resolver::Function(const ast::Function* func) { |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1762 | auto* info = function_infos_.Create<FunctionInfo>(func); |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 1763 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 1764 | if (func->IsEntryPoint()) { |
| 1765 | entry_points_.emplace_back(info); |
| 1766 | } |
| 1767 | |
Ben Clayton | b350500 | 2021-05-19 16:11:04 +0000 | [diff] [blame] | 1768 | TINT_SCOPED_ASSIGNMENT(current_function_, info); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 1769 | |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 1770 | variable_stack_.push_scope(); |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 1771 | uint32_t parameter_index = 0; |
Ben Clayton | 9021eb5 | 2021-08-26 15:40:06 +0000 | [diff] [blame] | 1772 | std::unordered_map<Symbol, Source> parameter_names; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1773 | for (auto* param : func->params) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1774 | Mark(param); |
Ben Clayton | 6d60c04 | 2021-08-23 19:01:19 +0000 | [diff] [blame] | 1775 | |
Ben Clayton | 9021eb5 | 2021-08-26 15:40:06 +0000 | [diff] [blame] | 1776 | { // Check the parameter name is unique for the function |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1777 | auto emplaced = parameter_names.emplace(param->symbol, param->source); |
Ben Clayton | 9021eb5 | 2021-08-26 15:40:06 +0000 | [diff] [blame] | 1778 | if (!emplaced.second) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1779 | auto name = builder_->Symbols().NameFor(param->symbol); |
| 1780 | AddError("redefinition of parameter '" + name + "'", param->source); |
Ben Clayton | 9021eb5 | 2021-08-26 15:40:06 +0000 | [diff] [blame] | 1781 | AddNote("previous definition is here", emplaced.first->second); |
| 1782 | return false; |
| 1783 | } |
Ben Clayton | 6d60c04 | 2021-08-23 19:01:19 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 1786 | auto* param_info = |
| 1787 | Variable(param, VariableKind::kParameter, parameter_index++); |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 1788 | if (!param_info) { |
| 1789 | return false; |
| 1790 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1791 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1792 | for (auto* deco : param->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1793 | Mark(deco); |
| 1794 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1795 | if (!ValidateNoDuplicateDecorations(param->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 1796 | return false; |
| 1797 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1798 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1799 | variable_stack_.set(param->symbol, param_info); |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1800 | info->parameters.emplace_back(param_info); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1801 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1802 | if (!ApplyStorageClassUsageToType(param->declared_storage_class, |
| 1803 | param_info->type, param->source)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1804 | AddNote("while instantiating parameter " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1805 | builder_->Symbols().NameFor(param->symbol), |
| 1806 | param->source); |
Ben Clayton | c87ff9c | 2021-03-31 17:23:07 +0000 | [diff] [blame] | 1807 | return false; |
| 1808 | } |
| 1809 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1810 | if (auto* str = param_info->type->As<sem::Struct>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1811 | switch (func->PipelineStage()) { |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1812 | case ast::PipelineStage::kVertex: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1813 | str->AddUsage(sem::PipelineStageUsage::kVertexInput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1814 | break; |
| 1815 | case ast::PipelineStage::kFragment: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1816 | str->AddUsage(sem::PipelineStageUsage::kFragmentInput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1817 | break; |
| 1818 | case ast::PipelineStage::kCompute: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1819 | str->AddUsage(sem::PipelineStageUsage::kComputeInput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1820 | break; |
| 1821 | case ast::PipelineStage::kNone: |
| 1822 | break; |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1827 | if (auto* ty = func->return_type) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1828 | info->return_type = Type(ty); |
| 1829 | info->return_type_name = ty->FriendlyName(builder_->Symbols()); |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1830 | if (!info->return_type) { |
| 1831 | return false; |
| 1832 | } |
| 1833 | } else { |
| 1834 | info->return_type = builder_->create<sem::Void>(); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1835 | info->return_type_name = |
| 1836 | info->return_type->FriendlyName(builder_->Symbols()); |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1839 | if (auto* str = info->return_type->As<sem::Struct>()) { |
James Price | 5c523f3 | 2021-03-31 15:44:07 +0000 | [diff] [blame] | 1840 | if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, str, |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1841 | func->source)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1842 | AddNote("while instantiating return type for " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1843 | builder_->Symbols().NameFor(func->symbol), |
| 1844 | func->source); |
James Price | 5c523f3 | 2021-03-31 15:44:07 +0000 | [diff] [blame] | 1845 | return false; |
| 1846 | } |
| 1847 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1848 | switch (func->PipelineStage()) { |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1849 | case ast::PipelineStage::kVertex: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1850 | str->AddUsage(sem::PipelineStageUsage::kVertexOutput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1851 | break; |
| 1852 | case ast::PipelineStage::kFragment: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1853 | str->AddUsage(sem::PipelineStageUsage::kFragmentOutput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1854 | break; |
| 1855 | case ast::PipelineStage::kCompute: |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1856 | str->AddUsage(sem::PipelineStageUsage::kComputeOutput); |
James Price | 494e82d | 2021-03-31 15:42:17 +0000 | [diff] [blame] | 1857 | break; |
| 1858 | case ast::PipelineStage::kNone: |
| 1859 | break; |
| 1860 | } |
dan sinclair | 011aed9 | 2020-06-22 20:18:17 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1863 | if (func->body) { |
| 1864 | Mark(func->body); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 1865 | if (current_compound_statement_) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 1866 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 1867 | << "Resolver::Function() called with a current compound statement"; |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 1868 | return false; |
| 1869 | } |
Ben Clayton | 22d891c | 2021-05-20 15:17:08 +0000 | [diff] [blame] | 1870 | auto* sem_block = builder_->create<sem::FunctionBlockStatement>(func); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1871 | builder_->Sem().Add(func->body, sem_block); |
| 1872 | if (!Scope(sem_block, [&] { return Statements(func->body->statements); })) { |
Ben Clayton | b502fdf | 2021-04-07 08:09:21 +0000 | [diff] [blame] | 1873 | return false; |
| 1874 | } |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 1875 | } |
| 1876 | variable_stack_.pop_scope(); |
| 1877 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1878 | for (auto* deco : func->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1879 | Mark(deco); |
| 1880 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1881 | if (!ValidateNoDuplicateDecorations(func->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 1882 | return false; |
| 1883 | } |
| 1884 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1885 | for (auto* deco : func->return_type_decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1886 | Mark(deco); |
| 1887 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1888 | if (!ValidateNoDuplicateDecorations(func->return_type_decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 1889 | return false; |
| 1890 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 1891 | |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 1892 | // Set work-group size defaults. |
| 1893 | for (int i = 0; i < 3; i++) { |
| 1894 | info->workgroup_size[i].value = 1; |
| 1895 | info->workgroup_size[i].overridable_const = nullptr; |
| 1896 | } |
| 1897 | |
| 1898 | if (auto* workgroup = |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1899 | ast::GetDecoration<ast::WorkgroupDecoration>(func->decorations)) { |
| 1900 | auto values = workgroup->Values(); |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1901 | auto any_i32 = false; |
| 1902 | auto any_u32 = false; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1903 | for (int i = 0; i < 3; i++) { |
| 1904 | // Each argument to this decoration can either be a literal, an |
| 1905 | // identifier for a module-scope constants, or nullptr if not specified. |
| 1906 | |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1907 | auto* expr = values[i]; |
| 1908 | if (!expr) { |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1909 | // Not specified, just use the default. |
| 1910 | continue; |
| 1911 | } |
| 1912 | |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1913 | if (!Expression(expr)) { |
| 1914 | return false; |
| 1915 | } |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1916 | |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1917 | constexpr const char* kErrBadType = |
James Price | 69ce5f7 | 2021-09-02 10:05:19 +0000 | [diff] [blame] | 1918 | "workgroup_size argument must be either literal or module-scope " |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1919 | "constant of type i32 or u32"; |
| 1920 | constexpr const char* kErrInconsistentType = |
James Price | 69ce5f7 | 2021-09-02 10:05:19 +0000 | [diff] [blame] | 1921 | "workgroup_size arguments must be of the same type, either i32 " |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1922 | "or u32"; |
| 1923 | |
| 1924 | auto* ty = TypeOf(expr); |
| 1925 | bool is_i32 = ty->UnwrapRef()->Is<sem::I32>(); |
| 1926 | bool is_u32 = ty->UnwrapRef()->Is<sem::U32>(); |
| 1927 | if (!is_i32 && !is_u32) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1928 | AddError(kErrBadType, expr->source); |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1929 | return false; |
| 1930 | } |
| 1931 | |
| 1932 | any_i32 = any_i32 || is_i32; |
| 1933 | any_u32 = any_u32 || is_u32; |
| 1934 | if (any_i32 && any_u32) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1935 | AddError(kErrInconsistentType, expr->source); |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1936 | return false; |
| 1937 | } |
| 1938 | |
| 1939 | if (auto* ident = expr->As<ast::IdentifierExpression>()) { |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1940 | // We have an identifier of a module-scope constant. |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1941 | VariableInfo* var = nullptr; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1942 | if (!variable_stack_.get(ident->symbol, &var) || |
| 1943 | !(var->declaration->is_const)) { |
| 1944 | AddError(kErrBadType, expr->source); |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1945 | return false; |
| 1946 | } |
| 1947 | |
| 1948 | // Capture the constant if an [[override]] attribute is present. |
| 1949 | if (ast::HasDecoration<ast::OverrideDecoration>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1950 | var->declaration->decorations)) { |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1951 | info->workgroup_size[i].overridable_const = var->declaration; |
| 1952 | } |
| 1953 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1954 | expr = var->declaration->constructor; |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1955 | if (!expr) { |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1956 | // No constructor means this value must be overriden by the user. |
| 1957 | info->workgroup_size[i].value = 0; |
| 1958 | continue; |
| 1959 | } |
James Price | 69ce5f7 | 2021-09-02 10:05:19 +0000 | [diff] [blame] | 1960 | } else if (!expr->Is<ast::ScalarConstructorExpression>()) { |
| 1961 | AddError( |
| 1962 | "workgroup_size argument must be either a literal or a " |
| 1963 | "module-scope constant", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1964 | values[i]->source); |
James Price | 69ce5f7 | 2021-09-02 10:05:19 +0000 | [diff] [blame] | 1965 | return false; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1968 | auto val = ConstantValueOf(expr); |
| 1969 | if (!val) { |
| 1970 | TINT_ICE(Resolver, diagnostics_) |
| 1971 | << "could not resolve constant workgroup_size constant value"; |
| 1972 | continue; |
| 1973 | } |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1974 | // Validate and set the default value for this dimension. |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1975 | if (is_i32 ? val.Elements()[0].i32 < 1 : val.Elements()[0].u32 < 1) { |
James Price | 69ce5f7 | 2021-09-02 10:05:19 +0000 | [diff] [blame] | 1976 | AddError("workgroup_size argument must be at least 1", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1977 | values[i]->source); |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1978 | return false; |
| 1979 | } |
Ben Clayton | 9ba6500 | 2021-07-28 21:24:06 +0000 | [diff] [blame] | 1980 | |
| 1981 | info->workgroup_size[i].value = |
| 1982 | is_i32 ? static_cast<uint32_t>(val.Elements()[0].i32) |
| 1983 | : val.Elements()[0].u32; |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1984 | } |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1987 | if (!ValidateFunction(func, info)) { |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 1988 | return false; |
| 1989 | } |
| 1990 | |
dan sinclair | f8fa6cf | 2021-02-24 22:11:34 +0000 | [diff] [blame] | 1991 | // Register the function information _after_ processing the statements. This |
| 1992 | // allows us to catch a function calling itself when determining the call |
| 1993 | // information as this function doesn't exist until it's finished. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 1994 | symbol_to_function_[func->symbol] = info; |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 1995 | function_to_info_.emplace(func, info); |
dan sinclair | f8fa6cf | 2021-02-24 22:11:34 +0000 | [diff] [blame] | 1996 | |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 1997 | return true; |
| 1998 | } |
| 1999 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2000 | bool Resolver::Statements(const ast::StatementList& stmts) { |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2001 | for (auto* stmt : stmts) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2002 | Mark(stmt); |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2003 | if (!Statement(stmt)) { |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 2004 | return false; |
| 2005 | } |
| 2006 | } |
Sarah | c3f8fdf | 2021-06-21 17:53:56 +0000 | [diff] [blame] | 2007 | if (!ValidateStatements(stmts)) { |
| 2008 | return false; |
| 2009 | } |
| 2010 | |
| 2011 | return true; |
| 2012 | } |
| 2013 | |
| 2014 | bool Resolver::ValidateStatements(const ast::StatementList& stmts) { |
Ben Clayton | 25517e9 | 2021-09-09 12:38:19 +0000 | [diff] [blame] | 2015 | bool unreachable = false; |
Sarah | c3f8fdf | 2021-06-21 17:53:56 +0000 | [diff] [blame] | 2016 | for (auto* stmt : stmts) { |
Ben Clayton | 25517e9 | 2021-09-09 12:38:19 +0000 | [diff] [blame] | 2017 | if (unreachable) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2018 | AddError("code is unreachable", stmt->source); |
Ben Clayton | 25517e9 | 2021-09-09 12:38:19 +0000 | [diff] [blame] | 2019 | return false; |
| 2020 | } |
| 2021 | |
| 2022 | auto* nested_stmt = stmt; |
| 2023 | while (auto* block = nested_stmt->As<ast::BlockStatement>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2024 | if (block->Empty()) { |
Ben Clayton | 25517e9 | 2021-09-09 12:38:19 +0000 | [diff] [blame] | 2025 | break; |
Sarah | c3f8fdf | 2021-06-21 17:53:56 +0000 | [diff] [blame] | 2026 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2027 | nested_stmt = block->statements.back(); |
Ben Clayton | 25517e9 | 2021-09-09 12:38:19 +0000 | [diff] [blame] | 2028 | } |
| 2029 | if (nested_stmt->IsAnyOf<ast::ReturnStatement, ast::BreakStatement, |
| 2030 | ast::ContinueStatement, ast::DiscardStatement>()) { |
| 2031 | unreachable = true; |
Sarah | c3f8fdf | 2021-06-21 17:53:56 +0000 | [diff] [blame] | 2032 | } |
| 2033 | } |
dan sinclair | 0975dd5 | 2020-07-27 15:25:00 +0000 | [diff] [blame] | 2034 | return true; |
| 2035 | } |
| 2036 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2037 | bool Resolver::Statement(const ast::Statement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2038 | if (stmt->Is<ast::CaseStatement>()) { |
| 2039 | AddError("case statement can only be used inside a switch statement", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2040 | stmt->source); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2041 | return false; |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2042 | } |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2043 | if (stmt->Is<ast::ElseStatement>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2044 | TINT_ICE(Resolver, diagnostics_) |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2045 | << "Resolver::Statement() encountered an Else statement. Else " |
| 2046 | "statements are embedded in If statements, so should never be " |
| 2047 | "encountered as top-level statements"; |
| 2048 | return false; |
| 2049 | } |
| 2050 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2051 | // Compound statements. These create their own sem::CompoundStatement |
| 2052 | // bindings. |
| 2053 | if (auto* b = stmt->As<ast::BlockStatement>()) { |
| 2054 | return BlockStatement(b); |
| 2055 | } |
| 2056 | if (auto* l = stmt->As<ast::ForLoopStatement>()) { |
| 2057 | return ForLoopStatement(l); |
| 2058 | } |
| 2059 | if (auto* l = stmt->As<ast::LoopStatement>()) { |
| 2060 | return LoopStatement(l); |
| 2061 | } |
| 2062 | if (auto* i = stmt->As<ast::IfStatement>()) { |
| 2063 | return IfStatement(i); |
| 2064 | } |
| 2065 | if (auto* s = stmt->As<ast::SwitchStatement>()) { |
| 2066 | return SwitchStatement(s); |
| 2067 | } |
| 2068 | |
| 2069 | // Non-Compound statements |
| 2070 | sem::Statement* sem_statement = |
| 2071 | builder_->create<sem::Statement>(stmt, current_compound_statement_); |
| 2072 | builder_->Sem().Add(stmt, sem_statement); |
| 2073 | TINT_SCOPED_ASSIGNMENT(current_statement_, sem_statement); |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2074 | if (auto* a = stmt->As<ast::AssignmentStatement>()) { |
Antonio Maiorano | e09989a | 2021-03-31 13:26:43 +0000 | [diff] [blame] | 2075 | return Assignment(a); |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 2076 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2077 | if (stmt->Is<ast::BreakStatement>()) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2078 | if (!sem_statement->FindFirstParent<sem::LoopBlockStatement>() && |
| 2079 | !sem_statement->FindFirstParent<sem::SwitchCaseBlockStatement>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2080 | AddError("break statement must be in a loop or switch case", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2081 | stmt->source); |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 2082 | return false; |
| 2083 | } |
dan sinclair | 6bd7061 | 2020-06-03 16:11:28 +0000 | [diff] [blame] | 2084 | return true; |
dan sinclair | b7ea6e2 | 2020-04-07 12:54:10 +0000 | [diff] [blame] | 2085 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2086 | if (auto* c = stmt->As<ast::CallStatement>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2087 | if (!Expression(c->expr)) { |
Ben Clayton | 7fe0106 | 2021-06-11 13:22:27 +0000 | [diff] [blame] | 2088 | return false; |
| 2089 | } |
| 2090 | if (!ValidateCallStatement(c)) { |
| 2091 | return false; |
| 2092 | } |
| 2093 | return true; |
dan sinclair | 50080b7 | 2020-07-21 13:42:13 +0000 | [diff] [blame] | 2094 | } |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 2095 | if (auto* c = stmt->As<ast::ContinueStatement>()) { |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2096 | // 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] | 2097 | if (auto* block = |
| 2098 | current_block_->FindFirstParent< |
| 2099 | sem::LoopBlockStatement, sem::LoopContinuingBlockStatement>()) { |
| 2100 | if (auto* loop_block = block->As<sem::LoopBlockStatement>()) { |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 2101 | if (!loop_block->FirstContinue()) { |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2102 | const_cast<sem::LoopBlockStatement*>(loop_block) |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 2103 | ->SetFirstContinue(c, loop_block->Decls().size()); |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2104 | } |
| 2105 | } else { |
| 2106 | AddError("continuing blocks must not contain a continue statement", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2107 | stmt->source); |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2108 | return false; |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2109 | } |
| 2110 | } else { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2111 | AddError("continue statement must be in a loop", stmt->source); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2112 | return false; |
| 2113 | } |
| 2114 | |
dan sinclair | 6bd7061 | 2020-06-03 16:11:28 +0000 | [diff] [blame] | 2115 | return true; |
dan sinclair | aec965e | 2020-04-07 12:54:29 +0000 | [diff] [blame] | 2116 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2117 | if (stmt->Is<ast::DiscardStatement>()) { |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2118 | if (auto* continuing = |
| 2119 | sem_statement |
| 2120 | ->FindFirstParent<sem::LoopContinuingBlockStatement>()) { |
| 2121 | AddError("continuing blocks must not contain a discard statement", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2122 | stmt->source); |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2123 | if (continuing != sem_statement->Parent()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2124 | AddNote("see continuing block here", continuing->Declaration()->source); |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 2125 | } |
| 2126 | return false; |
| 2127 | } |
dan sinclair | 8f3c635 | 2020-07-25 14:33:50 +0000 | [diff] [blame] | 2128 | return true; |
| 2129 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2130 | if (stmt->Is<ast::FallthroughStatement>()) { |
dan sinclair | 1913fc9 | 2020-04-07 12:54:59 +0000 | [diff] [blame] | 2131 | return true; |
| 2132 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2133 | if (auto* r = stmt->As<ast::ReturnStatement>()) { |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 2134 | return Return(r); |
dan sinclair | bf0fff8 | 2020-04-07 12:56:24 +0000 | [diff] [blame] | 2135 | } |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 2136 | if (auto* v = stmt->As<ast::VariableDeclStatement>()) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 2137 | return VariableDeclStatement(v); |
dan sinclair | ca893e3 | 2020-04-07 12:57:12 +0000 | [diff] [blame] | 2138 | } |
dan sinclair | 6c498fc | 2020-04-07 12:47:23 +0000 | [diff] [blame] | 2139 | |
Ben Clayton | 11ed0db | 2021-10-14 20:30:29 +0000 | [diff] [blame] | 2140 | AddError("unknown statement type for type determination: " + |
| 2141 | std::string(stmt->TypeInfo().name), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2142 | stmt->source); |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 2143 | return false; |
| 2144 | } |
| 2145 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2146 | bool Resolver::CaseStatement(const ast::CaseStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2147 | auto* sem = builder_->create<sem::SwitchCaseBlockStatement>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2148 | stmt->body, current_compound_statement_); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2149 | builder_->Sem().Add(stmt, sem); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2150 | builder_->Sem().Add(stmt->body, sem); |
| 2151 | Mark(stmt->body); |
| 2152 | for (auto* sel : stmt->selectors) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2153 | Mark(sel); |
| 2154 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2155 | return Scope(sem, [&] { return Statements(stmt->body->statements); }); |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2158 | bool Resolver::IfStatement(const ast::IfStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2159 | auto* sem = |
| 2160 | builder_->create<sem::IfStatement>(stmt, current_compound_statement_); |
| 2161 | builder_->Sem().Add(stmt, sem); |
| 2162 | return Scope(sem, [&] { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2163 | if (!Expression(stmt->condition)) { |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2164 | return false; |
| 2165 | } |
Ben Clayton | 5fb87dd | 2021-03-09 15:17:28 +0000 | [diff] [blame] | 2166 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2167 | auto* cond_type = TypeOf(stmt->condition)->UnwrapRef(); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2168 | if (!cond_type->Is<sem::Bool>()) { |
| 2169 | AddError("if statement condition must be bool, got " + |
| 2170 | cond_type->FriendlyName(builder_->Symbols()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2171 | stmt->condition->source); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2172 | return false; |
| 2173 | } |
| 2174 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2175 | Mark(stmt->body); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2176 | auto* body = builder_->create<sem::BlockStatement>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2177 | stmt->body, current_compound_statement_); |
| 2178 | builder_->Sem().Add(stmt->body, body); |
| 2179 | if (!Scope(body, [&] { return Statements(stmt->body->statements); })) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2180 | return false; |
| 2181 | } |
| 2182 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2183 | for (auto* else_stmt : stmt->else_statements) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2184 | Mark(else_stmt); |
| 2185 | if (!ElseStatement(else_stmt)) { |
| 2186 | return false; |
| 2187 | } |
| 2188 | } |
| 2189 | return true; |
| 2190 | }); |
| 2191 | } |
| 2192 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2193 | bool Resolver::ElseStatement(const ast::ElseStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2194 | auto* sem = |
| 2195 | builder_->create<sem::ElseStatement>(stmt, current_compound_statement_); |
| 2196 | builder_->Sem().Add(stmt, sem); |
| 2197 | return Scope(sem, [&] { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2198 | if (auto* cond = stmt->condition) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 2199 | if (!Expression(cond)) { |
| 2200 | return false; |
| 2201 | } |
Antonio Maiorano | 9fdfa1e | 2021-05-10 13:46:06 +0000 | [diff] [blame] | 2202 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2203 | auto* else_cond_type = TypeOf(cond)->UnwrapRef(); |
Ben Clayton | c03d3bd | 2021-05-20 14:22:28 +0000 | [diff] [blame] | 2204 | if (!else_cond_type->Is<sem::Bool>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2205 | AddError("else statement condition must be bool, got " + |
| 2206 | else_cond_type->FriendlyName(builder_->Symbols()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2207 | cond->source); |
Antonio Maiorano | 9fdfa1e | 2021-05-10 13:46:06 +0000 | [diff] [blame] | 2208 | return false; |
| 2209 | } |
Ben Clayton | dba65b7 | 2021-03-31 20:35:46 +0000 | [diff] [blame] | 2210 | } |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2211 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2212 | Mark(stmt->body); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2213 | auto* body = builder_->create<sem::BlockStatement>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2214 | stmt->body, current_compound_statement_); |
| 2215 | builder_->Sem().Add(stmt->body, body); |
| 2216 | return Scope(body, [&] { return Statements(stmt->body->statements); }); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2217 | }); |
| 2218 | } |
| 2219 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2220 | bool Resolver::BlockStatement(const ast::BlockStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2221 | auto* sem = builder_->create<sem::BlockStatement>( |
| 2222 | stmt->As<ast::BlockStatement>(), current_compound_statement_); |
| 2223 | builder_->Sem().Add(stmt, sem); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2224 | return Scope(sem, [&] { return Statements(stmt->statements); }); |
Ben Clayton | 5fb87dd | 2021-03-09 15:17:28 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2227 | bool Resolver::LoopStatement(const ast::LoopStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2228 | auto* sem = |
| 2229 | builder_->create<sem::LoopStatement>(stmt, current_compound_statement_); |
| 2230 | builder_->Sem().Add(stmt, sem); |
| 2231 | return Scope(sem, [&] { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2232 | Mark(stmt->body); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2233 | |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2234 | auto* body = builder_->create<sem::LoopBlockStatement>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2235 | stmt->body, current_compound_statement_); |
| 2236 | builder_->Sem().Add(stmt->body, body); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2237 | return Scope(body, [&] { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2238 | if (!Statements(stmt->body->statements)) { |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2239 | return false; |
| 2240 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2241 | if (stmt->continuing) { |
| 2242 | Mark(stmt->continuing); |
| 2243 | if (!stmt->continuing->Empty()) { |
| 2244 | auto* continuing = |
| 2245 | builder_->create<sem::LoopContinuingBlockStatement>( |
| 2246 | stmt->continuing, current_compound_statement_); |
| 2247 | builder_->Sem().Add(stmt->continuing, continuing); |
| 2248 | if (!Scope(continuing, [&] { |
| 2249 | return Statements(stmt->continuing->statements); |
| 2250 | })) { |
| 2251 | return false; |
| 2252 | } |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | return true; |
| 2256 | }); |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2257 | }); |
| 2258 | } |
| 2259 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2260 | bool Resolver::ForLoopStatement(const ast::ForLoopStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2261 | auto* sem = builder_->create<sem::ForLoopStatement>( |
| 2262 | stmt, current_compound_statement_); |
| 2263 | builder_->Sem().Add(stmt, sem); |
| 2264 | return Scope(sem, [&] { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2265 | if (auto* initializer = stmt->initializer) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2266 | Mark(initializer); |
| 2267 | if (!Statement(initializer)) { |
| 2268 | return false; |
| 2269 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2272 | if (auto* condition = stmt->condition) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2273 | if (!Expression(condition)) { |
| 2274 | return false; |
| 2275 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2276 | |
Ben Clayton | ada4864 | 2021-07-29 16:55:27 +0000 | [diff] [blame] | 2277 | if (!TypeOf(condition)->UnwrapRef()->Is<sem::Bool>()) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2278 | AddError( |
| 2279 | "for-loop condition must be bool, got " + TypeNameOf(condition), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2280 | condition->source); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2281 | return false; |
| 2282 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2283 | } |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2284 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2285 | if (auto* continuing = stmt->continuing) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2286 | Mark(continuing); |
| 2287 | if (!Statement(continuing)) { |
| 2288 | return false; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2292 | Mark(stmt->body); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2293 | |
| 2294 | auto* body = builder_->create<sem::LoopBlockStatement>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2295 | stmt->body, current_compound_statement_); |
| 2296 | builder_->Sem().Add(stmt->body, body); |
| 2297 | return Scope(body, [&] { return Statements(stmt->body->statements); }); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 2298 | }); |
Ben Clayton | f4075a7 | 2021-07-02 19:27:42 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2301 | bool Resolver::Expression(const ast::Expression* root) { |
| 2302 | std::vector<const ast::Expression*> sorted; |
Ben Clayton | f164a4a | 2021-10-21 20:38:54 +0000 | [diff] [blame] | 2303 | if (!ast::TraverseExpressions<ast::TraverseOrder::RightToLeft>( |
| 2304 | root, diagnostics_, [&](const ast::Expression* expr) { |
| 2305 | Mark(expr); |
| 2306 | sorted.emplace_back(expr); |
| 2307 | return ast::TraverseAction::Descend; |
| 2308 | })) { |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 2309 | return false; |
| 2310 | } |
| 2311 | |
Ben Clayton | b7bcbf0 | 2021-09-08 15:18:36 +0000 | [diff] [blame] | 2312 | for (auto* expr : utils::Reverse(sorted)) { |
| 2313 | bool ok = false; |
| 2314 | if (auto* array = expr->As<ast::ArrayAccessorExpression>()) { |
| 2315 | ok = ArrayAccessor(array); |
| 2316 | } else if (auto* bin_op = expr->As<ast::BinaryExpression>()) { |
| 2317 | ok = Binary(bin_op); |
| 2318 | } else if (auto* bitcast = expr->As<ast::BitcastExpression>()) { |
| 2319 | ok = Bitcast(bitcast); |
| 2320 | } else if (auto* call = expr->As<ast::CallExpression>()) { |
| 2321 | ok = Call(call); |
| 2322 | } else if (auto* ctor = expr->As<ast::ConstructorExpression>()) { |
| 2323 | ok = Constructor(ctor); |
| 2324 | } else if (auto* ident = expr->As<ast::IdentifierExpression>()) { |
| 2325 | ok = Identifier(ident); |
| 2326 | } else if (auto* member = expr->As<ast::MemberAccessorExpression>()) { |
| 2327 | ok = MemberAccessor(member); |
| 2328 | } else if (auto* unary = expr->As<ast::UnaryOpExpression>()) { |
| 2329 | ok = UnaryOp(unary); |
Ben Clayton | 1aa98e6 | 2021-10-21 23:04:44 +0000 | [diff] [blame] | 2330 | } else if (expr->Is<ast::PhonyExpression>()) { |
| 2331 | ok = true; // No-op |
Ben Clayton | b7bcbf0 | 2021-09-08 15:18:36 +0000 | [diff] [blame] | 2332 | } else { |
| 2333 | TINT_ICE(Resolver, diagnostics_) |
| 2334 | << "unhandled expression type: " << expr->TypeInfo().name; |
| 2335 | return false; |
| 2336 | } |
| 2337 | if (!ok) { |
| 2338 | return false; |
| 2339 | } |
| 2340 | } |
| 2341 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 2342 | return true; |
dan sinclair | 417a90d | 2020-04-06 21:07:41 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2345 | bool Resolver::ArrayAccessor(const ast::ArrayAccessorExpression* expr) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2346 | auto* idx = expr->index; |
| 2347 | auto* res = TypeOf(expr->array); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2348 | auto* parent_type = res->UnwrapRef(); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 2349 | const sem::Type* ret = nullptr; |
| 2350 | if (auto* arr = parent_type->As<sem::Array>()) { |
| 2351 | ret = arr->ElemType(); |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2352 | } else if (auto* vec = parent_type->As<sem::Vector>()) { |
Ben Clayton | 1b6a8ce | 2020-12-01 21:07:27 +0000 | [diff] [blame] | 2353 | ret = vec->type(); |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2354 | } else if (auto* mat = parent_type->As<sem::Matrix>()) { |
| 2355 | ret = builder_->create<sem::Vector>(mat->type(), mat->rows()); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2356 | } else { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2357 | AddError("cannot index type '" + TypeNameOf(expr->array) + "'", |
| 2358 | expr->source); |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2359 | return false; |
| 2360 | } |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2361 | |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2362 | if (!TypeOf(idx)->UnwrapRef()->IsAnyOf<sem::I32, sem::U32>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2363 | AddError("index must be of type 'i32' or 'u32', found: '" + |
| 2364 | TypeNameOf(idx) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2365 | idx->source); |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2366 | return false; |
| 2367 | } |
| 2368 | |
| 2369 | if (parent_type->Is<sem::Array>() || parent_type->Is<sem::Matrix>()) { |
| 2370 | if (!res->Is<sem::Reference>()) { |
| 2371 | // TODO(bclayton): expand this to allow any const_expr expression |
| 2372 | // https://github.com/gpuweb/gpuweb/issues/1272 |
| 2373 | auto* scalar = idx->As<ast::ScalarConstructorExpression>(); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2374 | if (!scalar || !scalar->literal->As<ast::IntLiteral>()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2375 | AddError("index must be signed or unsigned integer literal", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2376 | idx->source); |
Sarah | 10442ef | 2021-06-16 18:50:13 +0000 | [diff] [blame] | 2377 | return false; |
| 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2382 | // If we're extracting from a reference, we return a reference. |
| 2383 | if (auto* ref = res->As<sem::Reference>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 2384 | ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), |
| 2385 | ref->Access()); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2386 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2387 | SetExprInfo(expr, ret); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 2388 | |
dan sinclair | 973bd6a | 2020-04-07 12:57:42 +0000 | [diff] [blame] | 2389 | return true; |
| 2390 | } |
| 2391 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2392 | bool Resolver::Bitcast(const ast::BitcastExpression* expr) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2393 | auto* ty = Type(expr->type); |
Ben Clayton | 0e66b40 | 2021-06-03 08:17:44 +0000 | [diff] [blame] | 2394 | if (!ty) { |
| 2395 | return false; |
| 2396 | } |
Sarah | 4b1c9de | 2021-06-16 17:42:13 +0000 | [diff] [blame] | 2397 | if (ty->Is<sem::Pointer>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2398 | AddError("cannot cast to a pointer", expr->source); |
Sarah | 4b1c9de | 2021-06-16 17:42:13 +0000 | [diff] [blame] | 2399 | return false; |
| 2400 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2401 | SetExprInfo(expr, ty, expr->type->FriendlyName(builder_->Symbols())); |
dan sinclair | a01777c | 2020-04-07 12:57:52 +0000 | [diff] [blame] | 2402 | return true; |
| 2403 | } |
| 2404 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2405 | bool Resolver::Call(const ast::CallExpression* call) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2406 | Mark(call->func); |
| 2407 | auto* ident = call->func; |
| 2408 | auto name = builder_->Symbols().NameFor(ident->symbol); |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2409 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 2410 | auto intrinsic_type = sem::ParseIntrinsicType(name); |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 2411 | if (intrinsic_type != IntrinsicType::kNone) { |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 2412 | if (!IntrinsicCall(call, intrinsic_type)) { |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2413 | return false; |
| 2414 | } |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 2415 | } else { |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2416 | if (!FunctionCall(call)) { |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 2417 | return false; |
| 2418 | } |
Sarah Mashayekhi | 844f632 | 2020-08-18 02:10:03 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Ben Clayton | 1fa6f5c | 2021-08-21 08:50:40 +0000 | [diff] [blame] | 2421 | return ValidateCall(call); |
| 2422 | } |
| 2423 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2424 | bool Resolver::ValidateCall(const ast::CallExpression* call) { |
Ben Clayton | 1fa6f5c | 2021-08-21 08:50:40 +0000 | [diff] [blame] | 2425 | if (TypeOf(call)->Is<sem::Void>()) { |
| 2426 | bool is_call_statement = false; |
| 2427 | if (current_statement_) { |
| 2428 | if (auto* call_stmt = |
| 2429 | As<ast::CallStatement>(current_statement_->Declaration())) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2430 | if (call_stmt->expr == call) { |
Ben Clayton | 1fa6f5c | 2021-08-21 08:50:40 +0000 | [diff] [blame] | 2431 | is_call_statement = true; |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | if (!is_call_statement) { |
| 2436 | // https://gpuweb.github.io/gpuweb/wgsl/#function-call-expr |
| 2437 | // If the called function does not return a value, a function call |
| 2438 | // statement should be used instead. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2439 | auto* ident = call->func; |
| 2440 | auto name = builder_->Symbols().NameFor(ident->symbol); |
Ben Clayton | 1fa6f5c | 2021-08-21 08:50:40 +0000 | [diff] [blame] | 2441 | // A function call is made to either a user declared function or an |
| 2442 | // intrinsic. function_calls_ only maps CallExpression to user declared |
| 2443 | // functions |
| 2444 | bool is_function = function_calls_.count(call) != 0; |
| 2445 | AddError((is_function ? "function" : "intrinsic") + std::string(" '") + |
| 2446 | name + "' does not return a value", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2447 | call->source); |
Ben Clayton | 1fa6f5c | 2021-08-21 08:50:40 +0000 | [diff] [blame] | 2448 | return false; |
| 2449 | } |
| 2450 | } |
| 2451 | |
dan sinclair | 3ca8746 | 2020-04-07 16:41:10 +0000 | [diff] [blame] | 2452 | return true; |
| 2453 | } |
| 2454 | |
Ben Clayton | 72789de | 2021-10-21 20:36:04 +0000 | [diff] [blame] | 2455 | bool Resolver::ValidateCallStatement(const ast::CallStatement*) { |
Ben Clayton | 7fe0106 | 2021-06-11 13:22:27 +0000 | [diff] [blame] | 2456 | return true; |
| 2457 | } |
| 2458 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2459 | bool Resolver::IntrinsicCall(const ast::CallExpression* call, |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 2460 | sem::IntrinsicType intrinsic_type) { |
Ben Clayton | 12ed13d | 2021-04-28 12:38:13 +0000 | [diff] [blame] | 2461 | std::vector<const sem::Type*> arg_tys; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2462 | arg_tys.reserve(call->args.size()); |
| 2463 | for (auto* expr : call->args) { |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 2464 | arg_tys.emplace_back(TypeOf(expr)); |
| 2465 | } |
| 2466 | |
Ben Clayton | b29a59d | 2021-06-01 19:06:31 +0000 | [diff] [blame] | 2467 | auto* result = |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2468 | intrinsic_table_->Lookup(intrinsic_type, arg_tys, call->source); |
Ben Clayton | b29a59d | 2021-06-01 19:06:31 +0000 | [diff] [blame] | 2469 | if (!result) { |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2470 | return false; |
| 2471 | } |
| 2472 | |
Ben Clayton | 3e59eb0 | 2021-06-10 17:31:54 +0000 | [diff] [blame] | 2473 | if (result->IsDeprecated()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2474 | AddWarning("use of deprecated intrinsic", call->source); |
Ben Clayton | 3e59eb0 | 2021-06-10 17:31:54 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2477 | auto* out = builder_->create<sem::Call>(call, result, current_statement_); |
| 2478 | builder_->Sem().Add(call, out); |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2479 | SetExprInfo(call, result->ReturnType()); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 2480 | |
| 2481 | current_function_->intrinsic_calls.emplace_back( |
| 2482 | IntrinsicCallInfo{call, result}); |
| 2483 | |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2484 | if (IsTextureIntrinsic(intrinsic_type) && |
| 2485 | !ValidateTextureIntrinsicFunction(call, out)) { |
| 2486 | return false; |
| 2487 | } |
| 2488 | |
| 2489 | return true; |
| 2490 | } |
| 2491 | |
| 2492 | bool Resolver::ValidateTextureIntrinsicFunction( |
| 2493 | const ast::CallExpression* ast_call, |
| 2494 | const sem::Call* sem_call) { |
| 2495 | auto* intrinsic = sem_call->Target()->As<sem::Intrinsic>(); |
| 2496 | if (!intrinsic) { |
| 2497 | return false; |
| 2498 | } |
| 2499 | std::string func_name = intrinsic->str(); |
Ben Clayton | 1364f20 | 2021-10-14 21:17:29 +0000 | [diff] [blame] | 2500 | auto& signature = intrinsic->Signature(); |
| 2501 | auto index = signature.IndexOf(sem::ParameterUsage::kOffset); |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2502 | if (index > -1) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2503 | auto* param = ast_call->args[index]; |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2504 | if (param->Is<ast::TypeConstructorExpression>()) { |
| 2505 | auto values = ConstantValueOf(param); |
| 2506 | if (!values.IsValid()) { |
Sarah | 36b49e8 | 2021-07-21 03:34:34 +0000 | [diff] [blame] | 2507 | AddError( |
| 2508 | "'" + func_name + "' offset parameter must be a const_expression", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2509 | param->source); |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2510 | return false; |
| 2511 | } |
| 2512 | if (!values.Type()->Is<sem::Vector>() || |
| 2513 | !values.ElementType()->Is<sem::I32>()) { |
| 2514 | TINT_ICE(Resolver, diagnostics_) |
| 2515 | << "failed to resolve '" + func_name + "' offset parameter type"; |
| 2516 | return false; |
| 2517 | } |
| 2518 | for (auto offset : values.Elements()) { |
| 2519 | auto offset_value = offset.i32; |
| 2520 | if (offset_value < -8 || offset_value > 7) { |
| 2521 | AddError("each offset component of '" + func_name + |
| 2522 | "' must be at least -8 and at most 7. " |
| 2523 | "found: '" + |
| 2524 | std::to_string(offset_value) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2525 | param->source); |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2526 | return false; |
| 2527 | } |
| 2528 | } |
| 2529 | } else { |
Sarah | 36b49e8 | 2021-07-21 03:34:34 +0000 | [diff] [blame] | 2530 | AddError( |
| 2531 | "'" + func_name + "' offset parameter must be a const_expression", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2532 | param->source); |
Sarah | 7119843 | 2021-07-16 13:38:05 +0000 | [diff] [blame] | 2533 | return false; |
| 2534 | } |
| 2535 | } |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 2536 | return true; |
dan sinclair | 46e959d | 2020-06-01 13:43:22 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2539 | bool Resolver::FunctionCall(const ast::CallExpression* call) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2540 | auto* ident = call->func; |
| 2541 | auto name = builder_->Symbols().NameFor(ident->symbol); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2542 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2543 | auto callee_func_it = symbol_to_function_.find(ident->symbol); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2544 | if (callee_func_it == symbol_to_function_.end()) { |
| 2545 | if (current_function_ && |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2546 | current_function_->declaration->symbol == ident->symbol) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 2547 | AddError("recursion is not permitted. '" + name + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2548 | "' attempted to call itself.", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2549 | call->source); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2550 | } else { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2551 | AddError("unable to find called function: " + name, call->source); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2552 | } |
| 2553 | return false; |
| 2554 | } |
| 2555 | auto* callee_func = callee_func_it->second; |
| 2556 | |
| 2557 | if (current_function_) { |
| 2558 | callee_func->callsites.push_back(call); |
| 2559 | |
| 2560 | // Note: Requires called functions to be resolved first. |
| 2561 | // This is currently guaranteed as functions must be declared before |
| 2562 | // use. |
| 2563 | current_function_->transitive_calls.add(callee_func); |
| 2564 | for (auto* transitive_call : callee_func->transitive_calls) { |
| 2565 | current_function_->transitive_calls.add(transitive_call); |
| 2566 | } |
| 2567 | |
| 2568 | // We inherit any referenced variables from the callee. |
| 2569 | for (auto* var : callee_func->referenced_module_vars) { |
| 2570 | set_referenced_from_function_if_needed(var, false); |
| 2571 | } |
| 2572 | } |
| 2573 | |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2574 | function_calls_.emplace(call, |
| 2575 | FunctionCallInfo{callee_func, current_statement_}); |
| 2576 | SetExprInfo(call, callee_func->return_type, callee_func->return_type_name); |
| 2577 | |
| 2578 | if (!ValidateFunctionCall(call, callee_func)) { |
| 2579 | return false; |
| 2580 | } |
| 2581 | return true; |
| 2582 | } |
| 2583 | |
| 2584 | bool Resolver::ValidateFunctionCall(const ast::CallExpression* call, |
Ben Clayton | b09723e | 2021-08-31 16:14:46 +0000 | [diff] [blame] | 2585 | const FunctionInfo* target) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2586 | auto* ident = call->func; |
| 2587 | auto name = builder_->Symbols().NameFor(ident->symbol); |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2588 | |
Ben Clayton | b09723e | 2021-08-31 16:14:46 +0000 | [diff] [blame] | 2589 | if (target->declaration->IsEntryPoint()) { |
| 2590 | // https://www.w3.org/TR/WGSL/#function-restriction |
| 2591 | // An entry point must never be the target of a function call. |
| 2592 | AddError("entry point functions cannot be the target of a function call", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2593 | call->source); |
Ben Clayton | b09723e | 2021-08-31 16:14:46 +0000 | [diff] [blame] | 2594 | return false; |
| 2595 | } |
| 2596 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2597 | if (call->args.size() != target->parameters.size()) { |
| 2598 | bool more = call->args.size() > target->parameters.size(); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2599 | AddError("too " + (more ? std::string("many") : std::string("few")) + |
| 2600 | " arguments in call to '" + name + "', expected " + |
Ben Clayton | b09723e | 2021-08-31 16:14:46 +0000 | [diff] [blame] | 2601 | std::to_string(target->parameters.size()) + ", got " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2602 | std::to_string(call->args.size()), |
| 2603 | call->source); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2604 | return false; |
| 2605 | } |
| 2606 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2607 | for (size_t i = 0; i < call->args.size(); ++i) { |
Ben Clayton | b09723e | 2021-08-31 16:14:46 +0000 | [diff] [blame] | 2608 | const VariableInfo* param = target->parameters[i]; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2609 | const ast::Expression* arg_expr = call->args[i]; |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2610 | auto* arg_type = TypeOf(arg_expr)->UnwrapRef(); |
| 2611 | |
| 2612 | if (param->type != arg_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2613 | AddError("type mismatch for argument " + std::to_string(i + 1) + |
| 2614 | " in call to '" + name + "', expected '" + |
| 2615 | param->type->FriendlyName(builder_->Symbols()) + "', got '" + |
| 2616 | arg_type->FriendlyName(builder_->Symbols()) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2617 | arg_expr->source); |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2618 | return false; |
| 2619 | } |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2620 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2621 | if (param->declaration->type->Is<ast::Pointer>()) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2622 | auto is_valid = false; |
| 2623 | if (auto* ident_expr = arg_expr->As<ast::IdentifierExpression>()) { |
| 2624 | VariableInfo* var; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2625 | if (!variable_stack_.get(ident_expr->symbol, &var)) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2626 | TINT_ICE(Resolver, diagnostics_) << "failed to resolve identifier"; |
| 2627 | return false; |
| 2628 | } |
| 2629 | if (var->kind == VariableKind::kParameter) { |
| 2630 | is_valid = true; |
| 2631 | } |
| 2632 | } else if (auto* unary = arg_expr->As<ast::UnaryOpExpression>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2633 | if (unary->op == ast::UnaryOp::kAddressOf) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2634 | if (auto* ident_unary = |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2635 | unary->expr->As<ast::IdentifierExpression>()) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2636 | VariableInfo* var; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2637 | if (!variable_stack_.get(ident_unary->symbol, &var)) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2638 | TINT_ICE(Resolver, diagnostics_) |
| 2639 | << "failed to resolve identifier"; |
| 2640 | return false; |
| 2641 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2642 | if (var->declaration->is_const) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2643 | TINT_ICE(Resolver, diagnostics_) |
| 2644 | << "Resolver::FunctionCall() encountered an address-of " |
| 2645 | "expression of a constant identifier expression"; |
| 2646 | return false; |
| 2647 | } |
| 2648 | is_valid = true; |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
James Price | 1ca6fba | 2021-09-29 18:56:17 +0000 | [diff] [blame] | 2653 | if (!is_valid && |
| 2654 | IsValidationEnabled( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2655 | param->declaration->decorations, |
James Price | 1ca6fba | 2021-09-29 18:56:17 +0000 | [diff] [blame] | 2656 | ast::DisabledValidation::kIgnoreInvalidPointerArgument)) { |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2657 | AddError( |
| 2658 | "expected an address-of expression of a variable identifier " |
| 2659 | "expression or a function parameter", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2660 | arg_expr->source); |
Sarah | 3d441d4 | 2021-07-20 18:14:02 +0000 | [diff] [blame] | 2661 | return false; |
| 2662 | } |
| 2663 | } |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2664 | } |
Antonio Maiorano | 14b3403 | 2021-06-09 20:17:59 +0000 | [diff] [blame] | 2665 | return true; |
| 2666 | } |
| 2667 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2668 | bool Resolver::Constructor(const ast::ConstructorExpression* expr) { |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2669 | if (auto* type_ctor = expr->As<ast::TypeConstructorExpression>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2670 | auto* type = Type(type_ctor->type); |
Ben Clayton | 0e66b40 | 2021-06-03 08:17:44 +0000 | [diff] [blame] | 2671 | if (!type) { |
| 2672 | return false; |
| 2673 | } |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2674 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2675 | auto type_name = type_ctor->type->FriendlyName(builder_->Symbols()); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 2676 | |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2677 | // Now that the argument types have been determined, make sure that they |
| 2678 | // obey the constructor type rules laid out in |
| 2679 | // https://gpuweb.github.io/gpuweb/wgsl.html#type-constructor-expr. |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2680 | bool ok = true; |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 2681 | if (auto* vec_type = type->As<sem::Vector>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2682 | ok = ValidateVectorConstructor(type_ctor, vec_type, type_name); |
| 2683 | } else if (auto* mat_type = type->As<sem::Matrix>()) { |
| 2684 | ok = ValidateMatrixConstructor(type_ctor, mat_type, type_name); |
| 2685 | } else if (type->is_scalar()) { |
| 2686 | ok = ValidateScalarConstructor(type_ctor, type, type_name); |
| 2687 | } else if (auto* arr_type = type->As<sem::Array>()) { |
| 2688 | ok = ValidateArrayConstructor(type_ctor, arr_type); |
| 2689 | } else if (auto* struct_type = type->As<sem::Struct>()) { |
| 2690 | ok = ValidateStructureConstructor(type_ctor, struct_type); |
James Price | cbbb420 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 2691 | } else { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2692 | AddError("type is not constructible", type_ctor->source); |
James Price | cbbb420 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 2693 | return false; |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2694 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2695 | if (!ok) { |
| 2696 | return false; |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2697 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2698 | SetExprInfo(expr, type, type_name); |
| 2699 | return true; |
| 2700 | } |
| 2701 | |
| 2702 | if (auto* scalar_ctor = expr->As<ast::ScalarConstructorExpression>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2703 | Mark(scalar_ctor->literal); |
| 2704 | auto* type = TypeOf(scalar_ctor->literal); |
Ben Clayton | 0e66b40 | 2021-06-03 08:17:44 +0000 | [diff] [blame] | 2705 | if (!type) { |
| 2706 | return false; |
| 2707 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2708 | SetExprInfo(expr, type); |
| 2709 | return true; |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2710 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2711 | |
| 2712 | TINT_ICE(Resolver, diagnostics_) << "unexpected constructor expression type"; |
| 2713 | return false; |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
Sarah | e285911 | 2021-07-09 16:32:00 +0000 | [diff] [blame] | 2716 | bool Resolver::ValidateStructureConstructor( |
| 2717 | const ast::TypeConstructorExpression* ctor, |
| 2718 | const sem::Struct* struct_type) { |
James Price | cbbb420 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 2719 | if (!struct_type->IsConstructible()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2720 | AddError("struct constructor has non-constructible type", ctor->source); |
James Price | cbbb420 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 2721 | return false; |
| 2722 | } |
| 2723 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2724 | if (ctor->values.size() > 0) { |
| 2725 | if (ctor->values.size() != struct_type->Members().size()) { |
| 2726 | std::string fm = |
| 2727 | ctor->values.size() < struct_type->Members().size() ? "few" : "many"; |
Sarah | e285911 | 2021-07-09 16:32:00 +0000 | [diff] [blame] | 2728 | AddError("struct constructor has too " + fm + " inputs: expected " + |
| 2729 | std::to_string(struct_type->Members().size()) + ", found " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2730 | std::to_string(ctor->values.size()), |
| 2731 | ctor->source); |
Sarah | e285911 | 2021-07-09 16:32:00 +0000 | [diff] [blame] | 2732 | return false; |
| 2733 | } |
| 2734 | for (auto* member : struct_type->Members()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2735 | auto* value = ctor->values[member->Index()]; |
Sarah | e285911 | 2021-07-09 16:32:00 +0000 | [diff] [blame] | 2736 | if (member->Type() != TypeOf(value)->UnwrapRef()) { |
| 2737 | AddError( |
| 2738 | "type in struct constructor does not match struct member type: " |
| 2739 | "expected '" + |
| 2740 | member->Type()->FriendlyName(builder_->Symbols()) + |
| 2741 | "', found '" + TypeNameOf(value) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2742 | value->source); |
Sarah | e285911 | 2021-07-09 16:32:00 +0000 | [diff] [blame] | 2743 | return false; |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | return true; |
| 2748 | } |
| 2749 | |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2750 | bool Resolver::ValidateArrayConstructor( |
| 2751 | const ast::TypeConstructorExpression* ctor, |
| 2752 | const sem::Array* array_type) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2753 | auto& values = ctor->values; |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2754 | auto* elem_type = array_type->ElemType(); |
| 2755 | for (auto* value : values) { |
| 2756 | auto* value_type = TypeOf(value)->UnwrapRef(); |
| 2757 | if (value_type != elem_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2758 | AddError( |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2759 | "type in array constructor does not match array type: " |
| 2760 | "expected '" + |
| 2761 | elem_type->FriendlyName(builder_->Symbols()) + "', found '" + |
| 2762 | TypeNameOf(value) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2763 | value->source); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2764 | return false; |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | if (array_type->IsRuntimeSized()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2769 | AddError("cannot init a runtime-sized array", ctor->source); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2770 | return false; |
James Price | cbbb420 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 2771 | } else if (!elem_type->IsConstructible()) { |
| 2772 | AddError("array constructor has non-constructible element type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2773 | ctor->type->As<ast::Array>()->type->source); |
James Price | cbbb420 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 2774 | return false; |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2775 | } else if (!values.empty() && (values.size() != array_type->Count())) { |
| 2776 | std::string fm = values.size() < array_type->Count() ? "few" : "many"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2777 | AddError("array constructor has too " + fm + " elements: expected " + |
| 2778 | std::to_string(array_type->Count()) + ", found " + |
| 2779 | std::to_string(values.size()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2780 | ctor->source); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2781 | return false; |
| 2782 | } else if (values.size() > array_type->Count()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2783 | AddError("array constructor has too many elements: expected " + |
| 2784 | std::to_string(array_type->Count()) + ", found " + |
| 2785 | std::to_string(values.size()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2786 | ctor->source); |
Sarah | 5a4dc9a | 2021-06-11 15:51:26 +0000 | [diff] [blame] | 2787 | return false; |
| 2788 | } |
| 2789 | return true; |
| 2790 | } |
| 2791 | |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2792 | bool Resolver::ValidateVectorConstructor( |
| 2793 | const ast::TypeConstructorExpression* ctor, |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2794 | const sem::Vector* vec_type, |
| 2795 | const std::string& type_name) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2796 | auto& values = ctor->values; |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2797 | auto* elem_type = vec_type->type(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2798 | size_t value_cardinality_sum = 0; |
| 2799 | for (auto* value : values) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2800 | auto* value_type = TypeOf(value)->UnwrapRef(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2801 | if (value_type->is_scalar()) { |
| 2802 | if (elem_type != value_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2803 | AddError( |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2804 | "type in vector constructor does not match vector type: " |
| 2805 | "expected '" + |
| 2806 | elem_type->FriendlyName(builder_->Symbols()) + "', found '" + |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2807 | TypeNameOf(value) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2808 | value->source); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2809 | return false; |
| 2810 | } |
| 2811 | |
| 2812 | value_cardinality_sum++; |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 2813 | } else if (auto* value_vec = value_type->As<sem::Vector>()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2814 | auto* value_elem_type = value_vec->type(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2815 | // A mismatch of vector type parameter T is only an error if multiple |
| 2816 | // arguments are present. A single argument constructor constitutes a |
| 2817 | // type conversion expression. |
James Price | 1fa28ac | 2021-07-19 17:35:39 +0000 | [diff] [blame] | 2818 | if (elem_type != value_elem_type && values.size() > 1u) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2819 | AddError( |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2820 | "type in vector constructor does not match vector type: " |
| 2821 | "expected '" + |
| 2822 | elem_type->FriendlyName(builder_->Symbols()) + "', found '" + |
| 2823 | value_elem_type->FriendlyName(builder_->Symbols()) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2824 | value->source); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2825 | return false; |
| 2826 | } |
| 2827 | |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 2828 | value_cardinality_sum += value_vec->Width(); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2829 | } else { |
| 2830 | // A vector constructor can only accept vectors and scalars. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2831 | AddError("expected vector or scalar type in vector constructor; found: " + |
| 2832 | value_type->FriendlyName(builder_->Symbols()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2833 | value->source); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2834 | return false; |
| 2835 | } |
| 2836 | } |
| 2837 | |
Antonio Maiorano | b5508fd | 2021-06-10 13:23:31 +0000 | [diff] [blame] | 2838 | // A correct vector constructor must either be a zero-value expression, |
| 2839 | // a single-value initializer (splat) expression, or the number of components |
| 2840 | // of all constructor arguments must add up to the vector cardinality. |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 2841 | if (value_cardinality_sum > 1 && value_cardinality_sum != vec_type->Width()) { |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2842 | if (values.empty()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2843 | TINT_ICE(Resolver, diagnostics_) |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2844 | << "constructor arguments expected to be non-empty!"; |
| 2845 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2846 | const Source& values_start = values[0]->source; |
| 2847 | const Source& values_end = values[values.size() - 1]->source; |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2848 | AddError("attempted to construct '" + type_name + "' with " + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2849 | std::to_string(value_cardinality_sum) + " component(s)", |
| 2850 | Source::Combine(values_start, values_end)); |
Arman Uguray | 3549e2e | 2021-03-15 21:21:33 +0000 | [diff] [blame] | 2851 | return false; |
dan sinclair | b7edc4c | 2020-04-07 12:46:30 +0000 | [diff] [blame] | 2852 | } |
| 2853 | return true; |
| 2854 | } |
| 2855 | |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 2856 | bool Resolver::ValidateVector(const sem::Vector* ty, const Source& source) { |
| 2857 | if (!ty->type()->is_scalar()) { |
| 2858 | AddError("vector element type must be 'bool', 'f32', 'i32' or 'u32'", |
| 2859 | source); |
| 2860 | return false; |
| 2861 | } |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
| 2865 | bool Resolver::ValidateMatrix(const sem::Matrix* ty, const Source& source) { |
| 2866 | if (!ty->is_float_matrix()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2867 | AddError("matrix element type must be 'f32'", source); |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 2868 | return false; |
| 2869 | } |
| 2870 | return true; |
Ben Clayton | ffe7978 | 2021-07-05 20:21:35 +0000 | [diff] [blame] | 2871 | } |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 2872 | |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 2873 | bool Resolver::ValidateMatrixConstructor( |
| 2874 | const ast::TypeConstructorExpression* ctor, |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2875 | const sem::Matrix* matrix_type, |
| 2876 | const std::string& type_name) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2877 | auto& values = ctor->values; |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2878 | // Zero Value expression |
| 2879 | if (values.empty()) { |
| 2880 | return true; |
| 2881 | } |
| 2882 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2883 | if (!ValidateMatrix(matrix_type, ctor->source)) { |
Sarah | 4d69751 | 2021-06-21 17:08:05 +0000 | [diff] [blame] | 2884 | return false; |
| 2885 | } |
| 2886 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2887 | auto* elem_type = matrix_type->type(); |
James Price | 91689fb | 2021-10-25 19:20:31 +0000 | [diff] [blame] | 2888 | auto num_elements = matrix_type->columns() * matrix_type->rows(); |
| 2889 | |
| 2890 | // Print a generic error for an invalid matrix constructor, showing the |
| 2891 | // available overloads. |
| 2892 | auto print_error = [&]() { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2893 | const Source& values_start = values[0]->source; |
| 2894 | const Source& values_end = values[values.size() - 1]->source; |
James Price | 91689fb | 2021-10-25 19:20:31 +0000 | [diff] [blame] | 2895 | auto elem_type_name = elem_type->FriendlyName(builder_->Symbols()); |
| 2896 | std::stringstream ss; |
| 2897 | ss << "invalid constructor for " + type_name << std::endl << std::endl; |
| 2898 | ss << "3 candidates available:" << std::endl; |
| 2899 | ss << " " << type_name << "()" << std::endl; |
| 2900 | ss << " " << type_name << "(" << elem_type_name << ",...," |
| 2901 | << elem_type_name << ")" |
| 2902 | << " // " << std::to_string(num_elements) << " arguments" << std::endl; |
| 2903 | ss << " " << type_name << "("; |
| 2904 | for (uint32_t c = 0; c < matrix_type->columns(); c++) { |
| 2905 | if (c > 0) { |
| 2906 | ss << ", "; |
| 2907 | } |
| 2908 | ss << VectorPretty(matrix_type->rows(), elem_type); |
| 2909 | } |
| 2910 | ss << ")" << std::endl; |
| 2911 | AddError(ss.str(), Source::Combine(values_start, values_end)); |
| 2912 | }; |
| 2913 | |
| 2914 | const sem::Type* expected_arg_type = nullptr; |
| 2915 | if (num_elements == values.size()) { |
| 2916 | // Column-major construction from scalar elements. |
| 2917 | expected_arg_type = matrix_type->type(); |
| 2918 | } else if (matrix_type->columns() == values.size()) { |
| 2919 | // Column-by-column construction from vectors. |
| 2920 | expected_arg_type = matrix_type->ColumnType(); |
| 2921 | } else { |
| 2922 | print_error(); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2923 | return false; |
| 2924 | } |
| 2925 | |
| 2926 | for (auto* value : values) { |
James Price | 91689fb | 2021-10-25 19:20:31 +0000 | [diff] [blame] | 2927 | if (TypeOf(value)->UnwrapRef() != expected_arg_type) { |
| 2928 | print_error(); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 2929 | return false; |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | return true; |
| 2934 | } |
| 2935 | |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2936 | bool Resolver::ValidateScalarConstructor( |
| 2937 | const ast::TypeConstructorExpression* ctor, |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2938 | const sem::Type* type, |
| 2939 | const std::string& type_name) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2940 | if (ctor->values.size() == 0) { |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2941 | return true; |
| 2942 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2943 | if (ctor->values.size() > 1) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 2944 | AddError("expected zero or one value in constructor, got " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2945 | std::to_string(ctor->values.size()), |
| 2946 | ctor->source); |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2947 | return false; |
| 2948 | } |
| 2949 | |
| 2950 | // Validate constructor |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2951 | auto* value = ctor->values[0]; |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2952 | auto* value_type = TypeOf(value)->UnwrapRef(); |
| 2953 | |
| 2954 | using Bool = sem::Bool; |
| 2955 | using I32 = sem::I32; |
| 2956 | using U32 = sem::U32; |
| 2957 | using F32 = sem::F32; |
| 2958 | |
James Price | 1fa28ac | 2021-07-19 17:35:39 +0000 | [diff] [blame] | 2959 | const bool is_valid = (type->Is<Bool>() && value_type->is_scalar()) || |
| 2960 | (type->Is<I32>() && value_type->is_scalar()) || |
| 2961 | (type->Is<U32>() && value_type->is_scalar()) || |
| 2962 | (type->Is<F32>() && value_type->is_scalar()); |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2963 | if (!is_valid) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2964 | AddError("cannot construct '" + type_name + "' with a value of type '" + |
| 2965 | TypeNameOf(value) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2966 | ctor->source); |
Antonio Maiorano | adbbd0b | 2021-06-18 15:32:21 +0000 | [diff] [blame] | 2967 | |
| 2968 | return false; |
| 2969 | } |
| 2970 | |
| 2971 | return true; |
| 2972 | } |
| 2973 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 2974 | bool Resolver::Identifier(const ast::IdentifierExpression* expr) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2975 | auto symbol = expr->symbol; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 2976 | VariableInfo* var; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 2977 | if (variable_stack_.get(symbol, &var)) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 2978 | SetExprInfo(expr, var->type, var->type_name); |
dan sinclair | 13d2a3b | 2020-06-22 20:52:24 +0000 | [diff] [blame] | 2979 | |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 2980 | var->users.push_back(expr); |
Enrico Galli | 3d449d2 | 2020-12-08 21:07:24 +0000 | [diff] [blame] | 2981 | set_referenced_from_function_if_needed(var, true); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 2982 | |
James Price | 7166f6b | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 2983 | if (current_statement_) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 2984 | // If identifier is part of a loop continuing block, make sure it |
| 2985 | // doesn't refer to a variable that is bypassed by a continue statement |
| 2986 | // in the loop's body block. |
Ben Clayton | 9a3ba02 | 2021-05-19 21:22:07 +0000 | [diff] [blame] | 2987 | if (auto* continuing_block = |
James Price | 7166f6b | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 2988 | current_statement_ |
Ben Clayton | 9a3ba02 | 2021-05-19 21:22:07 +0000 | [diff] [blame] | 2989 | ->FindFirstParent<sem::LoopContinuingBlockStatement>()) { |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2990 | auto* loop_block = |
Ben Clayton | 9a3ba02 | 2021-05-19 21:22:07 +0000 | [diff] [blame] | 2991 | continuing_block->FindFirstParent<sem::LoopBlockStatement>(); |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 2992 | if (loop_block->FirstContinue()) { |
Alastair F. Donaldson | ac90829 | 2021-05-15 14:48:46 +0000 | [diff] [blame] | 2993 | auto& decls = loop_block->Decls(); |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2994 | // If our identifier is in loop_block->decls, make sure its index is |
| 2995 | // less than first_continue |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 2996 | auto iter = |
| 2997 | std::find_if(decls.begin(), decls.end(), |
| 2998 | [&symbol](auto* v) { return v->symbol == symbol; }); |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 2999 | if (iter != decls.end()) { |
| 3000 | auto var_decl_index = |
| 3001 | static_cast<size_t>(std::distance(decls.begin(), iter)); |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 3002 | if (var_decl_index >= loop_block->NumDeclsAtFirstContinue()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3003 | AddError("continue statement bypasses declaration of '" + |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 3004 | builder_->Symbols().NameFor(symbol) + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3005 | loop_block->FirstContinue()->source); |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 3006 | AddNote("identifier '" + builder_->Symbols().NameFor(symbol) + |
| 3007 | "' declared here", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3008 | (*iter)->source); |
James Price | 5ffae32 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 3009 | AddNote("identifier '" + builder_->Symbols().NameFor(symbol) + |
| 3010 | "' referenced in continuing block here", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3011 | expr->source); |
James Price | a07d21a | 2021-03-22 17:25:56 +0000 | [diff] [blame] | 3012 | return false; |
| 3013 | } |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 3019 | return true; |
| 3020 | } |
| 3021 | |
Ben Clayton | f6866a2 | 2021-01-11 22:02:42 +0000 | [diff] [blame] | 3022 | auto iter = symbol_to_function_.find(symbol); |
dan sinclair | 4ac6568 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 3023 | if (iter != symbol_to_function_.end()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3024 | AddError("missing '(' for function call", expr->source.End()); |
Ben Clayton | 33a8cdd | 2021-02-24 13:31:22 +0000 | [diff] [blame] | 3025 | return false; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 3028 | std::string name = builder_->Symbols().NameFor(symbol); |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3029 | if (sem::ParseIntrinsicType(name) != IntrinsicType::kNone) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3030 | AddError("missing '(' for intrinsic call", expr->source.End()); |
Ben Clayton | 33a8cdd | 2021-02-24 13:31:22 +0000 | [diff] [blame] | 3031 | return false; |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 3032 | } |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 3033 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3034 | AddError("identifier must be declared before use: " + name, expr->source); |
Ben Clayton | 1618f4b | 2021-02-03 21:02:25 +0000 | [diff] [blame] | 3035 | return false; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3038 | bool Resolver::MemberAccessor(const ast::MemberAccessorExpression* expr) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3039 | auto* structure = TypeOf(expr->structure); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3040 | auto* storage_type = structure->UnwrapRef(); |
dan sinclair | b445a9b | 2020-04-24 00:40:45 +0000 | [diff] [blame] | 3041 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3042 | const sem::Type* ret = nullptr; |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3043 | std::vector<uint32_t> swizzle; |
Ben Clayton | c1052a4 | 2021-02-03 23:55:56 +0000 | [diff] [blame] | 3044 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3045 | if (auto* str = storage_type->As<sem::Struct>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3046 | Mark(expr->member); |
| 3047 | auto symbol = expr->member->symbol; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3048 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3049 | const sem::StructMember* member = nullptr; |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3050 | for (auto* m : str->Members()) { |
Ben Clayton | 053559d | 2021-07-23 16:43:01 +0000 | [diff] [blame] | 3051 | if (m->Name() == symbol) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 3052 | ret = m->Type(); |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3053 | member = m; |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3054 | break; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3055 | } |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3058 | if (ret == nullptr) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3059 | AddError( |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 3060 | "struct member " + builder_->Symbols().NameFor(symbol) + " not found", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3061 | expr->source); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3062 | return false; |
| 3063 | } |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 3064 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3065 | // If we're extracting from a reference, we return a reference. |
| 3066 | if (auto* ref = structure->As<sem::Reference>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3067 | ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), |
| 3068 | ref->Access()); |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 3069 | } |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3070 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3071 | builder_->Sem().Add(expr, builder_->create<sem::StructMemberAccess>( |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3072 | expr, ret, current_statement_, member)); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3073 | } else if (auto* vec = storage_type->As<sem::Vector>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3074 | Mark(expr->member); |
| 3075 | std::string s = builder_->Symbols().NameFor(expr->member->symbol); |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3076 | auto size = s.size(); |
| 3077 | swizzle.reserve(s.size()); |
dan sinclair | eb737c2 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 3078 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3079 | for (auto c : s) { |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3080 | switch (c) { |
| 3081 | case 'x': |
| 3082 | case 'r': |
| 3083 | swizzle.emplace_back(0); |
| 3084 | break; |
| 3085 | case 'y': |
| 3086 | case 'g': |
| 3087 | swizzle.emplace_back(1); |
| 3088 | break; |
| 3089 | case 'z': |
| 3090 | case 'b': |
| 3091 | swizzle.emplace_back(2); |
| 3092 | break; |
| 3093 | case 'w': |
| 3094 | case 'a': |
| 3095 | swizzle.emplace_back(3); |
| 3096 | break; |
| 3097 | default: |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3098 | AddError("invalid vector swizzle character", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3099 | expr->member->source.Begin() + swizzle.size()); |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3100 | return false; |
| 3101 | } |
Antonio Maiorano | 39c05a9 | 2021-06-01 00:37:40 +0000 | [diff] [blame] | 3102 | |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3103 | if (swizzle.back() >= vec->Width()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3104 | AddError("invalid vector swizzle member", expr->member->source); |
Antonio Maiorano | 39c05a9 | 2021-06-01 00:37:40 +0000 | [diff] [blame] | 3105 | return false; |
| 3106 | } |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | if (size < 1 || size > 4) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3110 | AddError("invalid vector swizzle size", expr->member->source); |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3111 | return false; |
| 3112 | } |
| 3113 | |
Antonio Maiorano | fe2b417 | 2021-03-01 20:01:39 +0000 | [diff] [blame] | 3114 | // All characters are valid, check if they're being mixed |
| 3115 | auto is_rgba = [](char c) { |
| 3116 | return c == 'r' || c == 'g' || c == 'b' || c == 'a'; |
| 3117 | }; |
| 3118 | auto is_xyzw = [](char c) { |
| 3119 | return c == 'x' || c == 'y' || c == 'z' || c == 'w'; |
| 3120 | }; |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3121 | if (!std::all_of(s.begin(), s.end(), is_rgba) && |
| 3122 | !std::all_of(s.begin(), s.end(), is_xyzw)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3123 | AddError("invalid mixing of vector swizzle characters rgba with xyzw", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3124 | expr->member->source); |
Antonio Maiorano | fe2b417 | 2021-03-01 20:01:39 +0000 | [diff] [blame] | 3125 | return false; |
| 3126 | } |
| 3127 | |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 3128 | if (size == 1) { |
| 3129 | // A single element swizzle is just the type of the vector. |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3130 | ret = vec->type(); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3131 | // If we're extracting from a reference, we return a reference. |
| 3132 | if (auto* ref = structure->As<sem::Reference>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3133 | ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), |
| 3134 | ref->Access()); |
dan sinclair | 7cac245 | 2020-05-01 16:17:03 +0000 | [diff] [blame] | 3135 | } |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 3136 | } else { |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 3137 | // 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] | 3138 | // the swizzle. |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 3139 | ret = builder_->create<sem::Vector>(vec->type(), |
| 3140 | static_cast<uint32_t>(size)); |
dan sinclair | aac5865 | 2020-04-21 13:05:34 +0000 | [diff] [blame] | 3141 | } |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 3142 | builder_->Sem().Add( |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3143 | expr, builder_->create<sem::Swizzle>(expr, ret, current_statement_, |
| 3144 | std::move(swizzle))); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3145 | } else { |
Sarah | de767b1 | 2021-09-13 19:30:19 +0000 | [diff] [blame] | 3146 | AddError( |
| 3147 | "invalid member accessor expression. Expected vector or struct, got '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3148 | TypeNameOf(expr->structure) + "'", |
| 3149 | expr->structure->source); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3150 | return false; |
dan sinclair | 8ee1d22 | 2020-04-07 16:41:33 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3153 | SetExprInfo(expr, ret); |
dan sinclair | 8eddb78 | 2020-04-23 22:26:52 +0000 | [diff] [blame] | 3154 | |
| 3155 | return true; |
dan sinclair | cab0e73 | 2020-04-07 12:57:27 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3158 | bool Resolver::Binary(const ast::BinaryExpression* expr) { |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 3159 | using Bool = sem::Bool; |
| 3160 | using F32 = sem::F32; |
| 3161 | using I32 = sem::I32; |
| 3162 | using U32 = sem::U32; |
| 3163 | using Matrix = sem::Matrix; |
| 3164 | using Vector = sem::Vector; |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3165 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3166 | auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef(); |
| 3167 | auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef(); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3168 | |
| 3169 | auto* lhs_vec = lhs_type->As<Vector>(); |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 3170 | auto* lhs_vec_elem_type = lhs_vec ? lhs_vec->type() : nullptr; |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3171 | auto* rhs_vec = rhs_type->As<Vector>(); |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 3172 | auto* rhs_vec_elem_type = rhs_vec ? rhs_vec->type() : nullptr; |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3173 | |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3174 | const bool matching_vec_elem_types = |
| 3175 | lhs_vec_elem_type && rhs_vec_elem_type && |
| 3176 | (lhs_vec_elem_type == rhs_vec_elem_type) && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3177 | (lhs_vec->Width() == rhs_vec->Width()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3178 | |
Antonio Maiorano | 15c6ed0 | 2021-04-01 14:59:27 +0000 | [diff] [blame] | 3179 | const bool matching_types = matching_vec_elem_types || (lhs_type == rhs_type); |
| 3180 | |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3181 | // Binary logical expressions |
| 3182 | if (expr->IsLogicalAnd() || expr->IsLogicalOr()) { |
| 3183 | if (matching_types && lhs_type->Is<Bool>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3184 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3185 | return true; |
| 3186 | } |
| 3187 | } |
| 3188 | if (expr->IsOr() || expr->IsAnd()) { |
| 3189 | if (matching_types && lhs_type->Is<Bool>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3190 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3191 | return true; |
| 3192 | } |
| 3193 | 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] | 3194 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3195 | return true; |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | // Arithmetic expressions |
| 3200 | if (expr->IsArithmetic()) { |
| 3201 | // Binary arithmetic expressions over scalars |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3202 | if (matching_types && lhs_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3203 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3204 | return true; |
| 3205 | } |
| 3206 | |
| 3207 | // Binary arithmetic expressions over vectors |
| 3208 | if (matching_types && lhs_vec_elem_type && |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3209 | lhs_vec_elem_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3210 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3211 | return true; |
| 3212 | } |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3213 | |
| 3214 | // Binary arithmetic expressions with mixed scalar and vector operands |
| 3215 | if (lhs_vec_elem_type && (lhs_vec_elem_type == rhs_type)) { |
| 3216 | if (expr->IsModulo()) { |
| 3217 | if (rhs_type->is_integer_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3218 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3219 | return true; |
| 3220 | } |
| 3221 | } else if (rhs_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3222 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3223 | return true; |
| 3224 | } |
| 3225 | } |
| 3226 | if (rhs_vec_elem_type && (rhs_vec_elem_type == lhs_type)) { |
| 3227 | if (expr->IsModulo()) { |
| 3228 | if (lhs_type->is_integer_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3229 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3230 | return true; |
| 3231 | } |
| 3232 | } else if (lhs_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3233 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3234 | return true; |
| 3235 | } |
| 3236 | } |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
Antonio Maiorano | eaed2b6 | 2021-05-27 21:07:56 +0000 | [diff] [blame] | 3239 | // Matrix arithmetic |
Antonio Maiorano | c91f8f2 | 2021-05-31 15:53:20 +0000 | [diff] [blame] | 3240 | auto* lhs_mat = lhs_type->As<Matrix>(); |
| 3241 | auto* lhs_mat_elem_type = lhs_mat ? lhs_mat->type() : nullptr; |
| 3242 | auto* rhs_mat = rhs_type->As<Matrix>(); |
| 3243 | auto* rhs_mat_elem_type = rhs_mat ? rhs_mat->type() : nullptr; |
| 3244 | // Addition and subtraction of float matrices |
| 3245 | if ((expr->IsAdd() || expr->IsSubtract()) && lhs_mat_elem_type && |
| 3246 | lhs_mat_elem_type->Is<F32>() && rhs_mat_elem_type && |
| 3247 | rhs_mat_elem_type->Is<F32>() && |
| 3248 | (lhs_mat->columns() == rhs_mat->columns()) && |
| 3249 | (lhs_mat->rows() == rhs_mat->rows())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3250 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | c91f8f2 | 2021-05-31 15:53:20 +0000 | [diff] [blame] | 3251 | return true; |
| 3252 | } |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3253 | if (expr->IsMultiply()) { |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3254 | // Multiplication of a matrix and a scalar |
| 3255 | if (lhs_type->Is<F32>() && rhs_mat_elem_type && |
| 3256 | rhs_mat_elem_type->Is<F32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3257 | SetExprInfo(expr, rhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3258 | return true; |
| 3259 | } |
| 3260 | if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() && |
| 3261 | rhs_type->Is<F32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3262 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3263 | return true; |
| 3264 | } |
| 3265 | |
| 3266 | // Vector times matrix |
| 3267 | if (lhs_vec_elem_type && lhs_vec_elem_type->Is<F32>() && |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3268 | rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>() && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3269 | (lhs_vec->Width() == rhs_mat->rows())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3270 | SetExprInfo(expr, builder_->create<sem::Vector>(lhs_vec->type(), |
| 3271 | rhs_mat->columns())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3272 | return true; |
| 3273 | } |
| 3274 | |
| 3275 | // Matrix times vector |
| 3276 | if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() && |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3277 | rhs_vec_elem_type && rhs_vec_elem_type->Is<F32>() && |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3278 | (lhs_mat->columns() == rhs_vec->Width())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3279 | SetExprInfo(expr, builder_->create<sem::Vector>(rhs_vec->type(), |
| 3280 | lhs_mat->rows())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3281 | return true; |
| 3282 | } |
| 3283 | |
| 3284 | // Matrix times matrix |
| 3285 | if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() && |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 3286 | rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>() && |
| 3287 | (lhs_mat->columns() == rhs_mat->rows())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3288 | SetExprInfo(expr, builder_->create<sem::Matrix>( |
| 3289 | builder_->create<sem::Vector>(lhs_mat_elem_type, |
| 3290 | lhs_mat->rows()), |
| 3291 | rhs_mat->columns())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3292 | return true; |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | // Comparison expressions |
| 3297 | if (expr->IsComparison()) { |
| 3298 | if (matching_types) { |
| 3299 | // Special case for bools: only == and != |
| 3300 | if (lhs_type->Is<Bool>() && (expr->IsEqual() || expr->IsNotEqual())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3301 | SetExprInfo(expr, builder_->create<sem::Bool>()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3302 | return true; |
| 3303 | } |
| 3304 | |
| 3305 | // For the rest, we can compare i32, u32, and f32 |
| 3306 | if (lhs_type->IsAnyOf<I32, U32, F32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3307 | SetExprInfo(expr, builder_->create<sem::Bool>()); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3308 | return true; |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | // Same for vectors |
| 3313 | if (matching_vec_elem_types) { |
| 3314 | if (lhs_vec_elem_type->Is<Bool>() && |
| 3315 | (expr->IsEqual() || expr->IsNotEqual())) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3316 | SetExprInfo(expr, builder_->create<sem::Vector>( |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3317 | builder_->create<sem::Bool>(), lhs_vec->Width())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3318 | return true; |
| 3319 | } |
| 3320 | |
Antonio Maiorano | 0131ce2 | 2021-05-27 18:25:06 +0000 | [diff] [blame] | 3321 | if (lhs_vec_elem_type->is_numeric_scalar()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3322 | SetExprInfo(expr, builder_->create<sem::Vector>( |
Ben Clayton | f5ed2ba | 2021-07-22 18:31:34 +0000 | [diff] [blame] | 3323 | builder_->create<sem::Bool>(), lhs_vec->Width())); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3324 | return true; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | // Binary bitwise operations |
| 3330 | if (expr->IsBitwise()) { |
Antonio Maiorano | 0895c23 | 2021-06-01 17:42:41 +0000 | [diff] [blame] | 3331 | if (matching_types && lhs_type->is_integer_scalar_or_vector()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3332 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3333 | return true; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | // Bit shift expressions |
| 3338 | if (expr->IsBitshift()) { |
| 3339 | // Type validation rules are the same for left or right shift, despite |
| 3340 | // differences in computation rules (i.e. right shift can be arithmetic or |
| 3341 | // logical depending on lhs type). |
| 3342 | |
| 3343 | if (lhs_type->IsAnyOf<I32, U32>() && rhs_type->Is<U32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3344 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3345 | return true; |
| 3346 | } |
| 3347 | |
| 3348 | if (lhs_vec_elem_type && lhs_vec_elem_type->IsAnyOf<I32, U32>() && |
| 3349 | rhs_vec_elem_type && rhs_vec_elem_type->Is<U32>()) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3350 | SetExprInfo(expr, lhs_type); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3351 | return true; |
| 3352 | } |
| 3353 | } |
| 3354 | |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3355 | AddError("Binary expression operand types are invalid for this operation: " + |
| 3356 | lhs_type->FriendlyName(builder_->Symbols()) + " " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3357 | FriendlyName(expr->op) + " " + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3358 | rhs_type->FriendlyName(builder_->Symbols()), |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3359 | expr->source); |
Antonio Maiorano | be0fc4e | 2021-03-16 13:26:03 +0000 | [diff] [blame] | 3360 | return false; |
| 3361 | } |
| 3362 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3363 | bool Resolver::UnaryOp(const ast::UnaryOpExpression* unary) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3364 | auto* expr_type = TypeOf(unary->expr); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3365 | if (!expr_type) { |
| 3366 | return false; |
| 3367 | } |
| 3368 | |
| 3369 | std::string type_name; |
| 3370 | const sem::Type* type = nullptr; |
| 3371 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3372 | switch (unary->op) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3373 | case ast::UnaryOp::kNot: |
| 3374 | // Result type matches the deref'd inner type. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3375 | type_name = TypeNameOf(unary->expr); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3376 | type = expr_type->UnwrapRef(); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3377 | if (!type->Is<sem::Bool>() && !type->is_bool_vector()) { |
| 3378 | AddError("cannot logical negate expression of type '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3379 | TypeNameOf(unary->expr), |
| 3380 | unary->expr->source); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3381 | return false; |
| 3382 | } |
| 3383 | break; |
| 3384 | |
| 3385 | case ast::UnaryOp::kComplement: |
| 3386 | // Result type matches the deref'd inner type. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3387 | type_name = TypeNameOf(unary->expr); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3388 | type = expr_type->UnwrapRef(); |
| 3389 | if (!type->is_integer_scalar_or_vector()) { |
| 3390 | AddError("cannot bitwise complement expression of type '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3391 | TypeNameOf(unary->expr), |
| 3392 | unary->expr->source); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3393 | return false; |
| 3394 | } |
| 3395 | break; |
| 3396 | |
| 3397 | case ast::UnaryOp::kNegation: |
| 3398 | // Result type matches the deref'd inner type. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3399 | type_name = TypeNameOf(unary->expr); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3400 | type = expr_type->UnwrapRef(); |
| 3401 | if (!(type->IsAnyOf<sem::F32, sem::I32>() || |
| 3402 | type->is_signed_integer_vector() || type->is_float_vector())) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3403 | AddError("cannot negate expression of type '" + TypeNameOf(unary->expr), |
| 3404 | unary->expr->source); |
Sarah | a5715e3 | 2021-06-28 14:09:33 +0000 | [diff] [blame] | 3405 | return false; |
| 3406 | } |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | |
| 3409 | case ast::UnaryOp::kAddressOf: |
| 3410 | if (auto* ref = expr_type->As<sem::Reference>()) { |
Sarah | edecbb1 | 2021-07-28 03:57:22 +0000 | [diff] [blame] | 3411 | if (ref->StoreType()->UnwrapRef()->is_handle()) { |
| 3412 | AddError( |
| 3413 | "cannot take the address of expression in handle storage class", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3414 | unary->expr->source); |
Sarah | edecbb1 | 2021-07-28 03:57:22 +0000 | [diff] [blame] | 3415 | return false; |
| 3416 | } |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3417 | type = builder_->create<sem::Pointer>( |
| 3418 | ref->StoreType(), ref->StorageClass(), ref->Access()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3419 | } else { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3420 | AddError("cannot take the address of expression", unary->expr->source); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3421 | return false; |
| 3422 | } |
| 3423 | break; |
| 3424 | |
| 3425 | case ast::UnaryOp::kIndirection: |
| 3426 | if (auto* ptr = expr_type->As<sem::Pointer>()) { |
Ben Clayton | 1858854 | 2021-06-04 22:17:37 +0000 | [diff] [blame] | 3427 | type = builder_->create<sem::Reference>( |
| 3428 | ptr->StoreType(), ptr->StorageClass(), ptr->Access()); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3429 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3430 | AddError("cannot dereference expression of type '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3431 | TypeNameOf(unary->expr) + "'", |
| 3432 | unary->expr->source); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3433 | return false; |
| 3434 | } |
| 3435 | break; |
| 3436 | } |
| 3437 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3438 | SetExprInfo(unary, type); |
dan sinclair | 327ed1b | 2020-04-07 19:27:21 +0000 | [diff] [blame] | 3439 | return true; |
dan sinclair | 0e25762 | 2020-04-07 19:27:11 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 3442 | bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) { |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3443 | const ast::Variable* var = stmt->variable; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3444 | Mark(var); |
| 3445 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3446 | if (!ValidateNoDuplicateDefinition(var->symbol, var->source)) { |
Antonio Maiorano | 09356cc | 2021-04-06 14:07:07 +0000 | [diff] [blame] | 3447 | return false; |
| 3448 | } |
| 3449 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3450 | auto* info = Variable(var, VariableKind::kLocal); |
Ben Clayton | 5df7661 | 2021-05-12 10:41:21 +0000 | [diff] [blame] | 3451 | if (!info) { |
| 3452 | return false; |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3455 | for (auto* deco : var->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3456 | Mark(deco); |
James Price | d47a7ef | 2021-08-17 16:09:00 +0000 | [diff] [blame] | 3457 | if (!deco->Is<ast::InternalDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3458 | AddError("decorations are not valid on local variables", deco->source); |
James Price | d47a7ef | 2021-08-17 16:09:00 +0000 | [diff] [blame] | 3459 | return false; |
| 3460 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3463 | variable_stack_.set(var->symbol, info); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 3464 | if (current_block_) { // Not all statements are inside a block |
| 3465 | current_block_->AddDecl(var); |
| 3466 | } |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3467 | |
Ben Clayton | fcda15e | 2021-05-10 18:01:51 +0000 | [diff] [blame] | 3468 | if (!ValidateVariable(info)) { |
Antonio Maiorano | 09356cc | 2021-04-06 14:07:07 +0000 | [diff] [blame] | 3469 | return false; |
| 3470 | } |
| 3471 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3472 | if (!var->is_const && |
| 3473 | IsValidationEnabled(var->decorations, |
Sarah | 7249404 | 2021-07-28 22:43:36 +0000 | [diff] [blame] | 3474 | ast::DisabledValidation::kIgnoreStorageClass)) { |
| 3475 | if (!info->type->UnwrapRef()->IsConstructible()) { |
| 3476 | AddError("function variable must have a constructible type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3477 | var->type ? var->type->source : var->source); |
Sarah | 7249404 | 2021-07-28 22:43:36 +0000 | [diff] [blame] | 3478 | return false; |
| 3479 | } |
| 3480 | if (info->storage_class != ast::StorageClass::kFunction) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3481 | if (info->storage_class != ast::StorageClass::kNone) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3482 | AddError("function variable has a non-function storage class", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3483 | stmt->source); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3484 | return false; |
| 3485 | } |
| 3486 | info->storage_class = ast::StorageClass::kFunction; |
| 3487 | } |
| 3488 | } |
| 3489 | |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 3490 | if (!ApplyStorageClassUsageToType(info->storage_class, info->type, |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3491 | var->source)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3492 | AddNote("while instantiating variable " + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3493 | builder_->Symbols().NameFor(var->symbol), |
| 3494 | var->source); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 3495 | return false; |
Antonio Maiorano | 6ce2edf | 2021-02-26 18:25:56 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | return true; |
| 3499 | } |
| 3500 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3501 | sem::Type* Resolver::TypeDecl(const ast::TypeDecl* named_type) { |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3502 | sem::Type* result = nullptr; |
| 3503 | if (auto* alias = named_type->As<ast::Alias>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3504 | result = Type(alias->type); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3505 | } else if (auto* str = named_type->As<ast::Struct>()) { |
| 3506 | result = Structure(str); |
| 3507 | } else { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3508 | TINT_UNREACHABLE(Resolver, diagnostics_) << "Unhandled TypeDecl"; |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | if (!result) { |
| 3512 | return nullptr; |
| 3513 | } |
| 3514 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3515 | named_type_info_.emplace(named_type->name, TypeDeclInfo{named_type, result}); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3516 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3517 | if (!ValidateTypeDecl(named_type)) { |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3518 | return nullptr; |
| 3519 | } |
| 3520 | |
| 3521 | builder_->Sem().Add(named_type, result); |
| 3522 | return result; |
| 3523 | } |
| 3524 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 3525 | bool Resolver::ValidateTypeDecl(const ast::TypeDecl* named_type) const { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3526 | auto iter = named_type_info_.find(named_type->name); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3527 | if (iter == named_type_info_.end()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3528 | TINT_ICE(Resolver, diagnostics_) |
| 3529 | << "ValidateTypeDecl called() before TypeDecl()"; |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3530 | } |
| 3531 | if (iter->second.ast != named_type) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3532 | AddError("type with the name '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3533 | builder_->Symbols().NameFor(named_type->name) + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3534 | "' was already declared", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3535 | named_type->source); |
| 3536 | AddNote("first declared here", iter->second.ast->source); |
Antonio Maiorano | c5f5d36 | 2021-05-20 08:44:57 +0000 | [diff] [blame] | 3537 | return false; |
| 3538 | } |
| 3539 | return true; |
| 3540 | } |
| 3541 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3542 | sem::Type* Resolver::TypeOf(const ast::Expression* expr) { |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3543 | auto it = expr_info_.find(expr); |
| 3544 | if (it != expr_info_.end()) { |
Ben Clayton | 19d3205 | 2021-05-20 15:10:48 +0000 | [diff] [blame] | 3545 | return const_cast<sem::Type*>(it->second.type); |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3546 | } |
| 3547 | return nullptr; |
| 3548 | } |
| 3549 | |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 3550 | std::string Resolver::TypeNameOf(const ast::Expression* expr) { |
| 3551 | auto it = expr_info_.find(expr); |
| 3552 | if (it != expr_info_.end()) { |
| 3553 | return it->second.type_name; |
| 3554 | } |
| 3555 | return ""; |
| 3556 | } |
| 3557 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3558 | sem::Type* Resolver::TypeOf(const ast::Literal* lit) { |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 3559 | if (lit->Is<ast::SintLiteral>()) { |
| 3560 | return builder_->create<sem::I32>(); |
| 3561 | } |
| 3562 | if (lit->Is<ast::UintLiteral>()) { |
| 3563 | return builder_->create<sem::U32>(); |
| 3564 | } |
| 3565 | if (lit->Is<ast::FloatLiteral>()) { |
| 3566 | return builder_->create<sem::F32>(); |
| 3567 | } |
| 3568 | if (lit->Is<ast::BoolLiteral>()) { |
| 3569 | return builder_->create<sem::Bool>(); |
| 3570 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3571 | TINT_UNREACHABLE(Resolver, diagnostics_) |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 3572 | << "Unhandled literal type: " << lit->TypeInfo().name; |
| 3573 | return nullptr; |
| 3574 | } |
| 3575 | |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3576 | void Resolver::SetExprInfo(const ast::Expression* expr, |
| 3577 | const sem::Type* type, |
| 3578 | std::string type_name) { |
Ben Clayton | 90f43cf | 2021-03-31 20:43:26 +0000 | [diff] [blame] | 3579 | if (expr_info_.count(expr)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3580 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3581 | << "SetExprInfo() called twice for the same expression"; |
Ben Clayton | 90f43cf | 2021-03-31 20:43:26 +0000 | [diff] [blame] | 3582 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3583 | if (type_name.empty()) { |
| 3584 | type_name = type->FriendlyName(builder_->Symbols()); |
| 3585 | } |
| 3586 | auto constant_value = EvaluateConstantValue(expr, type); |
| 3587 | expr_info_.emplace( |
| 3588 | expr, ExpressionInfo{type, std::move(type_name), current_statement_, |
| 3589 | std::move(constant_value)}); |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3592 | bool Resolver::ValidatePipelineStages() { |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3593 | auto check_workgroup_storage = [&](FunctionInfo* func, |
| 3594 | FunctionInfo* entry_point) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3595 | auto stage = entry_point->declaration->PipelineStage(); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3596 | if (stage != ast::PipelineStage::kCompute) { |
| 3597 | for (auto* var : func->local_referenced_module_vars) { |
| 3598 | if (var->storage_class == ast::StorageClass::kWorkgroup) { |
| 3599 | std::stringstream stage_name; |
| 3600 | stage_name << stage; |
| 3601 | for (auto* user : var->users) { |
| 3602 | auto it = expr_info_.find(user->As<ast::Expression>()); |
| 3603 | if (it != expr_info_.end()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3604 | if (func->declaration->symbol == |
| 3605 | it->second.statement->Function()->symbol) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3606 | AddError("workgroup memory cannot be used by " + |
| 3607 | stage_name.str() + " pipeline stage", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3608 | user->source); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3609 | break; |
| 3610 | } |
| 3611 | } |
| 3612 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3613 | AddNote("variable is declared here", var->declaration->source); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3614 | if (func != entry_point) { |
| 3615 | TraverseCallChain(entry_point, func, [&](FunctionInfo* f) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3616 | AddNote("called by function '" + |
| 3617 | builder_->Symbols().NameFor(f->declaration->symbol) + |
| 3618 | "'", |
| 3619 | f->declaration->source); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3620 | }); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3621 | AddNote("called by entry point '" + |
| 3622 | builder_->Symbols().NameFor( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3623 | entry_point->declaration->symbol) + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3624 | "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3625 | entry_point->declaration->source); |
Sarah | 9548531 | 2021-06-24 08:56:16 +0000 | [diff] [blame] | 3626 | } |
| 3627 | return false; |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | return true; |
| 3632 | }; |
| 3633 | |
| 3634 | for (auto* entry_point : entry_points_) { |
| 3635 | if (!check_workgroup_storage(entry_point, entry_point)) { |
| 3636 | return false; |
| 3637 | } |
| 3638 | for (auto* func : entry_point->transitive_calls) { |
| 3639 | if (!check_workgroup_storage(func, entry_point)) { |
| 3640 | return false; |
| 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3645 | auto check_intrinsic_calls = [&](FunctionInfo* func, |
| 3646 | FunctionInfo* entry_point) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3647 | auto stage = entry_point->declaration->PipelineStage(); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3648 | for (auto& call : func->intrinsic_calls) { |
| 3649 | if (!call.intrinsic->SupportedStages().Contains(stage)) { |
| 3650 | std::stringstream err; |
| 3651 | err << "built-in cannot be used by " << stage << " pipeline stage"; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3652 | AddError(err.str(), call.call->source); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3653 | if (func != entry_point) { |
| 3654 | TraverseCallChain(entry_point, func, [&](FunctionInfo* f) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3655 | AddNote("called by function '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3656 | builder_->Symbols().NameFor(f->declaration->symbol) + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3657 | "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3658 | f->declaration->source); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3659 | }); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3660 | AddNote("called by entry point '" + |
| 3661 | builder_->Symbols().NameFor( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3662 | entry_point->declaration->symbol) + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3663 | "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3664 | entry_point->declaration->source); |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3665 | } |
| 3666 | return false; |
| 3667 | } |
| 3668 | } |
| 3669 | return true; |
| 3670 | }; |
| 3671 | |
| 3672 | for (auto* entry_point : entry_points_) { |
| 3673 | if (!check_intrinsic_calls(entry_point, entry_point)) { |
| 3674 | return false; |
| 3675 | } |
| 3676 | for (auto* func : entry_point->transitive_calls) { |
| 3677 | if (!check_intrinsic_calls(func, entry_point)) { |
| 3678 | return false; |
| 3679 | } |
| 3680 | } |
| 3681 | } |
| 3682 | return true; |
| 3683 | } |
| 3684 | |
| 3685 | template <typename CALLBACK> |
| 3686 | void Resolver::TraverseCallChain(FunctionInfo* from, |
| 3687 | FunctionInfo* to, |
| 3688 | CALLBACK&& callback) const { |
| 3689 | for (auto* f : from->transitive_calls) { |
| 3690 | if (f == to) { |
| 3691 | callback(f); |
| 3692 | return; |
| 3693 | } |
| 3694 | if (f->transitive_calls.contains(to)) { |
| 3695 | TraverseCallChain(f, to, callback); |
| 3696 | callback(f); |
| 3697 | return; |
| 3698 | } |
| 3699 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3700 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3701 | << "TraverseCallChain() 'from' does not transitively call 'to'"; |
| 3702 | } |
| 3703 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 3704 | void Resolver::CreateSemanticNodes() const { |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3705 | auto& sem = builder_->Sem(); |
| 3706 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 3707 | // Collate all the 'ancestor_entry_points' - this is a map of function |
| 3708 | // symbol to all the entry points that transitively call the function. |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3709 | std::unordered_map<Symbol, std::vector<Symbol>> ancestor_entry_points; |
Ben Clayton | 71786c9 | 2021-06-03 16:07:34 +0000 | [diff] [blame] | 3710 | for (auto* entry_point : entry_points_) { |
| 3711 | for (auto* call : entry_point->transitive_calls) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3712 | auto& vec = ancestor_entry_points[call->declaration->symbol]; |
| 3713 | vec.emplace_back(entry_point->declaration->symbol); |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3717 | // Create semantic nodes for all ast::Variables |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3718 | std::unordered_map<const tint::ast::Variable*, sem::Parameter*> sem_params; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3719 | for (auto it : variable_to_info_) { |
| 3720 | auto* var = it.first; |
| 3721 | auto* info = it.second; |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3722 | |
| 3723 | sem::Variable* sem_var = nullptr; |
| 3724 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3725 | if (ast::HasDecoration<ast::OverrideDecoration>(var->decorations)) { |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3726 | // Create a pipeline overridable constant. |
James Price | ee23c17 | 2021-09-30 17:29:50 +0000 | [diff] [blame] | 3727 | sem_var = builder_->create<sem::GlobalVariable>(var, info->type, |
| 3728 | info->constant_id); |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3729 | } else { |
| 3730 | switch (info->kind) { |
| 3731 | case VariableKind::kGlobal: |
| 3732 | sem_var = builder_->create<sem::GlobalVariable>( |
| 3733 | var, info->type, info->storage_class, info->access, |
| 3734 | info->binding_point); |
| 3735 | break; |
| 3736 | case VariableKind::kLocal: |
| 3737 | sem_var = builder_->create<sem::LocalVariable>( |
| 3738 | var, info->type, info->storage_class, info->access); |
| 3739 | break; |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3740 | case VariableKind::kParameter: { |
| 3741 | auto* param = builder_->create<sem::Parameter>( |
| 3742 | var, info->index, info->type, info->storage_class, info->access); |
| 3743 | sem_var = param; |
| 3744 | sem_params.emplace(var, param); |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3745 | break; |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3746 | } |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3747 | } |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3750 | std::vector<const sem::VariableUser*> users; |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 3751 | for (auto* user : info->users) { |
| 3752 | // Create semantic node for the identifier expression if necessary |
| 3753 | auto* sem_expr = sem.Get(user); |
| 3754 | if (sem_expr == nullptr) { |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3755 | auto& expr_info = expr_info_.at(user); |
| 3756 | auto* type = expr_info.type; |
| 3757 | auto* stmt = expr_info.statement; |
| 3758 | auto* sem_user = builder_->create<sem::VariableUser>( |
| 3759 | user, type, stmt, sem_var, expr_info.constant_value); |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3760 | sem_var->AddUser(sem_user); |
| 3761 | sem.Add(user, sem_user); |
| 3762 | } else { |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3763 | auto* sem_user = sem_expr->As<sem::VariableUser>(); |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3764 | if (!sem_user) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3765 | TINT_ICE(Resolver, diagnostics_) << "expected sem::VariableUser, got " |
| 3766 | << sem_expr->TypeInfo().name; |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3767 | } |
| 3768 | sem_var->AddUser(sem_user); |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 3769 | } |
James Price | c9af597 | 2021-02-16 21:15:01 +0000 | [diff] [blame] | 3770 | } |
Ben Clayton | 86c2cbf | 2021-04-07 08:09:01 +0000 | [diff] [blame] | 3771 | sem.Add(var, sem_var); |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) { |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3775 | std::vector<const sem::Variable*> out; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 3776 | out.reserve(in.size()); |
| 3777 | for (auto* info : in) { |
| 3778 | out.emplace_back(sem.Get(info->declaration)); |
| 3779 | } |
| 3780 | return out; |
| 3781 | }; |
| 3782 | |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3783 | // Create semantic nodes for all ast::Functions |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3784 | std::unordered_map<FunctionInfo*, sem::Function*> func_info_to_sem_func; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 3785 | for (auto it : function_to_info_) { |
| 3786 | auto* func = it.first; |
| 3787 | auto* info = it.second; |
Ben Clayton | 5a13258 | 2021-03-03 19:54:44 +0000 | [diff] [blame] | 3788 | |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3789 | std::vector<sem::Parameter*> parameters; |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3790 | parameters.reserve(info->parameters.size()); |
| 3791 | for (auto* p : info->parameters) { |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3792 | parameters.emplace_back(sem_params.at(p->declaration)); |
Ben Clayton | 0f2d95d | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3795 | auto* sem_func = builder_->create<sem::Function>( |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3796 | info->declaration, info->return_type, parameters, |
| 3797 | remap_vars(info->referenced_module_vars), |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 3798 | remap_vars(info->local_referenced_module_vars), info->return_statements, |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3799 | info->callsites, ancestor_entry_points[func->symbol], |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 3800 | info->workgroup_size); |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 3801 | func_info_to_sem_func.emplace(info, sem_func); |
| 3802 | sem.Add(func, sem_func); |
| 3803 | } |
| 3804 | |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3805 | // Create semantic nodes for all ast::CallExpressions |
Ben Clayton | 316f9f6 | 2021-02-08 22:31:44 +0000 | [diff] [blame] | 3806 | for (auto it : function_calls_) { |
| 3807 | auto* call = it.first; |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3808 | auto info = it.second; |
| 3809 | auto* sem_func = func_info_to_sem_func.at(info.function); |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 3810 | sem.Add(call, builder_->create<sem::Call>(call, sem_func, info.statement)); |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | // Create semantic nodes for all remaining expression types |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3814 | for (auto it : expr_info_) { |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3815 | auto* expr = it.first; |
Ben Clayton | f97b9c9 | 2021-02-16 18:45:45 +0000 | [diff] [blame] | 3816 | auto& info = it.second; |
Ben Clayton | 7b7d698 | 2021-02-09 17:38:05 +0000 | [diff] [blame] | 3817 | if (sem.Get(expr)) { |
| 3818 | // Expression has already been assigned a semantic node |
| 3819 | continue; |
| 3820 | } |
Ben Clayton | 71f619b | 2021-07-13 12:18:13 +0000 | [diff] [blame] | 3821 | sem.Add(expr, builder_->create<sem::Expression>( |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 3822 | expr, info.type, info.statement, info.constant_value)); |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3826 | sem::Array* Resolver::Array(const ast::Array* arr) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3827 | auto source = arr->source; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3828 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3829 | auto* elem_type = Type(arr->type); |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 3830 | if (!elem_type) { |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3831 | return nullptr; |
| 3832 | } |
| 3833 | |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 3834 | if (!IsPlain(elem_type)) { // Check must come before GetDefaultAlignAndSize() |
| 3835 | AddError(elem_type->FriendlyName(builder_->Symbols()) + |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3836 | " cannot be used as an element type of an array", |
| 3837 | source); |
Ben Clayton | efb741f | 2021-06-03 08:24:55 +0000 | [diff] [blame] | 3838 | return nullptr; |
| 3839 | } |
| 3840 | |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 3841 | uint32_t el_align = elem_type->Align(); |
| 3842 | uint32_t el_size = elem_type->Size(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3843 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3844 | if (!ValidateNoDuplicateDecorations(arr->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 3845 | return nullptr; |
| 3846 | } |
| 3847 | |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3848 | // Look for explicit stride via [[stride(n)]] decoration |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3849 | uint32_t explicit_stride = 0; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3850 | for (auto* deco : arr->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3851 | Mark(deco); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3852 | if (auto* sd = deco->As<ast::StrideDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3853 | explicit_stride = sd->stride; |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3854 | if (!ValidateArrayStrideDecoration(sd, el_size, el_align, source)) { |
Antonio Maiorano | fc03a46 | 2021-04-16 02:36:44 +0000 | [diff] [blame] | 3855 | return nullptr; |
| 3856 | } |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3857 | continue; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3858 | } |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3859 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3860 | AddError("decoration is not valid for array types", deco->source); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3861 | return nullptr; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 3862 | } |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3863 | |
| 3864 | // Calculate implicit stride |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 3865 | uint64_t implicit_stride = utils::RoundUp<uint64_t>(el_align, el_size); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3866 | |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 3867 | uint64_t stride = explicit_stride ? explicit_stride : implicit_stride; |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3868 | |
James Price | 4cc4315 | 2021-09-02 13:49:59 +0000 | [diff] [blame] | 3869 | // Evaluate the constant array size expression. |
| 3870 | // sem::Array uses a size of 0 for a runtime-sized array. |
| 3871 | uint32_t count = 0; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3872 | if (auto* count_expr = arr->count) { |
James Price | 4cc4315 | 2021-09-02 13:49:59 +0000 | [diff] [blame] | 3873 | if (!Expression(count_expr)) { |
| 3874 | return nullptr; |
| 3875 | } |
| 3876 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3877 | auto size_source = count_expr->source; |
James Price | 4cc4315 | 2021-09-02 13:49:59 +0000 | [diff] [blame] | 3878 | |
| 3879 | auto* ty = TypeOf(count_expr)->UnwrapRef(); |
| 3880 | if (!ty->is_integer_scalar()) { |
| 3881 | AddError("array size must be integer scalar", size_source); |
| 3882 | return nullptr; |
| 3883 | } |
| 3884 | |
| 3885 | if (auto* ident = count_expr->As<ast::IdentifierExpression>()) { |
| 3886 | // Make sure the identifier is a non-overridable module-scope constant. |
| 3887 | VariableInfo* var = nullptr; |
| 3888 | bool is_global = false; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3889 | if (!variable_stack_.get(ident->symbol, &var, &is_global) || !is_global || |
| 3890 | !var->declaration->is_const) { |
James Price | 4cc4315 | 2021-09-02 13:49:59 +0000 | [diff] [blame] | 3891 | AddError("array size identifier must be a module-scope constant", |
| 3892 | size_source); |
| 3893 | return nullptr; |
| 3894 | } |
| 3895 | if (ast::HasDecoration<ast::OverrideDecoration>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3896 | var->declaration->decorations)) { |
James Price | 4cc4315 | 2021-09-02 13:49:59 +0000 | [diff] [blame] | 3897 | AddError("array size expression must not be pipeline-overridable", |
| 3898 | size_source); |
| 3899 | return nullptr; |
| 3900 | } |
| 3901 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3902 | count_expr = var->declaration->constructor; |
James Price | 4cc4315 | 2021-09-02 13:49:59 +0000 | [diff] [blame] | 3903 | } else if (!count_expr->Is<ast::ScalarConstructorExpression>()) { |
| 3904 | AddError( |
| 3905 | "array size expression must be either a literal or a module-scope " |
| 3906 | "constant", |
| 3907 | size_source); |
| 3908 | return nullptr; |
| 3909 | } |
| 3910 | |
| 3911 | auto count_val = ConstantValueOf(count_expr); |
| 3912 | if (!count_val) { |
| 3913 | TINT_ICE(Resolver, diagnostics_) |
| 3914 | << "could not resolve array size expression"; |
| 3915 | return nullptr; |
| 3916 | } |
| 3917 | |
| 3918 | if (ty->is_signed_integer_scalar() ? count_val.Elements()[0].i32 < 1 |
| 3919 | : count_val.Elements()[0].u32 < 1u) { |
| 3920 | AddError("array size must be at least 1", size_source); |
| 3921 | return nullptr; |
| 3922 | } |
| 3923 | |
| 3924 | count = count_val.Elements()[0].u32; |
| 3925 | } |
| 3926 | |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 3927 | auto size = std::max<uint64_t>(count, 1) * stride; |
| 3928 | if (size > std::numeric_limits<uint32_t>::max()) { |
| 3929 | std::stringstream msg; |
| 3930 | msg << "array size in bytes must not exceed 0x" << std::hex |
| 3931 | << std::numeric_limits<uint32_t>::max() << ", but is 0x" << std::hex |
| 3932 | << size; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3933 | AddError(msg.str(), arr->source); |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 3934 | return nullptr; |
| 3935 | } |
| 3936 | if (stride > std::numeric_limits<uint32_t>::max() || |
| 3937 | implicit_stride > std::numeric_limits<uint32_t>::max()) { |
| 3938 | TINT_ICE(Resolver, diagnostics_) |
| 3939 | << "calculated array stride exceeds uint32"; |
| 3940 | return nullptr; |
| 3941 | } |
| 3942 | auto* out = builder_->create<sem::Array>( |
| 3943 | elem_type, count, el_align, static_cast<uint32_t>(size), |
| 3944 | static_cast<uint32_t>(stride), static_cast<uint32_t>(implicit_stride)); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3945 | |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 3946 | if (!ValidateArray(out, source)) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3947 | return nullptr; |
| 3948 | } |
| 3949 | |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 3950 | if (elem_type->Is<sem::Atomic>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3951 | atomic_composite_info_.emplace(out, arr->type->source); |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 3952 | } else { |
| 3953 | auto found = atomic_composite_info_.find(elem_type); |
| 3954 | if (found != atomic_composite_info_.end()) { |
| 3955 | atomic_composite_info_.emplace(out, found->second); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | return out; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 3962 | bool Resolver::ValidateArray(const sem::Array* arr, const Source& source) { |
| 3963 | auto* el_ty = arr->ElemType(); |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3964 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 3965 | if (auto* el_str = el_ty->As<sem::Struct>()) { |
| 3966 | if (el_str->IsBlockDecorated()) { |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3967 | // https://gpuweb.github.io/gpuweb/wgsl/#attributes |
| 3968 | // A structure type with the block attribute must not be: |
| 3969 | // * the element type of an array type |
| 3970 | // * the member type in another structure |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3971 | AddError( |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3972 | "A structure type with a [[block]] decoration cannot be used as an " |
| 3973 | "element of an array", |
| 3974 | source); |
| 3975 | return false; |
| 3976 | } |
| 3977 | } |
| 3978 | return true; |
| 3979 | } |
| 3980 | |
| 3981 | bool Resolver::ValidateArrayStrideDecoration(const ast::StrideDecoration* deco, |
| 3982 | uint32_t el_size, |
| 3983 | uint32_t el_align, |
| 3984 | const Source& source) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 3985 | auto stride = deco->stride; |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3986 | bool is_valid_stride = |
| 3987 | (stride >= el_size) && (stride >= el_align) && (stride % el_align == 0); |
| 3988 | if (!is_valid_stride) { |
| 3989 | // https://gpuweb.github.io/gpuweb/wgsl/#array-layout-rules |
| 3990 | // Arrays decorated with the stride attribute must have a stride that is |
| 3991 | // at least the size of the element type, and be a multiple of the |
| 3992 | // element type's alignment value. |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 3993 | AddError( |
Ben Clayton | 8db8188 | 2021-04-21 16:45:02 +0000 | [diff] [blame] | 3994 | "arrays decorated with the stride attribute must have a stride " |
| 3995 | "that is at least the size of the element type, and be a multiple " |
| 3996 | "of the element type's alignment value.", |
| 3997 | source); |
| 3998 | return false; |
| 3999 | } |
| 4000 | return true; |
| 4001 | } |
| 4002 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4003 | bool Resolver::ValidateStructure(const sem::Struct* str) { |
James Price | 37cabbb | 2021-07-01 08:13:41 +0000 | [diff] [blame] | 4004 | if (str->Members().empty()) { |
| 4005 | AddError("structures must have at least one member", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4006 | str->Declaration()->source); |
James Price | 37cabbb | 2021-07-01 08:13:41 +0000 | [diff] [blame] | 4007 | return false; |
| 4008 | } |
| 4009 | |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4010 | std::unordered_set<uint32_t> locations; |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4011 | for (auto* member : str->Members()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4012 | if (auto* r = member->Type()->As<sem::Array>()) { |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 4013 | if (r->IsRuntimeSized()) { |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4014 | if (member != str->Members().back()) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4015 | AddError( |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4016 | "runtime arrays may only appear as the last member of a struct", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4017 | member->Declaration()->source); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4018 | return false; |
| 4019 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4020 | if (!str->IsBlockDecorated()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4021 | AddError( |
| 4022 | "a struct containing a runtime-sized array " |
| 4023 | "requires the [[block]] attribute: '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4024 | builder_->Symbols().NameFor(str->Declaration()->name) + "'", |
| 4025 | member->Declaration()->source); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4026 | return false; |
| 4027 | } |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 4031 | auto has_position = false; |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4032 | const ast::InvariantDecoration* invariant_attribute = nullptr; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4033 | for (auto* deco : member->Declaration()->decorations) { |
Ben Clayton | 97668c8 | 2021-07-27 08:17:29 +0000 | [diff] [blame] | 4034 | if (!deco->IsAnyOf<ast::BuiltinDecoration, // |
| 4035 | ast::InternalDecoration, // |
| 4036 | ast::InterpolateDecoration, // |
| 4037 | ast::InvariantDecoration, // |
| 4038 | ast::LocationDecoration, // |
| 4039 | ast::StructMemberOffsetDecoration, // |
| 4040 | ast::StructMemberSizeDecoration, // |
| 4041 | ast::StructMemberAlignDecoration>()) { |
| 4042 | if (deco->Is<ast::StrideDecoration>() && |
| 4043 | IsValidationDisabled( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4044 | member->Declaration()->decorations, |
Ben Clayton | 97668c8 | 2021-07-27 08:17:29 +0000 | [diff] [blame] | 4045 | ast::DisabledValidation::kIgnoreStrideDecoration)) { |
| 4046 | continue; |
| 4047 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4048 | AddError("decoration is not valid for structure members", deco->source); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4049 | return false; |
| 4050 | } |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4051 | |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 4052 | if (auto* invariant = deco->As<ast::InvariantDecoration>()) { |
| 4053 | invariant_attribute = invariant; |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4054 | } else if (auto* location = deco->As<ast::LocationDecoration>()) { |
| 4055 | if (!ValidateLocationDecoration(location, member->Type(), locations, |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4056 | member->Declaration()->source)) { |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4057 | return false; |
| 4058 | } |
| 4059 | } else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { |
Sarah | 465c5aa | 2021-07-23 13:23:20 +0000 | [diff] [blame] | 4060 | if (!ValidateBuiltinDecoration(builtin, member->Type(), |
James Price | ceab140 | 2021-10-05 15:02:17 +0000 | [diff] [blame] | 4061 | /* is_input */ false)) { |
Sarah | 443c41d | 2021-06-18 19:58:27 +0000 | [diff] [blame] | 4062 | return false; |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 4063 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4064 | if (builtin->builtin == ast::Builtin::kPosition) { |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 4065 | has_position = true; |
| 4066 | } |
James Price | 989a8e4 | 2021-06-28 23:04:43 +0000 | [diff] [blame] | 4067 | } else if (auto* interpolate = deco->As<ast::InterpolateDecoration>()) { |
| 4068 | if (!ValidateInterpolateDecoration(interpolate, member->Type())) { |
| 4069 | return false; |
| 4070 | } |
Sarah | c58738a | 2021-06-09 16:01:29 +0000 | [diff] [blame] | 4071 | } |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4072 | } |
Antonio Maiorano | 8b2be2d | 2021-06-18 19:57:57 +0000 | [diff] [blame] | 4073 | |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 4074 | if (invariant_attribute && !has_position) { |
| 4075 | AddError("invariant attribute must only be applied to a position builtin", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4076 | invariant_attribute->source); |
Sarah | a469668 | 2021-07-21 12:16:35 +0000 | [diff] [blame] | 4077 | return false; |
| 4078 | } |
| 4079 | |
Antonio Maiorano | 8b2be2d | 2021-06-18 19:57:57 +0000 | [diff] [blame] | 4080 | if (auto* member_struct_type = member->Type()->As<sem::Struct>()) { |
| 4081 | if (auto* member_struct_type_block_decoration = |
| 4082 | ast::GetDecoration<ast::StructBlockDecoration>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4083 | member_struct_type->Declaration()->decorations)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4084 | AddError("structs must not contain [[block]] decorated struct members", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4085 | member->Declaration()->source); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4086 | AddNote("see member's struct decoration here", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4087 | member_struct_type_block_decoration->source); |
Antonio Maiorano | 8b2be2d | 2021-06-18 19:57:57 +0000 | [diff] [blame] | 4088 | return false; |
| 4089 | } |
| 4090 | } |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4093 | for (auto* deco : str->Declaration()->decorations) { |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4094 | if (!(deco->Is<ast::StructBlockDecoration>())) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4095 | AddError("decoration is not valid for struct declarations", deco->source); |
Antonio Maiorano | 9970ec6 | 2021-03-18 17:59:54 +0000 | [diff] [blame] | 4096 | return false; |
| 4097 | } |
| 4098 | } |
| 4099 | |
| 4100 | return true; |
| 4101 | } |
| 4102 | |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4103 | bool Resolver::ValidateLocationDecoration( |
| 4104 | const ast::LocationDecoration* location, |
| 4105 | const sem::Type* type, |
| 4106 | std::unordered_set<uint32_t>& locations, |
| 4107 | const Source& source, |
| 4108 | const bool is_input) { |
| 4109 | std::string inputs_or_output = is_input ? "inputs" : "output"; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4110 | if (current_function_ && current_function_->declaration->PipelineStage() == |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4111 | ast::PipelineStage::kCompute) { |
| 4112 | AddError("decoration is not valid for compute shader " + inputs_or_output, |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4113 | location->source); |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4114 | return false; |
| 4115 | } |
| 4116 | |
| 4117 | if (!type->is_numeric_scalar_or_vector()) { |
| 4118 | std::string invalid_type = type->FriendlyName(builder_->Symbols()); |
| 4119 | AddError("cannot apply 'location' attribute to declaration of type '" + |
| 4120 | invalid_type + "'", |
| 4121 | source); |
| 4122 | AddNote( |
| 4123 | "'location' attribute must only be applied to declarations of " |
| 4124 | "numeric scalar or numeric vector type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4125 | location->source); |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4126 | return false; |
| 4127 | } |
| 4128 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4129 | if (locations.count(location->value)) { |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4130 | AddError(deco_to_str(location) + " attribute appears multiple times", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4131 | location->source); |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4132 | return false; |
| 4133 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4134 | locations.emplace(location->value); |
Sarah | c0f1ed4 | 2021-07-28 18:51:37 +0000 | [diff] [blame] | 4135 | |
| 4136 | return true; |
| 4137 | } |
| 4138 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4139 | sem::Struct* Resolver::Structure(const ast::Struct* str) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4140 | if (!ValidateNoDuplicateDecorations(str->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4141 | return nullptr; |
| 4142 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4143 | for (auto* deco : str->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4144 | Mark(deco); |
| 4145 | } |
| 4146 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 4147 | sem::StructMemberList sem_members; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4148 | sem_members.reserve(str->members.size()); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4149 | |
| 4150 | // Calculate the effective size and alignment of each field, and the overall |
| 4151 | // size of the structure. |
| 4152 | // For size, use the size attribute if provided, otherwise use the default |
| 4153 | // size for the type. |
| 4154 | // For alignment, use the alignment attribute if provided, otherwise use the |
| 4155 | // default alignment for the member type. |
| 4156 | // Diagnostic errors are raised if a basic rule is violated. |
| 4157 | // Validation of storage-class rules requires analysing the actual variable |
| 4158 | // usage of the structure, and so is performed as part of the variable |
| 4159 | // validation. |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4160 | uint64_t struct_size = 0; |
| 4161 | uint64_t struct_align = 1; |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4162 | std::unordered_map<Symbol, const ast::StructMember*> member_map; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4163 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4164 | for (auto* member : str->members) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4165 | Mark(member); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4166 | auto result = member_map.emplace(member->symbol, member); |
Sarah | 0bbe24d | 2021-07-19 17:38:20 +0000 | [diff] [blame] | 4167 | if (!result.second) { |
| 4168 | AddError("redefinition of '" + |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4169 | builder_->Symbols().NameFor(member->symbol) + "'", |
| 4170 | member->source); |
| 4171 | AddNote("previous definition is here", result.first->second->source); |
Sarah | 0bbe24d | 2021-07-19 17:38:20 +0000 | [diff] [blame] | 4172 | return nullptr; |
| 4173 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4174 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4175 | // Resolve member type |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4176 | auto* type = Type(member->type); |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4177 | if (!type) { |
| 4178 | return nullptr; |
| 4179 | } |
Ben Clayton | 2ac55fe | 2021-04-28 14:13:13 +0000 | [diff] [blame] | 4180 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4181 | // Validate member type |
James Price | 3b26717 | 2021-06-09 09:12:57 +0000 | [diff] [blame] | 4182 | if (!IsPlain(type)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4183 | AddError(type->FriendlyName(builder_->Symbols()) + |
| 4184 | " cannot be used as the type of a structure member", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4185 | member->source); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4186 | return nullptr; |
| 4187 | } |
| 4188 | |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4189 | uint64_t offset = struct_size; |
| 4190 | uint64_t align = type->Align(); |
| 4191 | uint64_t size = type->Size(); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4192 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4193 | if (!ValidateNoDuplicateDecorations(member->decorations)) { |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4194 | return nullptr; |
| 4195 | } |
| 4196 | |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4197 | bool has_offset_deco = false; |
| 4198 | bool has_align_deco = false; |
| 4199 | bool has_size_deco = false; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4200 | for (auto* deco : member->decorations) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4201 | Mark(deco); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4202 | if (auto* o = deco->As<ast::StructMemberOffsetDecoration>()) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4203 | // Offset decorations are not part of the WGSL spec, but are emitted |
| 4204 | // by the SPIR-V reader. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4205 | if (o->offset < struct_size) { |
| 4206 | AddError("offsets must be in ascending order", o->source); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4207 | return nullptr; |
| 4208 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4209 | offset = o->offset; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4210 | align = 1; |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4211 | has_offset_deco = true; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4212 | } else if (auto* a = deco->As<ast::StructMemberAlignDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4213 | if (a->align <= 0 || !utils::IsPowerOfTwo(a->align)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4214 | AddError("align value must be a positive, power-of-two integer", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4215 | a->source); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4216 | return nullptr; |
| 4217 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4218 | align = a->align; |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4219 | has_align_deco = true; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4220 | } else if (auto* s = deco->As<ast::StructMemberSizeDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4221 | if (s->size < size) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4222 | AddError("size must be at least as big as the type's size (" + |
| 4223 | std::to_string(size) + ")", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4224 | s->source); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4225 | return nullptr; |
| 4226 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4227 | size = s->size; |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4228 | has_size_deco = true; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4229 | } |
| 4230 | } |
| 4231 | |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4232 | if (has_offset_deco && (has_align_deco || has_size_deco)) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4233 | AddError( |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4234 | "offset decorations cannot be used with align or size decorations", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4235 | member->source); |
Ben Clayton | 2f9ced0 | 2021-03-15 20:34:22 +0000 | [diff] [blame] | 4236 | return nullptr; |
| 4237 | } |
| 4238 | |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 4239 | offset = utils::RoundUp(align, offset); |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4240 | if (offset > std::numeric_limits<uint32_t>::max()) { |
| 4241 | std::stringstream msg; |
| 4242 | msg << "struct member has byte offset 0x" << std::hex << offset |
| 4243 | << ", but must not exceed 0x" << std::hex |
| 4244 | << std::numeric_limits<uint32_t>::max(); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4245 | AddError(msg.str(), member->source); |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4246 | return nullptr; |
| 4247 | } |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4248 | |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4249 | auto* sem_member = builder_->create<sem::StructMember>( |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4250 | member, member->symbol, type, static_cast<uint32_t>(sem_members.size()), |
Ben Clayton | c40d15d | 2021-10-14 22:43:21 +0000 | [diff] [blame] | 4251 | static_cast<uint32_t>(offset), static_cast<uint32_t>(align), |
| 4252 | static_cast<uint32_t>(size)); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4253 | builder_->Sem().Add(member, sem_member); |
| 4254 | sem_members.emplace_back(sem_member); |
| 4255 | |
| 4256 | struct_size = offset + size; |
| 4257 | struct_align = std::max(struct_align, align); |
| 4258 | } |
| 4259 | |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4260 | uint64_t size_no_padding = struct_size; |
Ben Clayton | a752052 | 2021-03-15 13:37:41 +0000 | [diff] [blame] | 4261 | struct_size = utils::RoundUp(struct_align, struct_size); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4262 | |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4263 | if (struct_size > std::numeric_limits<uint32_t>::max()) { |
| 4264 | std::stringstream msg; |
| 4265 | msg << "struct size in bytes must not exceed 0x" << std::hex |
| 4266 | << std::numeric_limits<uint32_t>::max() << ", but is 0x" << std::hex |
| 4267 | << struct_size; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4268 | AddError(msg.str(), str->source); |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4269 | return nullptr; |
| 4270 | } |
| 4271 | if (struct_align > std::numeric_limits<uint32_t>::max()) { |
| 4272 | TINT_ICE(Resolver, diagnostics_) |
| 4273 | << "calculated struct stride exceeds uint32"; |
| 4274 | return nullptr; |
| 4275 | } |
| 4276 | |
| 4277 | auto* out = builder_->create<sem::Struct>( |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4278 | str, str->name, sem_members, static_cast<uint32_t>(struct_align), |
Ben Clayton | d1d99bc | 2021-09-15 17:37:00 +0000 | [diff] [blame] | 4279 | static_cast<uint32_t>(struct_size), |
| 4280 | static_cast<uint32_t>(size_no_padding)); |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 4281 | |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 4282 | for (size_t i = 0; i < sem_members.size(); i++) { |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 4283 | auto* mem_type = sem_members[i]->Type(); |
| 4284 | if (mem_type->Is<sem::Atomic>()) { |
| 4285 | atomic_composite_info_.emplace(out, |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4286 | sem_members[i]->Declaration()->source); |
Sarah | 4038fa7 | 2021-08-05 15:18:29 +0000 | [diff] [blame] | 4287 | break; |
| 4288 | } else { |
| 4289 | auto found = atomic_composite_info_.find(mem_type); |
| 4290 | if (found != atomic_composite_info_.end()) { |
| 4291 | atomic_composite_info_.emplace(out, found->second); |
| 4292 | break; |
| 4293 | } |
Ben Clayton | 313e618 | 2021-06-17 19:56:14 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4296 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4297 | if (!ValidateStructure(out)) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4298 | return nullptr; |
| 4299 | } |
| 4300 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4301 | return out; |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4304 | bool Resolver::ValidateReturn(const ast::ReturnStatement* ret) { |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 4305 | auto* func_type = current_function_->return_type; |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4306 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4307 | auto* ret_type = ret->value ? TypeOf(ret->value)->UnwrapRef() |
| 4308 | : builder_->create<sem::Void>(); |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4309 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4310 | if (func_type->UnwrapRef() != ret_type) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4311 | AddError( |
| 4312 | "return statement type must match its function " |
| 4313 | "return type, returned '" + |
| 4314 | ret_type->FriendlyName(builder_->Symbols()) + "', expected '" + |
| 4315 | current_function_->return_type_name + "'", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4316 | ret->source); |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4317 | return false; |
| 4318 | } |
| 4319 | |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 4320 | auto* sem = builder_->Sem().Get(ret); |
| 4321 | if (auto* continuing = |
| 4322 | sem->FindFirstParent<sem::LoopContinuingBlockStatement>()) { |
| 4323 | AddError("continuing blocks must not contain a return statement", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4324 | ret->source); |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 4325 | if (continuing != sem->Parent()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4326 | AddNote("see continuing block here", continuing->Declaration()->source); |
Ben Clayton | cd7eb4f | 2021-07-17 18:09:26 +0000 | [diff] [blame] | 4327 | } |
| 4328 | return false; |
| 4329 | } |
| 4330 | |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4331 | return true; |
| 4332 | } |
| 4333 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4334 | bool Resolver::Return(const ast::ReturnStatement* ret) { |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4335 | current_function_->return_statements.push_back(ret); |
| 4336 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4337 | if (auto* value = ret->value) { |
James Price | 7e22196 | 2021-06-15 15:00:57 +0000 | [diff] [blame] | 4338 | if (!Expression(value)) { |
| 4339 | return false; |
| 4340 | } |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
James Price | 7e22196 | 2021-06-15 15:00:57 +0000 | [diff] [blame] | 4343 | // Validate after processing the return value expression so that its type is |
| 4344 | // available for validation. |
| 4345 | return ValidateReturn(ret); |
Antonio Maiorano | 2e97435 | 2021-03-22 23:20:17 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4348 | bool Resolver::ValidateSwitch(const ast::SwitchStatement* s) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4349 | auto* cond_type = TypeOf(s->condition)->UnwrapRef(); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4350 | if (!cond_type->is_integer_scalar()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4351 | AddError( |
| 4352 | "switch statement selector expression must be of a " |
| 4353 | "scalar integer type", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4354 | s->condition->source); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4355 | return false; |
| 4356 | } |
| 4357 | |
| 4358 | bool has_default = false; |
Ben Clayton | 11ed0db | 2021-10-14 20:30:29 +0000 | [diff] [blame] | 4359 | std::unordered_map<uint32_t, Source> selectors; |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4360 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4361 | for (auto* case_stmt : s->body) { |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4362 | if (case_stmt->IsDefault()) { |
| 4363 | if (has_default) { |
| 4364 | // More than one default clause |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4365 | AddError("switch statement must have exactly one default clause", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4366 | case_stmt->source); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4367 | return false; |
| 4368 | } |
| 4369 | has_default = true; |
| 4370 | } |
| 4371 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4372 | for (auto* selector : case_stmt->selectors) { |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 4373 | if (cond_type != TypeOf(selector)) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4374 | AddError( |
| 4375 | "the case selector values must have the same " |
| 4376 | "type as the selector expression.", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4377 | case_stmt->source); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4378 | return false; |
| 4379 | } |
| 4380 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4381 | auto v = selector->ValueAsU32(); |
Ben Clayton | 11ed0db | 2021-10-14 20:30:29 +0000 | [diff] [blame] | 4382 | auto it = selectors.find(v); |
| 4383 | if (it != selectors.end()) { |
| 4384 | auto val = selector->Is<ast::IntLiteral>() |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4385 | ? std::to_string(selector->ValueAsI32()) |
| 4386 | : std::to_string(selector->ValueAsU32()); |
| 4387 | AddError("duplicate switch case '" + val + "'", selector->source); |
Ben Clayton | 11ed0db | 2021-10-14 20:30:29 +0000 | [diff] [blame] | 4388 | AddNote("previous case declared here", it->second); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4389 | return false; |
| 4390 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4391 | selectors.emplace(v, selector->source); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4392 | } |
| 4393 | } |
| 4394 | |
| 4395 | if (!has_default) { |
| 4396 | // No default clause |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4397 | AddError("switch statement must have a default clause", s->source); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4398 | return false; |
| 4399 | } |
| 4400 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4401 | if (!s->body.empty()) { |
| 4402 | auto* last_clause = s->body.back()->As<ast::CaseStatement>(); |
| 4403 | auto* last_stmt = last_clause->body->Last(); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4404 | if (last_stmt && last_stmt->Is<ast::FallthroughStatement>()) { |
Ben Clayton | 5a88ec8 | 2021-06-29 14:42:19 +0000 | [diff] [blame] | 4405 | AddError( |
| 4406 | "a fallthrough statement must not appear as " |
| 4407 | "the last statement in last clause of a switch", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4408 | last_stmt->source); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4409 | return false; |
| 4410 | } |
| 4411 | } |
| 4412 | |
| 4413 | return true; |
| 4414 | } |
| 4415 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4416 | bool Resolver::SwitchStatement(const ast::SwitchStatement* stmt) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4417 | auto* sem = |
| 4418 | builder_->create<sem::SwitchStatement>(stmt, current_compound_statement_); |
| 4419 | builder_->Sem().Add(stmt, sem); |
| 4420 | return Scope(sem, [&] { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4421 | if (!Expression(stmt->condition)) { |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4422 | return false; |
| 4423 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4424 | for (auto* case_stmt : stmt->body) { |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4425 | Mark(case_stmt); |
| 4426 | if (!CaseStatement(case_stmt)) { |
| 4427 | return false; |
| 4428 | } |
| 4429 | } |
| 4430 | if (!ValidateSwitch(stmt)) { |
| 4431 | return false; |
| 4432 | } |
| 4433 | return true; |
| 4434 | }); |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4437 | bool Resolver::Assignment(const ast::AssignmentStatement* a) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4438 | if (!Expression(a->lhs) || !Expression(a->rhs)) { |
Antonio Maiorano | e09989a | 2021-03-31 13:26:43 +0000 | [diff] [blame] | 4439 | return false; |
| 4440 | } |
Ben Clayton | 1aa98e6 | 2021-10-21 23:04:44 +0000 | [diff] [blame] | 4441 | |
Antonio Maiorano | e09989a | 2021-03-31 13:26:43 +0000 | [diff] [blame] | 4442 | return ValidateAssignment(a); |
| 4443 | } |
| 4444 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4445 | bool Resolver::ValidateAssignment(const ast::AssignmentStatement* a) { |
Ben Clayton | 1aa98e6 | 2021-10-21 23:04:44 +0000 | [diff] [blame] | 4446 | auto const* rhs_type = TypeOf(a->rhs); |
| 4447 | |
| 4448 | if (a->lhs->Is<ast::PhonyExpression>()) { |
| 4449 | // https://www.w3.org/TR/WGSL/#phony-assignment-section |
| 4450 | auto* ty = rhs_type->UnwrapRef(); |
| 4451 | if (!ty->IsConstructible() && |
| 4452 | !ty->IsAnyOf<sem::Pointer, sem::Texture, sem::Sampler>()) { |
| 4453 | AddError( |
| 4454 | "cannot assign '" + TypeNameOf(a->rhs) + |
Ben Clayton | c73b57f | 2021-10-25 14:26:55 +0000 | [diff] [blame] | 4455 | "' to '_'. '_' can only be assigned a constructible, pointer, " |
Ben Clayton | 1aa98e6 | 2021-10-21 23:04:44 +0000 | [diff] [blame] | 4456 | "texture or sampler type", |
| 4457 | a->rhs->source); |
| 4458 | return false; |
| 4459 | } |
| 4460 | return true; // RHS can be anything. |
| 4461 | } |
| 4462 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4463 | // https://gpuweb.github.io/gpuweb/wgsl/#assignment-statement |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4464 | auto const* lhs_type = TypeOf(a->lhs); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4465 | |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4466 | if (auto* ident = a->lhs->As<ast::IdentifierExpression>()) { |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4467 | VariableInfo* var; |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4468 | if (variable_stack_.get(ident->symbol, &var)) { |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4469 | if (var->kind == VariableKind::kParameter) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4470 | AddError("cannot assign to function parameter", a->lhs->source); |
| 4471 | AddNote("'" + builder_->Symbols().NameFor(ident->symbol) + |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4472 | "' is declared here:", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4473 | var->declaration->source); |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4474 | return false; |
| 4475 | } |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4476 | if (var->declaration->is_const) { |
| 4477 | AddError("cannot assign to const", a->lhs->source); |
| 4478 | AddNote("'" + builder_->Symbols().NameFor(ident->symbol) + |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4479 | "' is declared here:", |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4480 | var->declaration->source); |
Sarah | cb0309e | 2021-06-28 20:44:31 +0000 | [diff] [blame] | 4481 | return false; |
| 4482 | } |
| 4483 | } |
| 4484 | } |
| 4485 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4486 | auto* lhs_ref = lhs_type->As<sem::Reference>(); |
| 4487 | if (!lhs_ref) { |
| 4488 | // LHS is not a reference, so it has no storage. |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4489 | AddError("cannot assign to value of type '" + TypeNameOf(a->lhs) + "'", |
| 4490 | a->lhs->source); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4491 | return false; |
| 4492 | } |
| 4493 | |
Antonio Maiorano | be607c0 | 2021-05-18 15:26:40 +0000 | [diff] [blame] | 4494 | auto* storage_type = lhs_ref->StoreType(); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4495 | auto* value_type = rhs_type->UnwrapRef(); // Implicit load of RHS |
| 4496 | |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4497 | // Value type has to match storage type |
| 4498 | if (storage_type != value_type) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4499 | AddError("cannot assign '" + TypeNameOf(a->rhs) + "' to '" + |
| 4500 | TypeNameOf(a->lhs) + "'", |
| 4501 | a->source); |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4502 | return false; |
| 4503 | } |
James Price | 06c86a5 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 4504 | if (!storage_type->IsConstructible()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4505 | AddError("storage type of assignment must be constructible", a->source); |
James Price | 06c86a5 | 2021-08-13 15:20:42 +0000 | [diff] [blame] | 4506 | return false; |
| 4507 | } |
Sarah | ebaa4b4 | 2021-06-15 22:39:19 +0000 | [diff] [blame] | 4508 | if (lhs_ref->Access() == ast::Access::kRead) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4509 | AddError("cannot store into a read-only type '" + TypeNameOf(a->lhs) + "'", |
| 4510 | a->source); |
Sarah | ebaa4b4 | 2021-06-15 22:39:19 +0000 | [diff] [blame] | 4511 | return false; |
| 4512 | } |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4513 | return true; |
| 4514 | } |
| 4515 | |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 4516 | bool Resolver::ValidateNoDuplicateDefinition(Symbol sym, |
| 4517 | const Source& source, |
| 4518 | bool check_global_scope_only) { |
| 4519 | if (check_global_scope_only) { |
| 4520 | bool is_global = false; |
| 4521 | VariableInfo* var; |
| 4522 | if (variable_stack_.get(sym, &var, &is_global)) { |
| 4523 | if (is_global) { |
| 4524 | AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'", |
| 4525 | source); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4526 | AddNote("previous definition is here", var->declaration->source); |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 4527 | return false; |
| 4528 | } |
| 4529 | } |
| 4530 | auto it = symbol_to_function_.find(sym); |
| 4531 | if (it != symbol_to_function_.end()) { |
| 4532 | AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'", |
| 4533 | source); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4534 | AddNote("previous definition is here", it->second->declaration->source); |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 4535 | return false; |
| 4536 | } |
| 4537 | } else { |
| 4538 | VariableInfo* var; |
| 4539 | if (variable_stack_.get(sym, &var)) { |
| 4540 | AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'", |
| 4541 | source); |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4542 | AddNote("previous definition is here", var->declaration->source); |
Sarah | c035366 | 2021-06-29 21:30:37 +0000 | [diff] [blame] | 4543 | return false; |
| 4544 | } |
| 4545 | } |
| 4546 | return true; |
| 4547 | } |
| 4548 | |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4549 | bool Resolver::ValidateNoDuplicateDecorations( |
| 4550 | const ast::DecorationList& decorations) { |
| 4551 | std::unordered_map<const TypeInfo*, Source> seen; |
| 4552 | for (auto* d : decorations) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4553 | auto res = seen.emplace(&d->TypeInfo(), d->source); |
James Price | 4daac0e | 2021-06-15 16:33:57 +0000 | [diff] [blame] | 4554 | if (!res.second && !d->Is<ast::InternalDecoration>()) { |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4555 | AddError("duplicate " + d->Name() + " decoration", d->source); |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4556 | AddNote("first decoration declared here", res.first->second); |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 4557 | return false; |
| 4558 | } |
| 4559 | } |
| 4560 | return true; |
| 4561 | } |
| 4562 | |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4563 | bool Resolver::ApplyStorageClassUsageToType(ast::StorageClass sc, |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4564 | sem::Type* ty, |
Antonio Maiorano | 2e0bd4a | 2021-04-16 01:15:43 +0000 | [diff] [blame] | 4565 | const Source& usage) { |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4566 | ty = const_cast<sem::Type*>(ty->UnwrapRef()); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4567 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4568 | if (auto* str = ty->As<sem::Struct>()) { |
| 4569 | if (str->StorageClassUsage().count(sc)) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4570 | return true; // Already applied |
| 4571 | } |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 4572 | |
| 4573 | str->AddUsage(sc); |
| 4574 | |
| 4575 | for (auto* member : str->Members()) { |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 4576 | if (!ApplyStorageClassUsageToType(sc, member->Type(), usage)) { |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4577 | std::stringstream err; |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4578 | err << "while analysing structure member " |
| 4579 | << str->FriendlyName(builder_->Symbols()) << "." |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4580 | << builder_->Symbols().NameFor(member->Declaration()->symbol); |
| 4581 | AddNote(err.str(), member->Declaration()->source); |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4582 | return false; |
| 4583 | } |
| 4584 | } |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4585 | return true; |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 4588 | if (auto* arr = ty->As<sem::Array>()) { |
| 4589 | return ApplyStorageClassUsageToType( |
| 4590 | sc, const_cast<sem::Type*>(arr->ElemType()), usage); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 4593 | if (ast::IsHostShareable(sc) && !IsHostShareable(ty)) { |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4594 | std::stringstream err; |
| 4595 | err << "Type '" << ty->FriendlyName(builder_->Symbols()) |
| 4596 | << "' cannot be used in storage class '" << sc |
Ben Clayton | 25eef8d | 2021-03-18 21:03:24 +0000 | [diff] [blame] | 4597 | << "' as it is non-host-shareable"; |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4598 | AddError(err.str(), usage); |
Ben Clayton | 722b5a2 | 2021-03-18 20:46:24 +0000 | [diff] [blame] | 4599 | return false; |
Ben Clayton | a88090b | 2021-03-17 22:47:33 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | return true; |
| 4603 | } |
| 4604 | |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 4605 | template <typename F> |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4606 | bool Resolver::Scope(sem::CompoundStatement* stmt, F&& callback) { |
| 4607 | auto* prev_current_statement = current_statement_; |
| 4608 | auto* prev_current_compound_statement = current_compound_statement_; |
| 4609 | auto* prev_current_block = current_block_; |
| 4610 | current_statement_ = stmt; |
| 4611 | current_compound_statement_ = stmt; |
| 4612 | current_block_ = stmt->As<sem::BlockStatement>(); |
Antonio Maiorano | 4682e3f | 2021-03-22 17:38:45 +0000 | [diff] [blame] | 4613 | variable_stack_.push_scope(); |
Ben Clayton | 6e459fe | 2021-07-14 09:44:41 +0000 | [diff] [blame] | 4614 | |
| 4615 | TINT_DEFER({ |
| 4616 | TINT_DEFER(variable_stack_.pop_scope()); |
| 4617 | current_block_ = prev_current_block; |
| 4618 | current_compound_statement_ = prev_current_compound_statement; |
| 4619 | current_statement_ = prev_current_statement; |
| 4620 | }); |
| 4621 | |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 4622 | return callback(); |
Ben Clayton | 9430cb4 | 2021-03-09 15:06:37 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 4625 | std::string Resolver::VectorPretty(uint32_t size, |
| 4626 | const sem::Type* element_type) { |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 4627 | sem::Vector vec_type(element_type, size); |
Arman Uguray | 097c75a | 2021-03-18 15:43:14 +0000 | [diff] [blame] | 4628 | return vec_type.FriendlyName(builder_->Symbols()); |
| 4629 | } |
| 4630 | |
Ben Clayton | 12ed13d | 2021-04-28 12:38:13 +0000 | [diff] [blame] | 4631 | void Resolver::Mark(const ast::Node* node) { |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4632 | if (node == nullptr) { |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4633 | TINT_ICE(Resolver, diagnostics_) << "Resolver::Mark() called with nullptr"; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4634 | } |
| 4635 | if (marked_.emplace(node).second) { |
| 4636 | return; |
| 4637 | } |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4638 | TINT_ICE(Resolver, diagnostics_) |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4639 | << "AST node '" << node->TypeInfo().name |
| 4640 | << "' was encountered twice in the same AST of a Program\n" |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 4641 | << "At: " << node->source << "\n" |
Ben Clayton | f0756e3 | 2021-04-29 20:57:55 +0000 | [diff] [blame] | 4642 | << "Pointer: " << node; |
Ben Clayton | 6fcefe4 | 2021-04-19 19:16:12 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Ben Clayton | ffd28e2 | 2021-06-24 11:27:36 +0000 | [diff] [blame] | 4645 | void Resolver::AddError(const std::string& msg, const Source& source) const { |
| 4646 | diagnostics_.add_error(diag::System::Resolver, msg, source); |
| 4647 | } |
| 4648 | |
| 4649 | void Resolver::AddWarning(const std::string& msg, const Source& source) const { |
| 4650 | diagnostics_.add_warning(diag::System::Resolver, msg, source); |
| 4651 | } |
| 4652 | |
| 4653 | void Resolver::AddNote(const std::string& msg, const Source& source) const { |
| 4654 | diagnostics_.add_note(diag::System::Resolver, msg, source); |
| 4655 | } |
| 4656 | |
Ben Clayton | 12ed13d | 2021-04-28 12:38:13 +0000 | [diff] [blame] | 4657 | Resolver::VariableInfo::VariableInfo(const ast::Variable* decl, |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4658 | sem::Type* ty, |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 4659 | const std::string& tn, |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4660 | ast::StorageClass sc, |
Ryan Harrison | 3832b8e | 2021-06-09 20:45:09 +0000 | [diff] [blame] | 4661 | ast::Access ac, |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 4662 | VariableKind k, |
| 4663 | uint32_t idx) |
Antonio Maiorano | 9ef1747 | 2021-03-26 12:47:58 +0000 | [diff] [blame] | 4664 | : declaration(decl), |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4665 | type(ty), |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 4666 | type_name(tn), |
Ben Clayton | 9b54a2e | 2021-05-18 10:28:48 +0000 | [diff] [blame] | 4667 | storage_class(sc), |
Ryan Harrison | 3832b8e | 2021-06-09 20:45:09 +0000 | [diff] [blame] | 4668 | access(ac), |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 4669 | kind(k), |
| 4670 | index(idx) {} |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 4671 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 4672 | Resolver::VariableInfo::~VariableInfo() = default; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 4673 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 4674 | Resolver::FunctionInfo::FunctionInfo(const ast::Function* decl) |
| 4675 | : declaration(decl) {} |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 4676 | Resolver::FunctionInfo::~FunctionInfo() = default; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 4677 | |
Ben Clayton | c7dcbae | 2021-03-09 15:08:48 +0000 | [diff] [blame] | 4678 | } // namespace resolver |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 4679 | } // namespace tint |