https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105449
Gilles Gouaillardet <gilles.gouaillardet at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |gilles.gouaillardet at gmail dot c | |om Summary|suspicious optimization |suspicious optimization |since GCC 10.1.0 from - |since GCC 10.1.0 from -O2 --- Comment #2 from Gilles Gouaillardet <gilles.gouaillardet at gmail dot com> --- The attached reproducer works fine when using -O1 or less aggressive optimization and on all GCC versions I tested. However, starting from GCC 10.1.0 and when using -O2 or more aggressive optimization, the local_pos() subroutine behaves differently compared to the global_pos() and volatile_pos() subroutines. All these subroutines do virtually the same things, but only the declaration of the bogus variable changes. I suspect recent GCC versions perform some register inlining when the bogus variable is declared as local and non volatile. Can you please have a look at the reproducer and establish whether: - this reproducer has an undefined behavior, and the recent optimizations are legit - this is a bug in GCC >= 10.1.0 For the record, here are my logs (on a x86_64 system, same behavior on aarch64. Though I did not test, a different behavior is expected on a 64 bits big endian processor). $ gcc --version gcc (GCC) 11.2.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc bug.c && ./a.out global: 0 local: 0 volatile: 0 $ gcc -O2 bug.c && ./a.out global: 0 local: 2 <--- UNEXPECTED VALUE volatile: 0 FWIW, a consequence of this optimization is an issue that has been reported at https://github.com/open-mpi/ompi/issues/10339