https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128733

            Bug ID: 128733
           Summary: Outer-loop vectorization lost inside OpenMP regions:
                    "runtime alias check not supported for outer loop"
           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: ---

Created attachment 66169
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66169&action=edit
omp_outer

An outer loop that GCC vectorizes in a plain function is no longer vectorized
once it is inside "#pragma omp parallel for" (and likewise "!$omp parallel do",
"!$acc parallel loop" and "!$acc kernels" in Fortran), even with one thread.

--- omp_outer.c ---
void
kern (int n, int m, const double *restrict a, const double *restrict b,
      double *restrict c)
{
#pragma omp parallel for
  for (int i = 0; i < n; i++)
    {
      double x = a[i], y = b[i], s = x + 0.5 * y;
      int r = 0;
      do
        s = 0.75 * s + 0.125 * x + 0.0625 * y;
      while (++r < m);
      c[i] = s;
    }
}
---

$ gcc -O3 -march=x86-64-v3 -fopt-info-vec -c omp_outer.c
omp_outer.c:6:21: optimized: loop vectorized using 32 byte vectors and unroll
factor 4

$ gcc -O3 -march=x86-64-v3 -fopenmp -fdump-tree-vect-details -c omp_outer.c
  versioning for alias required: can't determine dependence between *_27 and
*_39
  runtime alias check not supported for outer loop.

In the outlined function a, b and c are loaded from the .omp_data_i block.
install_var_field drops their restrict qualifier (PR68640, needed for C), so
the dependence between the loads and the store to c is unknown, and
runtime_alias_check_p refuses versioning for a loop with an inner loop:

  /* FORNOW: We don't support versioning with outer-loop in either
     vectorization or loop distribution.  */

Innermost loops in the same situation are vectorized with a runtime alias
check, so the loss only affects outer-loop vectorization.

Timings (n = 2**20, m = 32, 100 calls, one P-core of an i9-13950HX,
-O3 -march=x86-64-v3, median of 3; identical checksums):

  serial (no -fopenmp)                      0.38 s
  -fopenmp, parallel for, 1 thread          1.44 s
  -fopenmp, parallel for, 8 threads         0.21 s
  -fopenmp, parallel for simd, 1 thread     0.37 s
  -fopenmp, parallel for simd, 8 threads    0.07 s

"parallel for simd" is a workaround because the simd clause removes the need
for the alias check.

Fortran is affected more broadly: every array dummy argument is restrict in
the serial procedure (the language forbids overlapping dummies), and all of
that is lost in the outlined OpenMP/OpenACC body.  PR68768 proposed keeping
restrict for Fortran and was closed WONTFIX as fragile.  The Fortran
equivalent (attached outer_unswitch_omp.f90) gives 0.37 s serial versus
1.40 s in "!$omp parallel do", 1.40 s in "!$acc parallel loop" and 1.46 s in
"!$acc kernels" (-fopenacc, host).

Would it be acceptable to allow runtime alias checks for outer-loop
vectorization at least when all data references of the alias pairs are in
the outer loop body (not inside the inner loop), so that the segment
lengths are the same as for an innermost loop?  I can try to prepare a
patch if that direction is welcome.
  • [Bug tree-optimization/12873... suvarchal.cheedela at awi dot de via Gcc-bugs

Reply via email to