Re: [PATCH] match.pd: combine a pair of vector reductions
Jeffrey Law <[email protected]>
| Newsgroups | gmane.comp.gcc.patches |
|---|---|
| Message-ID | <[email protected]> |
On 8/4/2026 4:01 AM, [email protected] wrote: > From: Kyrylo Tkachov <[email protected]> > > Two reductions feeding the matching scalar operation are one reduction of > the elementwise operation: > > REDUC (a) op REDUC (b) -> REDUC (a op b) > > The two sides accumulate the same multiset of lanes, just grouped > differently, so for the associative and commutative reductions the result > is identical. This trades a lane-crossing reduction, which is the > expensive part, for an elementwise operation. > > MIN, MAX, AND, IOR and XOR need no flag. The sum needs one only for > floating point, where the regrouping is a reassociation; the integer sum > is done in the unsigned type, since the reduction wraps while the signed > vector type has undefined overflow. > > #include <arm_neon.h> > int f (int32x4_t a, int32x4_t b) { return vaddvq_s32 (a) + vaddvq_s32 (b); } > > aarch64 -O3 before: > > addv s1, v1.4s > addv s0, v0.4s > fmov w1, s1 > fmov w0, s0 > add w0, w0, w1 > > after: > > add v0.4s, v0.4s, v1.4s > addv s0, v0.4s > fmov w0, s0 > > A pair of maxima goes from 8 instructions to 3. On a target without a > horizontal reduce instruction the reduction is open coded as a chain of > shuffles, so the saving is larger there, never smaller. > > For integers, preserve trapping and sanitized scalar overflow. > For floating point, require reassociation, insignificant zero signs, and non-trapping exceptions. > > Restrict the reassociative alternative to floating-point types. > Floating-point options must not license fixed-point saturation changes. > > Bootstrapped and tested on aarch64-none-linux-gnu. > Ok for trunk? > Thanks, > Kyrill > > gcc/ChangeLog: > > * match.pd (REDUC (a) op REDUC (b)): New simplifications combining > two reductions into one. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/vec-reduc-pair-1.c: New test. > * gcc.dg/tree-ssa/vec-reduc-pair-2.c: New test. > * gcc.dg/tree-ssa/vec-reduc-pair-3.c: New test. > * gcc.dg/tree-ssa/vec-reduc-pair-4.c: New test. > > Signed-off-by: Kyrylo Tkachov <[email protected]> Nice. Given the often multi-cycle nature of reductions, particularly on vectors with many elements, this seems particularly helpful. OK jeff