Skip to content

Commit 6431a07

Browse files
authored
Preserve exactness optimizing ref.cast + ref.as_non_null (#7605)
We would previously drop exactness when making the cast type non-nullable.
1 parent f27ff3e commit 6431a07

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,7 @@ struct OptimizeInstructions
24232423
//
24242424
if (auto* as = curr->ref->dynCast<RefAs>(); as && as->op == RefAsNonNull) {
24252425
curr->ref = as->value;
2426-
curr->type = Type(curr->type.getHeapType(), NonNullable);
2426+
curr->type = curr->type.with(NonNullable);
24272427
}
24282428
}
24292429

test/lit/passes/optimize-instructions-exact.wast

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
22

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

55
(module
66
;; CHECK: (type $foo (sub (struct)))
@@ -63,4 +63,19 @@
6363
)
6464
)
6565
)
66+
67+
;; CHECK: (func $combine-non-null (type $3) (param $foo (ref null $foo)) (result (ref (exact $foo)))
68+
;; CHECK-NEXT: (ref.cast (ref (exact $foo))
69+
;; CHECK-NEXT: (local.get $foo)
70+
;; CHECK-NEXT: )
71+
;; CHECK-NEXT: )
72+
(func $combine-non-null (param $foo (ref null $foo)) (result (ref (exact $foo)))
73+
;; We should not lose the exactness of the cast when we combine the
74+
;; ref.as_non_null into it.
75+
(ref.cast (ref null (exact $foo))
76+
(ref.as_non_null
77+
(local.get $foo)
78+
)
79+
)
80+
)
6681
)

0 commit comments

Comments
 (0)