Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/wgsl/sem/function.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 30 | #include "src/tint/lang/core/type/depth_texture.h" |
| 31 | #include "src/tint/lang/core/type/external_texture.h" |
| 32 | #include "src/tint/lang/core/type/multisampled_texture.h" |
| 33 | #include "src/tint/lang/core/type/sampled_texture.h" |
| 34 | #include "src/tint/lang/core/type/storage_texture.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 35 | #include "src/tint/lang/wgsl/ast/function.h" |
| 36 | #include "src/tint/lang/wgsl/ast/identifier.h" |
| 37 | #include "src/tint/lang/wgsl/ast/must_use_attribute.h" |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 38 | #include "src/tint/lang/wgsl/sem/variable.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 39 | #include "src/tint/utils/containers/transform.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | |
| 41 | TINT_INSTANTIATE_TYPEINFO(tint::sem::Function); |
| 42 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 43 | namespace tint::sem { |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 44 | |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 45 | Function::Function(const ast::Function* declaration) |
Ben Clayton | 36c6155 | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 46 | : Base(core::EvaluationStage::kRuntime, |
Ben Clayton | b549b30 | 2023-02-22 16:18:22 +0000 | [diff] [blame] | 47 | ast::HasAttribute<ast::MustUseAttribute>(declaration->attributes)), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 48 | declaration_(declaration), |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 49 | workgroup_size_{1, 1, 1} {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
| 51 | Function::~Function() = default; |
| 52 | |
| 53 | std::vector<std::pair<const Variable*, const ast::LocationAttribute*>> |
| 54 | Function::TransitivelyReferencedLocationVariables() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 55 | std::vector<std::pair<const Variable*, const ast::LocationAttribute*>> ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 57 | for (auto* global : TransitivelyReferencedGlobals()) { |
| 58 | for (auto* attr : global->Declaration()->attributes) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | if (auto* location = attr->As<ast::LocationAttribute>()) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 60 | ret.push_back({global, location}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 61 | break; |
| 62 | } |
| 63 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 65 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ben Clayton | 535535b | 2023-10-09 15:14:34 +0000 | [diff] [blame] | 68 | void Function::AddTransitivelyReferencedGlobal(const sem::GlobalVariable* global) { |
| 69 | if (transitively_referenced_globals_.Add(global)) { |
| 70 | for (auto* ref : global->TransitivelyReferencedOverrides()) { |
| 71 | AddTransitivelyReferencedGlobal(ref); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 76 | Function::VariableBindings Function::TransitivelyReferencedUniformVariables() const { |
| 77 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 78 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 79 | for (auto* global : TransitivelyReferencedGlobals()) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 80 | if (global->AddressSpace() != core::AddressSpace::kUniform) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 81 | continue; |
| 82 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 84 | if (auto bp = global->Attributes().binding_point) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 85 | ret.push_back({global, *bp}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 87 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 88 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | Function::VariableBindings Function::TransitivelyReferencedStorageBufferVariables() const { |
| 92 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 93 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 94 | for (auto* global : TransitivelyReferencedGlobals()) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 95 | if (global->AddressSpace() != core::AddressSpace::kStorage) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | continue; |
| 97 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 98 | |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 99 | if (auto bp = global->Attributes().binding_point) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 100 | ret.push_back({global, *bp}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 101 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 102 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 103 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | std::vector<std::pair<const Variable*, const ast::BuiltinAttribute*>> |
| 107 | Function::TransitivelyReferencedBuiltinVariables() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | std::vector<std::pair<const Variable*, const ast::BuiltinAttribute*>> ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 109 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 110 | for (auto* global : TransitivelyReferencedGlobals()) { |
| 111 | for (auto* attr : global->Declaration()->attributes) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 112 | if (auto* builtin = attr->As<ast::BuiltinAttribute>()) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 113 | ret.push_back({global, builtin}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 117 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 118 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 119 | } |
| 120 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 121 | Function::VariableBindings Function::TransitivelyReferencedSamplerVariables() const { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 122 | return TransitivelyReferencedSamplerVariablesImpl(core::type::SamplerKind::kSampler); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 123 | } |
| 124 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 125 | Function::VariableBindings Function::TransitivelyReferencedComparisonSamplerVariables() const { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 126 | return TransitivelyReferencedSamplerVariablesImpl(core::type::SamplerKind::kComparisonSampler); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 129 | Function::VariableBindings Function::TransitivelyReferencedSampledTextureVariables() const { |
| 130 | return TransitivelyReferencedSampledTextureVariablesImpl(false); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 133 | Function::VariableBindings Function::TransitivelyReferencedMultisampledTextureVariables() const { |
| 134 | return TransitivelyReferencedSampledTextureVariablesImpl(true); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | Function::VariableBindings Function::TransitivelyReferencedVariablesOfType( |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 138 | const tint::TypeInfo* type) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 139 | VariableBindings ret; |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 140 | for (auto* global : TransitivelyReferencedGlobals()) { |
| 141 | auto* unwrapped_type = global->Type()->UnwrapRef(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 142 | if (unwrapped_type->TypeInfo().Is(type)) { |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 143 | if (auto bp = global->Attributes().binding_point) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 144 | ret.push_back({global, *bp}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 147 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 148 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bool Function::HasAncestorEntryPoint(Symbol symbol) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 152 | for (const auto* point : ancestor_entry_points_) { |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 153 | if (point->Declaration()->name->symbol == symbol) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 154 | return true; |
| 155 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 156 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 157 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | Function::VariableBindings Function::TransitivelyReferencedSamplerVariablesImpl( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 161 | core::type::SamplerKind kind) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 162 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 163 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 164 | for (auto* global : TransitivelyReferencedGlobals()) { |
| 165 | auto* unwrapped_type = global->Type()->UnwrapRef(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 166 | auto* sampler = unwrapped_type->As<core::type::Sampler>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 167 | if (sampler == nullptr || sampler->kind() != kind) { |
| 168 | continue; |
| 169 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 170 | |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 171 | if (auto bp = global->Attributes().binding_point) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 172 | ret.push_back({global, *bp}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 173 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 174 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 175 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 176 | } |
| 177 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 178 | Function::VariableBindings Function::TransitivelyReferencedSampledTextureVariablesImpl( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 179 | bool multisampled) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 180 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 181 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 182 | for (auto* global : TransitivelyReferencedGlobals()) { |
| 183 | auto* unwrapped_type = global->Type()->UnwrapRef(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 184 | auto* texture = unwrapped_type->As<core::type::Texture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 185 | if (texture == nullptr) { |
| 186 | continue; |
| 187 | } |
| 188 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 189 | auto is_multisampled = texture->Is<core::type::MultisampledTexture>(); |
| 190 | auto is_sampled = texture->Is<core::type::SampledTexture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 191 | |
| 192 | if ((multisampled && !is_multisampled) || (!multisampled && !is_sampled)) { |
| 193 | continue; |
| 194 | } |
| 195 | |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 196 | if (auto bp = global->Attributes().binding_point) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 197 | ret.push_back({global, *bp}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 198 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 199 | } |
| 200 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 201 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 204 | void Function::SetDiagnosticSeverity(wgsl::DiagnosticRule rule, wgsl::DiagnosticSeverity severity) { |
| 205 | diagnostic_severities_.Add(rule, severity); |
| 206 | } |
| 207 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 208 | } // namespace tint::sem |