https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128732
Bug ID: 128732
Summary: Outer loop not vectorized when the invariant guard of
a runtime-bounded inner loop does not skip all side
effects (follow-up to PR117510)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: suvarchal.cheedela at awi dot de
Target Milestone: ---
An outer loop whose iterations are independent and that contains an inner
loop with a runtime, outer-invariant trip count is not vectorized. The same
loop is vectorized as an outer loop when the inner trip count is a constant,
or when the inner loop is written so that it executes at least once. The
blocker appears to be the zero-trip guard around the inner loop (a BB with
two successors inside the outer loop that does not exit it).
--- outer.c ---
void
kern (int n, int m, const double *restrict a, const double *restrict b,
double *restrict c)
{
for (int i = 0; i < n; i++)
{
double x = a[i], y = b[i], s = x + 0.5 * y;
for (int r = 0; r < m; r++)
s = 0.75 * s + 0.125 * x + 0.0625 * y;
c[i] = s;
}
}
---
$ gcc -O3 -march=x86-64-v3 -fopt-info-vec-all -c outer.c
outer.c:5:21: missed: not vectorized: unsupported control flow in loop.
Replacing "r < m" by "r < 32":
outer.c:5:21: optimized: loop vectorized using 32 byte vectors and unroll
factor 4
Clamping m >= 1 and writing the inner loop as do { ... } while (++r < m):
outer_dowhile.c:7:21: optimized: loop vectorized using 32 byte vectors and
unroll factor 4
So outer-loop vectorization of the inner loop body with a runtime trip
count already works; only the guarded (possibly zero-trip) form is
rejected. The existing testcases gcc.dg/vect/no-scevccp-outer-9.c and
no-scevccp-outer-10.c xfail "OUTER LOOP VECTORIZED" for what looks like the
same shape.
Fortran DO loops always produce this guarded form, so any Fortran kernel of
the form
do i = 1, n
s = ...
do r = 1, m ! m a dummy argument / runtime value
s = 0.75d0*s + ...
end do
c(i) = s
end do
stays scalar, also with !$omp simd on the outer loop (-fopenmp-simd).
Timings (n = 2**20, m = 32, 100 calls, one P-core of an i9-13950HX,
-O3 -march=x86-64-v3, median of 3). The kernel is compute bound: the same
times within a few percent for working sets in L2, L3 and DRAM.
C, gcc trunk 7647c00b77e, guarded inner loop 1.42 s
C, gcc trunk, do-while inner loop 0.36 s
C, gcc trunk, outer loop versioned by hand 0.37 s (see below)
Fortran, gfortran trunk 1.40 s (also with !$omp simd)
Fortran, gfortran trunk, constant m = 32 0.36 s
Fortran, ifx 2025.3 -fp-model=precise,
!$omp simd on the outer loop 0.33 s
Tested with GCC 17.0.0 20261113 (experimental); GCC 15.2 and 16.0.1 behave
the same.
This is the case left open by PR117510 (r15-5082). There,
find_loop_guard hoists the "m > 0" guard because, with the guard false, the
outer loop has no side effects (the only store was sunk into the skipped
region), and the testcase from PR117510 vectorizes on trunk. Here the
outer body stores c[i] on both paths, so the guard cannot be hoisted
without duplicating the outer loop:
$ gcc -O3 -march=x86-64-v3 -fdump-tree-unswitch-details -c outer.c
outer.c:5:21: note: Considering guard 3 -> 6 in loop 1
outer.c:5:21: missed: Block 6 has side effects
Versioning the outer loop by hand on the invariant guard
if (m > 0)
for (i ...) { ...; int r = 0; do s = ...; while (++r < m); c[i] = s; }
else
for (i ...) c[i] = a[i] + 0.5 * b[i];
gets the m > 0 copy vectorized by trunk as it is (0.37 s, correct for all
m).
Would extending tree_unswitch_outer_loop to version the outer loop on such
a guard (with code duplication, under a size limit like
max-unswitch-insns) be acceptable, or should the vectorizer version the
loop on the guard itself? I am happy to work on a patch for either.