[libcxx-commits] [libcxx] [libc++] Make std::align an inline function (PR #167472)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 12 02:34:00 PST 2025


https://github.com/poor-circle updated https://github.com/llvm/llvm-project/pull/167472

>From 615eee277a552d4211607745f8599909c7c3ddd3 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Tue, 11 Nov 2025 18:07:31 +0800
Subject: [PATCH 1/3] [libc++] Make std::align an inline function from v2 ABI

---
 libcxx/include/__memory/align.h | 19 +++++++++++++++++++
 libcxx/src/memory.cpp           |  3 +++
 2 files changed, 22 insertions(+)

diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index 402eac3380925..6c8e21be03f76 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -11,6 +11,7 @@
 
 #include <__config>
 #include <__cstddef/size_t.h>
+#include <cstdint>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -18,7 +19,25 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+// From >=v2 ABI, std::align is an inline function.
+#if _LIBCPP_ABI_VERSION >= 2
+inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
+  void* __r = nullptr;
+  if (__sz <= __space) {
+    char* __p1 = static_cast<char*>(__ptr);
+    char* __p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(__p1 + (__align - 1)) & -__align);
+    size_t __d = static_cast<size_t>(__p2 - __p1);
+    if (__d <= __space - __sz) {
+      __r   = __p2;
+      __ptr = __r;
+      __space -= __d;
+    }
+  }
+  return __r;
+}
+#else
 _LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
+#endif
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 9be40cb9c1285..9efbb6eb8eca3 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -132,6 +132,8 @@ __sp_mut& __get_sp_mut(const void* p) {
 
 #endif // _LIBCPP_HAS_THREADS
 
+// Remove std::align from >=v2 dylib ABI, make it an inline function.
+#if _LIBCPP_ABI_VERSION == 1
 void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
   void* r = nullptr;
   if (size <= space) {
@@ -146,5 +148,6 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
   }
   return r;
 }
+#endif
 
 _LIBCPP_END_NAMESPACE_STD

>From 0987145ac0269c506738aabba6cce5ace7243ff0 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Wed, 12 Nov 2025 18:08:21 +0800
Subject: [PATCH 2/3] also make align an inline function in v1 abi

---
 libcxx/docs/ReleaseNotes/22.rst               |  6 ++++
 libcxx/include/__memory/align.h               | 13 +++++----
 libcxx/src/memory.cpp                         |  7 +++--
 libcxx/test/benchmarks/memory/align.bench.cpp | 28 +++++++++++++++++++
 4 files changed, 46 insertions(+), 8 deletions(-)
 create mode 100644 libcxx/test/benchmarks/memory/align.bench.cpp

diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst
index a6a0ac8670fb5..f1d6a34be6762 100644
--- a/libcxx/docs/ReleaseNotes/22.rst
+++ b/libcxx/docs/ReleaseNotes/22.rst
@@ -82,6 +82,8 @@ Improvements and New Features
   iterators, resulting in a performance improvement for ``std::deque<short>`` and
   ``std::join_view<vector<vector<short>>>`` iterators.
 
+- The performance of ``align`` has been improved about 3x by making it an inline function.
+
 Deprecations and Removals
 -------------------------
 
@@ -98,6 +100,8 @@ Potentially breaking changes
   ``_LIBCPP_ABI_NO_ITERATOR_BASES``. If you are using this flag and care about ABI stability, you should set
   ``_LIBCPP_ABI_NO_REVERSE_ITERATOR_SECOND_MEMBER`` as well.
 
+- ``align`` has been removed from libc++ dynamic library in ABI v2. You could use the ABI flag ``_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS`` to reserve it.
+
 Announcements About Future Releases
 -----------------------------------
 
@@ -113,5 +117,7 @@ ABI Affecting Changes
   potentially inheriting from the types they wrap. At this point in time we are not aware of any ABI changes caused by
   this.
 
+- ``align`` has been removed from libc++ dynamic library in ABI v2. You could use the ABI flag ``_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS`` to reserve it.
+
 Build System Changes
 --------------------
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index 6c8e21be03f76..ce77278e99cd1 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -19,8 +19,12 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-// From >=v2 ABI, std::align is an inline function.
-#if _LIBCPP_ABI_VERSION >= 2
+#ifdef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
+
+_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
+
+#else
+
 inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
   void* __r = nullptr;
   if (__sz <= __space) {
@@ -35,9 +39,8 @@ inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __p
   }
   return __r;
 }
