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

            Bug ID: 128915
           Summary: wrong code with -fopenmp-simd: '#pragma omp simd
                    reduction' over an array section with nonzero lower
                    bound merges into the wrong elements
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* Godbolt: x86-64 gcc (trunk), flags: -O2 -fopenmp-simd
   omp simd reduction over an array section with nonzero lower bound:
   the partial results are merged into h[0..3] instead of h[2..5].
   gcc -O0 -fopenmp-simd repro.c   (also -O2/-O3, GCC 11..trunk)
   Expected (sequential / clang -fopenmp-simd / gcc -fopenmp parallel for):
     0 0 11 22 44 88 0 0
   GCC -fopenmp-simd: 11 22 44 88 0 0 0 0   (-O0)
                      0 22 44 88 0 0 0 0    (GCC 11 -O2: h[0] also lost)  */
#include <stdio.h>
unsigned u[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
__attribute__((noipa)) void f(int n, unsigned *out) {
  unsigned h[8] = {0};
#pragma omp simd reduction(^:h[2:4])
  for (int i = 0; i < n; i++)
    h[2 + (i % 4)] ^= u[i];
  for (int k = 0; k < 8; k++) out[k] = h[k];
}
int main(void) {
  unsigned o[8], e[8] = {0, 0, 0x11, 0x22, 0x44, 0x88, 0, 0};
  f(8, o);
  for (int k = 0; k < 8; k++) printf("%x ", o[k]);
  printf("\n");
  for (int k = 0; k < 8; k++) if (o[k] != e[k]) return 1;
  return 0;
}
```

Resulted in this output:
```
$ gcc -O2 -fopenmp-simd repro.c && ./a.out
11 22 44 88 0 0 0 0
(exit 1; expected 0 0 11 22 44 88 0 0 as without -fopenmp-simd and with clang
-fopenmp-simd)
```

Trunk: https://godbolt.org/z/Mbx7PjPPd (x86-64 gcc trunk, -O2 -fopenmp-simd,
executed; wrong as of 2026-11-14; also GCC 14.2 and GCC 11.4; -O0 -fopenmp-simd
is wrong too)

Reply via email to