Skip to content

Commit 894c770

Browse files
authored
Fix assertion in OptimizeInstructions for exact refs (#7354)
When optimizing a cast to an exact reference to a bottom type, OptimizeInstructions previously triggered an assertion that expected the cast type to be inexact. Fix the assertion and surrounding code to be more robust to the presence of exact reference types and add a test.
1 parent f9b7876 commit 894c770

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

scripts/test/fuzzing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
'stack_switching_switch.wast',
104104
# TODO: fuzzer support for exact references
105105
'exact-references.wast',
106+
'optimize-instructions-exact.wast',
106107
]
107108

108109

src/passes/OptimizeInstructions.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,18 +2346,20 @@ struct OptimizeInstructions
23462346
}
23472347
[[fallthrough]];
23482348
case GCTypeUtils::SuccessOnlyIfNull: {
2349-
auto nullType = Type(curr->type.getHeapType().getBottom(), Nullable);
23502349
// The cast either returns null or traps. In trapsNeverHappen mode
23512350
// we know the result, since by assumption it will not trap.
23522351
if (getPassOptions().trapsNeverHappen) {
2353-
replaceCurrent(builder.makeBlock(
2354-
{builder.makeDrop(curr->ref), builder.makeRefNull(nullType)},
2355-
curr->type));
2352+
replaceCurrent(
2353+
builder.makeBlock({builder.makeDrop(curr->ref),
2354+
builder.makeRefNull(curr->type.getHeapType())},
2355+
curr->type));
23562356
return;
23572357
}
23582358
// Otherwise, we should have already refined the cast type to cast
2359-
// directly to null.
2360-
assert(curr->type == nullType);
2359+
// directly to null. We do not further refine the cast type to exact
2360+
// null because the extra precision is not useful and doing so would
2361+
// increase the size of the instruction encoding.
2362+
assert(curr->type.isNull());
23612363
break;
23622364
}
23632365
case GCTypeUtils::Unreachable:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
3+
;; Check that optimizations on casts involving exact reference types work
4+
;; correctly.
5+
6+
;; RUN: wasm-opt %s -all --optimize-instructions -S -o - | filecheck %s
7+
8+
(module
9+
;; CHECK: (func $cast-to-exact-none (type $0) (param $0 anyref)
10+
;; CHECK-NEXT: (drop
11+
;; CHECK-NEXT: (ref.cast (exact nullref)
12+
;; CHECK-NEXT: (local.get $0)
13+
;; CHECK-NEXT: )
14+
;; CHECK-NEXT: )
15+
;; CHECK-NEXT: )
16+
(func $cast-to-exact-none (param anyref)
17+
(drop
18+
;; This will not be changed, but should not trigger an assertion.
19+
(ref.cast (exact nullref)
20+
(local.get 0)
21+
)
22+
)
23+
)
24+
)

0 commit comments

Comments
 (0)