-#else
-_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
-#endif
+
+#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 9efbb6eb8eca3..56cf48a6e0cfb 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -8,6 +8,7 @@
 
 #include <__config>
 #ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
+#  define _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
 #  define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
 #endif
 
@@ -132,8 +133,8 @@ __sp_mut& __get_sp_mut(const void* p) {
 
 #endif // _LIBCPP_HAS_THREADS
 
-// Remove std::align from >=v2 dylib ABI, make it an inline function.
-#if _LIBCPP_ABI_VERSION == 1
+#if defined(_LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS)
+
 void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
   void* r = nullptr;
   if (size <= space) {
@@ -148,6 +149,6 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
   }
   return r;
 }
-#endif
+#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/test/benchmarks/memory/align.bench.cpp b/libcxx/test/benchmarks/memory/align.bench.cpp
new file mode 100644
index 0000000000000..ae874a6a0965e
--- /dev/null
+++ b/libcxx/test/benchmarks/memory/align.bench.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+#include <memory>
+
+#include "benchmark/benchmark.h"
+#include "test_macros.h"
+
+static void BM_align(benchmark::State& state) {
+  char buffer[1024];
+  char* data = buffer + 123;
+  std::size_t sz{1024 - 123};
+  void* p;
+
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(std::align(state.range(), state.range(), p, sz))
+  }
+}
+BENCHMARK(BM_align)->Range(1, 256);
+
+BENCHMARK_MAIN();

>From 7379749f68e7f6f98952c3f56e697356ca8575e1 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Wed, 12 Nov 2025 18:32:24 +0800
Subject: [PATCH 3/3] remove duplicated code

---
 libcxx/include/__memory/align.h               | 10 +++++-----
 libcxx/src/memory.cpp                         | 16 ++--------------
 libcxx/test/benchmarks/memory/align.bench.cpp |  7 +++----
 3 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index ce77278e99cd1..1474e671e88b2 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -20,12 +20,12 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #ifdef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
-
-_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
-
+#  define _LIBCPP_ALIGN_ABI_VISIBILITY _LIBCPP_EXPORTED_FROM_ABI
 #else
+#  define _LIBCPP_ALIGN_ABI_VISIBILITY _LIBCPP_HIDE_FROM_ABI
+#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
 
-inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
+inline _LIBCPP_ALIGN_ABI_VISIBILITY void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
   void* __r = nullptr;
   if (__sz <= __space) {
     char* __p1 = static_cast<char*>(__ptr);
@@ -40,7 +40,7 @@ inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __p
   return __r;
 }
 
-#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
+#undef _LIBCPP_ALIGN_ABI_VISIBILITY
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 56cf48a6e0cfb..6912117a1de0c 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -135,20 +135,8 @@ __sp_mut& __get_sp_mut(const void* p) {
 
 #if defined(_LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS)
 
-void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
-  void* r = nullptr;
-  if (size <= space) {
-    char* p1 = static_cast<char*>(ptr);
-    char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment);
-    size_t d = static_cast<size_t>(p2 - p1);
-    if (d <= space - size) {
-      r   = p2;
-      ptr = r;
-      space -= d;
-    }
-  }
-  return r;
-}
+constinit auto* __align_abi_keeper = align;
+
 #endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/test/benchmarks/memory/align.bench.cpp b/libcxx/test/benchmarks/memory/align.bench.cpp
index ae874a6a0965e..74be6f7a598f7 100644
--- a/libcxx/test/benchmarks/memory/align.bench.cpp
+++ b/libcxx/test/benchmarks/memory/align.bench.cpp
@@ -15,12 +15,11 @@
 
 static void BM_align(benchmark::State& state) {
   char buffer[1024];
-  char* data = buffer + 123;
-  std::size_t sz{1024 - 123};
-  void* p;
+  void* data = buffer + 123;
+  std::size_t sz{sizeof(buffer) - 123};
 
   for (auto _ : state) {
-    benchmark::DoNotOptimize(std::align(state.range(), state.range(), p, sz))
+    benchmark::DoNotOptimize(std::align(state.range(), state.range(), data, sz));
   }
 }
 BENCHMARK(BM_align)->Range(1, 256);



More information about the libcxx-commits mailing list