Add egraph cprop optimizations for splat (#6148)

This commit adds constant-propagation optimizations for
`splat`-of-constant to produce a `vconst` node. This should help later
hoisting these constants out of loops if it shows up in wasm.
This commit is contained in:
Alex Crichton
2023-04-05 11:10:45 -05:00
committed by GitHub
parent 3275c45993
commit d45cbba83f
3 changed files with 213 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ use crate::egraph::{NewOrExistingInst, OptimizeCtx};
use crate::ir::condcodes;
pub use crate::ir::condcodes::{FloatCC, IntCC};
use crate::ir::dfg::ValueDef;
pub use crate::ir::immediates::{Ieee32, Ieee64, Imm64, Offset32, Uimm32, Uimm64, Uimm8};
pub use crate::ir::immediates::{Ieee32, Ieee64, Imm64, Offset32, Uimm32, Uimm64, Uimm8, V128Imm};
pub use crate::ir::types::*;
pub use crate::ir::{
dynamic_to_fixed, AtomicRmwOp, Block, BlockCall, Constant, DataFlowGraph, DynamicStackSlot,
@@ -128,4 +128,11 @@ impl<'a, 'b, 'c> generated_code::Context for IsleContext<'a, 'b, 'c> {
self.ctx.stats.subsume += 1;
value
}
fn splat64(&mut self, val: u64) -> Constant {
let val = u128::from(val);
let val = val | (val << 64);
let imm = V128Imm(val.to_le_bytes());
self.ctx.func.dfg.constants.insert(imm.into())
}
}

View File

@@ -171,3 +171,27 @@
y)
;; TODO: fadd, fsub, fmul, fdiv, fneg, fabs
;; A splat of a constant can become a direct `vconst` with the appropriate bit
;; pattern.
(rule (simplify (splat dst (iconst $I8 n)))
(vconst dst (splat8 (u64_uextend_imm64 $I8 n))))
(rule (simplify (splat dst (iconst $I16 n)))
(vconst dst (splat16 (u64_uextend_imm64 $I16 n))))
(rule (simplify (splat dst (iconst $I32 n)))
(vconst dst (splat32 (u64_uextend_imm64 $I32 n))))
(rule (simplify (splat dst (iconst $I64 n)))
(vconst dst (splat64 (u64_uextend_imm64 $I64 n))))
(rule (simplify (splat dst (f32const _ (u32_from_ieee32 n))))
(vconst dst (splat32 n)))
(rule (simplify (splat dst (f64const _ (u64_from_ieee64 n))))
(vconst dst (splat64 n)))
(decl splat8 (u64) Constant)
(rule (splat8 n) (splat16 (u64_or n (u64_shl n 8))))
(decl splat16 (u64) Constant)
(rule (splat16 n) (splat32 (u64_or n (u64_shl n 16))))
(decl splat32 (u64) Constant)
(rule (splat32 n) (splat64 (u64_or n (u64_shl n 32))))
(decl splat64 (u64) Constant)
(extern constructor splat64 splat64)