Skip to content

Commit 75eaf13

Browse files
committed
Adding the constant collapse pass to the pipeline.
1 parent db86da5 commit 75eaf13

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

TODO-optimizations.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ This files describes all the optimizations ideas we left out for now.
77
But there shouldn't be any already, if the WASM is from an optimizing
88
compiler.
99

10-
- [dag]: if the final ISA supports instructions that
11-
operate on constants, like `addi $result, $input, 42`, then the DAG
12-
could be optimized to take advantage of this by folding constant
13-
values into the instructions that can use them.
14-
1510
- [locals_data_flow]: track locals permutation. If some locals are read into
1611
a block, but in the end they are just output unmodified (either on stack
1712
or on some local), this can be resolved statically when building the DAG,

src/loader/dag/const_collapse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl ConstantInputFinder {
128128
inputs: &[NodeInput],
129129
nodes: &mut [dag::Node],
130130
const_block_inputs: HashMap<u32, WasmValue>,
131-
) -> (bool, &'a mut [MaybeConstant]) {
131+
) -> (bool, &'a [MaybeConstant]) {
132132
let mut has_constant = false;
133133

134134
// Check if any of the inputs are constants.
@@ -160,6 +160,6 @@ impl ConstantInputFinder {
160160
})
161161
}));
162162

163-
(has_constant, &mut self.buffer)
163+
(has_constant, &self.buffer)
164164
}
165165
}

src/loader/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ pub enum FunctionProcessingStage<'a, S: Settings<'a>> {
413413
tree: LiftedBlockTree<'a>,
414414
},
415415
PlainDag(Dag<'a>),
416+
ConstCollapsedDag(Dag<'a>),
416417
ConstDedupDag(Dag<'a>),
417418
DanglingOptDag(Dag<'a>),
418419
BlocklessDag(BlocklessDag<'a>),
@@ -455,6 +456,15 @@ impl<'a, S: Settings<'a>> FunctionProcessingStage<'a, S> {
455456
FunctionProcessingStage::PlainDag(dag)
456457
}
457458
FunctionProcessingStage::PlainDag(mut dag) => {
459+
// Optimization pass: collapse constants into instructions that use them, if possible.
460+
let constants_collapsed =
461+
dag::const_collapse::constant_collapse(settings, &mut dag);
462+
if let Some(stats) = stats {
463+
stats.constants_deduplicated += constants_collapsed;
464+
}
465+
FunctionProcessingStage::ConstCollapsedDag(dag)
466+
}
467+
FunctionProcessingStage::ConstCollapsedDag(mut dag) => {
458468
// Optimization pass: deduplicate const definitions in the DAG.
459469
let constants_deduplicated = dag::const_dedup::deduplicate_constants(&mut dag);
460470
if let Some(stats) = stats {
@@ -692,6 +702,8 @@ fn pack_bytes_into_words(bytes: &[u8], mut alignment: u32) -> Vec<MemoryEntry> {
692702
pub struct Statistics {
693703
/// Number of register copies saved by the "smart" register allocation.
694704
pub register_copies_saved: usize,
705+
/// Number of constants collapsed into instructions.
706+
pub constants_collapsed: usize,
695707
/// Number of constants deduplicated in the DAG.
696708
pub constants_deduplicated: usize,
697709
/// Number of dangling nodes removed from the DAG.
@@ -706,8 +718,9 @@ impl Display for Statistics {
706718
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
707719
write!(
708720
f,
709-
"Optimization statistics:\n - {} register copies saved\n - {} constants deduplicated\n - {} dangling nodes removed\n - {} block outputs removed\n - {} useless jumps removed",
721+
"Optimization statistics:\n - {} register copies saved\n - {} constants collapsed\n - {} constants deduplicated\n - {} dangling nodes removed\n - {} block outputs removed\n - {} useless jumps removed",
710722
self.register_copies_saved,
723+
self.constants_collapsed,
711724
self.constants_deduplicated,
712725
self.dangling_nodes_removed,
713726
self.block_outputs_removed,

0 commit comments

Comments
 (0)