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

            Bug ID: 93434
           Summary: Miscompilation with -O3 starting from 8.1
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rlivings at mathworks dot com
  Target Milestone: ---

We're working on upgrading our code base to GCC 8.3. In doing so one of our
tests started failing with a wrong answer. After narrowing down we see that the
code:

#include <stdio.h>

typedef struct creal_T {
  double re;
  double im;
} creal_T;

#define N 16
int main() {
  int k;
  int i;
  int j;
  creal_T t2[N];
  double inval;

  inval = 1.0;
  for (j = 0; j < N; ++j) {
    t2[j].re = 0;
    t2[j].im = 0;
  }

  for (j = 0; j < N/4; j++) {
    i = j * 4;
    t2[i].re = inval;
    t2[i].im = inval;
    /* Uncomment for correct behavior */
    /* printf("t2[%d] = %g + %g*i\n", i, t2[i].re, t2[i].im); */
    k = i + 3;
    t2[k].re = inval;
    t2[k].im = inval;
    t2[i] = t2[k];
    t2[k].re = inval;
  }

  printf("\n");

  for (i = 0; i < 2; ++i) {
    printf("t2[%2d] = %g + %g*i\n", i, t2[i].re, t2[i].im);
  }
}

generates a binary that prints:

t2[ 0] = 0 + 1*i

t2[ 1] = 0 + 0*i

when compiled without warnings:

  -O3 -Wall -Wextra -fno-strict-aliasing -fwrapv

in GCC 8.1 up through trunk. The -O2 and lower output is the correct answer:

t2[ 0] = 1 + 1*i

t2[ 1] = 0 + 0*i

Adding -fsanitize=undefined,address runs without error and returns the expected
answer.

Godbolt link for reference:

https://godbolt.org/z/uxVJ2K

gcc -v
Using built-in specs.
COLLECT_GCC=.../gcc-8.3.0/bin/gcc
COLLECT_LTO_WRAPPER=.../gcc-8.3.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: .../gcc-8.3/configure --with-gmp=.../gcc-8.3/gmp-4.3
--with-mpfr=../gcc-8.3/mpfr --with-mpc=...gcc-8.3/mpc
--enable-languages=c,c++,fortran --enable-shared --enable-linker-build-id
--enable-plugin --enable-checking=release --enable-multiarch --enable-gold
--enable-ld=default --enable-libstdcxx-time=no --prefix=gcc-8.3.0
--with-pkgversion='MW GCC 8.3.0-gold' --with-tune=generic --with-system-zlib
--enable-multilib --with-multilib-list=m32,m64 --with-arch-directory=amd64
--with-arch-32=i586 --with-abi=m64
Thread model: posix
gcc version 8.3.0 (GCC 8.3.0-gold)

Reply via email to