Fix a case in which the vector cost model was ignored
authorRichard Sandiford <richard.sandiford@arm.com>
Mon, 18 Mar 2019 12:25:32 +0000 (12:25 +0000)
committerRichard Sandiford <rsandifo@gcc.gnu.org>
Mon, 18 Mar 2019 12:25:32 +0000 (12:25 +0000)
commitc57546fe94712a723bc2e123445dc5d9b0a0ca1a
treee39368cb43ea4317b388677a39f9487d60b6d38a
parentcfce6e055d98cf55a912dfe814d48a331ec2eee9
Fix a case in which the vector cost model was ignored

This patch fixes a case in which we vectorised something with a
fully-predicated loop even after the cost model had rejected it.
E.g. the loop in the testcase has the costs:

  Vector inside of loop cost: 27
  Vector prologue cost: 0
  Vector epilogue cost: 0
  Scalar iteration cost: 7
  Scalar outside cost: 6
  Vector outside cost: 0
  prologue iterations: 0
  epilogue iterations: 0

and we can see that the loop executes at most three times, but we
decided to vectorise it anyway.

(The costs here are equal for three iterations, but the same thing
happens even when the vector code is strictly more expensive.)

The problem is the handling of "/VF" in:

  /* Calculate number of iterations required to make the vector version
     profitable, relative to the loop bodies only.  The following condition
     must hold true:
     SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
     where
     SIC = scalar iteration cost, VIC = vector iteration cost,
     VOC = vector outside cost, VF = vectorization factor,
     PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
     SOC = scalar outside cost for run time cost model check.  */

We treat the "/VF" as truncating, but for fully-predicated loops, it's
closer to a ceil division, since fractional iterations are handled by a
full iteration with some predicate bits set to false.

The easiest fix seemed to be to calculate the minimum number of vector
iterations first, then use that to calculate the minimum number of scalar
iterations.

Calculating the minimum number of vector iterations might make sense for
unpredicated loops too, since calculating the scalar niters directly
doesn't take into account the fact that the VIC multiple has to be an
integer.  But the handling of PL_ITERS and EP_ITERS for unpredicated
loops is a bit hand-wavy anyway, so maybe vagueness here cancels out
vagueness there?

Either way, changing this for unpredicated loops would be much too
invasive for stage 4, so the patch keeps it specific to fully-predicated
loops (i.e. SVE) for now.  There's no functional change for other targets.

2019-03-18  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
* tree-vect-loop.c (vect_estimate_min_profitable_iters): Fix the
calculation of the minimum number of scalar iterations for
fully-predicated loops.

gcc/testsuite/
* gcc.target/aarch64/sve/cost_model_1.c: New test.

From-SVN: r269763
gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/aarch64/sve/cost_model_1.c [new file with mode: 0644]
gcc/tree-vect-loop.c