-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc] Change __builtin_memcpy to inline_memcpy. #158345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-libc Author: None (lntue) ChangesFull diff: https://github.com/llvm/llvm-project/pull/158345.diff 15 Files Affected:
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index b6e87ac336fb2..0ef09a9b8c9d0 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -302,6 +302,7 @@ add_header_library(
DEPENDS
libc.hdr.stdint_proxy
libc.src.__support.common
+ libc.src.string.memory_utils.inline_memcpy
)
add_header_library(
diff --git a/libc/src/__support/arg_list.h b/libc/src/__support/arg_list.h
index 1e26a5e8ef9c7..7b78a9c0fe619 100644
--- a/libc/src/__support/arg_list.h
+++ b/libc/src/__support/arg_list.h
@@ -12,6 +12,7 @@
#include "hdr/stdint_proxy.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
#include <stdarg.h>
#include <stddef.h>
@@ -126,7 +127,7 @@ template <bool packed> class StructArgList {
// Memcpy because pointer alignment may be illegal given a packed struct.
T val;
- __builtin_memcpy(&val, ptr, sizeof(T));
+ inline_memcpy(&val, ptr, sizeof(T));
ptr =
reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(ptr) + sizeof(T));
diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt
index 76eb0a2fdaaa5..ee66145e60156 100644
--- a/libc/src/stdio/printf_core/CMakeLists.txt
+++ b/libc/src/stdio/printf_core/CMakeLists.txt
@@ -112,6 +112,7 @@ add_header_library(
libc.src.__support.libc_assert
libc.src.__support.uint128
libc.src.__support.StringUtil.error_to_string
+ libc.src.string.memory_utils.inline_memcpy
)
add_header_library(
diff --git a/libc/src/stdio/printf_core/float_dec_converter_limited.h b/libc/src/stdio/printf_core/float_dec_converter_limited.h
index f468dbc8e2ae8..9cdc13573d320 100644
--- a/libc/src/stdio/printf_core/float_dec_converter_limited.h
+++ b/libc/src/stdio/printf_core/float_dec_converter_limited.h
@@ -53,6 +53,7 @@
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/float_inf_nan_converter.h"
#include "src/stdio/printf_core/writer.h"
+#include "src/string/memory_utils/inline_memcpy.h"
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
@@ -250,7 +251,7 @@ DigitsOutput decimal_digits(DigitsInput input, int precision, bool e_mode) {
// there's space for it in the DigitsOutput buffer).
DigitsOutput output;
output.ndigits = view.size();
- __builtin_memcpy(output.digits, view.data(), output.ndigits);
+ inline_memcpy(output.digits, view.data(), output.ndigits);
// Set up the output exponent, which is done differently depending on mode.
// Also, figure out whether we have one digit too many, and if so, set the
@@ -551,7 +552,7 @@ convert_float_inner(Writer<write_mode> *writer, const FormatSection &to_conv,
cpp::string_view expview = expcvt.view();
expbuf[0] = internal::islower(to_conv.conv_name) ? 'e' : 'E';
explen = expview.size() + 1;
- __builtin_memcpy(expbuf + 1, expview.data(), expview.size());
+ inline_memcpy(expbuf + 1, expview.data(), expview.size());
}
// Now we know enough to work out the length of the unpadded output:
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index aa653c38a8c3f..c464f82dcbda7 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -292,6 +292,7 @@ add_header_library(
libc.hdr.stdint_proxy
libc.include.stdlib
libc.src.__support.CPP.cstddef
+ libc.src.string.memory_utils.inline_memcpy
)
add_entrypoint_object(
diff --git a/libc/src/stdlib/qsort_data.h b/libc/src/stdlib/qsort_data.h
index 739fce88ab75d..4f9774088fbd3 100644
--- a/libc/src/stdlib/qsort_data.h
+++ b/libc/src/stdlib/qsort_data.h
@@ -12,6 +12,7 @@
#include "hdr/stdint_proxy.h"
#include "src/__support/CPP/cstddef.h"
#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
namespace LIBC_NAMESPACE_DECL {
namespace internal {
@@ -54,9 +55,9 @@ class ArrayGenericSize {
const cpp::byte *elem_i_block_end = elem_i + (elem_size - elem_size_rem);
while (elem_i != elem_i_block_end) {
- __builtin_memcpy(tmp_block, elem_i, BLOCK_SIZE);
- __builtin_memcpy(elem_i, elem_j, BLOCK_SIZE);
- __builtin_memcpy(elem_j, tmp_block, BLOCK_SIZE);
+ inline_memcpy(tmp_block, elem_i, BLOCK_SIZE);
+ inline_memcpy(elem_i, elem_j, BLOCK_SIZE);
+ inline_memcpy(elem_j, tmp_block, BLOCK_SIZE);
elem_i += BLOCK_SIZE;
elem_j += BLOCK_SIZE;
@@ -112,9 +113,9 @@ template <size_t ELEM_SIZE> class ArrayFixedSize {
cpp::byte *elem_i = get_internal(i);
cpp::byte *elem_j = get_internal(j);
- __builtin_memcpy(tmp, elem_i, ELEM_SIZE);
+ inline_memcpy(tmp, elem_i, ELEM_SIZE);
__builtin_memmove(elem_i, elem_j, ELEM_SIZE);
- __builtin_memcpy(elem_j, tmp, ELEM_SIZE);
+ inline_memcpy(elem_j, tmp, ELEM_SIZE);
}
LIBC_INLINE size_t len() const { return array_len; }
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 5c9f622d44397..b8cdb2a7d3538 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -22,6 +22,7 @@ add_header_library(
libc.src.__support.CPP.type_traits
libc.src.__support.CPP.simd
libc.src.__support.common
+ libc.src.string.memory_utils.inline_memcpy
${string_config_options}
)
diff --git a/libc/src/string/stpcpy.cpp b/libc/src/string/stpcpy.cpp
index 48c0db950ace0..fefae81172585 100644
--- a/libc/src/string/stpcpy.cpp
+++ b/libc/src/string/stpcpy.cpp
@@ -8,6 +8,7 @@
#include "src/string/stpcpy.h"
#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/string_utils.h"
#include "src/__support/common.h"
@@ -17,7 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(char *, stpcpy,
(char *__restrict dest, const char *__restrict src)) {
size_t size = internal::string_length(src) + 1;
- __builtin_memcpy(dest, src, size);
+ inline_memcpy(dest, src, size);
char *result = dest + size;
if (result != nullptr)
diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index 10803488b6cf5..9d636d02f4756 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -21,6 +21,7 @@
#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_memcpy.h"
#if defined(LIBC_COPT_STRING_UNSAFE_WIDE_READ)
#if LIBC_HAS_VECTOR_TYPE
@@ -242,7 +243,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;
- __builtin_memcpy(dst, src, n);
+ inline_memcpy(dst, src, n);
dst[n] = '\0';
return len;
}
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 9ba0a06c57b7f..adde382bf0950 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -452,6 +452,7 @@ add_entrypoint_object(
DEPENDS
libc.hdr.types.size_t
libc.hdr.wchar_macros
+ libc.src.string.memory_utils.inline_memcpy
)
add_entrypoint_object(
diff --git a/libc/src/wchar/wcpcpy.cpp b/libc/src/wchar/wcpcpy.cpp
index 9e2b12f09eb05..b6d80d4d671d9 100644
--- a/libc/src/wchar/wcpcpy.cpp
+++ b/libc/src/wchar/wcpcpy.cpp
@@ -19,7 +19,7 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(wchar_t *, wcpcpy,
(wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
size_t size = internal::string_length(s2);
- __builtin_memcpy(s1, s2, (size + 1) * sizeof(wchar_t));
+ inline_memcpy(s1, s2, (size + 1) * sizeof(wchar_t));
wchar_t *result = s1 + size;
return result;
}
diff --git a/libc/src/wchar/wcscpy.cpp b/libc/src/wchar/wcscpy.cpp
index 01ba994cecbb2..703706e6a7be8 100644
--- a/libc/src/wchar/wcscpy.cpp
+++ b/libc/src/wchar/wcscpy.cpp
@@ -19,7 +19,7 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(wchar_t *, wcscpy,
(wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
size_t size = internal::string_length(s2) + 1;
- __builtin_memcpy(s1, s2, size * sizeof(wchar_t));
+ inline_memcpy(s1, s2, size * sizeof(wchar_t));
return s1;
}
diff --git a/libc/src/wchar/wmemcpy.cpp b/libc/src/wchar/wmemcpy.cpp
index bf92309b20944..56708d6cee496 100644
--- a/libc/src/wchar/wmemcpy.cpp
+++ b/libc/src/wchar/wmemcpy.cpp
@@ -12,13 +12,14 @@
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(wchar_t *, wmemcpy,
(wchar_t *__restrict s1, const wchar_t *__restrict s2,
size_t n)) {
- __builtin_memcpy(s1, s2, n * sizeof(wchar_t));
+ inline_memcpy(s1, s2, n * sizeof(wchar_t));
return s1;
}
diff --git a/libc/src/wchar/wmempcpy.cpp b/libc/src/wchar/wmempcpy.cpp
index 21e16210a757a..d8b89c0a88d05 100644
--- a/libc/src/wchar/wmempcpy.cpp
+++ b/libc/src/wchar/wmempcpy.cpp
@@ -11,13 +11,14 @@
#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
+#include "src/string/memory_utils/inline_memcpy.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(wchar_t *, wmempcpy,
(wchar_t *__restrict to, const wchar_t *__restrict from,
size_t size)) {
- __builtin_memcpy(to, from, size * sizeof(wchar_t));
+ inline_memcpy(to, from, size * sizeof(wchar_t));
return reinterpret_cast<wchar_t *>(to) + size;
}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index d9b1bb5635aaf..a955f7f4916ac 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -818,6 +818,7 @@ libc_support_library(
hdrs = ["src/__support/arg_list.h"],
deps = [
":__support_common",
+ ":string_memory_utils",
],
)
@@ -5003,6 +5004,7 @@ libc_support_library(
":__support_cpp_bit",
":__support_cpp_cstddef",
":__support_macros_attributes",
+ ":string_memory_utils",
],
)
@@ -6945,6 +6947,7 @@ libc_function(
deps = [
":__support_common",
":__support_macros_config",
+ ":string_memory_utils",
":types_size_t",
":types_wchar_t",
],
@@ -6968,6 +6971,7 @@ libc_function(
hdrs = ["src/wchar/wmempcpy.h"],
deps = [
":__support_common",
+ ":string_memory_utils",
":types_size_t",
":types_wchar_t",
],
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
For context the reason behind this change is MSVC doesn't support __builtin_memcpy
so we're moving everything to inline_memcpy
. We can discuss how to adjust this so it's more optimal later but this change is needed to unblock MSVC.
No description provided.