[libc] [llvm] [libc] clean up string_utils memory functions (PR #143031)
Michael Jones via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 5 16:04:38 PDT 2025
https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/143031
>From 077d275f3d730af59e10cafdfe46c41ee451c635 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 5 Jun 2025 13:28:39 -0700
Subject: [PATCH 1/2] [libc] clean up string_utils memory functions
The string_utils.h file previously included both memcpy and bzero. There
were no uses of bzero, and only one use of memcpy which was replaced
with __builtin_memcpy.
---
libc/src/string/CMakeLists.txt | 3 ---
libc/src/string/string_utils.h | 4 +---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
3 files changed, 1 insertion(+), 7 deletions(-)
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 2c607bf8ea895..deb74d979a3f7 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -15,10 +15,7 @@ add_header_library(
HDRS
string_utils.h
DEPENDS
- .memory_utils.inline_bzero
- .memory_utils.inline_memcpy
libc.hdr.types.size_t
- libc.include.stdlib
libc.src.__support.CPP.bitset
libc.src.__support.CPP.type_traits
libc.src.__support.common
diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index e4659f65c93e2..e666636632184 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -19,8 +19,6 @@
#include "src/__support/CPP/type_traits.h" // cpp::is_same_v
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
-#include "src/string/memory_utils/inline_bzero.h"
-#include "src/string/memory_utils/inline_memcpy.h"
namespace LIBC_NAMESPACE_DECL {
namespace internal {
@@ -220,7 +218,7 @@ LIBC_INLINE size_t strlcpy(char *__restrict dst, const char *__restrict src,
if (!size)
return len;
size_t n = len < size - 1 ? len : size - 1;
- inline_memcpy(dst, src, n);
+ __builtin_memcpy(dst, src, n);
dst[n] = '\0';
return len;
}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index aa7f0a9389405..65e6a947eaed6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4190,7 +4190,6 @@ libc_support_library(
":__support_cpp_bitset",
":__support_macros_optimization",
":llvm_libc_types_size_t",
- ":string_memory_utils",
":types_size_t",
],
)
>From 4d0f0c1c44e519215ae6b128ffbecb5fd15ab30a Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 5 Jun 2025 16:03:56 -0700
Subject: [PATCH 2/2] fix strsep, remove dependency on cpp::byte
---
libc/src/string/CMakeLists.txt | 1 +
libc/src/string/string_utils.h | 7 ++++---
libc/src/string/strsep.cpp | 2 ++
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 ++
4 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index deb74d979a3f7..ca2f9e20454b7 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -16,6 +16,7 @@ add_header_library(
string_utils.h
DEPENDS
libc.hdr.types.size_t
+ libc.hdr.limits_macros
libc.src.__support.CPP.bitset
libc.src.__support.CPP.type_traits
libc.src.__support.common
diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index e666636632184..dcbfc7584a30e 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -14,6 +14,7 @@
#ifndef LLVM_LIBC_SRC_STRING_STRING_UTILS_H
#define LLVM_LIBC_SRC_STRING_STRING_UTILS_H
+#include "hdr/limits_macros.h"
#include "hdr/types/size_t.h"
#include "src/__support/CPP/bitset.h"
#include "src/__support/CPP/type_traits.h" // cpp::is_same_v
@@ -24,7 +25,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace internal {
template <typename Word> LIBC_INLINE constexpr Word repeat_byte(Word byte) {
- constexpr size_t BITS_IN_BYTE = 8;
+ static_assert(CHAR_BIT == 8, "repeat_byte assumes a byte is 8 bits.");
+ constexpr size_t BITS_IN_BYTE = CHAR_BIT;
constexpr size_t BYTE_MASK = 0xff;
Word result = 0;
byte = byte & BYTE_MASK;
@@ -187,8 +189,7 @@ LIBC_INLINE char *string_token(char *__restrict src,
if (LIBC_UNLIKELY(src == nullptr && ((src = *saveptr) == nullptr)))
return nullptr;
- static_assert(sizeof(char) == sizeof(cpp::byte),
- "bitset of 256 assumes char is 8 bits");
+ static_assert(CHAR_BIT == 8, "bitset of 256 assumes char is 8 bits");
cpp::bitset<256> delimiter_set;
for (; *delimiter_string != '\0'; ++delimiter_string)
delimiter_set.set(static_cast<size_t>(*delimiter_string));
diff --git a/libc/src/string/strsep.cpp b/libc/src/string/strsep.cpp
index 555c2f3c9791f..41874b6af2263 100644
--- a/libc/src/string/strsep.cpp
+++ b/libc/src/string/strsep.cpp
@@ -12,6 +12,8 @@
#include "src/__support/macros/null_check.h"
#include "src/string/string_utils.h"
+#include "src/__support/common.h"
+
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(char *, strsep,
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 65e6a947eaed6..b86d2f27e516a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4188,7 +4188,9 @@ libc_support_library(
deps = [
":__support_common",
":__support_cpp_bitset",
+ ":__support_cpp_type_traits",
":__support_macros_optimization",
+ ":hdr_limits_macros",
":llvm_libc_types_size_t",
":types_size_t",
],
More information about the llvm-commits
mailing list