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

            Bug ID: 129056
           Summary: [14/15/16/17 Regression] predcom store-store chain
                    writes back an array element the loop never stores
                    (store data race)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hhhuang at smu dot edu.sg
  Target Milestone: ---

Created attachment 66295
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66295&action=edit
preprocessed testcase

Compiler Explorer: https://godbolt.org/z/9EsKWd1fo

Note:
- works: 7.5
- fails: 8.1 and later at -O3, 12.1 and later also at -O2 (where
-ftree-loop-vectorize enables predcom), including 16.2 and trunk
- -fno-predictive-commoning makes it disappear
- -fno-allow-store-data-races (the default) does not help
- needs two CPUs; x86_64 and aarch64

$ gcc-17 -v
Using built-in specs.
COLLECT_GCC=gcc-17
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-17-20261122/configure --prefix=/w/out/gcc-trunk
--program-suffix=-17 --enable-languages=c --disable-bootstrap
--disable-multilib --disable-nls --disable-libsanitizer --enable-multiarch
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 17.0.0 20261122 (experimental) (GCC)
$ gcc-17 -O2 -pthread t.c && ./a.out
a[1] = 116327541, expected 200000000: lost 83672459 increments in 45320956
calls
$ gcc-17 -O2 -fno-predictive-commoning -pthread t.c && ./a.out
a[1] = 200000000, expected 200000000: lost 0 increments in 37927162 calls
$ cat t.c
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>

#define N 200000000u

uint32_t a[3];
atomic_int go, done;

__attribute__((noinline, noclone)) void
kernel (uint32_t *p, uint32_t x, uint32_t y, uint64_t lo, uint64_t hi)
{
  for (uint64_t i = lo; i < hi; i++)
    {
      p[i] = x + (uint32_t) i;
      p[i + 2] = y ^ (uint32_t) i;
    }
}

static void *
bump (void *arg)
{
  volatile uint32_t *b = &a[1];   /* volatile: N separate increments */
  atomic_store (&go, 1);
  for (unsigned k = 0; k < N; k++)
    (*b)++;
  atomic_store (&done, 1);
  return arg;
}

int
main (void)
{
  pthread_t t;
  unsigned long calls = 0;
  pthread_create (&t, 0, bump, 0);
  while (!atomic_load (&go))
    ;
  while (!atomic_load (&done))
    {
      kernel (a, 7, 9, 0, 1);     /* one iteration: stores a[0] and a[2] */
      calls++;
    }
  pthread_join (t, 0);
  printf ("a[1] = %u, expected %u: lost %u increments in %lu calls\n",
          a[1], N, N - a[1], calls);
  return a[1] != N;
}

The main thread only writes a[0] and a[2] and the other thread only touches
a[1], so the source has no data race and a[1] should end as N. No warnings with
-Wall -Wextra, -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations
makes no difference, and -fsanitize=thread is clean (gcc 16.2).

kernel at -O2 on x86_64:

kernel:
        movl    %edx, %r9d
        movq    %rcx, %r10
        cmpq    %r8, %rcx
        jnb     .L5
        movl    4(%rdi,%rcx,4), %edx    # load p[lo + 1] before the loop
        movq    %rcx, %rax
.L7:
        leal    (%rsi,%rax), %ecx
        movl    %ecx, (%rdi,%rax,4)     # p[i] = x + i
        movl    %edx, %ecx
        movl    %r9d, %edx
        xorl    %eax, %edx
        addq    $1, %rax
        cmpq    %rax, %r8
        jne     .L7
        subq    %r10, %r8
        leaq    (%rdi,%r8,4), %rax
        movl    %ecx, (%rax,%r10,4)     # store p[hi]
        leaq    4(%rdi,%r8,4), %rax
        movl    %edx, (%rax,%r10,4)     # store p[hi + 1]
.L5:
        ret

The loop runs once here (lo = 0, hi = 1), so the store to p[hi] writes back to
p[1] the value loaded before the loop, and an update from the other thread in
between is lost. See prepare_initializers_chain_store_elim (the "bubbles"
comment) and prepare_finalizers_chain in tree-predcom.cc.

Reply via email to