Skip to content

Commit 3ff3994

Browse files
authored
Add egraph optimization for fneg's cancelling out (bytecodealliance#5910)
This implements comments from bytecodealliance#5895 to cancel out `fneg` operations in `fma` instructions. Additional support for `fmul` is added as well.
1 parent 87672f7 commit 3ff3994

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cranelift/codegen/src/opts/algebraic.isle

+10
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,13 @@
527527
(rule (simplify
528528
(vselect ty (fcmp _ (FloatCC.GreaterThan) x y) x y))
529529
(fmax_pseudo ty x y))
530+
531+
;; If both of the multiplied arguments to an `fma` are negated then remove
532+
;; both of them since they cancel out.
533+
(rule (simplify (fma ty (fneg ty x) (fneg ty y) z))
534+
(fma ty x y z))
535+
536+
;; If both of the multiplied arguments to an `fmul` are negated then remove
537+
;; both of them since they cancel out.
538+
(rule (simplify (fmul ty (fneg ty x) (fneg ty y)))
539+
(fmul ty x y))

cranelift/filetests/filetests/egraph/algebraic.clif

+22
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,25 @@ block0(v1: i16):
483483

484484
; check: v4 = sextend.i64 v1
485485
; check: return v4
486+
487+
function %fma_double_fneg(f32, f32, f32) -> f32 {
488+
block0(v1: f32, v2: f32, v3: f32):
489+
v4 = fneg v1
490+
v5 = fneg v2
491+
v6 = fma v4, v5, v3
492+
return v6
493+
}
494+
495+
; check: v7 = fma v1, v2, v3
496+
; check: return v7
497+
498+
function %fmul_double_fneg(f32, f32) -> f32 {
499+
block0(v1: f32, v2: f32):
500+
v3 = fneg v1
501+
v4 = fneg v2
502+
v5 = fmul v3, v4
503+
return v5
504+
}
505+
506+
; check: v6 = fmul v1, v2
507+
; check: return v6

0 commit comments

Comments
 (0)