Skip to content

Commit

Permalink
[WGSL] Do not pack structs unnecessarily
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=262629
rdar://116468528

Reviewed by Mike Wyrzykowski.

Currently we just pack all structs that are used by global resources, which generates
unnecessarily large code and calls to pack/unpack. Instead, check if the struct actually
contains any members that needs to be packed, and skip packing if it doesn't.

* Source/WebGPU/WGSL/GlobalVariableRewriter.cpp:
(WGSL::RewriteGlobalVariables::packStructResource):
(WGSL::RewriteGlobalVariables::packType):
(WGSL::RewriteGlobalVariables::packStructType):

Canonical link: https://commits.webkit.org/268979@main
  • Loading branch information
tadeuzagallo committed Oct 6, 2023
1 parent 62439d6 commit d38a772
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Source/WebGPU/WGSL/GlobalVariableRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,9 @@ void RewriteGlobalVariables::packResource(AST::Variable& global)
void RewriteGlobalVariables::packStructResource(AST::Variable& global, const Types::Struct* structType)
{
const Type* packedStructType = packStructType(structType);
if (!packedStructType)
return;

auto& packedType = m_callGraph.ast().astBuilder().construct<AST::IdentifierExpression>(
SourceSpan::empty(),
AST::Identifier::make(std::get<Types::Struct>(*packedStructType).structure.name().id())
Expand Down Expand Up @@ -564,6 +567,10 @@ const Type* RewriteGlobalVariables::packType(const Type* type)
return packStructType(structType);
if (auto* arrayType = std::get_if<Types::Array>(type))
return packArrayType(arrayType);
if (auto* vectorType = std::get_if<Types::Vector>(type)) {
if (vectorType->size == 3)
return type;
}
return nullptr;
}

Expand All @@ -574,15 +581,20 @@ const Type* RewriteGlobalVariables::packStructType(const Types::Struct* structTy

m_callGraph.ast().setUsesPackedStructs();

// Ensure we pack nested structs
bool packedAnyMember = false;
for (auto& member : structType->structure.members()) {
if (packType(member.type().inferredType()))
packedAnyMember = true;
}
if (!packedAnyMember)
return nullptr;

ASSERT(structType->structure.role() == AST::StructureRole::UserDefined);
m_callGraph.ast().replace(&structType->structure.role(), AST::StructureRole::UserDefinedResource);

String packedStructName = makeString("__", structType->structure.name(), "_Packed");

// Ensure we pack nested structs
for (auto& member : structType->structure.members())
packType(member.type().inferredType());

auto& packedStruct = m_callGraph.ast().astBuilder().construct<AST::Structure>(
SourceSpan::empty(),
AST::Identifier::make(packedStructName),
Expand Down

0 comments on commit d38a772

Please sign in to comment.