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

            Bug ID: 106435
           Summary: constructor of thread_local static member is not
                    called
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fxcoudert at gcc dot gnu.org
  Target Milestone: ---

Originally reported to Homebrew at
https://github.com/Homebrew/homebrew-core/issues/105022



$ cat foo.hpp 
#pragma once

struct Foo {
  Foo();
  void func();
};

struct Bar {
  thread_local static Foo foo;
};

$ cat foo.cpp 
#include "foo.hpp"

#include <iostream>

Foo::Foo() {
  std::cout << "Foo::Foo(this=" << this << ")\n";
}

void Foo::func() {
  std::cout << "Foo::func(this=" << this << ")\n";
}

thread_local Foo Bar::foo;

$ cat main.cpp 
#include "foo.hpp"

int main() {
  Bar::foo.func();
}


Compiled with clang, the constructor is called. Compiled with gcc 11 or 12 on
aarch64-apple-darwin21, it is not:


$ g++-12 -std=c++17 foo.cpp main.cpp && ./a.out           
Foo::func(this=0x6000009e0038)
$ g++-11 -std=c++17 foo.cpp main.cpp && ./a.out
Foo::func(this=0x600001418038)
$ clang++ -std=c++17 foo.cpp main.cpp && ./a.out
Foo::Foo(this=0x60000209c000)
Foo::func(this=0x60000209c000)


$ g++-12 -v                                    
Using built-in specs.
COLLECT_GCC=g++-12
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc@12/12.1.0_1/bin/../libexec/gcc/aarch64-apple-darwin21/12/lto-wrapper
Target: aarch64-apple-darwin21
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc@12
--libdir=/opt/homebrew/opt/gcc@12/lib/gcc/12 --disable-nls
--enable-checking=release --with-gcc-major-version-only
--enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12
--with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr
--with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl
--with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.1.0_1'
--with-bugurl=https://github.com/Homebrew/homebrew-core/issues
--with-system-zlib --build=aarch64-apple-darwin21
--with-native-system-header-dir=/usr/include
--with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (Homebrew GCC 12.1.0_1)

Reply via email to