Skip to content

Commit aee3229

Browse files
authored
Do not let GUFA refine casts to exact when invalid (#7530)
We already prevented GUFA from introduce new exact casts when custom descriptors is not enabled, but now prevent it from refining existing casts to be exact as well.
1 parent eb00324 commit aee3229

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/passes/GUFA.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ struct GUFAOptimizer
266266
void visitRefCast(RefCast* curr) {
267267
auto currType = curr->type;
268268
auto inferredType = getContents(curr).getType();
269+
// Do not refine to an invalid exact cast.
270+
inferredType =
271+
inferredType.withInexactIfNoCustomDescs(getModule()->features);
269272
if (inferredType.isRef() && inferredType != currType &&
270273
Type::isSubType(inferredType, currType)) {
271274
// We have inferred that this will only contain something of a more

test/lit/passes/gufa-cast-all-exact.wast

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
;; Check that exact casts are only added when custom descriptors are enabled.
44

5-
;; RUN: wasm-opt %s -all --gufa-cast-all -S -o - | filecheck %s
6-
;; RUN: wasm-opt %s -all --disable-custom-descriptors --gufa-cast-all -S -o - | filecheck %s --check-prefix=NO_CD
5+
;; RUN: foreach %s %t wasm-opt -all --gufa-cast-all -S -o - \
6+
;; RUN: | filecheck %s
7+
8+
;; RUN: foreach %s %t wasm-opt -all --disable-custom-descriptors --gufa-cast-all -S -o - \
9+
;; RUN: | filecheck %s --check-prefix=NO_CD
710

811
(module
912
;; CHECK: (type $foo (struct))
@@ -52,3 +55,57 @@
5255
)
5356
)
5457
)
58+
59+
(module
60+
;; CHECK: (type $foo (struct))
61+
;; NO_CD: (type $foo (struct))
62+
(type $foo (struct))
63+
64+
;; CHECK: (import "" "a" (global $exact-a (ref (exact $foo))))
65+
;; NO_CD: (import "" "a" (global $exact-a (ref (exact $foo))))
66+
(import "" "a" (global $exact-a (ref (exact $foo))))
67+
;; CHECK: (import "" "b" (global $exact-b (ref (exact $foo))))
68+
;; NO_CD: (import "" "b" (global $exact-b (ref (exact $foo))))
69+
(import "" "b" (global $exact-b (ref (exact $foo))))
70+
71+
;; CHECK: (global $g (mut (ref $foo)) (global.get $exact-a))
72+
;; NO_CD: (global $g (mut (ref $foo)) (global.get $exact-a))
73+
(global $g (mut (ref $foo)) (global.get $exact-a))
74+
75+
;; CHECK: (func $set (type $1)
76+
;; CHECK-NEXT: (global.set $g
77+
;; CHECK-NEXT: (global.get $exact-b)
78+
;; CHECK-NEXT: )
79+
;; CHECK-NEXT: )
80+
;; NO_CD: (func $set (type $1)
81+
;; NO_CD-NEXT: (global.set $g
82+
;; NO_CD-NEXT: (global.get $exact-b)
83+
;; NO_CD-NEXT: )
84+
;; NO_CD-NEXT: )
85+
(func $set
86+
;; $g can now hold two different exact $foo references.
87+
(global.set $g
88+
(global.get $exact-b)
89+
)
90+
)
91+
92+
;; CHECK: (func $get (type $2) (result (ref $foo))
93+
;; CHECK-NEXT: (ref.cast (ref (exact $foo))
94+
;; CHECK-NEXT: (ref.cast (ref (exact $foo))
95+
;; CHECK-NEXT: (global.get $g)
96+
;; CHECK-NEXT: )
97+
;; CHECK-NEXT: )
98+
;; CHECK-NEXT: )
99+
;; NO_CD: (func $get (type $2) (result (ref $foo))
100+
;; NO_CD-NEXT: (ref.cast (ref $foo)
101+
;; NO_CD-NEXT: (global.get $g)
102+
;; NO_CD-NEXT: )
103+
;; NO_CD-NEXT: )
104+
(func $get (result (ref $foo))
105+
;; We can only refine this cast target to be exact if custom descriptors are
106+
;; allowed.
107+
(ref.cast (ref $foo)
108+
(global.get $g)
109+
)
110+
)
111+
)

0 commit comments

Comments
 (0)