[libcxx] [llvm] [libc++] Make std::align an inline function (PR #167472)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 13 05:12:14 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 01/11] [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 02/11] 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 f52e3fa4e7de4d363e4d3cba75aa747a38c2821d Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Wed, 12 Nov 2025 18:38:39 +0800
Subject: [PATCH 03/11] fix benchmark
---
libcxx/test/benchmarks/memory/align.bench.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
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);
>From 65e53c32333b761b29f9d9b094b9d254ea8a003b Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Wed, 12 Nov 2025 19:01:47 +0800
Subject: [PATCH 04/11] remove duplicated code
---
libcxx/include/__memory/align.h | 18 +++++++++++-------
libcxx/src/memory.cpp | 13 +------------
2 files changed, 12 insertions(+), 19 deletions(-)
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index ce77278e99cd1..f0a693f0360de 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -19,13 +19,7 @@
_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);
-
-#else
-
-inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
+__attribute__((always_inline)) inline void* __align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
void* __r = nullptr;
if (__sz <= __space) {
char* __p1 = static_cast<char*>(__ptr);
@@ -40,6 +34,16 @@ inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __p
return __r;
}
+#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) {
+ return __align_impl(__align, __sz, __ptr, __space);
+}
+
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 56cf48a6e0cfb..bb17ecce3ebeb 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -136,18 +136,7 @@ __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;
+ return __align_impl(alignment, size, ptr, space);
}
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
>From 535c59763a5ca675e9f666be0a2d5789f03730bb Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 10:42:15 +0800
Subject: [PATCH 05/11] remove useless decl in align.h
---
libcxx/include/__memory/align.h | 9 +++------
libcxx/src/memory.cpp | 2 +-
2 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index f0a693f0360de..227e8c267278c 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -19,7 +19,8 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-__attribute__((always_inline)) inline void* __align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE inline void*
+__align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
void* __r = nullptr;
if (__sz <= __space) {
char* __p1 = static_cast<char*>(__ptr);
@@ -34,11 +35,7 @@ __attribute__((always_inline)) inline void* __align_impl(size_t __align, size_t
return __r;
}
-#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
+#ifndef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
return __align_impl(__align, __sz, __ptr, __space);
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index bb17ecce3ebeb..681decfb785ea 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -135,7 +135,7 @@ __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) {
+_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
return __align_impl(alignment, size, ptr, space);
}
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
>From 3eb0164fd2c6886a9abdcd8d1492995dab48b32d Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 14:43:48 +0800
Subject: [PATCH 06/11] fix abi break change
---
libcxx/docs/ABIGuarantees.rst | 4 ++++
libcxx/docs/ReleaseNotes/22.rst | 4 +---
libcxx/include/__configuration/abi.h | 1 +
libcxx/include/__memory/align.h | 4 ++--
libcxx/src/memory.cpp | 8 +++++---
5 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/libcxx/docs/ABIGuarantees.rst b/libcxx/docs/ABIGuarantees.rst
index 4d4674c7756a4..7588cf7e55453 100644
--- a/libcxx/docs/ABIGuarantees.rst
+++ b/libcxx/docs/ABIGuarantees.rst
@@ -114,6 +114,10 @@ hand, backwards compatibility is generally guaranteed.
There are multiple ABI flags that change the symbols exported from the built library:
+``_LIBCPP_ABI_DO_NOT_EXPORT_ALIGN``
+-------------------------------------------------
+This removes ``align()``. In the past ``align()`` is not an inline function, but now it changed to an inline function for performance.
+
``_LIBCPP_ABI_DO_NOT_EXPORT_BASIC_STRING_COMMON``
-------------------------------------------------
This removes ``__basic_string_common<true>::__throw_length_error()`` and
diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst
index 23fe89cdbe738..670d73992e7e6 100644
--- a/libcxx/docs/ReleaseNotes/22.rst
+++ b/libcxx/docs/ReleaseNotes/22.rst
@@ -101,8 +101,6 @@ 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
-----------------------------------
@@ -118,7 +116,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.
+- ``align`` has been removed from libc++ library in ABI v2. We also add a new ABI flag ``_LIBCPP_ABI_DO_NOT_EXPORT_ALIGN``.
Build System Changes
--------------------
diff --git a/libcxx/include/__configuration/abi.h b/libcxx/include/__configuration/abi.h
index 38b85c6ac70de..f88de6442f90d 100644
--- a/libcxx/include/__configuration/abi.h
+++ b/libcxx/include/__configuration/abi.h
@@ -63,6 +63,7 @@
// These flags are documented in ABIGuarantees.rst
# define _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
+# define _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
# define _LIBCPP_ABI_DO_NOT_EXPORT_BASIC_STRING_COMMON
# define _LIBCPP_ABI_DO_NOT_EXPORT_VECTOR_BASE_COMMON
# define _LIBCPP_ABI_DO_NOT_EXPORT_TO_CHARS_BASE_10
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index 227e8c267278c..8aaf08192f2af 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -35,13 +35,13 @@ __align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
return __r;
}
-#ifndef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
+#ifndef _LIBCPP_EXPORT_ALIGN_SYMBOL
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
return __align_impl(__align, __sz, __ptr, __space);
}
-#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
+#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 681decfb785ea..19b2894b87ec1 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -8,9 +8,11 @@
#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
+#ifndef _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
+# define _LIBCPP_EXPORT_ALIGN_SYMBOL
+#endif
#include <__functional/hash.h>
#include <memory>
@@ -133,11 +135,11 @@ __sp_mut& __get_sp_mut(const void* p) {
#endif // _LIBCPP_HAS_THREADS
-#if defined(_LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS)
+#if defined(_LIBCPP_EXPORT_ALIGN_SYMBOL)
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
return __align_impl(alignment, size, ptr, space);
}
-#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
+#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
_LIBCPP_END_NAMESPACE_STD
>From 783de7336501fc85deca2017208699a3d03aee85 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 18:20:14 +0800
Subject: [PATCH 07/11] fix link error in PE/COFF
---
libcxx/include/__configuration/abi.h | 5 +++++
libcxx/include/__memory/align.h | 7 +++++++
libcxx/src/memory.cpp | 1 +
3 files changed, 13 insertions(+)
diff --git a/libcxx/include/__configuration/abi.h b/libcxx/include/__configuration/abi.h
index f88de6442f90d..6d519597b5a26 100644
--- a/libcxx/include/__configuration/abi.h
+++ b/libcxx/include/__configuration/abi.h
@@ -97,6 +97,11 @@
// so disable it.
# define _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
# endif
+// The PE/COFF format reports a linking error when encountering multiple symbol definitions where at least one is a
+// strong symbol. So we can't inlining a function without ABI breakchange.
+# if defined(_LIBCPP_OBJECT_FORMAT_COFF)
+# define _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
+# endif
// Feature macros for disabling pre ABI v1 features. All of these options
// are deprecated.
# if defined(__FreeBSD__)
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index 8aaf08192f2af..d987599a4cfc7 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -36,11 +36,18 @@ __align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
}
#ifndef _LIBCPP_EXPORT_ALIGN_SYMBOL
+# ifdef _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
+
+_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) {
return __align_impl(__align, __sz, __ptr, __space);
}
+# endif // _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
+
#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 19b2894b87ec1..547500cf75937 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -140,6 +140,7 @@ __sp_mut& __get_sp_mut(const void* p) {
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
return __align_impl(alignment, size, ptr, space);
}
+
#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
_LIBCPP_END_NAMESPACE_STD
>From 3edcceb1f1c20a5aaa2da32abad5eede065da050 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 20:14:08 +0800
Subject: [PATCH 08/11] resolve conversation
---
libcxx/docs/ReleaseNotes/22.rst | 2 +-
libcxx/include/CMakeLists.txt | 1 +
libcxx/include/__configuration/abi.h | 11 +++---
libcxx/include/__memory/align.h | 31 +++------------
libcxx/include/__memory/align_impl.h | 39 +++++++++++++++++++
libcxx/src/CMakeLists.txt | 1 +
libcxx/src/align.cpp | 21 ++++++++++
libcxx/src/memory.cpp | 11 ------
libcxx/test/benchmarks/memory/align.bench.cpp | 22 ++++++++---
9 files changed, 92 insertions(+), 47 deletions(-)
create mode 100644 libcxx/include/__memory/align_impl.h
create mode 100644 libcxx/src/align.cpp
diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst
index 670d73992e7e6..aca7a65aaa2fc 100644
--- a/libcxx/docs/ReleaseNotes/22.rst
+++ b/libcxx/docs/ReleaseNotes/22.rst
@@ -83,7 +83,7 @@ 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.
+- The performance of ``align`` has been improved about 2x by making it an inline function.
Deprecations and Removals
-------------------------
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 131ba99357d62..3527575863a9b 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -570,6 +570,7 @@ set(files
__mdspan/mdspan.h
__memory/addressof.h
__memory/align.h
+ __memory/align_impl.h
__memory/aligned_alloc.h
__memory/allocate_at_least.h
__memory/allocation_guard.h
diff --git a/libcxx/include/__configuration/abi.h b/libcxx/include/__configuration/abi.h
index 6d519597b5a26..fc3729dbb02af 100644
--- a/libcxx/include/__configuration/abi.h
+++ b/libcxx/include/__configuration/abi.h
@@ -97,11 +97,6 @@
// so disable it.
# define _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
# endif
-// The PE/COFF format reports a linking error when encountering multiple symbol definitions where at least one is a
-// strong symbol. So we can't inlining a function without ABI breakchange.
-# if defined(_LIBCPP_OBJECT_FORMAT_COFF)
-# define _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
-# endif
// Feature macros for disabling pre ABI v1 features. All of these options
// are deprecated.
# if defined(__FreeBSD__)
@@ -109,6 +104,12 @@
# endif
#endif
+// The PE/COFF format reports a linking error when encountering multiple symbol definitions where at least one is a
+// strong symbol. So we can't inlining a non-inline function without ABI break change.
+#if defined(_LIBCPP_OBJECT_FORMAT_COFF)
+# define _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
+#endif
+
// TODO(LLVM 22): Remove this check
#if defined(_LIBCPP_ABI_NO_ITERATOR_BASES) && !defined(_LIBCPP_ABI_NO_REVERSE_ITERATOR_SECOND_MEMBER)
# ifndef _LIBCPP_ONLY_NO_ITERATOR_BASES
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index d987599a4cfc7..ab8932d489128 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -9,9 +9,9 @@
#ifndef _LIBCPP___MEMORY_ALIGN_H
#define _LIBCPP___MEMORY_ALIGN_H
+#include "align_impl.h"
#include <__config>
-#include <__cstddef/size_t.h>
-#include <cstdint>
+#include <__memory/align_impl.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -19,36 +19,17 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE inline void*
-__align_impl(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;
-}
-
-#ifndef _LIBCPP_EXPORT_ALIGN_SYMBOL
-# ifdef _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
+#if defined(_LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR) && !defined(_LIBCPP_ABI_DO_NOT_EXPORT_ALIGN)
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
-# else
+#else
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
- return __align_impl(__align, __sz, __ptr, __space);
+ return align_impl(__align, __sz, __ptr, __space);
}
-# endif // _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
-
-#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
+#endif // _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/__memory/align_impl.h b/libcxx/include/__memory/align_impl.h
new file mode 100644
index 0000000000000..2e470b6451f9e
--- /dev/null
+++ b/libcxx/include/__memory/align_impl.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MEMORY_ALIGN_IMPL_H
+#define _LIBCPP___MEMORY_ALIGN_IMPL_H
+
+#include <__config>
+#include <__cstddef/size_t.h>
+#include <cstdint>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+inline _LIBCPP_HIDE_FROM_ABI void* align_impl(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;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___MEMORY_ALIGN_IMPL_H
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index f59fe0e08fccb..4656cfbe9b08e 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -1,6 +1,7 @@
# Get sources
set(LIBCXX_SOURCES
algorithm.cpp
+ align.cpp
any.cpp
bind.cpp
call_once.cpp
diff --git a/libcxx/src/align.cpp b/libcxx/src/align.cpp
new file mode 100644
index 0000000000000..152a3f0c121ad
--- /dev/null
+++ b/libcxx/src/align.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include <__config>
+#include <__memory/align_impl.h>
+
+#if !defined(_LIBCPP_ABI_DO_NOT_EXPORT_ALIGN)
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
+ return align_impl(alignment, size, ptr, space);
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
\ No newline at end of file
diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 547500cf75937..2cfbf4f7f7b30 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -10,9 +10,6 @@
#ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
# define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
#endif
-#ifndef _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
-# define _LIBCPP_EXPORT_ALIGN_SYMBOL
-#endif
#include <__functional/hash.h>
#include <memory>
@@ -135,12 +132,4 @@ __sp_mut& __get_sp_mut(const void* p) {
#endif // _LIBCPP_HAS_THREADS
-#if defined(_LIBCPP_EXPORT_ALIGN_SYMBOL)
-
-_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
- return __align_impl(alignment, size, ptr, space);
-}
-
-#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
-
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/test/benchmarks/memory/align.bench.cpp b/libcxx/test/benchmarks/memory/align.bench.cpp
index 74be6f7a598f7..9fb637c21f9ac 100644
--- a/libcxx/test/benchmarks/memory/align.bench.cpp
+++ b/libcxx/test/benchmarks/memory/align.bench.cpp
@@ -9,17 +9,29 @@
// UNSUPPORTED: c++03
#include <memory>
+#include <iostream>
#include "benchmark/benchmark.h"
-#include "test_macros.h"
+
+struct Input {
+ std::size_t align;
+ std::size_t size;
+ void* ptr;
+ std::size_t buffer_size;
+};
static void BM_align(benchmark::State& state) {
char buffer[1024];
- void* data = buffer + 123;
- std::size_t sz{sizeof(buffer) - 123};
-
+ Input input{};
+ void* ptr = buffer + 123;
+ std::size_t buffer_size = sizeof(buffer) - 123;
+ input.align = state.range();
+ input.size = state.range();
for (auto _ : state) {
- benchmark::DoNotOptimize(std::align(state.range(), state.range(), data, sz));
+ input.ptr = ptr;
+ input.buffer_size = buffer_size;
+ benchmark::DoNotOptimize(input);
+ benchmark::DoNotOptimize(std::align(input.align, input.size, input.ptr, input.buffer_size));
}
}
BENCHMARK(BM_align)->Range(1, 256);
>From b4fe903509a1a0ec97ee9463c7b62d0d94ab283b Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 20:34:07 +0800
Subject: [PATCH 09/11] fix missing endline
---
libcxx/src/align.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/src/align.cpp b/libcxx/src/align.cpp
index 152a3f0c121ad..e49f9e3ed537a 100644
--- a/libcxx/src/align.cpp
+++ b/libcxx/src/align.cpp
@@ -18,4 +18,4 @@ _LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr,
_LIBCPP_END_NAMESPACE_STD
-#endif // _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
\ No newline at end of file
+#endif // _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
>From 6ef9c2dba4acb1bf5402c84260d23936a181c9e8 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 20:38:49 +0800
Subject: [PATCH 10/11] fix duplicated include header
---
libcxx/include/__memory/align.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libcxx/include/__memory/align.h b/libcxx/include/__memory/align.h
index ab8932d489128..8f9155e608749 100644
--- a/libcxx/include/__memory/align.h
+++ b/libcxx/include/__memory/align.h
@@ -9,7 +9,6 @@
#ifndef _LIBCPP___MEMORY_ALIGN_H
#define _LIBCPP___MEMORY_ALIGN_H
-#include "align_impl.h"
#include <__config>
#include <__memory/align_impl.h>
>From b3fce71f48a636688c5b511d2fb7b9a924b160b4 Mon Sep 17 00:00:00 2001
From: "zezheng.li" <lizezheng.lzz at alibaba-inc.com>
Date: Thu, 13 Nov 2025 21:11:17 +0800
Subject: [PATCH 11/11] fix modulemap
---
libcxx/include/module.modulemap.in | 1 +
llvm/utils/gn/secondary/libcxx/include/BUILD.gn | 1 +
2 files changed, 2 insertions(+)
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 7ca57f6455dd8..fbc552b005ebc 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1637,6 +1637,7 @@ module std [system] {
module memory {
module addressof { header "__memory/addressof.h" }
module align { header "__memory/align.h" }
+ module align_impl { header "__memory/align_impl.h" }
module aligned_alloc { header "__memory/aligned_alloc.h" }
module allocate_at_least { header "__memory/allocate_at_least.h" }
module allocation_guard { header "__memory/allocation_guard.h" }
diff --git a/llvm/utils/gn/secondary/libcxx/include/BUILD.gn b/llvm/utils/gn/secondary/libcxx/include/BUILD.gn
index 3ca6b7fa0565e..f796bc6f05e69 100644
--- a/llvm/utils/gn/secondary/libcxx/include/BUILD.gn
+++ b/llvm/utils/gn/secondary/libcxx/include/BUILD.gn
@@ -1216,6 +1216,7 @@ if (current_toolchain == default_toolchain) {
"__mdspan/mdspan.h",
"__memory/addressof.h",
"__memory/align.h",
+ "__memory/align_impl.h",
"__memory/aligned_alloc.h",
"__memory/allocate_at_least.h",
"__memory/allocation_guard.h",
More information about the llvm-commits
mailing list