Skip to content

Commit 3edcceb

Browse files
committed
resolve conversation
1 parent 783de73 commit 3edcceb

File tree

9 files changed

+92
-47
lines changed

9 files changed

+92
-47
lines changed

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Improvements and New Features
8383
iterators, resulting in a performance improvement for ``std::deque<short>`` and
8484
``std::join_view<vector<vector<short>>>`` iterators.
8585

86-
- The performance of ``align`` has been improved about 3x by making it an inline function.
86+
- The performance of ``align`` has been improved about 2x by making it an inline function.
8787

8888
Deprecations and Removals
8989
-------------------------

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ set(files
570570
__mdspan/mdspan.h
571571
__memory/addressof.h
572572
__memory/align.h
573+
__memory/align_impl.h
573574
__memory/aligned_alloc.h
574575
__memory/allocate_at_least.h
575576
__memory/allocation_guard.h

libcxx/include/__configuration/abi.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,19 @@
9797
// so disable it.
9898
# define _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
9999
# endif
100-
// The PE/COFF format reports a linking error when encountering multiple symbol definitions where at least one is a
101-
// strong symbol. So we can't inlining a function without ABI breakchange.
102-
# if defined(_LIBCPP_OBJECT_FORMAT_COFF)
103-
# define _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
104-
# endif
105100
// Feature macros for disabling pre ABI v1 features. All of these options
106101
// are deprecated.
107102
# if defined(__FreeBSD__)
108103
# define _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR
109104
# endif
110105
#endif
111106

107+
// The PE/COFF format reports a linking error when encountering multiple symbol definitions where at least one is a
108+
// strong symbol. So we can't inlining a non-inline function without ABI break change.
109+
#if defined(_LIBCPP_OBJECT_FORMAT_COFF)
110+
# define _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
111+
#endif
112+
112113
// TODO(LLVM 22): Remove this check
113114
#if defined(_LIBCPP_ABI_NO_ITERATOR_BASES) && !defined(_LIBCPP_ABI_NO_REVERSE_ITERATOR_SECOND_MEMBER)
114115
# ifndef _LIBCPP_ONLY_NO_ITERATOR_BASES

libcxx/include/__memory/align.h

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,27 @@
99
#ifndef _LIBCPP___MEMORY_ALIGN_H
1010
#define _LIBCPP___MEMORY_ALIGN_H
1111

12+
#include "align_impl.h"
1213
#include <__config>
13-
#include <__cstddef/size_t.h>
14-
#include <cstdint>
14+
#include <__memory/align_impl.h>
1515

1616
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1717
# pragma GCC system_header
1818
#endif
1919

2020
_LIBCPP_BEGIN_NAMESPACE_STD
2121

22-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE inline void*
23-
__align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
24-
void* __r = nullptr;
25-
if (__sz <= __space) {
26-
char* __p1 = static_cast<char*>(__ptr);
27-
char* __p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(__p1 + (__align - 1)) & -__align);
28-
size_t __d = static_cast<size_t>(__p2 - __p1);
29-
if (__d <= __space - __sz) {
30-
__r = __p2;
31-
__ptr = __r;
32-
__space -= __d;
33-
}
34-
}
35-
return __r;
36-
}
37-
38-
#ifndef _LIBCPP_EXPORT_ALIGN_SYMBOL
39-
# ifdef _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
22+
#if defined(_LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR) && !defined(_LIBCPP_ABI_DO_NOT_EXPORT_ALIGN)
4023

4124
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
4225

43-
# else
26+
#else
4427

4528
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
46-
return __align_impl(__align, __sz, __ptr, __space);
29+
return align_impl(__align, __sz, __ptr, __space);
4730
}
4831

49-
# endif // _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
50-
51-
#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
32+
#endif // _LIBCPP_DISABLE_INLINE_OPTIMIZE_BECAUSE_MULTIPLY_SYMBOLS_ERROR
5233

