Skip to content

Commit

Permalink
[WGSL] Add typing for indexed access on vectors
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255598
rdar://108197306

Reviewed by Myles C. Maxfield.

So far we only supported field access on vectors (e.g. vec2.x) but not indexed
access (e.g. vec2[0]). This extends the typing to add support for that. Just
like our current support for indexed access on arrays, we still don't validate
if the access is out-of-bounds when the index is a constant.

* Source/WebGPU/WGSL/TypeCheck.cpp:
(WGSL::TypeChecker::visit):
* Source/WebGPU/WGSL/tests/invalid/vector.wgsl: Added.

Canonical link: https://commits.webkit.org/263122@main
  • Loading branch information
tadeuzagallo committed Apr 19, 2023
1 parent f6aa450 commit 381c922
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Source/WebGPU/WGSL/TypeCheck.cpp
Expand Up @@ -302,7 +302,7 @@ void TypeChecker::visit(AST::IndexAccessExpression& access)
return;
}

if (!unify(index, m_types.i32Type()) && !unify(index, m_types.u32Type()) && !unify(index, m_types.abstractIntType())) {
if (!unify(m_types.i32Type(), index) && !unify(m_types.u32Type(), index) && !unify(m_types.abstractIntType(), index)) {
typeError(access.span(), "index must be of type 'i32' or 'u32', found: '", *index, "'");
return;
}
Expand All @@ -314,6 +314,13 @@ void TypeChecker::visit(AST::IndexAccessExpression& access)
return;
}

if (std::holds_alternative<Types::Vector>(*base)) {
// FIXME: check bounds if index is constant
auto& vector = std::get<Types::Vector>(*base);
inferred(vector.element);
return;
}

// FIXME: Implement reference and matrix accesses
typeError(access.span(), "cannot index type '", *base, "'");
}
Expand Down
4 changes: 4 additions & 0 deletions Source/WebGPU/WGSL/tests/invalid/vector.wgsl
@@ -0,0 +1,4 @@
fn testIndexAccess() {
// CHECK-L: index must be of type 'i32' or 'u32', found: 'f32'
let x1 = vec2(0)[1f];
}

0 comments on commit 381c922

Please sign in to comment.