Skip to content

Commit

Permalink
Improve const folding via early precompute (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored Aug 20, 2020
1 parent e2f5ad9 commit 61df707
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 217 deletions.
3 changes: 1 addition & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,7 @@ export class Module {
passes.push("remove-unused-brs");
passes.push("remove-unused-names");
passes.push("merge-blocks");
passes.push("precompute");
}
if (optimizeLevel >= 3) {
passes.push("flatten");
Expand Down Expand Up @@ -1545,7 +1546,6 @@ export class Module {
} else {
passes.push("precompute");
}
passes.push("vacuum");
if (optimizeLevel >= 2 || shrinkLevel >= 1) {
passes.push("pick-load-signs");
passes.push("simplify-globals-optimizing");
Expand Down Expand Up @@ -1633,7 +1633,6 @@ export class Module {
passes.push("vacuum");

passes.push("precompute-propagate");
passes.push("vacuum");

// replace indirect with direct calls again and inline
passes.push("inlining-optimizing");
Expand Down
5 changes: 5 additions & 0 deletions tests/compiler/const-folding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"asc_flags": [
"--runtime none"
]
}
15 changes: 15 additions & 0 deletions tests/compiler/const-folding.optimized.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(module
(type $i64_=>_i64 (func (param i64) (result i64)))
(memory $0 0)
(export "memory" (memory $0))
(export "test" (func $const-folding/test))
(func $const-folding/test (param $0 i64) (result i64)
local.get $0
i64.const 1
i64.shl
local.get $0
i64.const 63
i64.shr_u
i64.add
)
)
11 changes: 11 additions & 0 deletions tests/compiler/const-folding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function test(value: u64): u64 {
var mod1: u64 = -1;
var mod2: u64 = -1;

var rlo = value << 1;
var rhi = rlo & ~mod2;

rhi |= rhi << 1;
rhi |= (value >> 63) & mod1;
return rlo + rhi;
}
44 changes: 44 additions & 0 deletions tests/compiler/const-folding.untouched.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(module
(type $i64_=>_i64 (func (param i64) (result i64)))
(memory $0 0)
(table $0 1 funcref)
(export "memory" (memory $0))
(export "test" (func $const-folding/test))
(func $const-folding/test (param $0 i64) (result i64)
(local $1 i64)
(local $2 i64)
(local $3 i64)
(local $4 i64)
i64.const -1
local.set $1
i64.const -1
local.set $2
local.get $0
i64.const 1
i64.shl
local.set $3
local.get $3
local.get $2
i64.const -1
i64.xor
i64.and
local.set $4
local.get $4
local.get $4
i64.const 1
i64.shl
i64.or
local.set $4
local.get $4
local.get $0
i64.const 63
i64.shr_u
local.get $1
i64.and
i64.or
local.set $4
local.get $3
local.get $4
i64.add
)
)
Loading

0 comments on commit 61df707

Please sign in to comment.