Re: Handle SLP of call pattern statements
Richard Biener writes: > On Fri, Jul 20, 2018 at 12:22 PM Richard Sandiford > wrote: >> >> We couldn't vectorise: >> >> for (int j = 0; j < n; ++j) >> { >> for (int i = 0; i < 16; ++i) >> a[i] = (b[i] + c[i]) >> 1; >> a += step; >> b += step; >> c += step; >> } >> >> at -O3 because cunrolli unrolled the inner loop and SLP couldn't handle >> AVG_FLOOR patterns (see also PR86504). The problem was some overly >> strict checking of pattern statements compared to normal statements >> in vect_get_and_check_slp_defs: >> >> switch (gimple_code (def_stmt)) >> { >> case GIMPLE_PHI: >> case GIMPLE_ASSIGN: >> break; >> >> default: >> if (dump_enabled_p ()) >> dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, >> "unsupported defining stmt:\n"); >> return -1; >> } >> >> The easy fix would have been to add GIMPLE_CALL to the switch, >> but I don't think the switch is doing anything useful. We only create >> pattern statements that the rest of the vectoriser can handle, and code >> in this function and elsewhere check whether SLP is possible. >> >> I'm also not sure why: >> >> if (!first && !oprnd_info->first_pattern >> /* Allow different pattern state for the defs of the >> first stmt in reduction chains. */ >> && (oprnd_info->first_dt != vect_reduction_def >> >> is necessary. All that should matter is that the statements in the >> node are "similar enough". It turned out to be quite hard to find a >> convincing example that used a mixture of pattern and non-pattern >> statements, so bb-slp-pow-1.c is the best I could come up with. >> But it does show that the combination of "xi * xi" statements and >> "pow (xj, 2) -> xj * xj" patterns are handled correctly. >> >> The patch therefore just removes the whole if block. >> >> The loop also needed commutative swapping to be extended to at least >> AVG_FLOOR. >> >> This gives +3.9% on 525.x264_r at -O3. >> >> Tested on aarch64-linux-gnu (with and without SVE), aarch64_be-elf >> and x86_64-linux-gnu. OK to install? > > Always nice to see seemingly dead code go. OK if you can still > build SPEC with this change and pass a test run. > > At least I _do_ seem to remember having "issues" in this area... Thanks. I've now applied it after testing SPEC2006 and SPEC2017 on aarch64-linux-gnu and x86_64-linux-gnu. I also ran it through our internal SVE benchmark set, which includes those two and various other things. Richard
Re: Handle SLP of call pattern statements
On Fri, Jul 20, 2018 at 12:22 PM Richard Sandiford wrote: > > We couldn't vectorise: > > for (int j = 0; j < n; ++j) > { > for (int i = 0; i < 16; ++i) > a[i] = (b[i] + c[i]) >> 1; > a += step; > b += step; > c += step; > } > > at -O3 because cunrolli unrolled the inner loop and SLP couldn't handle > AVG_FLOOR patterns (see also PR86504). The problem was some overly > strict checking of pattern statements compared to normal statements > in vect_get_and_check_slp_defs: > > switch (gimple_code (def_stmt)) > { > case GIMPLE_PHI: > case GIMPLE_ASSIGN: > break; > > default: > if (dump_enabled_p ()) > dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, > "unsupported defining stmt:\n"); > return -1; > } > > The easy fix would have been to add GIMPLE_CALL to the switch, > but I don't think the switch is doing anything useful. We only create > pattern statements that the rest of the vectoriser can handle, and code > in this function and elsewhere check whether SLP is possible. > > I'm also not sure why: > > if (!first && !oprnd_info->first_pattern > /* Allow different pattern state for the defs of the > first stmt in reduction chains. */ > && (oprnd_info->first_dt != vect_reduction_def > > is necessary. All that should matter is that the statements in the > node are "similar enough". It turned out to be quite hard to find a > convincing example that used a mixture of pattern and non-pattern > statements, so bb-slp-pow-1.c is the best I could come up with. > But it does show that the combination of "xi * xi" statements and > "pow (xj, 2) -> xj * xj" patterns are handled correctly. > > The patch therefore just removes the whole if block. > > The loop also needed commutative swapping to be extended to at least > AVG_FLOOR. > > This gives +3.9% on 525.x264_r at -O3. > > Tested on aarch64-linux-gnu (with and without SVE), aarch64_be-elf > and x86_64-linux-gnu. OK to install? Always nice to see seemingly dead code go. OK if you can still build SPEC with this change and pass a test run. At least I _do_ seem to remember having "issues" in this area... Thanks, Richard. > Richard > > > 2018-07-20 Richard Sandiford > > gcc/ > * internal-fn.h (first_commutative_argument): Declare. > * internal-fn.c (first_commutative_argument): New function. > * tree-vect-slp.c (vect_get_and_check_slp_defs): Remove extra > restrictions for pattern statements. Use first_commutative_argument > to look for commutative operands in calls to internal functions. > > gcc/testsuite/ > * gcc.dg/vect/bb-slp-over-widen-1.c: Expect AVG_FLOOR to be used > on vect_avg_qi targets. > * gcc.dg/vect/bb-slp-over-widen-2.c: Likewise. > * gcc.dg/vect/bb-slp-pow-1.c: New test. > * gcc.dg/vect/vect-avg-15.c: Likewise. > > Index: gcc/internal-fn.h > === > --- gcc/internal-fn.h 2018-07-13 10:11:14.009847140 +0100 > +++ gcc/internal-fn.h 2018-07-20 11:18:58.167047743 +0100 > @@ -201,6 +201,8 @@ direct_internal_fn_supported_p (internal > opt_type); > } > > +extern int first_commutative_argument (internal_fn); > + > extern bool set_edom_supported_p (void); > > extern internal_fn get_conditional_internal_fn (tree_code); > Index: gcc/internal-fn.c > === > --- gcc/internal-fn.c 2018-07-13 10:11:14.009847140 +0100 > +++ gcc/internal-fn.c 2018-07-20 11:18:58.163047778 +0100 > @@ -3183,6 +3183,42 @@ direct_internal_fn_supported_p (internal >return direct_internal_fn_supported_p (fn, tree_pair (type, type), > opt_type); > } > > +/* If FN is commutative in two consecutive arguments, return the > + index of the first, otherwise return -1. */ > + > +int > +first_commutative_argument (internal_fn fn) > +{ > + switch (fn) > +{ > +case IFN_FMA: > +case IFN_FMS: > +case IFN_FNMA: > +case IFN_FNMS: > +case IFN_AVG_FLOOR: > +case IFN_AVG_CEIL: > +case IFN_FMIN: > +case IFN_FMAX: > + return 0; > + > +case IFN_COND_ADD: > +case IFN_COND_MUL: > +case IFN_COND_MIN: > +case IFN_COND_MAX: > +case IFN_COND_AND: > +case IFN_COND_IOR: > +case IFN_COND_XOR: > +case IFN_COND_FMA: > +case IFN_COND_FMS: > +case IFN_COND_FNMA: > +case IFN_COND_FNMS: > + return 1; > + > +default: > + return -1; > +} > +} > + > /* Return true if IFN_SET_EDOM is supported. */ > > bool > Index: gcc/tree-vect-slp.c > === > --- gcc/tree-vect-slp.c 2018-07-13 10:11:15.113837768 +0100 > +++ gcc/tree-vect-slp.c
Handle SLP of call pattern statements
We couldn't vectorise: for (int j = 0; j < n; ++j) { for (int i = 0; i < 16; ++i) a[i] = (b[i] + c[i]) >> 1; a += step; b += step; c += step; } at -O3 because cunrolli unrolled the inner loop and SLP couldn't handle AVG_FLOOR patterns (see also PR86504). The problem was some overly strict checking of pattern statements compared to normal statements in vect_get_and_check_slp_defs: switch (gimple_code (def_stmt)) { case GIMPLE_PHI: case GIMPLE_ASSIGN: break; default: if (dump_enabled_p ()) dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, "unsupported defining stmt:\n"); return -1; } The easy fix would have been to add GIMPLE_CALL to the switch, but I don't think the switch is doing anything useful. We only create pattern statements that the rest of the vectoriser can handle, and code in this function and elsewhere check whether SLP is possible. I'm also not sure why: if (!first && !oprnd_info->first_pattern /* Allow different pattern state for the defs of the first stmt in reduction chains. */ && (oprnd_info->first_dt != vect_reduction_def is necessary. All that should matter is that the statements in the node are "similar enough". It turned out to be quite hard to find a convincing example that used a mixture of pattern and non-pattern statements, so bb-slp-pow-1.c is the best I could come up with. But it does show that the combination of "xi * xi" statements and "pow (xj, 2) -> xj * xj" patterns are handled correctly. The patch therefore just removes the whole if block. The loop also needed commutative swapping to be extended to at least AVG_FLOOR. This gives +3.9% on 525.x264_r at -O3. Tested on aarch64-linux-gnu (with and without SVE), aarch64_be-elf and x86_64-linux-gnu. OK to install? Richard 2018-07-20 Richard Sandiford gcc/ * internal-fn.h (first_commutative_argument): Declare. * internal-fn.c (first_commutative_argument): New function. * tree-vect-slp.c (vect_get_and_check_slp_defs): Remove extra restrictions for pattern statements. Use first_commutative_argument to look for commutative operands in calls to internal functions. gcc/testsuite/ * gcc.dg/vect/bb-slp-over-widen-1.c: Expect AVG_FLOOR to be used on vect_avg_qi targets. * gcc.dg/vect/bb-slp-over-widen-2.c: Likewise. * gcc.dg/vect/bb-slp-pow-1.c: New test. * gcc.dg/vect/vect-avg-15.c: Likewise. Index: gcc/internal-fn.h === --- gcc/internal-fn.h 2018-07-13 10:11:14.009847140 +0100 +++ gcc/internal-fn.h 2018-07-20 11:18:58.167047743 +0100 @@ -201,6 +201,8 @@ direct_internal_fn_supported_p (internal opt_type); } +extern int first_commutative_argument (internal_fn); + extern bool set_edom_supported_p (void); extern internal_fn get_conditional_internal_fn (tree_code); Index: gcc/internal-fn.c === --- gcc/internal-fn.c 2018-07-13 10:11:14.009847140 +0100 +++ gcc/internal-fn.c 2018-07-20 11:18:58.163047778 +0100 @@ -3183,6 +3183,42 @@ direct_internal_fn_supported_p (internal return direct_internal_fn_supported_p (fn, tree_pair (type, type), opt_type); } +/* If FN is commutative in two consecutive arguments, return the + index of the first, otherwise return -1. */ + +int +first_commutative_argument (internal_fn fn) +{ + switch (fn) +{ +case IFN_FMA: +case IFN_FMS: +case IFN_FNMA: +case IFN_FNMS: +case IFN_AVG_FLOOR: +case IFN_AVG_CEIL: +case IFN_FMIN: +case IFN_FMAX: + return 0; + +case IFN_COND_ADD: +case IFN_COND_MUL: +case IFN_COND_MIN: +case IFN_COND_MAX: +case IFN_COND_AND: +case IFN_COND_IOR: +case IFN_COND_XOR: +case IFN_COND_FMA: +case IFN_COND_FMS: +case IFN_COND_FNMA: +case IFN_COND_FNMS: + return 1; + +default: + return -1; +} +} + /* Return true if IFN_SET_EDOM is supported. */ bool Index: gcc/tree-vect-slp.c === --- gcc/tree-vect-slp.c 2018-07-13 10:11:15.113837768 +0100 +++ gcc/tree-vect-slp.c 2018-07-20 11:18:58.167047743 +0100 @@ -299,15 +299,20 @@ vect_get_and_check_slp_defs (vec_info *v bool pattern = false; slp_oprnd_info oprnd_info; int first_op_idx = 1; - bool commutative = false; + unsigned int commutative_op = -1U; bool first_op_cond = false; bool first = stmt_num == 0; bool second = stmt_num == 1; - if (is_gimple_call (stmt)) + if (gcall *call = dyn_cast (stmt)) { - number_of_oprnds = gimple_call_num_args (stmt); + number_of_oprnds = gimple_call_num_args