Skip to content

Commit 8751f26

Browse files
[libc] add an SVE implementation of strlen (#167259)
This PR creates an SVE-based implementation for strlen by translating from the AOR code in tree. Microbenchmark shows improvements against NEON when N>=64. Although both implementations fall behind glibc by a large margin, this may be a good start point to explore SVE implementations. Together with the PR: 1. Added two more tests of strlen with special nul symbols. 2. Added strlen's fuzzer and fix a typo in previous heap fuzzer. ``` === strlen(16 bytes) === libc: 1.56115 ns/call, 9.54499 GiB/s neon: 1.59393 ns/call, 9.34867 GiB/s sve: 1.66097 ns/call, 8.97134 GiB/s === strlen(64 bytes) === libc: 2.06967 ns/call, 28.7991 GiB/s neon: 2.59914 ns/call, 22.9325 GiB/s sve: 2.58628 ns/call, 23.0465 GiB/s === strlen(256 bytes) === libc: 3.74165 ns/call, 63.7202 GiB/s neon: 8.98243 ns/call, 26.5428 GiB/s sve: 7.36426 ns/call, 32.3751 GiB/s === strlen(1024 bytes) === libc: 10.5327 ns/call, 90.5438 GiB/s neon: 34.363 ns/call, 27.7529 GiB/s sve: 26.9329 ns/call, 35.4092 GiB/s === strlen(4096 bytes) === libc: 37.7304 ns/call, 101.104 GiB/s neon: 145.911 ns/call, 26.144 GiB/s sve: 103.208 ns/call, 36.9612 GiB/s === strlen(1048576 bytes) === libc: 9623.4 ns/call, 101.478 GiB/s neon: 36138.2 ns/call, 27.023 GiB/s sve: 26605.6 ns/call, 36.7051 GiB/s ```
1 parent cd6c761 commit 8751f26

File tree

5 files changed

+111
-6
lines changed

5 files changed

+111
-6
lines changed

libc/fuzzing/__support/freelist_heap_fuzz.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ asm(R"(
2424
_end:
2525
.fill 1024
2626
__llvm_libc_heap_limit:
27-
)";
27+
)");
2828

2929
using LIBC_NAMESPACE::FreeListHeap;
3030
using LIBC_NAMESPACE::inline_memset;

libc/fuzzing/string/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ add_libc_fuzzer(
4040
DEPENDS
4141
libc.src.strings.bcmp
4242
)
43+
44+
add_libc_fuzzer(
45+
strlen_fuzz
46+
SRCS
47+
strlen_fuzz.cpp
48+
DEPENDS
49+
libc.src.string.strlen
50+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- strlen_fuzz.cpp ---------------------------------------------------===//
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+
/// Fuzzing test for llvm-libc strlen implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/string/strlen.h"
14+
#include <cstdint>
15+
#include <cstring>
16+
17+
// always null terminate the data
18+
extern "C" size_t LLVMFuzzerMutate(uint8_t *data, size_t size, size_t max_size);
19+
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *data, size_t size,
20+
size_t max_size, unsigned int seed) {
21+
size = LLVMFuzzerMutate(data, size, max_size);
22+
data[size - 1] = '\0';
23+
return size;
24+
}
25+
26+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
27+
size_t ref = ::strlen(reinterpret_cast<const char *>(data));
28+
size_t impl = LIBC_NAMESPACE::strlen(reinterpret_cast<const char *>(data));
29+
if (ref != impl)
30+
__builtin_trap();
31+
return 0;
32+
}

libc/src/string/memory_utils/aarch64/inline_strlen.h

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H
99
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H
1010

11+
#include "src/__support/macros/properties/cpu_features.h"
12+
1113
#if defined(__ARM_NEON)
1214
#include "src/__support/CPP/bit.h" // countr_zero
13-
1415
#include <arm_neon.h>
1516
#include <stddef.h> // size_t
16-
1717
namespace LIBC_NAMESPACE_DECL {
18-
1918
namespace neon {
2019
[[maybe_unused]] LIBC_NO_SANITIZE_OOB_ACCESS LIBC_INLINE static size_t
2120
string_length(const char *src) {
@@ -45,9 +44,63 @@ string_length(const char *src) {
4544
}
4645
}
4746
} // namespace neon
47+
} // namespace LIBC_NAMESPACE_DECL
48+
#endif // __ARM_NEON
4849

