Skip to content

Commit

Permalink
[WGSL] Add support for accessing field of primitive structs at consta…
Browse files Browse the repository at this point in the history
…nt time

https://bugs.webkit.org/show_bug.cgi?id=264420
rdar://118126948

Reviewed by Mike Wyrzykowski.

While type checking, also compute the constant result of accessing a field from
a constant primitive struct (e.g. as produced by frexp)

* Source/WebGPU/WGSL/TypeCheck.cpp:
(WGSL::TypeChecker::visit):
* Source/WebGPU/WGSL/tests/valid/constants.wgsl:

Canonical link: https://commits.webkit.org/270439@main
  • Loading branch information
tadeuzagallo committed Nov 9, 2023
1 parent 9c5b697 commit 6b0bfc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Source/WebGPU/WGSL/TypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,10 @@ void TypeChecker::visit(AST::FieldAccessExpression& access)
typeError(access.span(), "struct '", *baseType, "' does not have a member called '", access.fieldName(), "'");
return nullptr;
}
if (auto constant = access.base().constantValue()) {
auto& constantStruct = std::get<ConstantStruct>(*constant);
access.setConstantValue(constantStruct.fields.get(access.fieldName().id()));
}
return primitiveStruct->values[*key];
}

Expand Down
7 changes: 7 additions & 0 deletions Source/WebGPU/WGSL/tests/valid/constants.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ fn testMixedConstantValue()
_ = x;
}

fn testPrimitiveStructAccess()
{
const f = frexp(1.25);
const fract = f.fract;
const exp = f.exp;
}

// Attribute constants
const group = 0;
const binding = 1;
Expand Down

0 comments on commit 6b0bfc8

Please sign in to comment.