5334
_LIBCPP_END_NAMESPACE_STD
5435

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___MEMORY_ALIGN_IMPL_H
10+
#define _LIBCPP___MEMORY_ALIGN_IMPL_H
11+
12+
#include <__config>
13+
#include <__cstddef/size_t.h>
14+
#include <cstdint>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
# pragma GCC system_header
18+
#endif
19+
20+
_LIBCPP_BEGIN_NAMESPACE_STD
21+
22+
inline _LIBCPP_HIDE_FROM_ABI void* align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
23+
void* __r = nullptr;
24+
if (__sz <= __space) {
25+
char* __p1 = static_cast<char*>(__ptr);
26+
char* __p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(__p1 + (__align - 1)) & -__align);
27+
size_t __d = static_cast<size_t>(__p2 - __p1);
28+
if (__d <= __space - __sz) {
29+
__r = __p2;
30+
__ptr = __r;
31+
__space -= __d;
32+
}
33+
}
34+
return __r;
35+
}
36+
37+
_LIBCPP_END_NAMESPACE_STD
38+
39+
#endif // _LIBCPP___MEMORY_ALIGN_IMPL_H

libcxx/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Get sources
22
set(LIBCXX_SOURCES
33
algorithm.cpp
4+
align.cpp
45
any.cpp
56
bind.cpp
67
call_once.cpp

libcxx/src/align.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <__config>
10+
#include <__memory/align_impl.h>
11+
12+
#if !defined(_LIBCPP_ABI_DO_NOT_EXPORT_ALIGN)
13+
14+
_LIBCPP_BEGIN_NAMESPACE_STD
15+
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
16+
return align_impl(alignment, size, ptr, space);
17+
}
18+
19+
_LIBCPP_END_NAMESPACE_STD
20+
21+
#endif // _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN

libcxx/src/memory.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
1111
# define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
1212
#endif
13-
#ifndef _LIBCPP_ABI_DO_NOT_EXPORT_ALIGN
14-
# define _LIBCPP_EXPORT_ALIGN_SYMBOL
15-
#endif
1613

1714
#include <__functional/hash.h>
1815
#include <memory>
@@ -135,12 +132,4 @@ __sp_mut& __get_sp_mut(const void* p) {
135132

136133
#endif // _LIBCPP_HAS_THREADS
137134

138-
#if defined(_LIBCPP_EXPORT_ALIGN_SYMBOL)
139-
140-
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
141-
return __align_impl(alignment, size, ptr, space);
142-
}
143-
144-
#endif // _LIBCPP_EXPORT_ALIGN_SYMBOL
145-
146135
_LIBCPP_END_NAMESPACE_STD

libcxx/test/benchmarks/memory/align.bench.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99
// UNSUPPORTED: c++03
1010

1111
#include <memory>
12+
#include <iostream>
1213

1314
#include "benchmark/benchmark.h"
14-
#include "test_macros.h"
15+
16+
struct Input {
17+
std::size_t align;
18+
std::size_t size;
19+
void* ptr;
20+
std::size_t buffer_size;
21+
};
1522

1623
static void BM_align(benchmark::State& state) {
1724
char buffer[1024];
18-
void* data = buffer + 123;
19-
std::size_t sz{sizeof(buffer) - 123};
20-
25+
Input input{};
26+
void* ptr = buffer + 123;
27+
std::size_t buffer_size = sizeof(buffer) - 123;
28+
input.align = state.range();
29+
input.size = state.range();
2130
for (auto _ : state) {
22-
benchmark::DoNotOptimize(std::align(state.range(), state.range(), data, sz));
31+
input.ptr = ptr;
32+
input.buffer_size = buffer_size;
33+
benchmark::DoNotOptimize(input);
34+
benchmark::DoNotOptimize(std::align(input.align, input.size, input.ptr, input.buffer_size));
2335
}
2436
}
2537
BENCHMARK(BM_align)->Range(1, 256);

0 commit comments

Comments
 (0)