public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/106435] New: constructor of thread_local static member is not called
@ 2022-07-25 16:37 fxcoudert at gcc dot gnu.org
  2022-07-25 16:39 ` [Bug target/106435] " fxcoudert at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: fxcoudert at gcc dot gnu.org @ 2022-07-25 16:37 UTC (permalink / raw)
  To: gcc-bugs

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)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug target/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
@ 2022-07-25 16:39 ` fxcoudert at gcc dot gnu.org
  2022-07-25 16:42 ` [Bug middle-end/106435] " pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: fxcoudert at gcc dot gnu.org @ 2022-07-25 16:39 UTC (permalink / raw)
  To: gcc-bugs

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

Francois-Xavier Coudert <fxcoudert at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |iains at gcc dot gnu.org
             Target|                            |aarch64-apple-darwin
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |wrong-code
      Known to fail|                            |11.3.0, 12.1.0
     Ever confirmed|0                           |1
              Build|                            |aarch64-apple-darwin
   Last reconfirmed|                            |2022-07-25
               Host|                            |aarch64-apple-darwin

--- Comment #1 from Francois-Xavier Coudert <fxcoudert at gcc dot gnu.org> ---
This appears to be darwin-specific. I cannot check on Intel darwin right now,
so it might be specific to arm-darwin, even.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
  2022-07-25 16:39 ` [Bug target/106435] " fxcoudert at gcc dot gnu.org
@ 2022-07-25 16:42 ` pinskia at gcc dot gnu.org
  2022-07-25 16:47 ` iains at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-25 16:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This might be an emulated TLS issue ... As it looks like it is not working on
mingw either.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
  2022-07-25 16:39 ` [Bug target/106435] " fxcoudert at gcc dot gnu.org
  2022-07-25 16:42 ` [Bug middle-end/106435] " pinskia at gcc dot gnu.org
@ 2022-07-25 16:47 ` iains at gcc dot gnu.org
  2022-07-25 16:49 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-07-25 16:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> This might be an emulated TLS issue ... As it looks like it is not working
> on mingw either.

quite possibly;
- there was a change made to support cross-TU static inits for TLS - but that
did not include an equivalent for emulated TLS (or targets without symbol
aliases).

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-07-25 16:47 ` iains at gcc dot gnu.org
@ 2022-07-25 16:49 ` pinskia at gcc dot gnu.org
  2022-07-25 16:56 ` fxcoudert at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-25 16:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Iain Sandoe from comment #3)
> (In reply to Andrew Pinski from comment #2)
> > This might be an emulated TLS issue ... As it looks like it is not working
> > on mingw either.
> 
> quite possibly;
> - there was a change made to support cross-TU static inits for TLS - but
> that did not include an equivalent for emulated TLS (or targets without
> symbol aliases).

It could also be a "non-weak" target issue too. both darwin and mingw are not
elf targets so the semantics for non-elf targets sometimes are wrong and not
always working because there are less people working on non-elf targets.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-07-25 16:49 ` pinskia at gcc dot gnu.org
@ 2022-07-25 16:56 ` fxcoudert at gcc dot gnu.org
  2022-07-25 16:59 ` iains at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: fxcoudert at gcc dot gnu.org @ 2022-07-25 16:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Francois-Xavier Coudert <fxcoudert at gcc dot gnu.org> ---
> there was a change made to support cross-TU static inits for TLS

For what it's worth, putting everything in a single cpp file makes it work as
expected.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-07-25 16:56 ` fxcoudert at gcc dot gnu.org
@ 2022-07-25 16:59 ` iains at gcc dot gnu.org
  2022-07-25 19:06 ` iains at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-07-25 16:59 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |13.0
              Build|aarch64-apple-darwin        |*-apple-darwin*
               Host|aarch64-apple-darwin        |*-apple-darwin*
             Target|aarch64-apple-darwin        |*-apple-darwin*

--- Comment #6 from Iain Sandoe <iains at gcc dot gnu.org> ---
confirmed with x86_64 on master; I would not expect this to be different on any
of the archs.

your finding that putting into one file makes it work confirms my suspicion
about the nature of the problem - I need to find Nathan's change that was done
for ELF-like targets and see if a corresponding one can be made for emulated
TLS.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2022-07-25 16:59 ` iains at gcc dot gnu.org
@ 2022-07-25 19:06 ` iains at gcc dot gnu.org
  2022-07-25 22:43 ` iains at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-07-25 19:06 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=84497

--- Comment #7 from Iain Sandoe <iains at gcc dot gnu.org> ---
So we need to figure out a solution for non-alias targets (actually, I suspect
that it can be done with weak defs .. but TBC).

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2022-07-25 19:06 ` iains at gcc dot gnu.org
@ 2022-07-25 22:43 ` iains at gcc dot gnu.org
  2022-07-26  8:10 ` [Bug c++/106435] " iains at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-07-25 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Iain Sandoe <iains at gcc dot gnu.org> ---
I think we can fix this for non-alias targets by building stub functions
instead of the alias. There is a small penalty for size and speed from the
call, but for at least x86 at O2 we sibcall and effectively elide the
__thread_init function for at least one of the direct inits.
... have to tidy up a bit and test more widely.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2022-07-25 22:43 ` iains at gcc dot gnu.org
@ 2022-07-26  8:10 ` iains at gcc dot gnu.org
  2022-07-26 11:36 ` iains at gcc dot gnu.org
  2022-12-28  8:09 ` iains at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-07-26  8:10 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|middle-end                  |c++
      Known to fail|                            |10.4.0
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |iains at gcc dot gnu.org

--- Comment #9 from Iain Sandoe <iains at gcc dot gnu.org> ---
actually, since this is unimplemented for emuTLS - it fails everywhere for
emuTLS targets and C++11+.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2022-07-26  8:10 ` [Bug c++/106435] " iains at gcc dot gnu.org
@ 2022-07-26 11:36 ` iains at gcc dot gnu.org
  2022-12-28  8:09 ` iains at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-07-26 11:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Iain Sandoe <iains at gcc dot gnu.org> ---
Created attachment 53358
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53358&action=edit
Patch under test

This works without regression on x86_64 Darwin, now doing wider testing.
The basic approach should be applicable to open (and supported closed)
branches.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/106435] constructor of thread_local static member is not called
  2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2022-07-26 11:36 ` iains at gcc dot gnu.org
@ 2022-12-28  8:09 ` iains at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: iains at gcc dot gnu.org @ 2022-12-28  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Iain Sandoe <iains at gcc dot gnu.org> ---
posted here
https://gcc.gnu.org/pipermail/gcc-patches/2022-December/608096.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-12-28  8:09 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-25 16:37 [Bug target/106435] New: constructor of thread_local static member is not called fxcoudert at gcc dot gnu.org
2022-07-25 16:39 ` [Bug target/106435] " fxcoudert at gcc dot gnu.org
2022-07-25 16:42 ` [Bug middle-end/106435] " pinskia at gcc dot gnu.org
2022-07-25 16:47 ` iains at gcc dot gnu.org
2022-07-25 16:49 ` pinskia at gcc dot gnu.org
2022-07-25 16:56 ` fxcoudert at gcc dot gnu.org
2022-07-25 16:59 ` iains at gcc dot gnu.org
2022-07-25 19:06 ` iains at gcc dot gnu.org
2022-07-25 22:43 ` iains at gcc dot gnu.org
2022-07-26  8:10 ` [Bug c++/106435] " iains at gcc dot gnu.org
2022-07-26 11:36 ` iains at gcc dot gnu.org
2022-12-28  8:09 ` iains at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).