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

            Bug ID: 119666
           Summary: Incorrect constant propagation of 'const' variable
                    with [[gnu::weak]] attribute
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 123321artyom at gmail dot com
  Target Milestone: ---

Given this code:
  extern const int my_number [[gnu::weak]] = 4;

  int get_num() {
    return my_number;
  }

GCC seems to incorrectly const propagate the variable 'my_number':
  get_num():
        mov     eax, 4
        ret

https://godbolt.org/z/qvz3oEGz4

OTOH, Clang respects the attribute and emits a memory load.


gcc version 15.0.1 20250407 (experimental)
(Compiler-Explorer-Build-gcc-8fbe7d24373556d40886c7c00e9e2be5d9718c55-binutils-2.42)
Target: x86_64-linux-gnu
Configured with: ../gcc-trunk-20250407/configure
--prefix=/opt/compiler-explorer/gcc-build/staging
--enable-libstdcxx-backtrace=yes --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu
--enable-languages=c,c++,fortran,ada,objc,obj-c++,go,d,rust,m2,cobol
--enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto
--enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build-gcc-8fbe7d24373556d40886c7c00e9e2be5d9718c55-binutils-2.42

Reply via email to