Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/tint/sem/function.h" |
| 16 | |
| 17 | #include "src/tint/ast/function.h" |
Ben Clayton | 01004b7 | 2022-04-28 18:49:04 +0000 | [diff] [blame] | 18 | #include "src/tint/sem/depth_texture.h" |
| 19 | #include "src/tint/sem/external_texture.h" |
| 20 | #include "src/tint/sem/multisampled_texture.h" |
| 21 | #include "src/tint/sem/sampled_texture.h" |
| 22 | #include "src/tint/sem/storage_texture.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | #include "src/tint/sem/variable.h" |
| 24 | #include "src/tint/utils/to_const_ptr_vec.h" |
| 25 | |
| 26 | TINT_INSTANTIATE_TYPEINFO(tint::sem::Function); |
| 27 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 28 | namespace tint::sem { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
| 30 | Function::Function(const ast::Function* declaration, |
| 31 | Type* return_type, |
| 32 | std::vector<Parameter*> parameters) |
| 33 | : Base(return_type, utils::ToConstPtrVec(parameters)), |
| 34 | declaration_(declaration), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 35 | workgroup_size_{WorkgroupDimension{1}, WorkgroupDimension{1}, WorkgroupDimension{1}} { |
| 36 | for (auto* parameter : parameters) { |
| 37 | parameter->SetOwner(this); |
| 38 | } |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 39 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | |
| 41 | Function::~Function() = default; |
| 42 | |
| 43 | std::vector<std::pair<const Variable*, const ast::LocationAttribute*>> |
| 44 | Function::TransitivelyReferencedLocationVariables() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 45 | std::vector<std::pair<const Variable*, const ast::LocationAttribute*>> ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 46 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 47 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 48 | for (auto* attr : var->Declaration()->attributes) { |
| 49 | if (auto* location = attr->As<ast::LocationAttribute>()) { |
| 50 | ret.push_back({var, location}); |
| 51 | break; |
| 52 | } |
| 53 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 54 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 55 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 58 | Function::VariableBindings Function::TransitivelyReferencedUniformVariables() const { |
| 59 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 61 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 62 | if (var->StorageClass() != ast::StorageClass::kUniform) { |
| 63 | continue; |
| 64 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 65 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 66 | if (auto binding_point = var->Declaration()->BindingPoint()) { |
| 67 | ret.push_back({var, binding_point}); |
| 68 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 69 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 70 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 71 | } |
| 72 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 73 | Function::VariableBindings Function::TransitivelyReferencedStorageBufferVariables() const { |
| 74 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 75 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 76 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 77 | if (var->StorageClass() != ast::StorageClass::kStorage) { |
| 78 | continue; |
| 79 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 80 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 81 | if (auto binding_point = var->Declaration()->BindingPoint()) { |
| 82 | ret.push_back({var, binding_point}); |
| 83 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 84 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 85 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::vector<std::pair<const Variable*, const ast::BuiltinAttribute*>> |
| 89 | Function::TransitivelyReferencedBuiltinVariables() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 90 | std::vector<std::pair<const Variable*, const ast::BuiltinAttribute*>> ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 91 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 92 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 93 | for (auto* attr : var->Declaration()->attributes) { |
| 94 | if (auto* builtin = attr->As<ast::BuiltinAttribute>()) { |
| 95 | ret.push_back({var, builtin}); |
| 96 | break; |
| 97 | } |
| 98 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 99 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 100 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | } |
| 102 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 103 | Function::VariableBindings Function::TransitivelyReferencedSamplerVariables() const { |
| 104 | return TransitivelyReferencedSamplerVariablesImpl(ast::SamplerKind::kSampler); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 105 | } |
| 106 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 107 | Function::VariableBindings Function::TransitivelyReferencedComparisonSamplerVariables() const { |
| 108 | return TransitivelyReferencedSamplerVariablesImpl(ast::SamplerKind::kComparisonSampler); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 109 | } |
| 110 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 111 | Function::VariableBindings Function::TransitivelyReferencedSampledTextureVariables() const { |
| 112 | return TransitivelyReferencedSampledTextureVariablesImpl(false); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 113 | } |
| 114 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 115 | Function::VariableBindings Function::TransitivelyReferencedMultisampledTextureVariables() const { |
| 116 | return TransitivelyReferencedSampledTextureVariablesImpl(true); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Function::VariableBindings Function::TransitivelyReferencedVariablesOfType( |
| 120 | const tint::TypeInfo* type) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 121 | VariableBindings ret; |
| 122 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 123 | auto* unwrapped_type = var->Type()->UnwrapRef(); |
| 124 | if (unwrapped_type->TypeInfo().Is(type)) { |
| 125 | if (auto binding_point = var->Declaration()->BindingPoint()) { |
| 126 | ret.push_back({var, binding_point}); |
| 127 | } |
| 128 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 129 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 130 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool Function::HasAncestorEntryPoint(Symbol symbol) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 134 | for (const auto* point : ancestor_entry_points_) { |
| 135 | if (point->Declaration()->symbol == symbol) { |
| 136 | return true; |
| 137 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 138 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 139 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Function::VariableBindings Function::TransitivelyReferencedSamplerVariablesImpl( |
| 143 | ast::SamplerKind kind) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 144 | VariableBindings ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 145 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 146 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 147 | auto* unwrapped_type = var->Type()->UnwrapRef(); |
| 148 | auto* sampler = unwrapped_type->As<sem::Sampler>(); |
| 149 | if (sampler == nullptr || sampler->kind() != kind) { |
| 150 | continue; |
| 151 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 152 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 153 | if (auto binding_point = var->Declaration()->BindingPoint()) { |
| 154 | ret.push_back({var, binding_point}); |
| 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 ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 160 | Function::VariableBindings Function::TransitivelyReferencedSampledTextureVariablesImpl( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 161 | bool multisampled) 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 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 164 | for (auto* var : TransitivelyReferencedGlobals()) { |
| 165 | auto* unwrapped_type = var->Type()->UnwrapRef(); |
| 166 | auto* texture = unwrapped_type->As<sem::Texture>(); |
| 167 | if (texture == nullptr) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | auto is_multisampled = texture->Is<sem::MultisampledTexture>(); |
| 172 | auto is_sampled = texture->Is<sem::SampledTexture>(); |
| 173 | |
| 174 | if ((multisampled && !is_multisampled) || (!multisampled && !is_sampled)) { |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | if (auto binding_point = var->Declaration()->BindingPoint()) { |
| 179 | ret.push_back({var, binding_point}); |
| 180 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 181 | } |
| 182 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame^] | 183 | return ret; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 184 | } |
| 185 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 186 | } // namespace tint::sem |