Skip to content

Commit a1fa205

Browse files
authored
OptimizeInstructions: Fix merging of exact cast + ref.as_non_null (#7646)
We must not forget exactness there.
1 parent 8c82b68 commit a1fa205

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,7 @@ struct OptimizeInstructions
17591759
}
17601760
}
17611761
if (canOptimize) {
1762-
cast->type = Type(cast->type.getHeapType(), NonNullable);
1762+
cast->type = cast->type.with(NonNullable);
17631763
}
17641764
}
17651765
}
@@ -2583,7 +2583,7 @@ struct OptimizeInstructions
25832583
// The cast cannot be non-nullable, or we would have handled this right
25842584
// above by just removing the ref.as, since it would not be needed.
25852585
assert(!cast->type.isNonNullable());
2586-
cast->type = Type(cast->type.getHeapType(), NonNullable);
2586+
cast->type = cast->type.with(NonNullable);
25872587
replaceCurrent(cast);
25882588
}
25892589
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; CHECK: (type $foo (sub (struct)))
77
(type $foo (sub (struct)))
88

9-
;; CHECK: (func $ref-cast-exact-fallthrough (type $1) (param $exact (ref (exact $foo))) (result (ref $foo))
9+
;; CHECK: (func $ref-cast-exact-fallthrough (type $2) (param $exact (ref (exact $foo))) (result (ref $foo))
1010
;; CHECK-NEXT: (local $inexact (ref $foo))
1111
;; CHECK-NEXT: (local $2 (ref (exact $foo)))
1212
;; CHECK-NEXT: (drop
@@ -29,7 +29,7 @@
2929
)
3030
)
3131

32-
;; CHECK: (func $prefer-exactness (type $2) (param $exact-null (ref null (exact $foo))) (result (ref $foo))
32+
;; CHECK: (func $prefer-exactness (type $3) (param $exact-null (ref null (exact $foo))) (result (ref $foo))
3333
;; CHECK-NEXT: (local $inexact-nn (ref $foo))
3434
;; CHECK-NEXT: (local $inexact-null (ref null $foo))
3535
;; CHECK-NEXT: (local $3 (ref null (exact $foo)))
@@ -64,7 +64,7 @@
6464
)
6565
)
6666

67-
;; CHECK: (func $combine-non-null (type $3) (param $foo (ref null $foo)) (result (ref (exact $foo)))
67+
;; CHECK: (func $combine-non-null (type $1) (param $foo (ref null $foo)) (result (ref (exact $foo)))
6868
;; CHECK-NEXT: (ref.cast (ref (exact $foo))
6969
;; CHECK-NEXT: (local.get $foo)
7070
;; CHECK-NEXT: )
@@ -78,4 +78,18 @@
7878
)
7979
)
8080
)
81+
82+
;; CHECK: (func $combine-non-null-reverse (type $1) (param $foo (ref null $foo)) (result (ref (exact $foo)))
83+
;; CHECK-NEXT: (ref.cast (ref (exact $foo))
84+
;; CHECK-NEXT: (local.get $foo)
85+
;; CHECK-NEXT: )
86+
;; CHECK-NEXT: )
87+
(func $combine-non-null-reverse (param $foo (ref null $foo)) (result (ref (exact $foo)))
88+
;; As above, but flipped.
89+
(ref.as_non_null
90+
(ref.cast (ref null (exact $foo))
91+
(local.get $foo)
92+
)
93+
)
94+
)
8195
)

0 commit comments

Comments
 (0)