diff options
author | Alon Zakai <azakai@google.com> | 2023-01-06 09:46:59 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-06 09:46:59 -0800 |
commit | 73a1cfcacd028ed7aefe304c2e140cda4068dcb0 (patch) | |
tree | fdeefcc7e054af52546609bcdcf2b5874c4ee492 /src/passes/OptimizeInstructions.cpp | |
parent | ad38ddefb3728aaef0df69bd265412a38bcfd20d (diff) | |
download | binaryen-73a1cfcacd028ed7aefe304c2e140cda4068dcb0.tar.gz binaryen-73a1cfcacd028ed7aefe304c2e140cda4068dcb0.tar.bz2 binaryen-73a1cfcacd028ed7aefe304c2e140cda4068dcb0.zip |
[Wasm GC] Fix optimizations on ref.cast of null, and optimize to ref.as_non_null (#5398)
We were checking the heap type, but now casts care about the nullability as
well.
If the nullability is the only problem, that is, the heap type will be fine but we
might have a null, we can at least switch a ref.cast (non-null) to a
ref.as_non_null.
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 916991ecb..ed99705ae 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1963,7 +1963,7 @@ struct OptimizeInstructions } // Check whether the cast will definitely succeed. - if (HeapType::isSubType(curr->ref->type.getHeapType(), intendedType)) { + if (Type::isSubType(curr->ref->type, curr->type)) { replaceCurrent(curr->ref); // We must refinalize here, as we may be returning a more specific @@ -1987,6 +1987,24 @@ struct OptimizeInstructions return; } + // The cast will not definitely succeed, but perhaps the heap type part of + // the cast will, at least. That would leave only nullability as an issue, + // that is, this means that the input ref is nullable but we are casting to + // non-null. + if (HeapType::isSubType(curr->ref->type.getHeapType(), intendedType)) { + assert(curr->ref->type.isNullable()); + assert(curr->type.isNonNullable()); + + // Given the heap type will cast ok, all we need to do is check for a null + // here. + // + // As above, we must refinalize as we may now be emitting a more refined + // type (specifically a more refined heap type). + replaceCurrent(Builder(*getModule()).makeRefAs(RefAsNonNull, curr->ref)); + refinalize = true; + return; + } + // Repeated identical ref.cast operations are unnecessary. First, find the // immediate child cast, if there is one. // TODO: Look even further through incompatible casts? |