Skip to content

Commit 2459e14

Browse files
committed
[WGSL] Field access should support pointers
https://bugs.webkit.org/show_bug.cgi?id=272838 rdar://126621076 Reviewed by Mike Wyrzykowski. The WGSL spec was updated to support struct field access on pointers (e.g. (&x).y), and while the type checker was updated to support it, there were a couple other changes necessary: - the renamer/mangler did not correctly rename the field when accessing a pointer - the global rewriter asserted that the base should be either a reference or a struct - the code generator was not emitting the correct code (i.e. x->y instead of x.y) * Source/WebGPU/WGSL/GlobalVariableRewriter.cpp: (WGSL::RewriteGlobalVariables::getPacking): * Source/WebGPU/WGSL/MangleNames.cpp: (WGSL::NameManglerVisitor::visit): * Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp: (WGSL::Metal::FunctionDefinitionWriter::visit): * Source/WebGPU/WGSL/tests/valid/access-expression.wgsl: Canonical link: https://commits.webkit.org/277665@main
1 parent 9b709d7 commit 2459e14

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

Source/WebGPU/WGSL/GlobalVariableRewriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ Packing RewriteGlobalVariables::getPacking(AST::CallExpression& call)
474474
auto* type = base.inferredType();
475475
if (auto* reference = std::get_if<Types::Reference>(type))
476476
type = reference->element;
477+
if (auto* pointer = std::get_if<Types::Pointer>(type))
478+
type = pointer->element;
477479
auto& structure = std::get<Types::Struct>(*type).structure;
478480
auto& lastMember = structure.members().last();
479481
RELEASE_ASSERT(lastMember.name().id() == fieldAccess->fieldName().id());

Source/WebGPU/WGSL/MangleNames.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ void NameManglerVisitor::visit(AST::FieldAccessExpression& access)
163163
auto* baseType = access.base().inferredType();
164164
if (auto* reference = std::get_if<Types::Reference>(baseType))
165165
baseType = reference->element;
166+
if (auto* pointer = std::get_if<Types::Pointer>(baseType))
167+
baseType = pointer->element;
166168
auto* structType = std::get_if<Types::Struct>(baseType);
167169
if (!structType)
168170
return;

Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,12 @@ void FunctionDefinitionWriter::visit(AST::IdentifierExpression& identifier)
20802080
void FunctionDefinitionWriter::visit(AST::FieldAccessExpression& access)
20812081
{
20822082
visit(access.base());
2083-
m_stringBuilder.append(".", access.fieldName());
2083+
auto* baseType = access.base().inferredType();
2084+
if (baseType && std::holds_alternative<Types::Pointer>(*baseType))
2085+
m_stringBuilder.append("->");
2086+
else
2087+
m_stringBuilder.append(".");
2088+
m_stringBuilder.append(access.fieldName());
20842089
}
20852090

20862091
void FunctionDefinitionWriter::visit(AST::BoolLiteral& literal)

Source/WebGPU/WGSL/tests/valid/access-expression.wgsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,9 @@ fn testStructAccessExpression()
227227
var s: S;
228228
let x: i32 = s.x;
229229
s.x = 0;
230+
231+
{
232+
let s = &s;
233+
s.x = 0;
234+
}
230235
}

0 commit comments

Comments
 (0)