Skip to content

Commit 41a697f

Browse files
committed
Mark the module as closed-world when possible to allow DAE optimizations
Due to WebAssembly/binaryen#7135, any function reference is treated as potentially leaked to the outside world, unless the module is marked as closed-world. This prevents DAE from taking place. To fix this, mark the module as closed-world when tables are neither imported nor exported and when exports do not return/contain funcrefs.
1 parent 2615317 commit 41a697f

9 files changed

+50
-79
lines changed

src/compiler.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ export class Compiler extends DiagnosticEmitter {
536536
// initialize lookup maps, built-ins, imports, exports, etc.
537537
this.program.initialize();
538538

539+
540+
// Binaryen treats all function references as being leaked to the outside world when
541+
// the module isn't marked as closed-world (see WebAssembly/binaryen#7135). Therefore,
542+
// we should mark the module as closed-world when we're definitely sure it is.
543+
module.setClosedWorld(true);
544+
539545
// obtain the main start function
540546
let startFunctionInstance = this.currentFlow.targetFunction;
541547
assert(startFunctionInstance.internalName == BuiltinNames.start);
@@ -857,6 +863,7 @@ export class Compiler extends DiagnosticEmitter {
857863
ImportNames.DefaultNamespace,
858864
ImportNames.Table
859865
);
866+
module.setClosedWorld(false);
860867
if (options.pedantic && options.willOptimize) {
861868
this.pedantic(
862869
DiagnosticCode.Importing_the_table_disables_some_indirect_call_optimizations,
@@ -866,6 +873,7 @@ export class Compiler extends DiagnosticEmitter {
866873
}
867874
if (options.exportTable) {
868875
module.addTableExport(CommonNames.DefaultTable, ExportNames.Table);
876+
module.setClosedWorld(false);
869877
if (options.pedantic && options.willOptimize) {
870878
this.pedantic(
871879
DiagnosticCode.Exporting_the_table_disables_some_indirect_call_optimizations,
@@ -977,6 +985,7 @@ export class Compiler extends DiagnosticEmitter {
977985
}
978986
}
979987
}
988+
if (functionInstance.signature.returnType.kind == TypeKind.Func) this.module.setClosedWorld(false);
980989
}
981990
return;
982991
}
@@ -1007,6 +1016,7 @@ export class Compiler extends DiagnosticEmitter {
10071016
this.desiresExportRuntime = true;
10081017
}
10091018
}
1019+
if (global.type.kind == TypeKind.Func) this.module.setClosedWorld(false);
10101020
}
10111021
if (global.type == Type.v128) {
10121022
this.warning(

src/module.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,14 @@ export class Module {
24712471
binaryen._BinaryenSetDebugInfo(on);
24722472
}
24732473

2474+
getClosedWorld(): bool {
2475+
return binaryen._BinaryenGetClosedWorld();
2476+
}
2477+
2478+
setClosedWorld(on: bool): void {
2479+
binaryen._BinaryenSetClosedWorld(on);
2480+
}
2481+
24742482
getLowMemoryUnused(): bool {
24752483
return binaryen._BinaryenGetLowMemoryUnused();
24762484
}

tests/compiler/call-optional.release.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
i32.const -2
3232
local.set $2
3333
end
34-
local.get $0
3534
local.get $1
35+
i32.const 3
3636
i32.add
3737
local.get $2
3838
i32.add

tests/compiler/call-rest.release.wat

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(module
22
(type $0 (func (param i32 i32 i32) (result i32)))
3-
(type $1 (func))
4-
(type $2 (func (param i32)))
5-
(type $3 (func (param i32 i32)))
6-
(type $4 (func (param i32 i32) (result i32)))
3+
(type $1 (func (param i32 i32) (result i32)))
4+
(type $2 (func))
5+
(type $3 (func (param i32)))
6+
(type $4 (func (param i32 i32)))
77
(type $5 (func (param i32) (result i32)))
88
(type $6 (func (param i32 i32 i32 i32)))
99
(type $7 (func (param i32 i32 i64)))
@@ -1874,7 +1874,8 @@
18741874
global.set $~lib/memory/__stack_pointer
18751875
local.get $0
18761876
)
1877-
(func $call-rest/fn (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
1877+
(func $call-rest/fn (param $0 i32) (param $1 i32) (result i32)
1878+
(local $2 i32)
18781879
(local $3 i32)
18791880
global.get $~lib/memory/__stack_pointer
18801881
i32.const 4
@@ -1895,43 +1896,41 @@
18951896
i32.const 0
18961897
i32.store
18971898
local.get $0
1898-
local.get $1
1899+
i32.const 1
18991900
i32.add
1900-
local.set $1
1901-
i32.const 0
19021901
local.set $0
19031902
global.get $~lib/memory/__stack_pointer
1904-
local.get $2
1903+
local.get $1
19051904
i32.store
1906-
local.get $2
1905+
local.get $1
19071906
call $~lib/array/Array<i32>#get:length
19081907
local.set $3
19091908
loop $for-loop|0
1910-
local.get $0
1909+
local.get $2
19111910
local.get $3
19121911
i32.lt_s
19131912
if
19141913
global.get $~lib/memory/__stack_pointer
1915-
local.get $2
1914+
local.get $1
19161915
i32.store
1916+
local.get $1
19171917
local.get $2
1918-
local.get $0
19191918
call $~lib/array/Array<i32>#__get
1920-
local.get $1
1921-
i32.add
1922-
local.set $1
19231919
local.get $0
1924-
i32.const 1
19251920
i32.add
19261921
local.set $0
1922+
local.get $2
1923+
i32.const 1
1924+
i32.add
1925+
local.set $2
19271926
br $for-loop|0
19281927
end
19291928
end
19301929
global.get $~lib/memory/__stack_pointer
19311930
i32.const 4
19321931
i32.add
19331932
global.set $~lib/memory/__stack_pointer
1934-
local.get $1
1933+
local.get $0
19351934
)
19361935
(func $call-rest/fn@varargs (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
19371936
global.get $~lib/memory/__stack_pointer
@@ -1977,7 +1976,6 @@
19771976
global.get $~lib/memory/__stack_pointer
19781977
local.get $2
19791978
i32.store offset=4
1980-
local.get $0
19811979
local.get $1
19821980
local.get $2
19831981
call $call-rest/fn
@@ -2587,7 +2585,6 @@
25872585
global.get $~lib/memory/__stack_pointer
25882586
local.get $0
25892587
i32.store
2590-
i32.const 1
25912588
i32.const 2
25922589
local.get $0
25932590
call $call-rest/fn
@@ -2609,7 +2606,6 @@
26092606
global.get $~lib/memory/__stack_pointer
26102607
local.get $0
26112608
i32.store
2612-
i32.const 1
26132609
i32.const 2
26142610
local.get $0
26152611
call $call-rest/fn

tests/compiler/function-call.release.wat

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
i32.add
6666
)
6767
(func $start:function-call~fn2|4 (param $0 i32) (result i32)
68-
local.get $0
68+
i32.const 1
6969
)
7070
(func $~lib/rt/itcms/visitRoots
7171
(local $0 i32)
@@ -1412,9 +1412,7 @@
14121412
nop
14131413
)
14141414
(func $function-call/Foo#fnRet (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
1415-
local.get $1
1416-
local.get $2
1417-
i32.add
1415+
i32.const 3
14181416
)
14191417
(func $~lib/rt/__visit_members (param $0 i32)
14201418
block $folding-inner0
@@ -1533,16 +1531,7 @@
15331531
i32.const 1248
15341532
i32.load
15351533
call_indirect (type $1)
1536-
i32.const 1
1537-
i32.ne
1538-
if
1539-
i32.const 0
1540-
i32.const 1152
1541-
i32.const 20
1542-
i32.const 1
1543-
call $~lib/builtins/abort
1544-
unreachable
1545-
end
1534+
drop
15461535
memory.size
15471536
i32.const 16
15481537
i32.shl
@@ -1641,16 +1630,7 @@
16411630
i32.const 1712
16421631
i32.load
16431632
call_indirect (type $1)
1644-
i32.const 1
1645-
i32.ne
1646-
if
1647-
i32.const 0
1648-
i32.const 1152
1649-
i32.const 33
1650-
i32.const 1
1651-
call $~lib/builtins/abort
1652-
unreachable
1653-
end
1633+
drop
16541634
global.get $~lib/memory/__stack_pointer
16551635
global.get $function-call/foo
16561636
local.tee $0

tests/compiler/function-expression.release.wat

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,10 @@
8686
i32.const 1
8787
)
8888
(func $start:function-expression~anonymous|4 (param $0 i32) (param $1 i32) (result i32)
89-
local.get $0
90-
local.get $1
91-
i32.add
89+
i32.const 3
9290
)
9391
(func $start:function-expression~anonymous|5 (param $0 i32) (param $1 i32) (result i32)
94-
local.get $0
92+
i32.const 1
9593
)
9694
(func $start:function-expression~anonymous|6 (param $0 i32) (param $1 i32) (result i32)
9795
i32.const 42

tests/compiler/function-types.release.wat

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@
2828
i32.add
2929
)
3030
(func $function-types/makeAdder<i64>~anonymous|0 (param $0 i64) (param $1 i64) (result i64)
31-
local.get $0
32-
local.get $1
33-
i64.add
31+
i64.const 30
3432
)
3533
(func $function-types/makeAdder<f64>~anonymous|0 (param $0 f64) (param $1 f64) (result f64)
36-
local.get $0
37-
local.get $1
38-
f64.add
34+
f64.const 4
3935
)
4036
(func $~start
4137
(local $0 i32)

tests/compiler/inlining.release.wat

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
i32.const 3
5050
)
5151
(func $inlining/func_fe~anonymous|0 (param $0 i32) (result i32)
52-
local.get $0
52+
i32.const 2
5353
)
5454
(func $~lib/rt/itcms/visitRoots
5555
(local $0 i32)
@@ -1561,16 +1561,7 @@
15611561
i32.const 1104
15621562
i32.load
15631563
call_indirect (type $2)
1564-
i32.const 2
1565-
i32.ne
1566-
if
1567-
i32.const 0
1568-
i32.const 1056
1569-
i32.const 69
1570-
i32.const 3
1571-
call $~lib/builtins/abort
1572-
unreachable
1573-
end
1564+
drop
15741565
global.get $~lib/memory/__stack_pointer
15751566
i32.const 123
15761567
i32.store

tests/compiler/resolve-ternary.release.wat

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,24 +2418,16 @@
24182418
end
24192419
)
24202420
(func $start:resolve-ternary~anonymous|0 (param $0 i32) (result i32)
2421-
local.get $0
2422-
i32.const 1
2423-
i32.add
2421+
i32.const 2
24242422
)
24252423
(func $start:resolve-ternary~anonymous|1 (param $0 i32) (result i32)
2426-
local.get $0
2427-
i32.const 2
2428-
i32.add
2424+
i32.const 3
24292425
)
24302426
(func $resolve-ternary/g1 (param $0 i32) (result i32)
2431-
local.get $0
2432-
i32.const 3
2433-
i32.add
2427+
i32.const 4
24342428
)
24352429
(func $resolve-ternary/g2 (param $0 i32) (result i32)
2436-
local.get $0
2437-
i32.const 4
2438-
i32.add
2430+
i32.const 5
24392431
)
24402432
(func $~lib/rt/__visit_members (param $0 i32)
24412433
block $invalid

0 commit comments

Comments
 (0)