Skip to content

Commit 61df707

Browse files
authored
Improve const folding via early precompute (#1439)
1 parent e2f5ad9 commit 61df707

File tree

7 files changed

+289
-217
lines changed

7 files changed

+289
-217
lines changed

src/module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ export class Module {
15141514
passes.push("remove-unused-brs");
15151515
passes.push("remove-unused-names");
15161516
passes.push("merge-blocks");
1517+
passes.push("precompute");
15171518
}
15181519
if (optimizeLevel >= 3) {
15191520
passes.push("flatten");
@@ -1545,7 +1546,6 @@ export class Module {
15451546
} else {
15461547
passes.push("precompute");
15471548
}
1548-
passes.push("vacuum");
15491549
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
15501550
passes.push("pick-load-signs");
15511551
passes.push("simplify-globals-optimizing");
@@ -1633,7 +1633,6 @@ export class Module {
16331633
passes.push("vacuum");
16341634

16351635
passes.push("precompute-propagate");
1636-
passes.push("vacuum");
16371636

16381637
// replace indirect with direct calls again and inline
16391638
passes.push("inlining-optimizing");

tests/compiler/const-folding.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none"
4+
]
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(module
2+
(type $i64_=>_i64 (func (param i64) (result i64)))
3+
(memory $0 0)
4+
(export "memory" (memory $0))
5+
(export "test" (func $const-folding/test))
6+
(func $const-folding/test (param $0 i64) (result i64)
7+
local.get $0
8+
i64.const 1
9+
i64.shl
10+
local.get $0
11+
i64.const 63
12+
i64.shr_u
13+
i64.add
14+
)
15+
)

tests/compiler/const-folding.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function test(value: u64): u64 {
2+
var mod1: u64 = -1;
3+
var mod2: u64 = -1;
4+
5+
var rlo = value << 1;
6+
var rhi = rlo & ~mod2;
7+
8+
rhi |= rhi << 1;
9+
rhi |= (value >> 63) & mod1;
10+
return rlo + rhi;
11+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(module
2+
(type $i64_=>_i64 (func (param i64) (result i64)))
3+
(memory $0 0)
4+
(table $0 1 funcref)
5+
(export "memory" (memory $0))
6+
(export "test" (func $const-folding/test))
7+
(func $const-folding/test (param $0 i64) (result i64)
8+
(local $1 i64)
9+
(local $2 i64)
10+
(local $3 i64)
11+
(local $4 i64)
12+
i64.const -1
13+
local.set $1
14+
i64.const -1
15+
local.set $2
16+
local.get $0
17+
i64.const 1
18+
i64.shl
19+
local.set $3
20+
local.get $3
21+
local.get $2
22+
i64.const -1
23+
i64.xor
24+
i64.and
25+
local.set $4
26+
local.get $4
27+
local.get $4
28+
i64.const 1
29+
i64.shl
30+
i64.or
31+
local.set $4
32+
local.get $4
33+
local.get $0
34+
i64.const 63
35+
i64.shr_u
36+
local.get $1
37+
i64.and
38+
i64.or
39+
local.set $4
40+
local.get $3
41+
local.get $4
42+
i64.add
43+
)
44+
)

0 commit comments

Comments
 (0)