Skip to content

Commit 6d41e58

Browse files
MaxGraeydcodeIO
authored andcommitted
Reorder/extend pass order, improving all sorts of optimizations (AssemblyScript#1031)
1 parent 4611609 commit 6d41e58

File tree

62 files changed

+5274
-6829
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+5274
-6829
lines changed

cli/asc.js

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,12 @@ exports.main = function main(argv, options, callback) {
648648
if (optimizeLevel >= 3 || shrinkLevel >= 1) {
649649
add("ssa-nomerge");
650650
}
651-
if (optimizeLevel >= 4) {
651+
if (optimizeLevel >= 3) {
652652
add("flatten");
653653
add("local-cse");
654654
}
655655
if (hasARC) { // differs
656-
if (optimizeLevel < 4) {
656+
if (optimizeLevel < 3) {
657657
add("flatten");
658658
}
659659
add("post-assemblyscript");
@@ -670,9 +670,10 @@ exports.main = function main(argv, options, callback) {
670670
} else {
671671
add("precompute");
672672
}
673-
if (optimizeLevel >= 2 || shrinkLevel >= 2) {
674-
add("code-pushing");
675-
}
673+
// this will be done later
674+
// if (optimizeLevel >= 2 || shrinkLevel >= 2) {
675+
// add("code-pushing");
676+
// }
676677
add("simplify-locals-nostructure");
677678
add("vacuum");
678679
add("reorder-locals");
@@ -703,30 +704,64 @@ exports.main = function main(argv, options, callback) {
703704
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
704705
add("rse");
705706
}
706-
if (hasARC) { // differs
707-
add("post-assemblyscript-finalize");
708-
}
709707
add("vacuum");
710-
711708
// PassRunner::addDefaultGlobalOptimizationPostPasses
712709
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
713710
add("dae-optimizing");
714711
}
715712
if (optimizeLevel >= 2 || shrinkLevel >= 2) {
716713
add("inlining-optimizing");
717714
}
718-
add("duplicate-function-elimination");
715+
// "duplicate-function-elimination" will better done later
716+
// add("duplicate-function-elimination");
719717
add("duplicate-import-elimination");
720718
if (optimizeLevel >= 2 || shrinkLevel >= 2) {
721719
add("simplify-globals-optimizing");
722720
} else {
723721
add("simplify-globals");
724722
}
725-
add("remove-unused-module-elements");
726-
add("memory-packing");
727-
add("directize");
723+
// replace indirect calls with direct, reduce arity and
724+
// inline this calls if possible
725+
add("directize"); // differs
726+
add("dae-optimizing"); // differs
728727
add("inlining-optimizing"); // differs
729-
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
728+
// ARC finalization should be done exactly after inlining for better release/retain reduction
729+
if (hasARC) { // differs
730+
add("post-assemblyscript-finalize");
731+
}
732+
if (optimizeLevel >= 2 || shrinkLevel >= 1) { // differs
733+
add("rse");
734+
add("vacuum");
735+
736+
// rearrange / reduce switch cases again
737+
add("remove-unused-brs");
738+
add("remove-unused-names");
739+
add("merge-blocks");
740+
add("vacuum");
741+
742+
// replace indirect calls with direct and inline if possible again.
743+
add("directize");
744+
add("inlining-optimizing");
745+
// move some code after early return which potentially could reduce computations
746+
// do this after CFG cleanup (originally it was done before)
747+
add("code-pushing");
748+
749+
// this quite expensive so do this only for highest opt level
750+
if (optimizeLevel >= 3) {
751+
add("simplify-locals-nostructure");
752+
add("reorder-locals");
753+
add("vacuum");
754+
}
755+
// finally optimize all remaining peepholes
756+
add("simplify-globals-optimizing");
757+
add("optimize-instructions");
758+
}
759+
// remove unused elements of table and pack / reduce memory
760+
add("duplicate-function-elimination"); // differs
761+
add("remove-unused-nonfunction-module-elements"); // differs
762+
add("memory-packing");
763+
add("remove-unused-module-elements"); // differs
764+
if (optimizeLevel >= 3 || shrinkLevel >= 1) { // differs. was optimizeLevel >= 2
730765
add("generate-stack-ir");
731766
add("optimize-stack-ir");
732767
}

tests/compiler/binary.optimized.wat

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@
1111
(export "memory" (memory $0))
1212
(start $start)
1313
(func $~lib/math/NativeMath.pow (; 0 ;) (param $0 f64) (result f64)
14-
(local $1 i32)
15-
(local $2 i64)
14+
(local $1 i64)
15+
(local $2 i32)
1616
(local $3 i32)
1717
local.get $0
1818
i64.reinterpret_f64
19-
local.tee $2
19+
local.tee $1
2020
i64.const 32
2121
i64.shr_u
2222
i32.wrap_i64
23-
local.set $1
23+
local.set $2
2424
i32.const 1
2525
i32.const 0
2626
i32.const 1
2727
i32.const 0
2828
i32.const 1
29-
local.get $2
29+
local.get $1
3030
i32.wrap_i64
3131
i32.const 0
32-
local.get $1
32+
local.get $2
3333
i32.const 2147483647
3434
i32.and
3535
local.tee $3
@@ -55,21 +55,16 @@
5555
(local $2 i32)
5656
(local $3 i32)
5757
(local $4 i32)
58+
i32.const 1
59+
i32.const 0
5860
local.get $0
5961
i32.reinterpret_f32
6062
local.tee $1
6163
i32.const 23
6264
i32.shr_u
6365
i32.const 255
6466
i32.and
65-
local.set $2
66-
local.get $1
67-
i32.const -2147483648
68-
i32.and
69-
local.set $4
70-
i32.const 1
71-
i32.const 0
72-
local.get $2
67+
local.tee $2
7368
i32.const 255
7469
i32.eq
7570
select
@@ -79,6 +74,10 @@
7974
f32.div
8075
return
8176
end
77+
local.get $1
78+
i32.const -2147483648
79+
i32.and
80+
local.set $4
8281
block $folding-inner0
8382
local.get $1
8483
i32.const 1
@@ -196,12 +195,10 @@
196195
f32.mul
197196
)
198197
(func $~lib/math/NativeMathf.pow (; 2 ;) (param $0 f32) (result f32)
199-
(local $1 i32)
200198
i32.const 1
201199
i32.const 0
202200
local.get $0
203201
i32.reinterpret_f32
204-
local.tee $1
205202
i32.const 2147483647
206203
i32.and
207204
i32.const 2139095040
@@ -220,21 +217,16 @@
220217
(local $2 i64)
221218
(local $3 i64)
222219
(local $4 i64)
220+
i32.const 1
221+
i32.const 0
223222
local.get $0
224223
i64.reinterpret_f64
225224
local.tee $1
226225
i64.const 52
227226
i64.shr_u
228227
i64.const 2047
229228
i64.and
230-
local.set $2
231-
local.get $1
232-
i64.const 63
233-
i64.shr_u
234-
local.set $4
235-
i32.const 1
236-
i32.const 0
237-
local.get $2
229+
local.tee $2
238230
i64.const 2047
239231
i64.eq
240232
select
@@ -244,6 +236,10 @@
244236
f64.div
245237
return
246238
end
239+
local.get $1
240+
i64.const 63
241+
i64.shr_u
242+
local.set $4
247243
block $folding-inner0
248244
local.get $1
249245
i64.const 1

tests/compiler/bool.optimized.wat

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
(module
2-
(type $none_=>_none (func))
32
(memory $0 1)
43
(data (i32.const 16) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
54
(export "memory" (memory $0))
6-
(start $start)
7-
(func $start (; 0 ;)
8-
nop
9-
)
105
)

tests/compiler/builtins.optimized.wat

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $none_=>_none (func))
3-
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
43
(type $i32_=>_i32 (func (param i32) (result i32)))
4+
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
55
(type $i32_i32_f64_f64_f64_f64_f64_=>_none (func (param i32 i32 f64 f64 f64 f64 f64)))
66
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
77
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
@@ -180,10 +180,7 @@
180180
(func $start:builtins~anonymous|0 (; 6 ;)
181181
nop
182182
)
183-
(func $start:builtins~anonymous|1 (; 7 ;) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32)
184-
nop
185-
)
186-
(func $start:builtins (; 8 ;)
183+
(func $start:builtins (; 7 ;)
187184
i32.const 31
188185
global.set $builtins/i
189186
i32.const 0
@@ -216,17 +213,6 @@
216213
global.set $builtins/I
217214
i64.const 1
218215
global.set $builtins/I
219-
global.get $builtins/i
220-
i32.const 1
221-
i32.ne
222-
if
223-
i32.const 0
224-
i32.const 80
225-
i32.const 87
226-
i32.const 20
227-
call $~lib/builtins/abort
228-
unreachable
229-
end
230216
f32.const nan:0x400000
231217
global.set $builtins/f
232218
f32.const inf
@@ -867,7 +853,7 @@
867853
unreachable
868854
end
869855
)
870-
(func $start (; 9 ;)
856+
(func $start (; 8 ;)
871857
call $start:builtins
872858
)
873859
)

tests/compiler/call-optional.optimized.wat

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
66
(memory $0 1)
77
(data (i32.const 16) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00c\00a\00l\00l\00-\00o\00p\00t\00i\00o\00n\00a\00l\00.\00t\00s")
8-
(table $0 2 funcref)
9-
(elem (i32.const 1) $call-optional/opt|trampoline)
108
(global $~lib/argc (mut i32) (i32.const 0))
119
(export "memory" (memory $0))
1210
(start $start)
1311
(func $call-optional/opt (; 1 ;) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
12+
local.get $2
1413
local.get $0
1514
local.get $1
1615
i32.add
17-
local.get $2
1816
i32.add
1917
)
2018
(func $call-optional/opt|trampoline (; 2 ;) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)

tests/compiler/class-static-function.optimized.wat

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
(module
22
(type $none_=>_none (func))
3-
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
4-
(type $none_=>_i32 (func (result i32)))
5-
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
63
(memory $0 1)
74
(data (i32.const 16) "0\00\00\00\01\00\00\00\01\00\00\000\00\00\00c\00l\00a\00s\00s\00-\00s\00t\00a\00t\00i\00c\00-\00f\00u\00n\00c\00t\00i\00o\00n\00.\00t\00s")
8-
(table $0 2 funcref)
9-
(elem (i32.const 1) $class-static-function/Example.staticFunc)
105
(global $~lib/argc (mut i32) (i32.const 0))
116
(export "memory" (memory $0))
127
(start $start)
13-
(func $class-static-function/Example.staticFunc (; 1 ;) (result i32)
14-
i32.const 42
15-
)
16-
(func $start (; 2 ;)
8+
(func $start (; 0 ;)
179
i32.const 0
1810
global.set $~lib/argc
1911
)

tests/compiler/comma.optimized.wat

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,8 @@
7171
global.set $comma/a
7272
i32.const 1
7373
global.set $comma/a
74-
global.get $comma/a
75-
global.set $comma/b
76-
global.get $comma/a
7774
i32.const 1
78-
i32.ne
79-
if
80-
i32.const 0
81-
i32.const 32
82-
i32.const 14
83-
i32.const 0
84-
call $~lib/builtins/abort
85-
unreachable
86-
end
75+
global.set $comma/b
8776
global.get $comma/b
8877
i32.const 1
8978
i32.ne

tests/compiler/continue.optimized.wat

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
(export "testInherit" (func $continue/testInherit))
66
(func $continue/testInherit (; 0 ;) (param $0 i32)
77
(local $1 i32)
8-
block $break|0
9-
loop $loop|0
8+
loop $loop|0
9+
block $break|0
1010
local.get $1
1111
i32.const 10
1212
i32.ge_s
@@ -26,7 +26,6 @@
2626
local.set $1
2727
br $loop|0
2828
end
29-
unreachable
3029
end
3130
)
3231
)

0 commit comments

Comments
 (0)