Add semantic::Function, use it.

Pull the mutable semantic fields from ast::Function and into a new semantic::Function node.
Have the TypeDeterminer create these semantic::Function nodes.

Bug: tint:390
Change-Id: I237b1bed8709dd9a3cfa24d85d48fc77b7e532da
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/39902
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-02-03 16:43:20 +00:00
committed by Commit Bot service account
parent c694d43c75
commit 87c78ddabc
22 changed files with 744 additions and 512 deletions

View File

@@ -17,8 +17,6 @@
#include "src/semantic/node.h"
#include "src/semantic/type_mappings.h"
namespace tint {
// Forward declarations

148
src/semantic/function.h Normal file
View File

@@ -0,0 +1,148 @@
// 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_SEMANTIC_FUNCTION_H_
#define SRC_SEMANTIC_FUNCTION_H_
#include <utility>
#include <vector>
#include "src/semantic/node.h"
#include "src/type/sampler_type.h"
namespace tint {
// Forward declarations
namespace ast {
class BindingDecoration;
class GroupDecoration;
class Variable;
class LocationDecoration;
class BuiltinDecoration;
} // namespace ast
namespace type {
class Type;
} // namespace type
namespace semantic {
/// Function holds the semantic information for function nodes.
class Function : public Castable<Function, Node> {
public:
/// Information about a binding
struct BindingInfo {
/// The binding decoration
ast::BindingDecoration* binding = nullptr;
/// The group decoration
ast::GroupDecoration* group = nullptr;
};
/// Constructor
/// @param referenced_module_vars the referenced module variables
/// @param local_referenced_module_vars the locally referenced module
/// variables
/// @param ancestor_entry_points the ancestor entry points
explicit Function(std::vector<ast::Variable*> referenced_module_vars,
std::vector<ast::Variable*> local_referenced_module_vars,
std::vector<Symbol> ancestor_entry_points);
/// Destructor
~Function() override;
/// Note: If this function calls other functions, the return will also include
/// all of the referenced variables from the callees.
/// @returns the referenced module variables
const std::vector<ast::Variable*>& ReferencedModuleVariables() const {
return referenced_module_vars_;
}
/// @returns the locally referenced module variables
const std::vector<ast::Variable*>& LocalReferencedModuleVariables() const {
return local_referenced_module_vars_;
}
/// @returns the ancestor entry points
const std::vector<Symbol>& AncestorEntryPoints() const {
return ancestor_entry_points_;
}
/// Retrieves any referenced location variables
/// @returns the <variable, decoration> pair.
const std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>>
ReferencedLocationVariables() const;
/// Retrieves any referenced builtin variables
/// @returns the <variable, decoration> pair.
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
ReferencedBuiltinVariables() const;
/// Retrieves any referenced uniform variables. Note, the variables must be
/// decorated with both binding and group decorations.
/// @returns the referenced uniforms
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedUniformVariables() const;
/// Retrieves any referenced storagebuffer variables. Note, the variables
/// must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedStoragebufferVariables() const;
/// Retrieves any referenced regular Sampler variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedSamplerVariables() const;
/// Retrieves any referenced comparison Sampler variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedComparisonSamplerVariables() const;
/// Retrieves any referenced sampled textures variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced sampled textures
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedSampledTextureVariables() const;
/// Retrieves any referenced multisampled textures variables. Note, the
/// variables must be decorated with both binding and group decorations.
/// @returns the referenced sampled textures
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedMultisampledTextureVariables() const;
/// Retrieves any locally referenced builtin variables
/// @returns the <variable, decoration> pairs.
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
LocalReferencedBuiltinVariables() const;
/// Checks if the given entry point is an ancestor
/// @param sym the entry point symbol
/// @returns true if `sym` is an ancestor entry point of this function
bool HasAncestorEntryPoint(Symbol sym) const;
private:
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedSamplerVariablesImpl(type::SamplerKind kind) const;
const std::vector<std::pair<ast::Variable*, BindingInfo>>
ReferencedSampledTextureVariablesImpl(bool multisampled) const;
std::vector<ast::Variable*> const referenced_module_vars_;
std::vector<ast::Variable*> const local_referenced_module_vars_;
std::vector<Symbol> const ancestor_entry_points_;
};
} // namespace semantic
} // namespace tint
#endif // SRC_SEMANTIC_FUNCTION_H_

View File

@@ -0,0 +1,237 @@
// 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.
#include "src/semantic/function.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/group_decoration.h"
#include "src/ast/location_decoration.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decoration.h"
#include "src/type/multisampled_texture_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/type/texture_type.h"
TINT_INSTANTIATE_CLASS_ID(tint::semantic::Function);
namespace tint {
namespace semantic {
Function::Function(std::vector<ast::Variable*> referenced_module_vars,
std::vector<ast::Variable*> local_referenced_module_vars,
std::vector<Symbol> ancestor_entry_points)
: referenced_module_vars_(std::move(referenced_module_vars)),
local_referenced_module_vars_(std::move(local_referenced_module_vars)),
ancestor_entry_points_(std::move(ancestor_entry_points)) {}
Function::~Function() = default;
const std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>>
Function::ReferencedLocationVariables() const {
std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->decorations()) {
if (auto* location = deco->As<ast::LocationDecoration>()) {
ret.push_back({var, location});
break;
}
}
}
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedUniformVariables() const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
if (var->storage_class() != ast::StorageClass::kUniform) {
continue;
}
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* g = deco->As<ast::GroupDecoration>()) {
group = g;
}
}
if (binding == nullptr || group == nullptr) {
continue;
}
ret.push_back({var, BindingInfo{binding, group}});
}
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedStoragebufferVariables() const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
if (var->storage_class() != ast::StorageClass::kStorage) {
continue;
}
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::GroupDecoration>()) {
group = s;
}
}
if (binding == nullptr || group == nullptr) {
continue;
}
ret.push_back({var, BindingInfo{binding, group}});
}
return ret;
}
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
Function::ReferencedBuiltinVariables() const {
std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->decorations()) {
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
ret.push_back({var, builtin});
break;
}
}
}
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedSamplerVariables() const {
return ReferencedSamplerVariablesImpl(type::SamplerKind::kSampler);
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedComparisonSamplerVariables() const {
return ReferencedSamplerVariablesImpl(type::SamplerKind::kComparisonSampler);
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedSampledTextureVariables() const {
return ReferencedSampledTextureVariablesImpl(false);
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedMultisampledTextureVariables() const {
return ReferencedSampledTextureVariablesImpl(true);
}
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>>
Function::LocalReferencedBuiltinVariables() const {
std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> ret;
for (auto* var : LocalReferencedModuleVariables()) {
for (auto* deco : var->decorations()) {
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
ret.push_back({var, builtin});
break;
}
}
}
return ret;
}
bool Function::HasAncestorEntryPoint(Symbol symbol) const {
for (const auto& point : ancestor_entry_points_) {
if (point == symbol) {
return true;
}
}
return false;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->type()->UnwrapIfNeeded();
auto* sampler = unwrapped_type->As<type::Sampler>();
if (sampler == nullptr || sampler->kind() != kind) {
continue;
}
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
}
if (auto* s = deco->As<ast::GroupDecoration>()) {
group = s;
}
}
if (binding == nullptr || group == nullptr) {
continue;
}
ret.push_back({var, BindingInfo{binding, group}});
}
return ret;
}
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>>
Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->type()->UnwrapIfNeeded();
auto* texture = unwrapped_type->As<type::Texture>();
if (texture == nullptr) {
continue;
}
auto is_multisampled = texture->Is<type::MultisampledTexture>();
auto is_sampled = texture->Is<type::SampledTexture>();
if ((multisampled && !is_multisampled) || (!multisampled && !is_sampled)) {
continue;
}
ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b;
} else if (auto* s = deco->As<ast::GroupDecoration>()) {
group = s;
}
}
if (binding == nullptr || group == nullptr) {
continue;
}
ret.push_back({var, BindingInfo{binding, group}});
}
return ret;
}
} // namespace semantic
} // namespace tint

