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

            Bug ID: 129068
           Summary: [aarch64] wrong code at -O3: cse1 deletes a
                    type-changing store to a shared stack slot, sched2
                    then hoists a load above the surviving store
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: simon.sigurdhsson at smarteye dot se
  Target Milestone: ---

The testcase below includes no other files, so it is the complete source; no
preprocessed
file is needed.

```cpp
struct Ts { constexpr Ts() : t(0) {} unsigned long t; };

struct P { Ts ts; int ids[2]; bool v; unsigned char st; unsigned char n; };
struct C { bool v; float q; unsigned char st; Ts ts; int id; };
struct Q { Q() { for (auto& d : data) d = 1; } int data[50]; };

template <typename T, int N> void fill(T const& v, T (&a)[N]) { for (int i = 0;
i < N; ++i) a[i] = v; }

__attribute__((noipa)) void sink(void*) {}

int main() {
  Q q[1];
  P p[5];
  C c[5];
  P p2[5];
  C c2[5];
  sink(p); sink(p2); sink(c2); sink(q);
  fill({}, q);
  sink(q);
  fill({}, p);
  fill({}, c);
  sink(c);
  return c[1].q != 0.0f;
}
```

## Command line and result

```
$ aarch64-unknown-linux-gnu-g++ -Wall -Wextra -O3 -march=armv8-a+crc
-fno-stack-protector t.cc
$ qemu-aarch64 -L /usr/aarch64-linux-gnu ./a.out; echo $?
1
```

The compiler prints no errors or warnings, also with `-Wall -Wextra`.

Expected result: 0, because `fill({}, c)` zeroes `c`. Actual result: 1.

`-fno-stack-protector` is only necessary with compilers that enable
`-fstack-protector-strong`
by default (for example Ubuntu packages), because the stack protector changes
the stack layout.

## Output of `gcc -v`

```
Target: aarch64-unknown-linux-gnu
Configured with: /opt/.build/aarch64-unknown-linux-gnu/src/gcc/configure
--build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu
--target=aarch64-unknown-linux-gnu
--prefix=/opt/compiler-explorer/arm64/gcc-trunk-20261125/aarch64-unknown-linux-gnu
--exec_prefix=/opt/compiler-explorer/arm64/gcc-trunk-20261125/aarch64-unknown-linux-gnu
--with-sysroot=/opt/compiler-explorer/arm64/gcc-trunk-20261125/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot
--enable-languages=c,c++,fortran --enable-__cxa_atexit --disable-libmudflap
--enable-libgomp --enable-libssp --enable-libquadmath
--enable-libquadmath-support --disable-libsanitizer --disable-libmpx
--with-gmp=/opt/.build/aarch64-unknown-linux-gnu/buildtools
--with-mpfr=/opt/.build/aarch64-unknown-linux-gnu/buildtools
--with-mpc=/opt/.build/aarch64-unknown-linux-gnu/buildtools
--with-isl=/opt/.build/aarch64-unknown-linux-gnu/buildtools --enable-lto
--enable-threads=posix --enable-default-pie --enable-target-optspace
--disable-plugin --disable-nls --disable-multilib
--with-local-prefix=/opt/compiler-explorer/arm64/gcc-trunk-20261125/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot
--enable-long-long
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 17.0.0 20261125 (experimental) (GCC)
```

## Checks for a user error

- `-fsanitize=undefined`: the program runs with no diagnostics.
- `-Wall -Wextra`: no warnings.
- `-fwrapv` and `-fno-aggressive-loop-optimizations`: the program still returns
1.
- `-D_GLIBCXX_ASSERTIONS` does not apply: the testcase uses no library code.
- `-fno-strict-aliasing` makes the bug go away, but the testcase does not
violate the aliasing
  rules. The objects are only accessed through their own types.

## Workarounds

The program returns 0 with `-O2`, `-fstack-reuse=none`, `-fno-strict-aliasing`,
or
`-fno-schedule-insns2`. Without `constexpr` on `Ts()`, the program returns 0
with GCC 11.5 to
16.0.1 (Ubuntu packages), but still returns 1 with trunk 20261125.

