diff options
author | Alon Zakai <azakai@google.com> | 2021-04-21 12:40:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-21 12:40:51 -0700 |
commit | a72f5b7f440a73c23b7f1b0532446584d4da0854 (patch) | |
tree | 6043f4c06c2cb358eb940dfbfc614c0f177069d8 | |
parent | b4e96d8dd6fded302b7c6be8a34b0a75773737b6 (diff) | |
download | binaryen-a72f5b7f440a73c23b7f1b0532446584d4da0854.tar.gz binaryen-a72f5b7f440a73c23b7f1b0532446584d4da0854.tar.bz2 binaryen-a72f5b7f440a73c23b7f1b0532446584d4da0854.zip |
[Wasm GC] Fix handleNonDefaultableLocals on tees (#3830)
When we change a local's type, as we do in that method when we
turn a non-nullable (invalid) local into a nullable (valid) one, we must
update tees as well as gets - their type must match the changed
local type, and we must cast them so that their users do not see
a change.
-rw-r--r-- | src/ir/type-updating.cpp | 23 | ||||
-rw-r--r-- | test/lit/passes/dae-gc.wast | 49 |
2 files changed, 71 insertions, 1 deletions
diff --git a/src/ir/type-updating.cpp b/src/ir/type-updating.cpp index d909bffb4..658fabc83 100644 --- a/src/ir/type-updating.cpp +++ b/src/ir/type-updating.cpp @@ -39,6 +39,7 @@ void handleNonDefaultableLocals(Function* func, Module& wasm) { if (!hasNonNullable) { return; } + // Rewrite the local.gets. Builder builder(wasm); for (auto** getp : FindAllPointers<LocalGet>(func->body).list) { @@ -56,8 +57,28 @@ void handleNonDefaultableLocals(Function* func, Module& wasm) { } } + // Update tees, whose type must match the local (if the wasm spec changes for + // the type to be that of the value, then this can be removed). + for (auto** setp : FindAllPointers<LocalSet>(func->body).list) { + auto* set = (*setp)->cast<LocalSet>(); + if (!func->isVar(set->index)) { + // We do not need to process params, which can legally be non-nullable. + continue; + } + // Non-tees do not change, and unreachable tees can be ignored here as their + // type is unreachable anyhow. + if (!set->isTee() || set->type == Type::unreachable) { + continue; + } + auto type = func->getLocalType(set->index); + if (type.isRef() && !type.isNullable()) { + set->type = Type(type.getHeapType(), Nullable); + *setp = builder.makeRefAs(RefAsNonNull, set); + } + } + // Rewrite the types of the function's vars (which we can do now, after we - // are done using them to know which local.gets to fix). + // are done using them to know which local.gets etc to fix). for (auto& type : func->vars) { if (type.isRef() && !type.isNullable()) { type = Type(type.getHeapType(), Nullable); diff --git a/test/lit/passes/dae-gc.wast b/test/lit/passes/dae-gc.wast new file mode 100644 index 000000000..87e480cc8 --- /dev/null +++ b/test/lit/passes/dae-gc.wast @@ -0,0 +1,49 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s -all --dae -S -o - | filecheck %s + +(module + ;; CHECK: (func $foo + ;; CHECK-NEXT: (call $bar) + ;; CHECK-NEXT: ) + (func $foo + (call $bar + (i31.new + (i32.const 1) + ) + ) + ) + ;; CHECK: (func $bar + ;; CHECK-NEXT: (local $0 (ref null i31)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.as_non_null + ;; CHECK-NEXT: (local.tee $0 + ;; CHECK-NEXT: (i31.new + ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.tee $0 + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $bar (param $0 i31ref) + (drop + ;; after the parameter is removed, we create a nullable local to replace it, + ;; and must update the tee's type accordingly to avoid a validation error, + ;; and also add a ref.as_non_null so that the outside still receives the + ;; same type as before + (local.tee $0 + (i31.new + (i32.const 2) + ) + ) + ) + ;; test for an unreachable tee, whose type must be unreachable even after + ;; the change (the tee would need to be dropped if it were not unreachable, + ;; so the correctness in this case is visible in the output) + (local.tee $0 + (unreachable) + ) + ) +) |