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

            Bug ID: 128766
           Summary: [14/15/16/17 Regression] wrong doloop count after
                    runtime unrolling of do ... while (--n) with n == 0
                    (niter + 1 wraps in unroll_loop_runtime_iterations):
                    infinite loop on powerpc64le
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---

Fails on powerpc64le with trunk (gcc 17.0.0 20261115) and with 11.2.0 through
16.1.0 at -O2; 6.3.0 works, also with -funroll-loops. The gcc 8 and gcc 9 based
powerpc64le compilers on Compiler Explorer (AT12.0, AT13.0) do not unroll at
-O2, but compute a wrong count with -O2 -funroll-loops (CTR = 2^32 for n == 0,
so the body runs 2^35 times). s390x also fails at -O2 with 13.4.0 through
16.1.0 (11.2.0 and 12.5.0 do not unroll this loop at -O2 there).
Target: powerpc64le-linux-gnu and s390x-linux-gnu (targets with doloop, where
-O2 enables -funroll-loops).

    unsigned a[2] = { 1, 1 };

    __attribute__((noipa)) unsigned long
    f (unsigned n)
    {
      unsigned long c = 0;
      do
        c += a[n & 1];
      while (--n != 0);
      return c;
    }

    int
    main (void)
    {
      if (f (0) != 0x100000000UL)
        __builtin_abort ();
      return 0;
    }

    powerpc64le-linux-gnu-gcc -O2 t.c && qemu-ppc64le ./a.out
    s390x-linux-gnu-gcc -O2 t.c && qemu-s390x ./a.out

Expected: exit 0. With n == 0 the body runs 2^32 times, so f returns
0x100000000.
Observed: powerpc64le does not terminate (CTR is loaded with 0, so bdnz runs
2^64 times). s390x aborts (f returns 0x200000000).
-fno-unroll-loops, -fno-branch-count-reg and -fdisable-rtl-loop2_doloop work.

Trunk -O2 code for f on powerpc64le: for even n the unrolled loop is entered
with rldicl 9,9,63,33; mtctr 9, i.e. CTR = n >> 1, which is 0 for n == 0.

Godbolt (powerpc64le trunk, assembly): https://godbolt.org/z/eo59Eoqz6

Cause: unroll_loop_runtime_iterations (loop-unroll.cc) adds 1 to the latch
count when the loop exits at the end. For n == 0 this is 2^32, which wraps to 0
in SImode. The unrolled code handles this, but the cached loop description is
then updated as

      desc->niter_expr = simplify_gen_binary (UDIV, desc->mode, old_niter,
max_unroll + 1);
      ...
      if (exit_at_end)
        desc->niter_expr = simplify_gen_binary (MINUS, desc->mode,
desc->niter_expr, const1_rtx);

which gives 0 / 2 - 1 = 0xffffffff for n == 0. loop2_doloop builds the count
register value from this description. On powerpc64le, doloop_simplify_count
then folds zero_extend (x - 1) + 1 to zero_extend (x), so CTR becomes 0.

Possible fix: when niter + 1 can wrap, compute the new latch count from the
original latch count, for example (niter - ((niter + 1) & max_unroll)) /
(max_unroll + 1), without the later subtraction of one.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.
  • [Bug rtl-optimization/128766] ... lzy20000419 at outlook dot com via Gcc-bugs

Reply via email to