blob: 18e46177e707b6ab0695528c28b860778a0975fd [file] [log] [blame]
Ben Clayton87c78dd2021-02-03 16:43:20 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0(the "License");
Ben Clayton87c78dd2021-02-03 16:43:20 +00004// 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 Maiorano5cd71b82021-04-16 19:07:51 +000015#ifndef SRC_SEM_FUNCTION_H_
16#define SRC_SEM_FUNCTION_H_
Ben Clayton87c78dd2021-02-03 16:43:20 +000017
James Pricece8f8682021-05-19 08:15:18 +000018#include <array>
Ben Clayton87c78dd2021-02-03 16:43:20 +000019#include <utility>
20#include <vector>
21
Ben Claytona864f242021-03-29 21:21:35 +000022#include "src/ast/variable.h"
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000023#include "src/sem/call_target.h"
Ben Clayton87c78dd2021-02-03 16:43:20 +000024
25namespace tint {
26
27// Forward declarations
28namespace ast {
29class BindingDecoration;
Ben Clayton2ddb1782021-02-08 19:13:19 +000030class BuiltinDecoration;
James Pricebcefe462021-05-22 12:48:24 +000031class CallExpression;
Ben Clayton2ddb1782021-02-08 19:13:19 +000032class Function;
Ben Clayton87c78dd2021-02-03 16:43:20 +000033class GroupDecoration;
Ben Clayton87c78dd2021-02-03 16:43:20 +000034class LocationDecoration;
James Price4ffd3e22021-03-17 14:24:04 +000035class ReturnStatement;
Ben Clayton87c78dd2021-02-03 16:43:20 +000036} // namespace ast
Ben Clayton87c78dd2021-02-03 16:43:20 +000037
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000038namespace sem {
Ben Clayton87c78dd2021-02-03 16:43:20 +000039
Ben Claytonb17aea12021-02-03 17:51:09 +000040class Variable;
41
James Pricece8f8682021-05-19 08:15:18 +000042/// WorkgroupDimension describes the size of a single dimension of an entry
43/// point's workgroup size.
44struct 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 Clayton87c78dd2021-02-03 16:43:20 +000052/// Function holds the semantic information for function nodes.
Ben Clayton2ddb1782021-02-08 19:13:19 +000053class Function : public Castable<Function, CallTarget> {
Ben Clayton87c78dd2021-02-03 16:43:20 +000054 public:
Ben Clayton4f3ff572021-10-15 17:33:10 +000055 /// A vector of [Variable*, ast::VariableBindingPoint] pairs
Ben Claytona864f242021-03-29 21:21:35 +000056 using VariableBindings =
Ben Clayton4f3ff572021-10-15 17:33:10 +000057 std::vector<std::pair<const Variable*, ast::VariableBindingPoint>>;
Ben Clayton1d5a7eb2021-02-18 16:36:18 +000058
Ben Clayton87c78dd2021-02-03 16:43:20 +000059 /// Constructor
Ben Clayton43a160d2021-02-17 01:08:41 +000060 /// @param declaration the ast::Function
Ben Clayton3068dcb2021-04-30 19:24:29 +000061 /// @param return_type the return type of the function
Ben Claytone9c49842021-04-09 13:56:08 +000062 /// @param parameters the parameters to the function
Ben Clayton87c78dd2021-02-03 16:43:20 +000063 /// @param referenced_module_vars the referenced module variables
64 /// @param local_referenced_module_vars the locally referenced module
James Price4ffd3e22021-03-17 14:24:04 +000065 /// @param return_statements the function return statements
James Pricebcefe462021-05-22 12:48:24 +000066 /// @param callsites the callsites of the function
Ben Clayton87c78dd2021-02-03 16:43:20 +000067 /// @param ancestor_entry_points the ancestor entry points
James Pricece8f8682021-05-19 08:15:18 +000068 /// @param workgroup_size the workgroup size
Ben Clayton86481202021-10-19 18:38:54 +000069 Function(const ast::Function* declaration,
Ben Clayton3068dcb2021-04-30 19:24:29 +000070 Type* return_type,
Ben Clayton4ffcf062021-07-22 13:24:59 +000071 std::vector<Parameter*> parameters,
Ben Clayton2ddb1782021-02-08 19:13:19 +000072 std::vector<const Variable*> referenced_module_vars,
73 std::vector<const Variable*> local_referenced_module_vars,
James Price4ffd3e22021-03-17 14:24:04 +000074 std::vector<const ast::ReturnStatement*> return_statements,
James Pricebcefe462021-05-22 12:48:24 +000075 std::vector<const ast::CallExpression*> callsites,
James Pricece8f8682021-05-19 08:15:18 +000076 std::vector<Symbol> ancestor_entry_points,
77 std::array<WorkgroupDimension, 3> workgroup_size);
Ben Clayton87c78dd2021-02-03 16:43:20 +000078
79 /// Destructor
80 ~Function() override;
81
Ben Clayton43a160d2021-02-17 01:08:41 +000082 /// @returns the ast::Function declaration
Ben Clayton86481202021-10-19 18:38:54 +000083 const ast::Function* Declaration() const { return declaration_; }
Ben Clayton43a160d2021-02-17 01:08:41 +000084
Ben Clayton87c78dd2021-02-03 16:43:20 +000085 /// 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 Claytonb17aea12021-02-03 17:51:09 +000088 const std::vector<const Variable*>& ReferencedModuleVariables() const {
Ben Clayton87c78dd2021-02-03 16:43:20 +000089 return referenced_module_vars_;
90 }
91 /// @returns the locally referenced module variables
Ben Claytonb17aea12021-02-03 17:51:09 +000092 const std::vector<const Variable*>& LocalReferencedModuleVariables() const {
Ben Clayton87c78dd2021-02-03 16:43:20 +000093 return local_referenced_module_vars_;
94 }
James Price4ffd3e22021-03-17 14:24:04 +000095 /// @returns the return statements
96 const std::vector<const ast::ReturnStatement*> ReturnStatements() const {
97 return return_statements_;
98 }
James Pricebcefe462021-05-22 12:48:24 +000099 /// @returns the list of callsites of this function
100 std::vector<const ast::CallExpression*> CallSites() const {
101 return callsites_;
102 }
Ben Clayton87c78dd2021-02-03 16:43:20 +0000103 /// @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 Clayton86481202021-10-19 18:38:54 +0000109 std::vector<std::pair<const Variable*, const ast::LocationDecoration*>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000110 ReferencedLocationVariables() const;
111
112 /// Retrieves any referenced builtin variables
113 /// @returns the <variable, decoration> pair.
Ben Clayton86481202021-10-19 18:38:54 +0000114 std::vector<std::pair<const Variable*, const ast::BuiltinDecoration*>>
Ben Clayton87c78dd2021-02-03 16:43:20 +0000115 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 Clayton1d5a7eb2021-02-18 16:36:18 +0000120 VariableBindings ReferencedUniformVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000121
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 Clayton1d5a7eb2021-02-18 16:36:18 +0000125 VariableBindings ReferencedStorageBufferVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000126
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 Clayton1d5a7eb2021-02-18 16:36:18 +0000130 VariableBindings ReferencedSamplerVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000131
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 Clayton1d5a7eb2021-02-18 16:36:18 +0000135 VariableBindings ReferencedComparisonSamplerVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000136
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 Clayton1d5a7eb2021-02-18 16:36:18 +0000140 VariableBindings ReferencedSampledTextureVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000141
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 Clayton1d5a7eb2021-02-18 16:36:18 +0000145 VariableBindings ReferencedMultisampledTextureVariables() const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000146
Ben Claytonfd35aa82021-07-26 22:19:48 +0000147 /// Retrieves any referenced variables of the given type. Note, the variables
Ryan Harrisond086c142021-02-16 15:10:23 +0000148 /// must be decorated with both binding and group decorations.
Ben Claytonfd35aa82021-07-26 22:19:48 +0000149 /// @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 Harrisond086c142021-02-16 15:10:23 +0000153
Ben Claytonfd35aa82021-07-26 22:19:48 +0000154 /// Retrieves any referenced variables of the given type. Note, the variables
Ryan Harrison04d93c82021-03-03 13:46:43 +0000155 /// must be decorated with both binding and group decorations.
Ben Claytonfd35aa82021-07-26 22:19:48 +0000156 /// @returns the referenced variables
157 template <typename T>
158 VariableBindings ReferencedVariablesOfType() const {
159 return ReferencedVariablesOfType(TypeInfo::Of<T>());
160 }
Brandon Jones7b257692021-05-17 17:40:17 +0000161
Ben Clayton87c78dd2021-02-03 16:43:20 +0000162 /// 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 Pricece8f8682021-05-19 08:15:18 +0000167 /// @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 Clayton87c78dd2021-02-03 16:43:20 +0000172 private:
Ben Claytonfec63b72021-04-21 13:47:12 +0000173 VariableBindings ReferencedSamplerVariablesImpl(ast::SamplerKind kind) const;
Ben Clayton1d5a7eb2021-02-18 16:36:18 +0000174 VariableBindings ReferencedSampledTextureVariablesImpl(
175 bool multisampled) const;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000176
Ben Clayton86481202021-10-19 18:38:54 +0000177 const ast::Function* const declaration_;
Ben Claytonb17aea12021-02-03 17:51:09 +0000178 std::vector<const Variable*> const referenced_module_vars_;
179 std::vector<const Variable*> const local_referenced_module_vars_;
James Price4ffd3e22021-03-17 14:24:04 +0000180 std::vector<const ast::ReturnStatement*> const return_statements_;
James Pricebcefe462021-05-22 12:48:24 +0000181 std::vector<const ast::CallExpression*> const callsites_;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000182 std::vector<Symbol> const ancestor_entry_points_;
James Pricece8f8682021-05-19 08:15:18 +0000183 std::array<WorkgroupDimension, 3> workgroup_size_;
Ben Clayton87c78dd2021-02-03 16:43:20 +0000184};
185
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000186} // namespace sem
Ben Clayton87c78dd2021-02-03 16:43:20 +0000187} // namespace tint
188
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000189#endif // SRC_SEM_FUNCTION_H_