[cfe-dev] Linkage of static local variables in available_externally definitions

Mehdi Amini via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 26 17:42:59 PST 2017


> On Jan 26, 2017, at 2:51 PM, David Blaikie via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> In implementing modular codegen, specifically verifying the behavior for static local variables in inline functions in modular headers, I came across a piece of logic in Clang's linkage calculation that seems off.
> 
> To demonstrate a real-world example:
> 
> template<typename T>
> inline void f() { static int i; }
> extern template void f<int>();
> ...
> template void f<int>();
> 
> In the TU with the extern template declaration, an available_externally definition of f<int> and f<int>::i are provided (when optimizations are enabled).
> 
> In the TU with the extern template definition, a weak_odr definition of f<int> is provided but a linkonce_odr definition of f<int>::i is provided.
> 
> This surprises me and doesn't seem correct. What if the TU with the weak_odr definition is optimized to the point of removing the f<int>::i definition - but the available_externally TU is not - it inlines f<int> but leaves a use of f<int>::i available?
> 
> What's the deal here - am I missing something that makes this safe/correct?

Seems buggy to me, either it should be (decl / def ): available_externally / weak_odr or: linkonce_odr / linkonce_odr.

Indeed based on your example, here are two source files and a header that fail to link on macOS when optimizations are enabled.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: a.h
Type: application/octet-stream
Size: 153 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170126/be3ba06e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: b.cpp
Type: application/octet-stream
Size: 49 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170126/be3ba06e/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: a.cpp
Type: application/octet-stream
Size: 70 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170126/be3ba06e/attachment-0002.obj>
-------------- next part --------------



— 
Mehdi



More information about the cfe-dev mailing list