## Analysis

The `Q{}`, `P{}` and `C{}` temporaries share one stack slot. From
`-fdump-rtl-*-details`
(trunk 20261125, `-O3 -march=armv8-a+crc -fno-stack-protector`):

- cse1, insn 359 zeroes the first 16 bytes of the `P{}` temporary, alias set 7:
  ```
  (insn 359 358 360 2 (set (mem/c:TI (plus:DI (reg/f:DI 64 sfp)
                  (const_int -208 [0xffffffffffffff30])) [7 MEM[(struct P
*)_342]+0 S16 A128])
          (const_int 0 [0])) "t.cc":20:7 124 {*movti_aarch64}
  ```
- cse1, insn 411 zeroes the first 16 bytes of the `C{}` temporary at the same
address, alias
  set 10:
  ```
  (insn 411 410 412 2 (set (mem/c:TI (plus:DI (reg/f:DI 64 sfp)
                  (const_int -208 [0xffffffffffffff30])) [10 MEM[(struct C
*)_342]+0 S16 A128])
          (const_int 0 [0])) "t.cc":21:7 124 {*movti_aarch64}
  ```
  cse1 deletes it, because the same value is already there:
  `deferring deletion of insn with uid = 411.`
- cse1, insn 426 loads the `C{}` temporary, alias set 10, to copy it into
`c[1]`:
  ```
  (insn 426 425 428 2 (set (reg:V4SI 168)
          (mem/c:V4SI (reg:DI 167) [10 MEM <unsigned char[28]> [(struct C
*)_342]+0 S16 A128])) "t.cc":7:93 discrim 163841 1442 {*aarch64_simd_movv4si}
  ```
- sched2 moves load 426 (alias set 10) above store 359 (alias set 7), because
the alias sets do
  not conflict:
  ```
  ;;     74--> b  0: i 426 v30=[sp+0x320]
  ;;     75--> b  0: i 359 [x0]=0
  ```
  The load reads the stale `Q{}` data (`1`s) instead of zeros.

The deleted store was the only store of the type that the load uses, so the
TBAA information
that sched2 relies on is wrong after cse1.

Related bugs:

- PR 111231 (open, armhf): the same mechanism, but postreload deletes the
store, not cse1.
  Comment 29 says that `alias_set_subset_of ()` and `alias_sets_conflict_p ()`
disagree. `P`
  contains `unsigned char` members, so the same disagreement can apply here. I
did not confirm
  this.
- PR 93946 (fixed in GCC 10): r10-7635 added the TBAA check for redundant store
removal in
  `cse_insn`. That check does not prevent the deletion of insn 411.
- PR 106187 (fixed in 11.4 and 12.1): the cselib/postreload counterpart. The
fix did not change
  cse.cc.
- PR 111591 (reopened, ppc64be): wrong code with stack slot sharing and
different alias sets;
  `-fstack-reuse=none` also hides it.
- PR 107115 (open): the general problem of stores that change the effective
type, and sched2.

## Versions

Regression in GCC 11. Results with `-O3 -march=armv8-a+crc
-fno-stack-protector`, with and
without `-mtune=cortex-a72`:

| GCC                                                      | Build             
   | Result      |
|----------------------------------------------------------|-----------------------|-------------|
| 7.3, 7.5, 9.3, 10.2, 10.5                                | Compiler Explorer 
   | correct     |
| 11.1, 11.4, 12.1, 12.5, 15.2, 16.1, 16.2, trunk 20261125 | Compiler Explorer 
   | wrong code  |
| 11.5, 12.5, 13.4, 14.3, 16.0.1 20260322                  | Ubuntu 26.04
packages | wrong code  |
| 15.2                                                     | Ubuntu 26.04
package  | wrong code* |

*: with `-mtune=cortex-a72` only
  • [Bug rtl-optimization/12... simon.sigurdhsson at smarteye dot se via Gcc-bugs

Reply via email to