<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jan 9, 2020 at 6:47 AM Yafei Liu via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">We know that in C++, the constructor of a static member will get called when the program starts up. I checked the generated IR code and found this is implemented by defining a __cxx_global_var_init() function and marked it as section ".text.startup" and assign it to @llvm.global_ctors.</div></blockquote><div><br></div><div>The last is the important part. But that's just an abstraction across the various global constructor mechanisms provided by the underlying platforms.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>We also know that in C++, the constructor of a static thread_local member will <b>not</b> get called when the program starts, but the first time this member is used, I also checked the IR code, this is implemented by calling it's constructor at the usage, and then call __cxa_thread_atexit to register it's destructor.</div></div></blockquote><div><br></div><div>This behavior is not guaranteed by C++. It would also be acceptable according to the standard to construct the thread-locals eagerly upon thread startup, as you want to do. However, yes, clang does always lazy initialize them.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Now I want to add thread_local feature to my own language, and I want my thread_local member acts simlar like a static member, that is,  when a new thread is created, the constructors of thread_local members will automatically get called, instead get called when used?</div><div><br></div><div>I read the LLVM Language Reference Manual, but it doesn't talk much in Thread Local Storage Models. </div></div></blockquote><div> </div><div>No platform I know of provides such a mechanism that you (or llvm) could hook into. If you want this, you'll need to create your own thread-startup routine that calls your own thread-local initializers.</div><div><br></div><div>The most obvious way to do this when there's no shared-libraries in the mix would be to emit the initializer-function pointers into a custom named section, and then iterate over that section in your thread startup routine. The linker automatically creates __start_SECTIONNAME and __stop_SECTIONNAME symbols, so you can just iterate on the pointers between them. (At least this works in ELF, not sure if it's the same on COFF/MachO).</div><div><br></div><div>If you need to also support shared libraries, then finding all constructors across all libraries gets somewhat more complex...</div><div><br></div></div></div>