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

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> ---
This is not limited to signed IVs or to a standalone tile. The tile end that
omp_apply_tile computes can wrap even when the source loop does not, and an
unsigned IV under a worksharing loop gives a wrong iteration count:

    #include <stdio.h>
    int n;
    __attribute__((noipa)) int f (void)
    {
      n = 0;
    #pragma omp for
    #pragma omp tile sizes(4)
      for (unsigned i = 4294967288u; i < 4294967294u; i++)   /* 6 iterations,
no wrap in the source */
        n++;
      return n;
    }
    int main (void) { printf ("%d\n", f ()); return 0; }

    $ gcc -O2 -fopenmp t.c && ./a.out
    4

Expected 6. 15.1, 15.3, 16.1, 16.2 and trunk (17.0.0 20261119) print 4 at -O0
and -O2 with -fopenmp. With int i = __INT_MAX__ - 7; i < __INT_MAX__ under
#pragma omp for, both tile sizes(4) and unroll partial(4) give 4 instead of 7
with the same versions. Without #pragma omp for, the unsigned loop above does
not terminate on trunk with -O2 -fopenmp (it prints 6 without -fopenmp), so the
hang from comment 0 is not specific to signed IVs either.

The -O0 gimple dump of the unsigned case shows where the iterations go:

    for (D.3608 = 4294967288; D.3608 < 4294967294; D.3608 = D.3608 + 4)
      ...
        _1 = D.3608 + 4;
        D.3609 = MIN_EXPR <_1, 4294967294>;

omp_apply_tile (gcc/omp-general.cc) builds the tile end as MIN (griddecl +
size*step, ub) in the IV type (MAX for > and >=, ub + 1 for <=). For the second
tile, 4294967292 + 4 wraps to 0, MIN picks 0, and the tile is skipped. For the
int loop the same expression has no MIN_EXPR at all in the dump (only _1 =
D.3608 + 4;), because the MIN is folded away on the assumption that the signed
addition does not overflow. The grid loop increment D.3608 = D.3608 + 4 wraps
in the same way, which is why the standalone construct (lowered in
gimplify_omp_loop_xform, without the logical iteration count of the worksharing
loop) does not terminate.

So doing the MIN in an unsigned type would not be enough. The tile end and the
grid loop exit test probably have to be computed without wrapping, for example
by comparing against the remaining distance to ub before adding size*step. This
is not a regression, since tile and unroll are new in GCC 15.

Godbolt (trunk, unsigned case with execution): https://godbolt.org/z/7rvfa8W79

Suggested fields: Known to fail 15.1.0, 15.3.0, 16.1.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