Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0(the "License"); |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 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 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 15 | #ifndef SRC_SEM_FUNCTION_H_ |
| 16 | #define SRC_SEM_FUNCTION_H_ |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 17 | |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 18 | #include <array> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 19 | #include <utility> |
| 20 | #include <vector> |
| 21 | |
Ben Clayton | a864f24 | 2021-03-29 21:21:35 +0000 | [diff] [blame] | 22 | #include "src/ast/variable.h" |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 23 | #include "src/sem/call_target.h" |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 24 | |
| 25 | namespace tint { |
| 26 | |
| 27 | // Forward declarations |
| 28 | namespace ast { |
| 29 | class BindingDecoration; |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 30 | class BuiltinDecoration; |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 31 | class CallExpression; |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 32 | class Function; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 33 | class GroupDecoration; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 34 | class LocationDecoration; |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 35 | class ReturnStatement; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 36 | } // namespace ast |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 37 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 38 | namespace sem { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 39 | |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 40 | class Variable; |
| 41 | |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 42 | /// WorkgroupDimension describes the size of a single dimension of an entry |
| 43 | /// point's workgroup size. |
| 44 | struct WorkgroupDimension { |
| 45 | /// The size of this dimension. |
| 46 | uint32_t value; |
| 47 | /// A pipeline-overridable constant that overrides the size, or nullptr if |
| 48 | /// this dimension is not overridable. |
| 49 | const ast::Variable* overridable_const = nullptr; |
| 50 | }; |
| 51 | |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 52 | /// Function holds the semantic information for function nodes. |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 53 | class Function : public Castable<Function, CallTarget> { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 54 | public: |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 55 | /// A vector of [Variable*, ast::VariableBindingPoint] pairs |
Ben Clayton | a864f24 | 2021-03-29 21:21:35 +0000 | [diff] [blame] | 56 | using VariableBindings = |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 57 | std::vector<std::pair<const Variable*, ast::VariableBindingPoint>>; |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 58 | |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 59 | /// Constructor |
Ben Clayton | 43a160d | 2021-02-17 01:08:41 +0000 | [diff] [blame] | 60 | /// @param declaration the ast::Function |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 61 | /// @param return_type the return type of the function |
Ben Clayton | e9c4984 | 2021-04-09 13:56:08 +0000 | [diff] [blame] | 62 | /// @param parameters the parameters to the function |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 63 | /// @param referenced_module_vars the referenced module variables |
| 64 | /// @param local_referenced_module_vars the locally referenced module |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 65 | /// @param return_statements the function return statements |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 66 | /// @param callsites the callsites of the function |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 67 | /// @param ancestor_entry_points the ancestor entry points |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 68 | /// @param workgroup_size the workgroup size |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 69 | Function(const ast::Function* declaration, |
Ben Clayton | 3068dcb | 2021-04-30 19:24:29 +0000 | [diff] [blame] | 70 | Type* return_type, |
Ben Clayton | 4ffcf06 | 2021-07-22 13:24:59 +0000 | [diff] [blame] | 71 | std::vector<Parameter*> parameters, |
Ben Clayton | 2ddb178 | 2021-02-08 19:13:19 +0000 | [diff] [blame] | 72 | std::vector<const Variable*> referenced_module_vars, |
| 73 | std::vector<const Variable*> local_referenced_module_vars, |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 74 | std::vector<const ast::ReturnStatement*> return_statements, |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 75 | std::vector<const ast::CallExpression*> callsites, |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 76 | std::vector<Symbol> ancestor_entry_points, |
| 77 | std::array<WorkgroupDimension, 3> workgroup_size); |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 78 | |
| 79 | /// Destructor |
| 80 | ~Function() override; |
| 81 | |
Ben Clayton | 43a160d | 2021-02-17 01:08:41 +0000 | [diff] [blame] | 82 | /// @returns the ast::Function declaration |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 83 | const ast::Function* Declaration() const { return declaration_; } |
Ben Clayton | 43a160d | 2021-02-17 01:08:41 +0000 | [diff] [blame] | 84 | |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 85 | /// Note: If this function calls other functions, the return will also include |
| 86 | /// all of the referenced variables from the callees. |
| 87 | /// @returns the referenced module variables |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 88 | const std::vector<const Variable*>& ReferencedModuleVariables() const { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 89 | return referenced_module_vars_; |
| 90 | } |
| 91 | /// @returns the locally referenced module variables |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 92 | const std::vector<const Variable*>& LocalReferencedModuleVariables() const { |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 93 | return local_referenced_module_vars_; |
| 94 | } |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 95 | /// @returns the return statements |
| 96 | const std::vector<const ast::ReturnStatement*> ReturnStatements() const { |
| 97 | return return_statements_; |
| 98 | } |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 99 | /// @returns the list of callsites of this function |
| 100 | std::vector<const ast::CallExpression*> CallSites() const { |
| 101 | return callsites_; |
| 102 | } |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 103 | /// @returns the ancestor entry points |
| 104 | const std::vector<Symbol>& AncestorEntryPoints() const { |
| 105 | return ancestor_entry_points_; |
| 106 | } |
| 107 | /// Retrieves any referenced location variables |
| 108 | /// @returns the <variable, decoration> pair. |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 109 | std::vector<std::pair<const Variable*, const ast::LocationDecoration*>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 110 | ReferencedLocationVariables() const; |
| 111 | |
| 112 | /// Retrieves any referenced builtin variables |
| 113 | /// @returns the <variable, decoration> pair. |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 114 | std::vector<std::pair<const Variable*, const ast::BuiltinDecoration*>> |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 115 | ReferencedBuiltinVariables() const; |
| 116 | |
| 117 | /// Retrieves any referenced uniform variables. Note, the variables must be |
| 118 | /// decorated with both binding and group decorations. |
| 119 | /// @returns the referenced uniforms |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 120 | VariableBindings ReferencedUniformVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 121 | |
| 122 | /// Retrieves any referenced storagebuffer variables. Note, the variables |
| 123 | /// must be decorated with both binding and group decorations. |
| 124 | /// @returns the referenced storagebuffers |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 125 | VariableBindings ReferencedStorageBufferVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 126 | |
| 127 | /// Retrieves any referenced regular Sampler variables. Note, the |
| 128 | /// variables must be decorated with both binding and group decorations. |
| 129 | /// @returns the referenced storagebuffers |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 130 | VariableBindings ReferencedSamplerVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 131 | |
| 132 | /// Retrieves any referenced comparison Sampler variables. Note, the |
| 133 | /// variables must be decorated with both binding and group decorations. |
| 134 | /// @returns the referenced storagebuffers |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 135 | VariableBindings ReferencedComparisonSamplerVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 136 | |
| 137 | /// Retrieves any referenced sampled textures variables. Note, the |
| 138 | /// variables must be decorated with both binding and group decorations. |
| 139 | /// @returns the referenced sampled textures |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 140 | VariableBindings ReferencedSampledTextureVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 141 | |
| 142 | /// Retrieves any referenced multisampled textures variables. Note, the |
| 143 | /// variables must be decorated with both binding and group decorations. |
| 144 | /// @returns the referenced sampled textures |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 145 | VariableBindings ReferencedMultisampledTextureVariables() const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 146 | |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 147 | /// Retrieves any referenced variables of the given type. Note, the variables |
Ryan Harrison | d086c14 | 2021-02-16 15:10:23 +0000 | [diff] [blame] | 148 | /// must be decorated with both binding and group decorations. |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 149 | /// @param type_info the type of the variables to find |
| 150 | /// @returns the referenced variables |
| 151 | VariableBindings ReferencedVariablesOfType( |
| 152 | const tint::TypeInfo& type_info) const; |
Ryan Harrison | d086c14 | 2021-02-16 15:10:23 +0000 | [diff] [blame] | 153 | |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 154 | /// Retrieves any referenced variables of the given type. Note, the variables |
Ryan Harrison | 04d93c8 | 2021-03-03 13:46:43 +0000 | [diff] [blame] | 155 | /// must be decorated with both binding and group decorations. |
Ben Clayton | fd35aa8 | 2021-07-26 22:19:48 +0000 | [diff] [blame] | 156 | /// @returns the referenced variables |
| 157 | template <typename T> |
| 158 | VariableBindings ReferencedVariablesOfType() const { |
| 159 | return ReferencedVariablesOfType(TypeInfo::Of<T>()); |
| 160 | } |
Brandon Jones | 7b25769 | 2021-05-17 17:40:17 +0000 | [diff] [blame] | 161 | |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 162 | /// Checks if the given entry point is an ancestor |
| 163 | /// @param sym the entry point symbol |
| 164 | /// @returns true if `sym` is an ancestor entry point of this function |
| 165 | bool HasAncestorEntryPoint(Symbol sym) const; |
| 166 | |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 167 | /// @returns the workgroup size {x, y, z} for the function. |
| 168 | const std::array<WorkgroupDimension, 3>& workgroup_size() const { |
| 169 | return workgroup_size_; |
| 170 | } |
| 171 | |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 172 | private: |
Ben Clayton | fec63b7 | 2021-04-21 13:47:12 +0000 | [diff] [blame] | 173 | VariableBindings ReferencedSamplerVariablesImpl(ast::SamplerKind kind) const; |
Ben Clayton | 1d5a7eb | 2021-02-18 16:36:18 +0000 | [diff] [blame] | 174 | VariableBindings ReferencedSampledTextureVariablesImpl( |
| 175 | bool multisampled) const; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 176 | |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 177 | const ast::Function* const declaration_; |
Ben Clayton | b17aea1 | 2021-02-03 17:51:09 +0000 | [diff] [blame] | 178 | std::vector<const Variable*> const referenced_module_vars_; |
| 179 | std::vector<const Variable*> const local_referenced_module_vars_; |
James Price | 4ffd3e2 | 2021-03-17 14:24:04 +0000 | [diff] [blame] | 180 | std::vector<const ast::ReturnStatement*> const return_statements_; |
James Price | bcefe46 | 2021-05-22 12:48:24 +0000 | [diff] [blame] | 181 | std::vector<const ast::CallExpression*> const callsites_; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 182 | std::vector<Symbol> const ancestor_entry_points_; |
James Price | ce8f868 | 2021-05-19 08:15:18 +0000 | [diff] [blame] | 183 | std::array<WorkgroupDimension, 3> workgroup_size_; |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 186 | } // namespace sem |
Ben Clayton | 87c78dd | 2021-02-03 16:43:20 +0000 | [diff] [blame] | 187 | } // namespace tint |
| 188 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 189 | #endif // SRC_SEM_FUNCTION_H_ |