mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-10 05:57:51 +00:00
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:
committed by
Commit Bot service account
parent
c694d43c75
commit
87c78ddabc
@@ -65,161 +65,6 @@ PipelineStage Function::pipeline_stage() const {
|
||||
return PipelineStage::kNone;
|
||||
}
|
||||
|
||||
void Function::add_referenced_module_variable(Variable* var) {
|
||||
for (const auto* v : referenced_module_vars_) {
|
||||
if (v->symbol() == var->symbol()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
referenced_module_vars_.push_back(var);
|
||||
}
|
||||
|
||||
void Function::add_local_referenced_module_variable(Variable* var) {
|
||||
for (const auto* v : local_referenced_module_vars_) {
|
||||
if (v->symbol() == var->symbol()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
local_referenced_module_vars_.push_back(var);
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, LocationDecoration*>>
|
||||
Function::referenced_location_variables() const {
|
||||
std::vector<std::pair<Variable*, LocationDecoration*>> ret;
|
||||
|
||||
for (auto* var : referenced_module_variables()) {
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* location = deco->As<LocationDecoration>()) {
|
||||
ret.push_back({var, location});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::referenced_uniform_variables() const {
|
||||
std::vector<std::pair<Variable*, Function::BindingInfo>> ret;
|
||||
|
||||
for (auto* var : referenced_module_variables()) {
|
||||
if (var->storage_class() != StorageClass::kUniform) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BindingDecoration* binding = nullptr;
|
||||
GroupDecoration* group = nullptr;
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* b = deco->As<BindingDecoration>()) {
|
||||
binding = b;
|
||||
} else if (auto* g = deco->As<GroupDecoration>()) {
|
||||
group = g;
|
||||
}
|
||||
}
|
||||
if (binding == nullptr || group == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.push_back({var, BindingInfo{binding, group}});
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::referenced_storagebuffer_variables() const {
|
||||
std::vector<std::pair<Variable*, Function::BindingInfo>> ret;
|
||||
|
||||
for (auto* var : referenced_module_variables()) {
|
||||
if (var->storage_class() != StorageClass::kStorage) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BindingDecoration* binding = nullptr;
|
||||
GroupDecoration* group = nullptr;
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* b = deco->As<BindingDecoration>()) {
|
||||
binding = b;
|
||||
} else if (auto* s = deco->As<GroupDecoration>()) {
|
||||
group = s;
|
||||
}
|
||||
}
|
||||
if (binding == nullptr || group == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.push_back({var, BindingInfo{binding, group}});
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, BuiltinDecoration*>>
|
||||
Function::referenced_builtin_variables() const {
|
||||
std::vector<std::pair<Variable*, BuiltinDecoration*>> ret;
|
||||
|
||||
for (auto* var : referenced_module_variables()) {
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* builtin = deco->As<BuiltinDecoration>()) {
|
||||
ret.push_back({var, builtin});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::referenced_sampler_variables() const {
|
||||
return ReferencedSamplerVariablesImpl(type::SamplerKind::kSampler);
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::referenced_comparison_sampler_variables() const {
|
||||
return ReferencedSamplerVariablesImpl(type::SamplerKind::kComparisonSampler);
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::referenced_sampled_texture_variables() const {
|
||||
return ReferencedSampledTextureVariablesImpl(false);
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::referenced_multisampled_texture_variables() const {
|
||||
return ReferencedSampledTextureVariablesImpl(true);
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, BuiltinDecoration*>>
|
||||
Function::local_referenced_builtin_variables() const {
|
||||
std::vector<std::pair<Variable*, BuiltinDecoration*>> ret;
|
||||
|
||||
for (auto* var : local_referenced_module_variables()) {
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* builtin = deco->As<BuiltinDecoration>()) {
|
||||
ret.push_back({var, builtin});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Function::add_ancestor_entry_point(Symbol ep) {
|
||||
for (const auto& point : ancestor_entry_points_) {
|
||||
if (point == ep) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
ancestor_entry_points_.push_back(ep);
|
||||
}
|
||||
|
||||
bool Function::HasAncestorEntryPoint(Symbol symbol) const {
|
||||
for (const auto& point : ancestor_entry_points_) {
|
||||
if (point == symbol) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const Statement* Function::get_last_statement() const {
|
||||
return body_->last();
|
||||
}
|
||||
@@ -295,73 +140,6 @@ std::string Function::type_name() const {
|
||||
return out.str();
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
|
||||
std::vector<std::pair<Variable*, Function::BindingInfo>> ret;
|
||||
|
||||
for (auto* var : referenced_module_variables()) {
|
||||
auto* unwrapped_type = var->type()->UnwrapIfNeeded();
|
||||
auto* sampler = unwrapped_type->As<type::Sampler>();
|
||||
if (sampler == nullptr || sampler->kind() != kind) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BindingDecoration* binding = nullptr;
|
||||
GroupDecoration* group = nullptr;
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* b = deco->As<BindingDecoration>()) {
|
||||
binding = b;
|
||||
}
|
||||
if (auto* s = deco->As<GroupDecoration>()) {
|
||||
group = s;
|
||||
}
|
||||
}
|
||||
if (binding == nullptr || group == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.push_back({var, BindingInfo{binding, group}});
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
|
||||
std::vector<std::pair<Variable*, Function::BindingInfo>> ret;
|
||||
|
||||
for (auto* var : referenced_module_variables()) {
|
||||
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;
|
||||
}
|
||||
|
||||
BindingDecoration* binding = nullptr;
|
||||
GroupDecoration* group = nullptr;
|
||||
for (auto* deco : var->decorations()) {
|
||||
if (auto* b = deco->As<BindingDecoration>()) {
|
||||
binding = b;
|
||||
} else if (auto* s = deco->As<GroupDecoration>()) {
|
||||
group = s;
|
||||
}
|
||||
}
|
||||
if (binding == nullptr || group == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.push_back({var, BindingInfo{binding, group}});
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Function* FunctionList::Find(Symbol sym) const {
|
||||
for (auto* func : *this) {
|
||||
if (func->symbol() == sym) {
|
||||
|
||||
@@ -43,14 +43,6 @@ namespace ast {
|
||||
/// A Function statement.
|
||||
class Function : public Castable<Function, Node> {
|
||||
public:
|
||||
/// Information about a binding
|
||||
struct BindingInfo {
|
||||
/// The binding decoration
|
||||
BindingDecoration* binding = nullptr;
|
||||
/// The group decoration
|
||||
GroupDecoration* group = nullptr;
|
||||
};
|
||||
|
||||
/// Create a function
|
||||
/// @param source the variable source
|
||||
/// @param symbol the function symbol
|
||||
@@ -87,82 +79,9 @@ class Function : public Castable<Function, Node> {
|
||||
/// @returns true if this function is an entry point
|
||||
bool IsEntryPoint() const { return pipeline_stage() != PipelineStage::kNone; }
|
||||
|
||||
/// Adds the given variable to the list of referenced module variables if it
|
||||
/// is not already included.
|
||||
/// @param var the module variable to add
|
||||
void add_referenced_module_variable(Variable* var);
|
||||
/// Adds the given variable to the list of locally referenced module variables
|
||||
/// if it is not already included.
|
||||
/// @param var the module variable to add
|
||||
void add_local_referenced_module_variable(Variable* var);
|
||||
/// 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<Variable*>& referenced_module_variables() const {
|
||||
return referenced_module_vars_;
|
||||
}
|
||||
/// @returns the locally referenced module variables
|
||||
const std::vector<Variable*>& local_referenced_module_variables() const {
|
||||
return local_referenced_module_vars_;
|
||||
}
|
||||
/// Retrieves any referenced location variables
|
||||
/// @returns the <variable, decoration> pair.
|
||||
const std::vector<std::pair<Variable*, LocationDecoration*>>
|
||||
referenced_location_variables() const;
|
||||
/// Retrieves any referenced builtin variables
|
||||
/// @returns the <variable, decoration> pair.
|
||||
const std::vector<std::pair<Variable*, BuiltinDecoration*>>
|
||||
referenced_builtin_variables() 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<Variable*, Function::BindingInfo>>
|
||||
referenced_uniform_variables() 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<Variable*, Function::BindingInfo>>
|
||||
referenced_storagebuffer_variables() 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<Variable*, Function::BindingInfo>>
|
||||
referenced_sampler_variables() 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<Variable*, Function::BindingInfo>>
|
||||
referenced_comparison_sampler_variables() 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<Variable*, Function::BindingInfo>>
|
||||
referenced_sampled_texture_variables() 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<Variable*, Function::BindingInfo>>
|
||||
referenced_multisampled_texture_variables() const;
|
||||
|
||||
/// Retrieves any locally referenced builtin variables
|
||||
/// @returns the <variable, decoration> pairs.
|
||||
const std::vector<std::pair<Variable*, BuiltinDecoration*>>
|
||||
local_referenced_builtin_variables() const;
|
||||
|
||||
/// Adds an ancestor entry point
|
||||
/// @param ep the entry point ancestor
|
||||
void add_ancestor_entry_point(Symbol ep);
|
||||
/// @returns the ancestor entry points
|
||||
const std::vector<Symbol>& ancestor_entry_points() const {
|
||||
return ancestor_entry_points_;
|
||||
}
|
||||
/// 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;
|
||||
|
||||
/// @returns the function return type.
|
||||
type::Type* return_type() const { return return_type_; }
|
||||
|
||||
/// @returns a pointer to the last statement of the function or nullptr if
|
||||
// function is empty
|
||||
const Statement* get_last_statement() const;
|
||||
@@ -196,20 +115,12 @@ class Function : public Castable<Function, Node> {
|
||||
|
||||
private:
|
||||
Function(const Function&) = delete;
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
ReferencedSamplerVariablesImpl(type::SamplerKind kind) const;
|
||||
const std::vector<std::pair<Variable*, Function::BindingInfo>>
|
||||
ReferencedSampledTextureVariablesImpl(bool multisampled) const;
|
||||
|
||||
Symbol const symbol_;
|
||||
VariableList const params_;
|
||||
type::Type* const return_type_;
|
||||
BlockStatement* const body_;
|
||||
|
||||
std::vector<Variable*> referenced_module_vars_; // Semantic info
|
||||
std::vector<Variable*> local_referenced_module_vars_; // Semantic info
|
||||
std::vector<Symbol> ancestor_entry_points_; // Semantic info
|
||||
FunctionDecorationList decorations_; // Semantic info
|
||||
FunctionDecorationList const decorations_;
|
||||
};
|
||||
|
||||
/// A list of functions
|
||||
|
||||
@@ -53,114 +53,6 @@ TEST_F(FunctionTest, Creation_WithSource) {
|
||||
EXPECT_EQ(src.range.begin.column, 2u);
|
||||
}
|
||||
|
||||
TEST_F(FunctionTest, AddDuplicateReferencedVariables) {
|
||||
auto* v = Var("var", StorageClass::kInput, ty.i32());
|
||||
auto* f = Func("func", VariableList{}, ty.void_(), StatementList{},
|
||||
FunctionDecorationList{});
|
||||
|
||||
f->add_referenced_module_variable(v);
|
||||
ASSERT_EQ(f->referenced_module_variables().size(), 1u);
|
||||
EXPECT_EQ(f->referenced_module_variables()[0], v);
|
||||
|
||||
f->add_referenced_module_variable(v);
|
||||
ASSERT_EQ(f->referenced_module_variables().size(), 1u);
|
||||
|
||||
auto* v2 = Var("var2", StorageClass::kOutput, ty.i32());
|
||||
f->add_referenced_module_variable(v2);
|
||||
ASSERT_EQ(f->referenced_module_variables().size(), 2u);
|
||||
EXPECT_EQ(f->referenced_module_variables()[1], v2);
|
||||
}
|
||||
|
||||
TEST_F(FunctionTest, GetReferenceLocations) {
|
||||
auto* loc1 = Var("loc1", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<LocationDecoration>(0),
|
||||
});
|
||||
|
||||
auto* loc2 = Var("loc2", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<LocationDecoration>(1),
|
||||
});
|
||||
|
||||
auto* builtin1 = Var("builtin1", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<BuiltinDecoration>(Builtin::kPosition),
|
||||
});
|
||||
|
||||
auto* builtin2 = Var("builtin2", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<BuiltinDecoration>(Builtin::kFragDepth),
|
||||
});
|
||||
|
||||
auto* f = Func("func", VariableList{}, ty.void_(), StatementList{},
|
||||
FunctionDecorationList{});
|
||||
|
||||
f->add_referenced_module_variable(loc1);
|
||||
f->add_referenced_module_variable(builtin1);
|
||||
f->add_referenced_module_variable(loc2);
|
||||
f->add_referenced_module_variable(builtin2);
|
||||
ASSERT_EQ(f->referenced_module_variables().size(), 4u);
|
||||
|
||||
auto ref_locs = f->referenced_location_variables();
|
||||
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", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<LocationDecoration>(0),
|
||||
});
|
||||
|
||||
auto* loc2 = Var("loc2", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<LocationDecoration>(1),
|
||||
});
|
||||
|
||||
auto* builtin1 = Var("builtin1", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<BuiltinDecoration>(Builtin::kPosition),
|
||||
});
|
||||
|
||||
auto* builtin2 = Var("builtin2", StorageClass::kInput, ty.i32(), nullptr,
|
||||
ast::VariableDecorationList{
|
||||
create<BuiltinDecoration>(Builtin::kFragDepth),
|
||||
});
|
||||
|
||||
auto* f = Func("func", VariableList{}, ty.void_(), StatementList{},
|
||||
FunctionDecorationList{});
|
||||
|
||||
f->add_referenced_module_variable(loc1);
|
||||
f->add_referenced_module_variable(builtin1);
|
||||
f->add_referenced_module_variable(loc2);
|
||||
f->add_referenced_module_variable(builtin2);
|
||||
ASSERT_EQ(f->referenced_module_variables().size(), 4u);
|
||||
|
||||
auto ref_locs = f->referenced_builtin_variables();
|
||||
ASSERT_EQ(ref_locs.size(), 2u);
|
||||
EXPECT_EQ(ref_locs[0].first, builtin1);
|
||||
EXPECT_EQ(ref_locs[0].second->value(), Builtin::kPosition);
|
||||
EXPECT_EQ(ref_locs[1].first, builtin2);
|
||||
EXPECT_EQ(ref_locs[1].second->value(), Builtin::kFragDepth);
|
||||
}
|
||||
|
||||
TEST_F(FunctionTest, AddDuplicateEntryPoints) {
|
||||
auto* f = Func("func", VariableList{}, ty.void_(), StatementList{},
|
||||
FunctionDecorationList{});
|
||||
|
||||
auto main_sym = Symbols().Get("main");
|
||||
f->add_ancestor_entry_point(main_sym);
|
||||
ASSERT_EQ(1u, f->ancestor_entry_points().size());
|
||||
EXPECT_EQ(main_sym, f->ancestor_entry_points()[0]);
|
||||
|
||||
f->add_ancestor_entry_point(main_sym);
|
||||
ASSERT_EQ(1u, f->ancestor_entry_points().size());
|
||||
EXPECT_EQ(main_sym, f->ancestor_entry_points()[0]);
|
||||
}
|
||||
|
||||
TEST_F(FunctionTest, IsValid) {
|
||||
VariableList params;
|
||||
params.push_back(Var("var", StorageClass::kNone, ty.i32()));
|
||||
|
||||
Reference in New Issue
Block a user