View File

@@ -0,0 +1,107 @@
// 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.
#include "src/semantic/function.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/location_decoration.h"
#include "src/semantic/test_helper.h"
namespace tint {
namespace semantic {
namespace {
using FunctionTest = TestHelper;
TEST_F(FunctionTest, GetReferenceLocations) {
auto* loc1 = Var("loc1", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::LocationDecoration>(0),
});
auto* loc2 = Var("loc2", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::LocationDecoration>(1),
});
auto* builtin1 =
Var("builtin1", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kPosition),
});
auto* builtin2 =
Var("builtin2", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kFragDepth),
});
auto* f = create<Function>(
/* referenced_module_vars */ std::vector<ast::Variable*>{loc1, builtin1,
loc2, builtin2},
/* local_referenced_module_vars */ std::vector<ast::Variable*>{},
/* ancestor_entry_points */ std::vector<Symbol>{});
ASSERT_EQ(f->ReferencedModuleVariables().size(), 4u);
auto ref_locs = f->ReferencedLocationVariables();
ASSERT_EQ(ref_locs.size(), 2u);
EXPECT_EQ(ref_locs[0].first, loc1);
EXPECT_EQ(ref_locs[0].second->value(), 0u);
EXPECT_EQ(ref_locs[1].first, loc2);
EXPECT_EQ(ref_locs[1].second->value(), 1u);
}
TEST_F(FunctionTest, GetReferenceBuiltins) {
auto* loc1 = Var("loc1", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::LocationDecoration>(0),
});
auto* loc2 = Var("loc2", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::LocationDecoration>(1),
});
auto* builtin1 =
Var("builtin1", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kPosition),
});
auto* builtin2 =
Var("builtin2", ast::StorageClass::kInput, ty.i32(), nullptr,
ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kFragDepth),
});
auto* f = create<Function>(
/* referenced_module_vars */ std::vector<ast::Variable*>{loc1, builtin1,
loc2, builtin2},
/* local_referenced_module_vars */ std::vector<ast::Variable*>{},
/* ancestor_entry_points */ std::vector<Symbol>{});
ASSERT_EQ(f->ReferencedModuleVariables().size(), 4u);
auto ref_locs = f->ReferencedBuiltinVariables();
ASSERT_EQ(ref_locs.size(), 2u);
EXPECT_EQ(ref_locs[0].first, builtin1);
EXPECT_EQ(ref_locs[0].second->value(), ast::Builtin::kPosition);
EXPECT_EQ(ref_locs[1].first, builtin2);
EXPECT_EQ(ref_locs[1].second->value(), ast::Builtin::kFragDepth);
}
} // namespace
} // namespace semantic
} // namespace tint

