Skip to content

Commit 7289b67

Browse files
authored
[libc] Improve memcpy for ARM Cortex-M supporting unaligned accesses. (#144872)
This implementation has been compiled with the [pigweed toolchain](https://pigweed.dev/toolchain.html) and tested on: - Raspberry Pi Pico 2 with the following options\ `--target=armv8m.main-none-eabi` `-march=armv8m.main+fp+dsp` `-mcpu=cortex-m33` - Raspberry Pi Pico with the following options\ `--target=armv6m-none-eabi` `-march=armv6m` `-mcpu=cortex-m0+` They both compile down to a little bit more than 200 bytes and are between 2 and 10 times faster than byte per byte copies. For best performance the following options can be set in the `libc/config/baremetal/arm/config.json` ``` { "codegen": { "LIBC_CONF_KEEP_FRAME_POINTER": { "value": false } }, "general": { "LIBC_ADD_NULL_CHECKS": { "value": false } } } ```
1 parent 0e26879 commit 7289b67

File tree

6 files changed

+226
-2
lines changed

6 files changed

+226
-2
lines changed

libc/src/__support/macros/optimization.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H
1111
#define LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H
1212

13-
#include "src/__support/macros/attributes.h" // LIBC_INLINE
13+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1414
#include "src/__support/macros/config.h"
1515
#include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG
1616

@@ -30,8 +30,10 @@ LIBC_INLINE constexpr bool expects_bool_condition(T value, T expected) {
3030

3131
#if defined(LIBC_COMPILER_IS_CLANG)
3232
#define LIBC_LOOP_NOUNROLL _Pragma("nounroll")
33+
#define LIBC_LOOP_UNROLL _Pragma("unroll")
3334
#elif defined(LIBC_COMPILER_IS_GCC)
3435
#define LIBC_LOOP_NOUNROLL _Pragma("GCC unroll 0")
36+
#define LIBC_LOOP_UNROLL _Pragma("GCC unroll 2048")
3537
#else
3638
#error "Unhandled compiler"
3739
#endif

libc/src/string/memory_utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_header_library(
77
aarch64/inline_memcpy.h
88
aarch64/inline_memmove.h
99
aarch64/inline_memset.h
10+
arm/inline_memcpy.h
1011
generic/aligned_access.h
1112
generic/byte_per_byte.h
1213
inline_bcmp.h
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
//===-- Memcpy implementation for arm ---------------------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H
9+
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H
10+
11+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
12+
#include "src/__support/macros/optimization.h" // LIBC_LOOP_NOUNROLL
13+
#include "src/string/memory_utils/utils.h" // memcpy_inline, distance_to_align
14+
15+
#include <stddef.h> // size_t
16+
17+
// https://libc.llvm.org/compiler_support.html
18+
// Support for [[likely]] / [[unlikely]]
19+
// [X] GCC 12.2
20+
// [X] Clang 12
21+
// [ ] Clang 11
22+
#define LIBC_ATTR_LIKELY [[likely]]
23+
#define LIBC_ATTR_UNLIKELY [[unlikely]]
24+
25+
#if defined(LIBC_COMPILER_IS_CLANG)
26+
#if LIBC_COMPILER_CLANG_VER < 1200
27+
#undef LIBC_ATTR_LIKELY
28+
#undef LIBC_ATTR_UNLIKELY
29+
#define LIBC_ATTR_LIKELY
30+
#define LIBC_ATTR_UNLIKELY
31+
#endif
32+
#endif
33+
34+
namespace LIBC_NAMESPACE_DECL {
35+
36+
namespace {
37+
38+
LIBC_INLINE_VAR constexpr size_t kWordSize = sizeof(uint32_t);
39+
40+
enum Strategy {
41+
ForceWordLdStChain,
42+
AssumeWordAligned,
43+
AssumeUnaligned,
44+
};
45+
46+
template <size_t bytes, Strategy strategy = AssumeUnaligned>
47+
LIBC_INLINE void copy_and_bump_pointers(Ptr &dst, CPtr &src) {
48+
if constexpr (strategy == AssumeUnaligned) {
49+
memcpy_inline<bytes>(assume_aligned<1>(dst), assume_aligned<1>(src));
50+
} else if constexpr (strategy == AssumeWordAligned) {
51+
static_assert(bytes >= kWordSize);
52+
memcpy_inline<bytes>(assume_aligned<kWordSize>(dst),
53+
assume_aligned<kWordSize>(src));
54+
} else if constexpr (strategy == ForceWordLdStChain) {
55+
// We restrict loads/stores to 4 byte to prevent the use of load/store
56+
// multiple (LDM, STM) and load/store double (LDRD, STRD). First, they may
57+
// fault (see notes below) and second, they use more registers which in turn
58+
// adds push/pop instructions in the hot path.
59+
static_assert((bytes % kWordSize == 0) && (bytes >= kWordSize));
60+
LIBC_LOOP_UNROLL
61+
for (size_t i = 0; i < bytes / kWordSize; ++i) {
62+
const size_t offset = i * kWordSize;
63+
memcpy_inline<kWordSize>(dst + offset, src + offset);
64+
}
65+
}
66+
// In the 1, 2, 4 byte copy case, the compiler can fold pointer offsetting
67+
// into the load/store instructions.
68+
// e.g.,
69+
// ldrb r3, [r1], #1
70+
// strb r3, [r0], #1
71+
dst += bytes;
72+
src += bytes;
73+
}
74+
75+
LIBC_INLINE void copy_bytes_and_bump_pointers(Ptr &dst, CPtr &src,
76+
const size_t size) {
77+
LIBC_LOOP_NOUNROLL
78+
for (size_t i = 0; i < size; ++i)
79+
*dst++ = *src++;
80+
}
81+
82+
template <size_t block_size, Strategy strategy>
83+
LIBC_INLINE void copy_blocks_and_update_args(Ptr &dst, CPtr &src,
84+
size_t &size) {
85+
LIBC_LOOP_NOUNROLL
86+
for (size_t i = 0; i < size / block_size; ++i)
87+
copy_and_bump_pointers<block_size, strategy>(dst, src);
88+
// Update `size` once at the end instead of once per iteration.
89+
size %= block_size;
90+
}
91+
92+
LIBC_INLINE CPtr bitwise_or(CPtr a, CPtr b) {
93+
return cpp::bit_cast<CPtr>(cpp::bit_cast<uintptr_t>(a) |
94+
cpp::bit_cast<uintptr_t>(b));
95+
}
96+
97+
LIBC_INLINE auto misaligned(CPtr a) {
98+
return distance_to_align_down<kWordSize>(a);
99+
}
100+
101+
} // namespace
102+
103+
// Implementation for Cortex-M0, M0+, M1.
104+
// Notes:
105+
// - It compiles down to 196 bytes, but 220 bytes when used through `memcpy`
106+
// that also needs to return the `dst` ptr.
107+
// - These cores do not allow for unaligned loads/stores.
108+
// - When `src` and `dst` are coaligned, we start by aligning them and perform
109+
// bulk copies. We let the compiler know the pointers are aligned so it can
110+
// use load/store multiple (LDM, STM). This significantly increase throughput
111+
// but it also requires more registers and push/pop instructions. This impacts
112+
// latency for small size copies.
113+
// - When `src` and `dst` are misaligned, we align `dst` and recompose words
114+
// using multiple aligned loads. `load_aligned` takes care of endianness
115+
// issues.
116+
[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm_low_end(Ptr dst, CPtr src,
117+
size_t size) {
118+
if (size >= 8) {
119+
if (const size_t offset = distance_to_align_up<kWordSize>(dst))
120+
LIBC_ATTR_UNLIKELY {
121+
copy_bytes_and_bump_pointers(dst, src, offset);
122+
size -= offset;
123+
}
124+
const auto src_alignment = distance_to_align_down<kWordSize>(src);
125+
if (src_alignment == 0)
126+
LIBC_ATTR_LIKELY {
127+
// Both `src` and `dst` are now word-aligned.
128+
copy_blocks_and_update_args<64, AssumeWordAligned>(dst, src, size);
129+
copy_blocks_and_update_args<16, AssumeWordAligned>(dst, src, size);
130+
copy_blocks_and_update_args<4, AssumeWordAligned>(dst, src, size);
131+
}
132+
else {
133+
// `dst` is aligned but `src` is not.
134+
LIBC_LOOP_NOUNROLL
135+
while (size >= kWordSize) {
136+
// Recompose word from multiple loads depending on the alignment.
137+
const uint32_t value =
138+
src_alignment == 2
139+
? load_aligned<uint32_t, uint16_t, uint16_t>(src)
140+
: load_aligned<uint32_t, uint8_t, uint16_t, uint8_t>(src);
141+
memcpy_inline<kWordSize>(assume_aligned<kWordSize>(dst), &value);
142+
dst += kWordSize;
143+
src += kWordSize;
144+
size -= kWordSize;
145+
}
146+
}
147+
// Up to 3 bytes may still need to be copied.
148+
// Handling them with the slow loop below.
149+
}
150+
copy_bytes_and_bump_pointers(dst, src, size);
151+
}
152+
153+
// Implementation for Cortex-M3, M4, M7, M23, M33, M35P, M52 with hardware
154+
// support for unaligned loads and stores.
155+
// Notes:
156+
// - It compiles down to 266 bytes.
157+
// - `dst` and `src` are not `__restrict` to prevent the compiler from
158+
// reordering loads/stores.
159+
// - We keep state variables to a strict minimum to keep everything in the free
160+
// registers and prevent costly push / pop.
161+
// - If unaligned single loads/stores to normal memory are supported, unaligned
162+
// accesses for load/store multiple (LDM, STM) and load/store double (LDRD,
163+
// STRD) instructions are generally not supported and will still fault so we
164+
// make sure to restrict unrolling to word loads/stores.
165+
[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm_mid_end(Ptr dst, CPtr src,
166+
size_t size) {
167+
if (misaligned(bitwise_or(src, dst)))
168+
LIBC_ATTR_UNLIKELY {
169+
if (size < 8)
170+
LIBC_ATTR_UNLIKELY {
171+
if (size & 1)
172+
copy_and_bump_pointers<1>(dst, src);
173+
if (size & 2)
174+
copy_and_bump_pointers<2>(dst, src);
175+
if (size & 4)
176+
copy_and_bump_pointers<4>(dst, src);
177+
return;
178+
}
179+
if (misaligned(src))
180+
LIBC_ATTR_UNLIKELY {
181+
const size_t offset = distance_to_align_up<kWordSize>(dst);
182+
if (offset & 1)
183+
copy_and_bump_pointers<1>(dst, src);
184+
if (offset & 2)
185+
copy_and_bump_pointers<2>(dst, src);
186+
size -= offset;
187+
}
188+
}
189+
copy_blocks_and_update_args<64, ForceWordLdStChain>(dst, src, size);
190+
copy_blocks_and_update_args<16, ForceWordLdStChain>(dst, src, size);
191+
copy_blocks_and_update_args<4, AssumeUnaligned>(dst, src, size);
192+
if (size & 1)
193+
copy_and_bump_pointers<1>(dst, src);
194+
if (size & 2)
195+
LIBC_ATTR_UNLIKELY
196+
copy_and_bump_pointers<2>(dst, src);
197+
}
198+
199+
[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm(void *__restrict dst_,
200+
const void *__restrict src_,
201+
size_t size) {
202+
Ptr dst = cpp::bit_cast<Ptr>(dst_);
203+
CPtr src = cpp::bit_cast<CPtr>(src_);
204+
#ifdef __ARM_FEATURE_UNALIGNED
205+
return inline_memcpy_arm_mid_end(dst, src, size);
206+
#else
207+
return inline_memcpy_arm_low_end(dst, src, size);
208+
#endif
209+
}
210+
211+
} // namespace LIBC_NAMESPACE_DECL
212+
213+
// Cleanup local macros
214+
#undef LIBC_ATTR_LIKELY
215+
#undef LIBC_ATTR_UNLIKELY
216+
217+
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H

libc/src/string/memory_utils/inline_memcpy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include "src/string/memory_utils/x86_64/inline_memcpy.h"
2323
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY \
2424
inline_memcpy_x86_maybe_interpose_repmovsb
25+
#elif defined(LIBC_TARGET_ARCH_IS_ARM)
26+
#include "src/string/memory_utils/arm/inline_memcpy.h"
27+
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_arm
2528
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
2629
#include "src/string/memory_utils/aarch64/inline_memcpy.h"
2730
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_aarch64

libc/src/string/memory_utils/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ LIBC_INLINE void memcpy_inline(void *__restrict dst,
101101
}
102102

103103
using Ptr = cpp::byte *; // Pointer to raw data.
104-
using CPtr = const cpp::byte *; // Const pointer to raw data.
104+
using CPtr = const cpp::byte *; // Pointer to const raw data.
105105

106106
// This type makes sure that we don't accidentally promote an integral type to
107107
// another one. It is only constructible from the exact T type.

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,6 +4218,7 @@ libc_support_library(
42184218
"src/string/memory_utils/aarch64/inline_memcpy.h",
42194219
"src/string/memory_utils/aarch64/inline_memmove.h",
42204220
"src/string/memory_utils/aarch64/inline_memset.h",
4221+
"src/string/memory_utils/arm/inline_memcpy.h",
42214222
"src/string/memory_utils/generic/aligned_access.h",
42224223
"src/string/memory_utils/generic/byte_per_byte.h",
42234224
"src/string/memory_utils/inline_bcmp.h",

0 commit comments

Comments
 (0)