Skip to content

Commit 2a29cab

Browse files
authored
feat: Improve pass pipeline when flattening code (#1368)
1 parent 36c706d commit 2a29cab

Some content is hidden

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

46 files changed

+7236
-7181
lines changed

src/module.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,7 @@ export class Module {
15121512
passes.push("flatten");
15131513
passes.push("simplify-locals-notee-nostructure");
15141514
passes.push("vacuum");
1515+
15151516
passes.push("code-folding");
15161517
passes.push("flatten");
15171518
passes.push("local-cse");
@@ -1529,8 +1530,10 @@ export class Module {
15291530
// passes.push("post-assemblyscript");
15301531
// }
15311532
passes.push("optimize-instructions");
1532-
passes.push("inlining");
1533-
passes.push("dce");
1533+
if (optimizeLevel >= 3 || shrinkLevel >= 1) {
1534+
passes.push("dce");
1535+
passes.push("inlining");
1536+
}
15341537
passes.push("remove-unused-brs");
15351538
passes.push("remove-unused-names");
15361539
passes.push("inlining-optimizing");
@@ -1549,11 +1552,13 @@ export class Module {
15491552
}
15501553
passes.push("simplify-locals-nostructure");
15511554
passes.push("vacuum");
1555+
15521556
passes.push("reorder-locals");
15531557
passes.push("remove-unused-brs");
15541558
passes.push("coalesce-locals");
15551559
passes.push("simplify-locals");
15561560
passes.push("vacuum");
1561+
15571562
passes.push("reorder-locals");
15581563
passes.push("coalesce-locals");
15591564
passes.push("reorder-locals");
@@ -1576,8 +1581,8 @@ export class Module {
15761581
}
15771582
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
15781583
passes.push("rse");
1584+
passes.push("vacuum");
15791585
}
1580-
passes.push("vacuum");
15811586

15821587
// --- PassRunner::addDefaultGlobalOptimizationPostPasses ---
15831588

@@ -1623,22 +1628,30 @@ export class Module {
16231628
// very expensive, so O3 only
16241629
passes.push("simplify-globals");
16251630
passes.push("vacuum");
1631+
1632+
passes.push("precompute-propagate");
1633+
passes.push("vacuum");
1634+
16261635
// replace indirect with direct calls again and inline
16271636
passes.push("inlining-optimizing");
16281637
passes.push("directize");
16291638
passes.push("dae-optimizing");
1630-
passes.push("precompute-propagate");
1631-
passes.push("vacuum");
1639+
16321640
passes.push("merge-locals");
16331641
passes.push("coalesce-locals");
1634-
passes.push("simplify-locals-nostructure");
1642+
passes.push("simplify-locals");
16351643
passes.push("vacuum");
1644+
16361645
passes.push("inlining-optimizing");
16371646
passes.push("precompute-propagate");
1647+
passes.push("vacuum");
1648+
1649+
passes.push("coalesce-locals");
16381650
}
16391651
passes.push("remove-unused-brs");
16401652
passes.push("remove-unused-names");
16411653
passes.push("vacuum");
1654+
16421655
passes.push("optimize-instructions");
16431656
passes.push("simplify-globals-optimizing");
16441657
}

tests/compiler/binary.optimized.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@
114114
local.get $1
115115
i32.const 8388608
116116
i32.ge_u
117-
if
117+
if (result i32)
118118
local.get $1
119119
i32.const 8388608
120120
i32.eq
121121
br_if $folding-inner0
122122
local.get $1
123123
i32.const 8388608
124124
i32.sub
125-
local.set $1
125+
else
126+
local.get $1
126127
end
127-
local.get $1
128128
i32.const 1
129129
i32.shl
130130
local.set $1
@@ -261,17 +261,17 @@
261261
local.get $1
262262
i64.const 4503599627370496
263263
i64.ge_u
264-
if
264+
if (result i64)
265265
local.get $1
266266
i64.const 4503599627370496
267267
i64.eq
268268
br_if $folding-inner0
269269
local.get $1
270270
i64.const 4503599627370496
271271
i64.sub
272-
local.set $1
272+
else
273+
local.get $1
273274
end
274-
local.get $1
275275
i64.const 1
276276
i64.shl
277277
local.set $1

tests/compiler/class-overloading.optimized.wat

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@
9898
local.set $0
9999
end
100100
local.get $0
101-
i32.eqz
102101
if (result i32)
102+
local.get $0
103+
else
103104
i32.const 3
104105
call $~lib/rt/stub/__alloc
105-
else
106-
local.get $0
107106
end
108107
)
109108
(func $~lib/string/String#get:length (param $0 i32) (result i32)

0 commit comments

Comments
 (0)