49-
namespace string_length_impl = neon;
50+
#ifdef LIBC_TARGET_CPU_HAS_SVE
51+
#include "src/__support/macros/optimization.h"
52+
#include <arm_sve.h>
53+
namespace LIBC_NAMESPACE_DECL {
54+
namespace sve {
55+
[[maybe_unused]] LIBC_INLINE static size_t string_length(const char *src) {
56+
const uint8_t *ptr = reinterpret_cast<const uint8_t *>(src);
57+
// Initialize the first-fault register to all true
58+
svsetffr();
59+
const svbool_t all_true = svptrue_b8(); // all true predicate
60+
svbool_t cmp_zero;
61+
size_t len = 0;
5062

63+
for (;;) {
64+
// Read a vector's worth of bytes, stopping on first fault.
65+
svuint8_t data = svldff1_u8(all_true, &ptr[len]);
66+
svbool_t fault_mask = svrdffr_z(all_true);
67+
bool has_no_fault = svptest_last(all_true, fault_mask);
68+
if (LIBC_LIKELY(has_no_fault)) {
69+
// First fault did not fail: the whole vector is valid.
70+
// Avoid depending on the contents of FFR beyond the branch.
71+
len += svcntb(); // speculative increment
72+
cmp_zero = svcmpeq_n_u8(all_true, data, 0);
73+
bool has_no_zero = !svptest_any(all_true, cmp_zero);
74+
if (LIBC_LIKELY(has_no_zero))
75+
continue;
76+
len -= svcntb(); // undo speculative increment
77+
break;
78+
} else {
79+
// First fault failed: only some of the vector is valid.
80+
// Perform the comparison only on the valid bytes.
81+
cmp_zero = svcmpeq_n_u8(fault_mask, data, 0);
82+
bool has_zero = svptest_any(fault_mask, cmp_zero);
83+
if (LIBC_LIKELY(has_zero))
84+
break;
85+
svsetffr();
86+
len += svcntp_b8(all_true, fault_mask);
87+
continue;
88+
}
89+
}
90+
// Select the bytes before the first and count them.
91+
svbool_t before_zero = svbrkb_z(all_true, cmp_zero);
92+
len += svcntp_b8(all_true, before_zero);
93+
return len;
94+
}
95+
} // namespace sve
96+
} // namespace LIBC_NAMESPACE_DECL
97+
#endif // LIBC_TARGET_CPU_HAS_SVE
98+
99+
namespace LIBC_NAMESPACE_DECL {
100+
#ifdef LIBC_TARGET_CPU_HAS_SVE
101+
namespace string_length_impl = sve;
102+
#elif defined(__ARM_NEON)
103+
namespace string_length_impl = neon;
104+
#endif
51105
} // namespace LIBC_NAMESPACE_DECL
52-
#endif // __ARM_NEON
53106
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H

libc/test/src/string/strlen_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ TEST(LlvmLibcStrLenTest, AnyString) {
2222
size_t result = LIBC_NAMESPACE::strlen(any);
2323
ASSERT_EQ((size_t)12, result);
2424
}
25+
26+
TEST(LlvmLibcStrLenTest, DataAfterNulString) {
27+
constexpr char A[10] = {'a', 'b', 'c', 'd', 'e', 'f', 0, 'h', 'i', 'j'};
28+
size_t result = LIBC_NAMESPACE::strlen(A);
29+
ASSERT_EQ((size_t)6, result);
30+
}
31+
32+
TEST(LlvmLibcStrLenTest, MultipleNulsInOneWord) {
33+
constexpr char A[10] = {'a', 'b', 0, 'd', 'e', 'f', 0, 'h', 'i', 'j'};
34+
size_t result = LIBC_NAMESPACE::strlen(A);
35+
ASSERT_EQ((size_t)2, result);
36+
}

0 commit comments

Comments
 (0)