Skip to content

[libc++] Annotate the data member of variant with no_unique_address #137783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gizmondo
Copy link

This allows clients to reuse tail padding after the index (if any), by applying no_unique_address attribute to their variant fields.

E.g. the following struct would have smaller size.

struct S {
    [[no_unique_address]] std::variant<int, string> a;
    bool b;
};

Currently no_unique_address in this code doesn't do anything, so this is an ABI break.

@gizmondo gizmondo requested a review from a team as a code owner April 29, 2025 10:32
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-libcxx

Author: Alex Aktsipetrov (gizmondo)

Changes

This allows clients to reuse tail padding after the index (if any), by applying no_unique_address attribute to their variant fields.

E.g. the following struct would have smaller size.

struct S {
    [[no_unique_address]] std::variant&lt;int, string&gt; a;
    bool b;
};

Currently no_unique_address in this code doesn't do anything, so this is an ABI break.


Full diff: https://github.com/llvm/llvm-project/pull/137783.diff

3 Files Affected:

  • (modified) libcxx/include/__configuration/abi.h (+2)
  • (modified) libcxx/include/variant (+5-1)
  • (modified) libcxx/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp (+19)
diff --git a/libcxx/include/__configuration/abi.h b/libcxx/include/__configuration/abi.h
index 01a4a4c023983..f867fb12206a7 100644
--- a/libcxx/include/__configuration/abi.h
+++ b/libcxx/include/__configuration/abi.h
@@ -57,6 +57,8 @@
 // Use the smallest possible integer type to represent the index of the variant.
 // Previously libc++ used "unsigned int" exclusively.
 #  define _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
+// Allow to reuse tail padding after the index of the variant with [[no_unique_address]] attribute.
+#  define _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
 // Unstable attempt to provide a more optimized std::function
 #  define _LIBCPP_ABI_OPTIMIZED_FUNCTION
 // All the regex constants must be distinct and nonzero.
diff --git a/libcxx/include/variant b/libcxx/include/variant
index 74a464d27ead4..b4f0856ebb17f 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -1319,7 +1319,11 @@ public:
 #    endif
 
 private:
-  __variant_detail::__impl<_Types...> __impl_;
+#    ifdef _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
+  _LIBCPP_NO_UNIQUE_ADDRESS
+#    endif // _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
+  __variant_detail::__impl<_Types...>
+      __impl_;
 
   friend struct __variant_detail::__access::__variant;
   friend struct __variant_detail::__visitation::__variant;
diff --git a/libcxx/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp b/libcxx/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp
index 2f1ea8bffb479..62abcd364c7ac 100644
--- a/libcxx/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp
+++ b/libcxx/test/libcxx/utilities/variant/variant.variant/variant_size.pass.cpp
@@ -70,6 +70,22 @@ struct type_with_index {
 #endif
 };
 
+struct alignas(16) A16 {};
+struct VariantWithNoUniqueAddress {
+  TEST_NO_UNIQUE_ADDRESS std::variant<A16> a;
+  bool b;
+};
+struct VariantWithoutNoUniqueAddress {
+  std::variant<A16> a;
+  bool b;
+};
+constexpr bool ExpectSmallerSizeWithNoUniqueAddress =
+#ifdef _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
+    true;
+#else
+    false;
+#endif
+
 int main(int, char**) {
   test_index_type<unsigned char>();
   // This won't compile due to template depth issues.
@@ -84,5 +100,8 @@ int main(int, char**) {
   static_assert(sizeof(std::variant<char, int, long>) == sizeof(type_with_index<long>));
   static_assert(sizeof(std::variant<std::size_t, std::size_t, std::size_t>) == sizeof(type_with_index<std::size_t>));
 
+  static_assert((sizeof(VariantWithNoUniqueAddress) < sizeof(VariantWithoutNoUniqueAddress)) ==
+                ExpectSmallerSizeWithNoUniqueAddress);
+
   return 0;
 }

…ttribute.

This allows clients to reuse tail padding after the index (if any), by applying
[[no_unique_address]] to variant fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants