Skip to content

Commit 93883fd

Browse files
authored
[WasmGC] OptimizeInstructions: Cancel out internalize+externalize pairs (#7005)
1 parent e201819 commit 93883fd

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2266,9 +2266,19 @@ struct OptimizeInstructions
22662266
refAsChild->value = curr;
22672267
refAsChild->finalize();
22682268
replaceCurrent(refAsChild);
2269+
return;
2270+
}
2271+
2272+
// We can optimize away externalizations of internalizations and vice
2273+
// versa.
2274+
if ((curr->op == ExternConvertAny &&
2275+
refAsChild->op == AnyConvertExtern) ||
2276+
(curr->op == AnyConvertExtern &&
2277+
refAsChild->op == ExternConvertAny)) {
2278+
replaceCurrent(refAsChild->value);
2279+
return;
22692280
}
22702281
}
2271-
// TODO: optimize away ExternConvertAny of AnyConvertExtern, etc.
22722282
return;
22732283
}
22742284

test/lit/extern-conversions.wast

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77

88

99
(module
10-
;; CHECK: (type $0 (func (param (ref any)) (result (ref extern))))
10+
;; CHECK: (type $0 (func (param externref) (result anyref)))
1111

12-
;; CHECK: (type $1 (func (param externref) (result anyref)))
12+
;; CHECK: (type $1 (func (param (ref any)) (result (ref extern))))
1313

14-
;; CHECK: (type $2 (func (param externref) (result externref)))
14+
;; CHECK: (type $2 (func (param anyref) (result externref)))
1515

1616
;; CHECK: (export "ext" (func $extern.convert_any))
1717

1818
;; CHECK: (export "int" (func $any.convert_extern))
1919

20-
;; CHECK: (export "legacy" (func $legacy_notation))
20+
;; CHECK: (export "legacy.1" (func $legacy_notation.1))
2121

22-
;; CHECK: (func $extern.convert_any (type $0) (param $0 (ref any)) (result (ref extern))
22+
;; CHECK: (export "legacy.2" (func $legacy_notation.2))
23+
24+
;; CHECK: (func $extern.convert_any (type $1) (param $0 (ref any)) (result (ref extern))
2325
;; CHECK-NEXT: (extern.convert_any
2426
;; CHECK-NEXT: (local.get $0)
2527
;; CHECK-NEXT: )
@@ -30,7 +32,7 @@
3032
)
3133
)
3234

33-
;; CHECK: (func $any.convert_extern (type $1) (param $0 externref) (result anyref)
35+
;; CHECK: (func $any.convert_extern (type $0) (param $0 externref) (result anyref)
3436
;; CHECK-NEXT: (any.convert_extern
3537
;; CHECK-NEXT: (local.get $0)
3638
;; CHECK-NEXT: )
@@ -41,18 +43,30 @@
4143
)
4244
)
4345

44-
;; CHECK: (func $legacy_notation (type $2) (param $0 externref) (result externref)
45-
;; CHECK-NEXT: (extern.convert_any
46+
;; CHECK: (func $legacy_notation.1 (type $0) (param $0 externref) (result anyref)
47+
;; CHECK-NEXT: (ref.as_non_null
4648
;; CHECK-NEXT: (any.convert_extern
4749
;; CHECK-NEXT: (local.get $0)
4850
;; CHECK-NEXT: )
4951
;; CHECK-NEXT: )
5052
;; CHECK-NEXT: )
51-
(func $legacy_notation (export "legacy") (param $x (ref null extern)) (result (ref null extern))
52-
(extern.externalize
53-
(extern.internalize
53+
(func $legacy_notation.1 (export "legacy.1") (param $x (ref null extern)) (result (ref null any))
54+
(extern.internalize
55+
(ref.as_non_null ;; Add this to avoid the entire function being merged with
56+
;; another.
5457
(local.get $x)
5558
)
5659
)
5760
)
61+
62+
;; CHECK: (func $legacy_notation.2 (type $2) (param $0 anyref) (result externref)
63+
;; CHECK-NEXT: (extern.convert_any
64+
;; CHECK-NEXT: (local.get $0)
65+
;; CHECK-NEXT: )
66+
;; CHECK-NEXT: )
67+
(func $legacy_notation.2 (export "legacy.2") (param $x (ref null any)) (result (ref null extern))
68+
(extern.externalize
69+
(local.get $x)
70+
)
71+
)
5872
)

test/lit/passes/optimize-instructions-gc-extern.wast

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,30 @@
8585
)
8686
)
8787
)
88+
89+
;; CHECK: (func $extern.intern (type $3) (param $ext externref) (param $any anyref)
90+
;; CHECK-NEXT: (drop
91+
;; CHECK-NEXT: (local.get $any)
92+
;; CHECK-NEXT: )
93+
;; CHECK-NEXT: (drop
94+
;; CHECK-NEXT: (local.get $ext)
95+
;; CHECK-NEXT: )
96+
;; CHECK-NEXT: )
97+
(func $extern.intern (param $ext externref) (param $any anyref)
98+
;; Internalize/externalize operations cancel out.
99+
(drop
100+
(any.convert_extern
101+
(extern.convert_any
102+
(local.get $any)
103+
)
104+
)
105+
)
106+
(drop
107+
(extern.convert_any
108+
(any.convert_extern
109+
(local.get $ext)
110+
)
111+
)
112+
)
113+
)
88114
)

0 commit comments

Comments
 (0)