Skip to content

Commit

Permalink
[WGSL] Serialize global overrides
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255600
rdar://108197601

Reviewed by Myles C. Maxfield.

Add serialization support for override declarations. Notice that we temporarily
disable the mangling of globals, which ironically wasn't being used yet, but
requires notifying the API about the mangling. I will be implementing that next
and will re-enable the mangling.

* Source/WebGPU/WGSL/MangleNames.cpp:
(WGSL::NameManglerVisitor::visit):
* Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp:
(WGSL::Metal::FunctionDefinitionWriter::write):
(WGSL::Metal::FunctionDefinitionWriter::visit):
(WGSL::Metal::FunctionDefinitionWriter::visitGlobal):
(WGSL::Metal::FunctionDefinitionWriter::serializeVariable):

Canonical link: https://commits.webkit.org/263167@main
  • Loading branch information
tadeuzagallo committed Apr 20, 2023
1 parent f4b8a8d commit e8be4ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Source/WebGPU/WGSL/MangleNames.cpp
Expand Up @@ -158,9 +158,11 @@ void NameManglerVisitor::visit(AST::Structure& structure)
ASSERT_UNUSED(result, result.isNewEntry);
}

void NameManglerVisitor::visit(AST::Variable& variable)
void NameManglerVisitor::visit(AST::Variable&)
{
visitVariableDeclaration(variable, MangledName::Global);
// FIXME: we need to notify the API about these renames
// https://bugs.webkit.org/show_bug.cgi?id=250441
// visitVariableDeclaration(variable, MangledName::Global);
}

void NameManglerVisitor::visit(AST::VariableStatement& variable)
Expand Down
22 changes: 21 additions & 1 deletion Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp
Expand Up @@ -96,19 +96,24 @@ class FunctionDefinitionWriter : public AST::Visitor {

private:
void visit(const Type*);
void visitGlobal(AST::Variable&);
void serializeVariable(AST::Variable&);

StringBuilder& m_stringBuilder;
CallGraph& m_callGraph;
Indentation<4> m_indent { 0 };
std::optional<AST::StructureRole> m_structRole;
std::optional<AST::StageAttribute::Stage> m_entryPointStage;
std::optional<String> m_suffix;
unsigned m_functionConstantIndex { 0 };
};

void FunctionDefinitionWriter::write()
{
for (auto& structure : m_callGraph.ast().structures())
visit(structure);
for (auto& variable : m_callGraph.ast().variables())
visitGlobal(variable);
for (auto& entryPoint : m_callGraph.entrypoints())
visit(entryPoint.function);
}
Expand Down Expand Up @@ -180,6 +185,22 @@ void FunctionDefinitionWriter::visit(AST::Structure& structDecl)
}

void FunctionDefinitionWriter::visit(AST::Variable& variable)
{
serializeVariable(variable);
m_stringBuilder.append(";\n");
}

void FunctionDefinitionWriter::visitGlobal(AST::Variable& variable)
{
if (variable.flavor() != AST::VariableFlavor::Override)
return;

m_stringBuilder.append("constant ");
serializeVariable(variable);
m_stringBuilder.append(" [[function_constant(", m_functionConstantIndex++, ")]];\n");
}

void FunctionDefinitionWriter::serializeVariable(AST::Variable& variable)
{
if (variable.maybeTypeName())
visit(*variable.maybeTypeName());
Expand All @@ -193,7 +214,6 @@ void FunctionDefinitionWriter::visit(AST::Variable& variable)
m_stringBuilder.append(" = ");
visit(*variable.maybeInitializer());
}
m_stringBuilder.append(";\n");
}

void FunctionDefinitionWriter::visit(AST::Attribute& attribute)
Expand Down

0 comments on commit e8be4ea

Please sign in to comment.