Skip to content

Commit

Permalink
[WGSL] Variables initializer shouldn't be a reference
Browse files Browse the repository at this point in the history
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<i32>`, not `ref<ref<i32>>`. 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<i32>`), 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
  • Loading branch information
tadeuzagallo committed Jun 5, 2023
1 parent 4eb6767 commit e90fdfd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Source/WebGPU/WGSL/TypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Types::Reference>(initializerType)) {
initializerType = reference->element;
variable.maybeInitializer()->m_inferredType = initializerType;
}

if (!result)
result = initializerType;
else if (unify(result, initializerType))
Expand Down
15 changes: 15 additions & 0 deletions Source/WebGPU/WGSL/tests/valid/var-initialization-with-var.wgsl
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit e90fdfd

Please sign in to comment.