Skip to content

Commit 5bf47e1

Browse files
committed
[libc] CopyAlignedBlocks can now specify alignment on top of block size
This has been requested in D92236 Differential Revision: https://reviews.llvm.org/D94770
1 parent cf0173d commit 5bf47e1

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

libc/src/string/memory_utils/memcpy_utils.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,35 @@ static void CopyBlockOverlap(char *__restrict dst, const char *__restrict src,
7272

7373
// Copies `count` bytes by blocks of `kBlockSize` bytes.
7474
// Copies at the start and end of the buffer are unaligned.
75-
// Copies in the middle of the buffer are aligned to `kBlockSize`.
75+
// Copies in the middle of the buffer are aligned to `kAlignment`.
7676
//
7777
// e.g. with
7878
// [12345678123456781234567812345678]
79-
// [__XXXXXXXXXXXXXXXXXXXXXXXXXXX___]
80-
// [__XXXXXXXX______________________]
81-
// [________XXXXXXXX________________]
82-
// [________________XXXXXXXX________]
83-
// [_____________________XXXXXXXX___]
79+
// [__XXXXXXXXXXXXXXXXXXXXXXXXXXXX___]
80+
// [__XXXX___________________________]
81+
// [_____XXXXXXXX____________________]
82+
// [_____________XXXXXXXX____________]
83+
// [_____________________XXXXXXXX____]
84+
// [______________________XXXXXXXX___]
8485
//
85-
// Precondition: `count > 2 * kBlockSize` for efficiency.
86-
// `count >= kBlockSize` for correctness.
87-
template <size_t kBlockSize>
86+
// Precondition: `kAlignment <= kBlockSize`
87+
// `count > 2 * kBlockSize` for efficiency.
88+
// `count >= kAlignment` for correctness.
89+
template <size_t kBlockSize, size_t kAlignment = kBlockSize>
8890
static void CopyAlignedBlocks(char *__restrict dst, const char *__restrict src,
8991
size_t count) {
90-
CopyBlock<kBlockSize>(dst, src); // Copy first block
92+
static_assert(is_power2(kAlignment), "kAlignment must be a power of two");
93+
static_assert(is_power2(kBlockSize), "kBlockSize must be a power of two");
94+
static_assert(kAlignment <= kBlockSize,
95+
"kAlignment must be less or equal to block size");
96+
CopyBlock<kAlignment>(dst, src); // Copy first block
9197

9298
// Copy aligned blocks
93-
const size_t ofla = offset_from_last_aligned<kBlockSize>(src);
99+
const size_t ofla = offset_from_last_aligned<kAlignment>(src);
94100
const size_t limit = count + ofla - kBlockSize;
95-
for (size_t offset = kBlockSize; offset < limit; offset += kBlockSize)
96-
CopyBlock<kBlockSize>(dst - ofla + offset, src - ofla + offset);
101+
for (size_t offset = kAlignment; offset < limit; offset += kBlockSize)
102+
CopyBlock<kBlockSize>(dst - ofla + offset,
103+
assume_aligned<kAlignment>(src - ofla + offset));
97104

98105
CopyLastBlock<kBlockSize>(dst, src, count); // Copy last block
99106
}

libc/src/string/memory_utils/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ static inline intptr_t offset_to_next_cache_line(const void *ptr) {
6060
return offset_to_next_aligned<LLVM_LIBC_CACHELINE_SIZE>(ptr);
6161
}
6262

63+
template <size_t alignment, typename T> static T *assume_aligned(T *ptr) {
64+
return reinterpret_cast<T *>(__builtin_assume_aligned(ptr, alignment));
65+
}
66+
6367
} // namespace __llvm_libc
6468

6569
#endif // LLVM_LIBC_SRC_MEMORY_UTILS_H

libc/test/src/string/memory_utils/memcpy_utils_test.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,24 @@ TEST(MemcpyUtilsTest, CopyAlignedBlocks) {
211211
EXPECT_STREQ(trace.Read(), "011121111111");
212212
}
213213

214-
TEST(MemcpyUtilsTest, MaxReloads) {
214+
TEST(MemcpyUtilsTest, CopyAlignedBlocksWithAlignment) {
215+
auto &trace = GetTrace();
216+
// Source is aligned and multiple of alignment.
217+
// "11111111"
218+
trace.Clear();
219+
CopyAlignedBlocks<8, 4>(I(0), I(0), 8);
220+
EXPECT_STREQ(trace.Write(), "22221111");
221+
EXPECT_STREQ(trace.Read(), "22221111");
222+
223+
// Source is aligned and multiple of alignment.
224+
// "111111111"
225+
trace.Clear();
226+
CopyAlignedBlocks<8, 4>(I(0), I(0), 9);
227+
EXPECT_STREQ(trace.Write(), "122211111");
228+
EXPECT_STREQ(trace.Read(), "122211111");
229+
}
230+
231+
TEST(MemcpyUtilsTest, CopyAlignedBlocksMaxReloads) {
215232
auto &trace = GetTrace();
216233
for (size_t alignment = 0; alignment < 32; ++alignment) {
217234
for (size_t count = 64; count < 768; ++count) {
@@ -231,4 +248,24 @@ TEST(MemcpyUtilsTest, MaxReloads) {
231248
}
232249
}
233250

251+
TEST(MemcpyUtilsTest, CopyAlignedBlocksWithAlignmentMaxReloads) {
252+
auto &trace = GetTrace();
253+
for (size_t alignment = 0; alignment < 32; ++alignment) {
254+
for (size_t count = 64; count < 768; ++count) {
255+
trace.Clear();
256+
// We should never reload more than twice when copying from count = 2x32.
257+
CopyAlignedBlocks<32, 16>(I(alignment), I(0), count);
258+
const char *const written = trace.Write();
259+
// First bytes are untouched.
260+
for (size_t i = 0; i < alignment; ++i)
261+
EXPECT_EQ(written[i], '0');
262+
// Next bytes are loaded once or twice but no more.
263+
for (size_t i = alignment; i < count; ++i) {
264+
EXPECT_GE(written[i], '1');
265+
EXPECT_LE(written[i], '2');
266+
}
267+
}
268+
}
269+
}
270+
234271
} // namespace __llvm_libc

0 commit comments

Comments
 (0)