Skip to content

Account for exactness when finding the most refined fallthrough #7544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/test/fuzzing.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
'exact-references-lowering.wast',
'exact-casts.wast',
'exact-casts-trivial.wast',
'optimize-instructions-exact.wast',
'optimize-instructions-all-casts-exact.wast',
'local-subtyping-exact.wast',
'remove-unused-types-exact.wast',
Expand Down
20 changes: 12 additions & 8 deletions src/ir/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ inline Expression** getMostRefinedFallthrough(Expression** currp,
return currp;
}
auto bestType = curr->type.getHeapType();
auto bestExactness = curr->type.getExactness();
auto bestNullability = curr->type.getNullability();
auto** bestp = currp;
while (1) {
Expand All @@ -412,20 +413,23 @@ inline Expression** getMostRefinedFallthrough(Expression** currp,
}
assert(next->type.isRef());
auto nextType = next->type.getHeapType();
auto nextExactness = next->type.getExactness();
auto nextNullability = next->type.getNullability();
if (nextType == bestType) {
if (nextType == bestType && nextExactness == bestExactness) {
// Heap types match: refine nullability if possible.
if (bestNullability == Nullable && nextNullability == NonNullable) {
bestp = nextp;
bestNullability = NonNullable;
}
} else {
// Refine heap type if possible, resetting nullability.
if (HeapType::isSubType(nextType, bestType)) {
bestp = nextp;
bestNullability = nextNullability;
bestType = nextType;
}
} else if ((nextType != bestType &&
HeapType::isSubType(nextType, bestType)) ||
(nextType == bestType && nextExactness == Exact &&
bestExactness == Inexact)) {
// Refine heap, resetting nullability.
bestp = nextp;
bestType = nextType;
bestExactness = nextExactness;
bestNullability = nextNullability;
}
currp = nextp;
}
Expand Down
66 changes: 66 additions & 0 deletions test/lit/passes/optimize-instructions-exact.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.

;; RUN: wasm-opt %s -all --disable-custom-descriptors --optimize-instructions -S -o - | filecheck %s

(module
;; CHECK: (type $foo (sub (struct)))
(type $foo (sub (struct)))

;; CHECK: (func $ref-cast-exact-fallthrough (type $1) (param $exact (ref (exact $foo))) (result (ref $foo))
;; CHECK-NEXT: (local $inexact (ref $foo))
;; CHECK-NEXT: (local $2 (ref (exact $foo)))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $inexact
;; CHECK-NEXT: (local.tee $2
;; CHECK-NEXT: (local.get $exact)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
(func $ref-cast-exact-fallthrough (param $exact (ref (exact $foo))) (result (ref $foo))
(local $inexact (ref $foo))
;; We should find that the local.get is the most precise fallthrough value and
;; hoist it to eliminate the cast.
(ref.cast (ref $foo)
(local.tee $inexact
(local.get $exact)
)
)
)

;; CHECK: (func $prefer-exactness (type $2) (param $exact-null (ref null (exact $foo))) (result (ref $foo))
;; CHECK-NEXT: (local $inexact-nn (ref $foo))
;; CHECK-NEXT: (local $inexact-null (ref null $foo))
;; CHECK-NEXT: (local $3 (ref null (exact $foo)))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $inexact-nn
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.tee $inexact-null
;; CHECK-NEXT: (local.tee $3
;; CHECK-NEXT: (local.get $exact-null)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $prefer-exactness (param $exact-null (ref null (exact $foo))) (result (ref $foo))
(local $inexact-nn (ref $foo))
(local $inexact-null (ref null $foo))
;; We should prefer to hoist the exact expression and introduce another null
;; check rather than hoisting the non-null, inexact expression.
(ref.cast (ref $foo)
(local.tee $inexact-nn
(ref.as_non_null
(local.tee $inexact-null
(local.get $exact-null)
)
)
)
)
)
)
Loading