From 381c92243cdde79aaee748e8b8c57aac87877b56 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Wed, 19 Apr 2023 01:54:54 -0700 Subject: [PATCH] [WGSL] Add typing for indexed access on vectors 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 --- Source/WebGPU/WGSL/TypeCheck.cpp | 9 ++++++++- Source/WebGPU/WGSL/tests/invalid/vector.wgsl | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 Source/WebGPU/WGSL/tests/invalid/vector.wgsl diff --git a/Source/WebGPU/WGSL/TypeCheck.cpp b/Source/WebGPU/WGSL/TypeCheck.cpp index 4eab76a20471..bd8d1e0aa379 100644 --- a/Source/WebGPU/WGSL/TypeCheck.cpp +++ b/Source/WebGPU/WGSL/TypeCheck.cpp @@ -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; } @@ -314,6 +314,13 @@ void TypeChecker::visit(AST::IndexAccessExpression& access) return; } + if (std::holds_alternative(*base)) { + // FIXME: check bounds if index is constant + auto& vector = std::get(*base); + inferred(vector.element); + return; + } + // FIXME: Implement reference and matrix accesses typeError(access.span(), "cannot index type '", *base, "'"); } diff --git a/Source/WebGPU/WGSL/tests/invalid/vector.wgsl b/Source/WebGPU/WGSL/tests/invalid/vector.wgsl new file mode 100644 index 000000000000..8e77966c5766 --- /dev/null +++ b/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]; +}