[libcxx] r282345 - Use __attribute__((internal_linkage)) when available.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 24 20:14:13 PDT 2016

Author: ericwf
Date: Sat Sep 24 22:14:13 2016
New Revision: 282345

URL: http://llvm.org/viewvc/llvm-project?rev=282345&view=rev
Use __attribute__((internal_linkage)) when available.

This patch has been a long time coming (Thanks @eugenis). It changes `_LIBCPP_INLINE_VISIBILITY` to use `__attribute__((internal_linkage))` instead of `__attribute__((visibility("hidden"), always_inline))`.

The point of `_LIBCPP_INLINE_VISIBILITY` is to prevent inline functions from being exported from both the libc++ library and from user libraries. This helps libc++ better manage it's ABI.
Previously this was done by forcing inlining and modifying the symbols visibility. However inlining isn't guaranteed and symbol visibility only affects shared libraries making this an imperfect solution.  `internal_linkage` improves this situation by making all symbols local to the TU they are emitted in, regardless of inlining or visibility. IIRC the effect of applying `__attribute__((internal_linkage))` to an inline function is the same as applying `static`.

For more information about the attribute see: http://lists.llvm.org/pipermail/cfe-dev/2015-October/045580.html

Most of the work for this patch was done by @eugenis.

Reviewers: mclow.lists, eugenis

Subscribers: eugenis, cfe-commits

Differential Revision: https://reviews.llvm.org/D24642


Modified: libcxx/trunk/include/__config
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__config?rev=282345&r1=282344&r2=282345&view=diff
--- libcxx/trunk/include/__config (original)
+++ libcxx/trunk/include/__config Sat Sep 24 22:14:13 2016
@@ -34,6 +34,7 @@
 // Change short string representation so that string data starts at offset 0,
 // improving its alignment in some cases.
@@ -49,6 +50,7 @@
 // Feature macros for disabling pre ABI v1 features. All of these options
 // are deprecated.
 #if defined(__FreeBSD__)
@@ -629,11 +631,19 @@ namespace std {
-#define _LIBCPP_INLINE_VISIBILITY __attribute__ ((__visibility__("hidden"), __always_inline__))
+# if __has_attribute(__internal_linkage__)
+#   define _LIBCPP_INLINE_VISIBILITY __attribute__((__internal_linkage__, __always_inline__))
+# else
+#   define _LIBCPP_INLINE_VISIBILITY __attribute__ ((__visibility__("hidden"), __always_inline__))
+# endif
-#define _LIBCPP_ALWAYS_INLINE  __attribute__ ((__visibility__("hidden"), __always_inline__))
+# if __has_attribute(__internal_linkage__)
+#   define _LIBCPP_ALWAYS_INLINE __attribute__((__internal_linkage__, __always_inline__))
+# else
+#   define _LIBCPP_ALWAYS_INLINE  __attribute__ ((__visibility__("hidden"), __always_inline__))
+# endif

Modified: libcxx/trunk/src/string.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/string.cpp?rev=282345&r1=282344&r2=282345&view=diff
--- libcxx/trunk/src/string.cpp (original)
+++ libcxx/trunk/src/string.cpp Sat Sep 24 22:14:13 2016
@@ -29,6 +29,29 @@ template
     operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
+// These external instantiations are required to maintain dylib compatibility
+// for ABI v1 when using __attribute__((internal_linkage)) as opposed to
+// __attribute__((visibility("hidden"), always_inline)).
+template basic_string<char>::iterator
+                           char const *, char const *);
+template basic_string<wchar_t>::iterator
+                              wchar_t const *, wchar_t const *);
+template basic_string<char> &
+                               basic_string<char>::const_iterator,
+                               char const *, char const *);
+template basic_string<wchar_t> &
+                               basic_string<wchar_t>::const_iterator,
+                               wchar_t const *, wchar_t const *);

More information about the cfe-commits mailing list