From e8be4ea461d38fbc0c474eae04c13a20552e16d8 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Thu, 20 Apr 2023 01:03:56 -0700 Subject: [PATCH] [WGSL] Serialize global overrides 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 --- Source/WebGPU/WGSL/MangleNames.cpp | 6 +++-- .../WebGPU/WGSL/Metal/MetalFunctionWriter.cpp | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Source/WebGPU/WGSL/MangleNames.cpp b/Source/WebGPU/WGSL/MangleNames.cpp index 4e0abb4d549c..0e29f61364a9 100644 --- a/Source/WebGPU/WGSL/MangleNames.cpp +++ b/Source/WebGPU/WGSL/MangleNames.cpp @@ -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) diff --git a/Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp b/Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp index 2e4f2cb28abd..5802a40535d3 100644 --- a/Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp +++ b/Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp @@ -96,6 +96,8 @@ class FunctionDefinitionWriter : public AST::Visitor { private: void visit(const Type*); + void visitGlobal(AST::Variable&); + void serializeVariable(AST::Variable&); StringBuilder& m_stringBuilder; CallGraph& m_callGraph; @@ -103,12 +105,15 @@ class FunctionDefinitionWriter : public AST::Visitor { std::optional m_structRole; std::optional m_entryPointStage; std::optional 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); } @@ -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()); @@ -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)