From 4ffcf067a3ac76465d76295985441f36d82be7f2 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 22 Jul 2021 13:24:59 +0000 Subject: [PATCH] sem: Add Owner() to sem::Parameter Change-Id: I3de1e2437b9604378b8368494363e19070443670 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/59201 Kokoro: Kokoro Reviewed-by: Ryan Harrison Reviewed-by: James Price --- src/intrinsic_table.cc | 6 +++--- src/resolver/resolver.cc | 32 +++++++++++++++++++---------- src/resolver/resolver.h | 9 +++++++-- src/sem/function.cc | 11 +++++++--- src/sem/function.h | 2 +- src/sem/intrinsic.cc | 14 ++++++++++--- src/sem/intrinsic.h | 3 ++- src/sem/variable.cc | 5 ++++- src/sem/variable.h | 17 +++++++++++++--- src/utils/to_const_ptr_vec.h | 39 ++++++++++++++++++++++++++++++++++++ 10 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 src/utils/to_const_ptr_vec.h diff --git a/src/intrinsic_table.cc b/src/intrinsic_table.cc index 4f1fa28276..c134f43e88 100644 --- a/src/intrinsic_table.cc +++ b/src/intrinsic_table.cc @@ -960,12 +960,12 @@ const sem::Intrinsic* Impl::Match(sem::IntrinsicType intrinsic_type, // De-duplicate intrinsics that are identical. return utils::GetOrCreate(intrinsics, intrinsic, [&] { - sem::ParameterList params; + std::vector params; params.reserve(intrinsic.parameters.size()); for (auto& p : intrinsic.parameters) { params.emplace_back(builder.create( - nullptr, p.type, ast::StorageClass::kNone, ast::Access::kUndefined, - p.usage)); + nullptr, static_cast(params.size()), p.type, + ast::StorageClass::kNone, ast::Access::kUndefined, p.usage)); } return builder.create( intrinsic.type, const_cast(intrinsic.return_type), diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 842b34882f..2904df81e3 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -477,7 +477,8 @@ bool Resolver::ValidateStorageTexture(const ast::StorageTexture* t) { } Resolver::VariableInfo* Resolver::Variable(ast::Variable* var, - VariableKind kind) { + VariableKind kind, + uint32_t index /* = 0 */) { if (variable_to_info_.count(var)) { TINT_ICE(Resolver, diagnostics_) << "Variable " << builder_->Symbols().NameFor(var->symbol()) @@ -576,8 +577,9 @@ Resolver::VariableInfo* Resolver::Variable(ast::Variable* var, return nullptr; } - auto* info = variable_infos_.Create(var, const_cast(type), - type_name, storage_class, access, kind); + auto* info = + variable_infos_.Create(var, const_cast(type), type_name, + storage_class, access, kind, index); variable_to_info_.emplace(var, info); return info; @@ -1716,9 +1718,11 @@ bool Resolver::Function(ast::Function* func) { TINT_SCOPED_ASSIGNMENT(current_function_, info); variable_stack_.push_scope(); + uint32_t parameter_index = 0; for (auto* param : func->params()) { Mark(param); - auto* param_info = Variable(param, VariableKind::kParameter); + auto* param_info = + Variable(param, VariableKind::kParameter, parameter_index++); if (!param_info) { return false; } @@ -3667,6 +3671,7 @@ void Resolver::CreateSemanticNodes() const { uint16_t next_constant_id = 0; // Create semantic nodes for all ast::Variables + std::unordered_map sem_params; for (auto it : variable_to_info_) { auto* var = it.first; auto* info = it.second; @@ -3706,10 +3711,13 @@ void Resolver::CreateSemanticNodes() const { sem_var = builder_->create( var, info->type, info->storage_class, info->access); break; - case VariableKind::kParameter: - sem_var = builder_->create( - var, info->type, info->storage_class, info->access); + case VariableKind::kParameter: { + auto* param = builder_->create( + var, info->index, info->type, info->storage_class, info->access); + sem_var = param; + sem_params.emplace(var, param); break; + } } } @@ -3752,10 +3760,10 @@ void Resolver::CreateSemanticNodes() const { auto* func = it.first; auto* info = it.second; - sem::ParameterList parameters; + std::vector parameters; parameters.reserve(info->parameters.size()); for (auto* p : info->parameters) { - parameters.emplace_back(sem.Get(p->declaration)); + parameters.emplace_back(sem_params.at(p->declaration)); } auto* sem_func = builder_->create( @@ -4453,13 +4461,15 @@ Resolver::VariableInfo::VariableInfo(const ast::Variable* decl, const std::string& tn, ast::StorageClass sc, ast::Access ac, - VariableKind k) + VariableKind k, + uint32_t idx) : declaration(decl), type(ty), type_name(tn), storage_class(sc), access(ac), - kind(k) {} + kind(k), + index(idx) {} Resolver::VariableInfo::~VariableInfo() = default; diff --git a/src/resolver/resolver.h b/src/resolver/resolver.h index 6afb4c7109..600ef229e4 100644 --- a/src/resolver/resolver.h +++ b/src/resolver/resolver.h @@ -103,7 +103,8 @@ class Resolver { const std::string& type_name, ast::StorageClass storage_class, ast::Access ac, - VariableKind k); + VariableKind k, + uint32_t idx); ~VariableInfo(); ast::Variable const* const declaration; @@ -114,6 +115,7 @@ class Resolver { std::vector users; sem::BindingPoint binding_point; VariableKind kind; + uint32_t index = 0; // Parameter index, if kind == kParameter }; struct IntrinsicCallInfo { @@ -359,7 +361,10 @@ class Resolver { /// context-dependent (global, local, parameter) /// @param var the variable to create or return the `VariableInfo` for /// @param kind what kind of variable we are declaring - VariableInfo* Variable(ast::Variable* var, VariableKind kind); + /// @param index the index of the parameter, if this variable is a parameter + VariableInfo* Variable(ast::Variable* var, + VariableKind kind, + uint32_t index = 0); /// Records the storage class usage for the given type, and any transient /// dependencies of the type. Validates that the type can be used for the diff --git a/src/sem/function.cc b/src/sem/function.cc index 21fabf9490..454be7425e 100644 --- a/src/sem/function.cc +++ b/src/sem/function.cc @@ -21,6 +21,7 @@ #include "src/sem/sampled_texture_type.h" #include "src/sem/storage_texture_type.h" #include "src/sem/variable.h" +#include "src/utils/to_const_ptr_vec.h" TINT_INSTANTIATE_TYPEINFO(tint::sem::Function); @@ -29,21 +30,25 @@ namespace sem { Function::Function(ast::Function* declaration, Type* return_type, - ParameterList parameters, + std::vector parameters, std::vector referenced_module_vars, std::vector local_referenced_module_vars, std::vector return_statements, std::vector callsites, std::vector ancestor_entry_points, std::array workgroup_size) - : Base(return_type, std::move(parameters)), + : Base(return_type, utils::ToConstPtrVec(parameters)), declaration_(declaration), referenced_module_vars_(std::move(referenced_module_vars)), local_referenced_module_vars_(std::move(local_referenced_module_vars)), return_statements_(std::move(return_statements)), callsites_(callsites), ancestor_entry_points_(std::move(ancestor_entry_points)), - workgroup_size_(std::move(workgroup_size)) {} + workgroup_size_(std::move(workgroup_size)) { + for (auto* parameter : parameters) { + parameter->SetOwner(this); + } +} Function::~Function() = default; diff --git a/src/sem/function.h b/src/sem/function.h index 6ab1d39543..ebf6303fac 100644 --- a/src/sem/function.h +++ b/src/sem/function.h @@ -68,7 +68,7 @@ class Function : public Castable { /// @param workgroup_size the workgroup size Function(ast::Function* declaration, Type* return_type, - ParameterList parameters, + std::vector parameters, std::vector referenced_module_vars, std::vector local_referenced_module_vars, std::vector return_statements, diff --git a/src/sem/intrinsic.cc b/src/sem/intrinsic.cc index b7d57a932f..2e38901bd8 100644 --- a/src/sem/intrinsic.cc +++ b/src/sem/intrinsic.cc @@ -14,6 +14,10 @@ #include "src/sem/intrinsic.h" +#include + +#include "src/utils/to_const_ptr_vec.h" + TINT_INSTANTIATE_TYPEINFO(tint::sem::Intrinsic); namespace tint { @@ -98,13 +102,17 @@ bool IsAtomicIntrinsic(IntrinsicType i) { Intrinsic::Intrinsic(IntrinsicType type, sem::Type* return_type, - ParameterList parameters, + std::vector parameters, PipelineStageSet supported_stages, bool is_deprecated) - : Base(return_type, parameters), + : Base(return_type, utils::ToConstPtrVec(parameters)), type_(type), supported_stages_(supported_stages), - is_deprecated_(is_deprecated) {} + is_deprecated_(is_deprecated) { + for (auto* parameter : parameters) { + parameter->SetOwner(this); + } +} Intrinsic::~Intrinsic() = default; diff --git a/src/sem/intrinsic.h b/src/sem/intrinsic.h index 74f332f4a8..27f3136334 100644 --- a/src/sem/intrinsic.h +++ b/src/sem/intrinsic.h @@ -16,6 +16,7 @@ #define SRC_SEM_INTRINSIC_H_ #include +#include #include "src/sem/call_target.h" #include "src/sem/intrinsic_type.h" @@ -88,7 +89,7 @@ class Intrinsic : public Castable { /// deprecated Intrinsic(IntrinsicType type, sem::Type* return_type, - ParameterList parameters, + std::vector parameters, PipelineStageSet supported_stages, bool is_deprecated); diff --git a/src/sem/variable.cc b/src/sem/variable.cc index fb6f48fb51..bc783a1c59 100644 --- a/src/sem/variable.cc +++ b/src/sem/variable.cc @@ -69,11 +69,14 @@ GlobalVariable::GlobalVariable(const ast::Variable* declaration, GlobalVariable::~GlobalVariable() = default; Parameter::Parameter(const ast::Variable* declaration, + uint32_t index, const sem::Type* type, ast::StorageClass storage_class, ast::Access access, const ParameterUsage usage /* = ParameterUsage::kNone */) - : Base(declaration, type, storage_class, access), usage_(usage) {} + : Base(declaration, type, storage_class, access), + index_(index), + usage_(usage) {} Parameter::~Parameter() = default; diff --git a/src/sem/variable.h b/src/sem/variable.h index fc8d9beb47..5bbe3e1ab5 100644 --- a/src/sem/variable.h +++ b/src/sem/variable.h @@ -34,6 +34,7 @@ class Variable; namespace sem { // Forward declarations +class CallTarget; class Type; class VariableUser; @@ -143,27 +144,37 @@ class Parameter : public Castable { public: /// Constructor for function parameters /// @param declaration the AST declaration node + /// @param index the index of the parmeter in the function /// @param type the variable type /// @param storage_class the variable storage class /// @param access the variable access control type /// @param usage the semantic usage for the parameter Parameter(const ast::Variable* declaration, + uint32_t index, const sem::Type* type, ast::StorageClass storage_class, ast::Access access, const ParameterUsage usage = ParameterUsage::kNone); - /// Copy constructor - Parameter(const Parameter&); - /// Destructor ~Parameter() override; + /// @return the index of the parmeter in the function + uint32_t Index() const { return index_; } + /// @returns the semantic usage for the parameter ParameterUsage Usage() const { return usage_; } + /// @returns the CallTarget owner of this parameter + CallTarget const* Owner() const { return owner_; } + + /// @param owner the CallTarget owner of this parameter + void SetOwner(CallTarget const* owner) { owner_ = owner; } + private: + uint32_t const index_; ParameterUsage const usage_; + CallTarget const* owner_; }; /// ParameterList is a list of Parameter diff --git a/src/utils/to_const_ptr_vec.h b/src/utils/to_const_ptr_vec.h new file mode 100644 index 0000000000..25e9261b2d --- /dev/null +++ b/src/utils/to_const_ptr_vec.h @@ -0,0 +1,39 @@ + +// Copyright 2021 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_UTILS_TO_CONST_PTR_VEC_H_ +#define SRC_UTILS_TO_CONST_PTR_VEC_H_ + +#include + +namespace tint { +namespace utils { + +/// @param in a vector of `T*` +/// @returns a vector of `const T*` with the content of `in`. +template +std::vector ToConstPtrVec(const std::vector& in) { + std::vector out; + out.reserve(in.size()); + for (auto* ptr : in) { + out.emplace_back(ptr); + } + return out; +} + +} // namespace utils +} // namespace tint + +#endif // SRC_UTILS_TO_CONST_PTR_VEC_H_