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

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #1 from liu zhenyu <lzy20000419 at outlook dot com> ---
The chunk size overflows in omp_adjust_chunk_size (gcc/omp-expand.cc), which
rounds a simd: chunk up to a multiple of the maximum vectorization factor:

      chunk_size = fold_build2 (PLUS_EXPR, type, chunk_size, vf_minus_one);
      return fold_build2 (BIT_AND_EXPR, type, chunk_size, negative_vf);

type is the type of the chunk expression (long here), so LONG_MAX + (vf - 1)
wraps. With -O2 -fopenmp the ompexp dump of the testcase in comment 0 has

      __builtin_GOMP_loop_nonmonotonic_dynamic_start (0, 100, 1,
-9223372036854775808, &.istart0.5, &.iend0.6);

At -O0 omp_max_vf () returns 1, so nothing is rounded and the call gets
9223372036854775807, which is why -O0 works. On x86-64 trunk with -O2 -fopenmp,
schedule(simd:dynamic, LONG_MAX - 15) prints 100 and schedule(simd:dynamic,
LONG_MAX - 14) hangs, which matches vf = 16.

The same happens when the chunk is not a constant; the addition is then done at
run time:

    #include <limits.h>
    #include <stdio.h>
    volatile long vch = LONG_MAX;
    int main (void)
    {
      int count = 0;
      long ch = vch;
    #pragma omp parallel for simd num_threads(2) schedule(simd:dynamic, ch)
reduction(+:count)
      for (long i = 0; i < 100; ++i)
        count++;
      printf ("%d\n", count);
      return count != 100;
    }

    $ gcc -O0 -fopenmp t.c && ./a.out
    100
    $ gcc -O2 -fopenmp t.c && ./a.out
    (does not terminate)

This is not a regression. The testcase in comment 0 hangs at -O2 -fopenmp with
6.2, 6.5, 7.5, 8.5, 9.1, 9.5, 10.5, 11.5, 12.5, 13.4, 14.4, 15.3, 16.2 and
trunk (17.0.0 20261119), and the non-constant form above hangs with 9.1, 9.5,
10.5, 11.5, 12.5, 13.4, 14.4, 15.3, 16.2 and trunk. 9.1 through trunk print 100
at -O0. The rounding probably has to saturate, or be skipped, when chunk_size +
vf - 1 does not fit in the chunk type.

Godbolt (trunk, non-constant chunk): https://godbolt.org/z/GWafb6dah

Suggested fields: Known to fail 6.2.0, 6.5.0, 7.5.0, 8.5.0, 9.1.0, 9.5.0,
10.5.0, 11.5.0, 12.5.0, 13.4.0, 14.4.0, 15.3.0, 16.2.0, 17.0.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to