View File

@@ -0,0 +1,39 @@
// Copyright 2020 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_SEMANTIC_TEST_HELPER_H_
#define SRC_SEMANTIC_TEST_HELPER_H_
#include <memory>
#include <string>
#include <utility>
#include "gtest/gtest.h"
#include "src/program_builder.h"
namespace tint {
namespace semantic {
/// Helper class for testing
template <typename BASE>
class TestHelperBase : public BASE, public ProgramBuilder {};
using TestHelper = TestHelperBase<testing::Test>;
template <typename T>
using TestParamHelper = TestHelperBase<testing::TestWithParam<T>>;
} // namespace semantic
} // namespace tint
#endif // SRC_SEMANTIC_TEST_HELPER_H_

View File

@@ -23,12 +23,14 @@ namespace tint {
namespace ast {
class Expression;
class Function;
} // namespace ast
namespace semantic {
class Expression;
class Function;
/// TypeMappings is a struct that holds dummy `operator()` methods that's used
/// by SemanticNodeTypeFor to map AST node types to their corresponding semantic
@@ -38,6 +40,7 @@ class Expression;
struct TypeMappings {
//! @cond Doxygen_Suppress
semantic::Expression* operator()(ast::Expression*);
semantic::Function* operator()(ast::Function*);
//! @endcond
};