From e90fdfdcdf8ca3d871a80896ba5e635b83c172e4 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Mon, 5 Jun 2023 01:52:32 -0700 Subject: [PATCH] [WGSL] Variables initializer shouldn't be a reference https://bugs.webkit.org/show_bug.cgi?id=257601 rdar://110105334 Reviewed by Mike Wyrzykowski. When initializing a variable, the value used to initialize it should be unpacked if it's a reference. E.g.: var a = 0; var b = a; b = 1; The above program should not change the value of `a`, and references to `b` should have type `ref`, not `ref>`. In order to fix that, when we infer the type of a variable initializer (which doesn't have an explicit type annotation), if the is a reference (as it would be for `a` in the example above, which would have type `ref`), we should use the referenced type instead (i.e. `i32` in this example) * Source/WebGPU/WGSL/TypeCheck.cpp: (WGSL::TypeChecker::visitVariable): Canonical link: https://commits.webkit.org/264860@main --- Source/WebGPU/WGSL/TypeCheck.cpp | 5 +++++ .../tests/valid/var-initialization-with-var.wgsl | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Source/WebGPU/WGSL/tests/valid/var-initialization-with-var.wgsl diff --git a/Source/WebGPU/WGSL/TypeCheck.cpp b/Source/WebGPU/WGSL/TypeCheck.cpp index ff1e33694f326..7c8ba09b4fd16 100644 --- a/Source/WebGPU/WGSL/TypeCheck.cpp +++ b/Source/WebGPU/WGSL/TypeCheck.cpp @@ -226,6 +226,11 @@ void TypeChecker::visitVariable(AST::Variable& variable, VariableKind variableKi result = resolve(*variable.maybeTypeName()); if (variable.maybeInitializer()) { auto* initializerType = infer(*variable.maybeInitializer()); + if (auto* reference = std::get_if(initializerType)) { + initializerType = reference->element; + variable.maybeInitializer()->m_inferredType = initializerType; + } + if (!result) result = initializerType; else if (unify(result, initializerType)) diff --git a/Source/WebGPU/WGSL/tests/valid/var-initialization-with-var.wgsl b/Source/WebGPU/WGSL/tests/valid/var-initialization-with-var.wgsl new file mode 100644 index 0000000000000..e8b84bfb2bf91 --- /dev/null +++ b/Source/WebGPU/WGSL/tests/valid/var-initialization-with-var.wgsl @@ -0,0 +1,15 @@ +// RUN: %metal main 2>&1 | %check + +fn f(x: f32) { } + +@compute @workgroup_size(1) +fn main() { + // CHECK: float local\d+ = 1 + var a = 1.0; + // CHECK: float local\d+ = local\d + var b = a; + // CHECK: local\d+ = 0 + b = 0.0; + // CHECK: f\(local\d+\) + _ = f(b); +}