Skip to content

Commit f9864ac

Browse files
authored
wasm-merge: Refine call results (#8018)
When a call's type is refined, the results may change, and need to be updated.
1 parent ff7c909 commit f9864ac

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

src/tools/wasm-merge.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,16 @@ void updateTypes(Module& wasm) {
574574
curr->type = getModule()->getGlobal(curr->name)->type;
575575
}
576576

577+
void visitCall(Call* curr) {
578+
if (curr->type != Type::unreachable) {
579+
curr->type = getModule()
580+
->getFunction(curr->target)
581+
->type.getHeapType()
582+
.getSignature()
583+
.results;
584+
}
585+
}
586+
577587
void visitRefFunc(RefFunc* curr) {
578588
curr->finalize(getModule()->getFunction(curr->func)->type.getHeapType());
579589
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
3+
;; RUN: wasm-merge %s primary %s.second secondary --skip-export-conflicts -all -S -o - | filecheck %s
4+
5+
;; Export a function with a subtype. It is imported using the supertype, and
6+
;; after we merge, the refined return type must be updated - the call
7+
;; instruction now returns something new.
8+
(module
9+
;; CHECK: (type $0 (func (result anyref)))
10+
11+
;; CHECK: (type $super (sub (func (param i32) (result anyref))))
12+
(type $super (sub (func (param i32) (result anyref))))
13+
14+
;; CHECK: (type $sub (sub final $super (func (param i32) (result (ref any)))))
15+
(type $sub (sub final $super (func (param i32) (result (ref any)))))
16+
17+
;; CHECK: (export "sub" (func $sub))
18+
19+
;; CHECK: (export "caller" (func $caller))
20+
21+
;; CHECK: (export "caller-unreachable" (func $caller-unreachable))
22+
23+
;; CHECK: (func $sub (type $sub) (param $0 i32) (result (ref any))
24+
;; CHECK-NEXT: (unreachable)
25+
;; CHECK-NEXT: )
26+
(func $sub (export "sub") (type $sub)
27+
(unreachable)
28+
)
29+
)
30+
31+
;; CHECK: (func $caller (type $0) (result anyref)
32+
;; CHECK-NEXT: (block $block (result (ref any))
33+
;; CHECK-NEXT: (call $sub
34+
;; CHECK-NEXT: (i32.const 42)
35+
;; CHECK-NEXT: )
36+
;; CHECK-NEXT: )
37+
;; CHECK-NEXT: )
38+
39+
;; CHECK: (func $caller-unreachable (type $0) (result anyref)
40+
;; CHECK-NEXT: (block $block
41+
;; CHECK-NEXT: (call $sub
42+
;; CHECK-NEXT: (unreachable)
43+
;; CHECK-NEXT: )
44+
;; CHECK-NEXT: )
45+
;; CHECK-NEXT: )
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(module
2+
(type $second-super (sub (func (param i32) (result anyref))))
3+
4+
(import "primary" "sub" (func $import (type $second-super)))
5+
6+
(func $caller (export "caller") (result anyref)
7+
;; This will get refined from anyref to non-nullable.
8+
(block $block (result anyref)
9+
(call $import
10+
(i32.const 42)
11+
)
12+
)
13+
)
14+
15+
(func $caller-unreachable (export "caller-unreachable") (result anyref)
16+
;; Unreachable code is not modified.
17+
(block $block (result anyref)
18+
(call $import
19+
(unreachable)
20+
)
21+
)
22+
)
23+
)
24+

0 commit comments

Comments
 (0)