[libc] Rely on __builtin_memcpy_inline for memcpy implementation
authorGuillaume Chatelet <gchatelet@google.com>
Fri, 17 Jun 2022 13:28:47 +0000 (13:28 +0000)
committerGuillaume Chatelet <gchatelet@google.com>
Fri, 17 Jun 2022 14:22:26 +0000 (14:22 +0000)
This patch removes usage of `-mllvm -combiner-global-alias-analysis`
and relies on compiler builtin to implement `memcpy`.

Note that `-mllvm -combiner-global-alias-analysis` is actually only useful for
functions where buffers can alias (namely `memcpy` and `memmove`). The other
memory functions where not benefiting from the flag anyways.

The upside is that the memory functions can now be compiled from source with
thinlto (thinlto would not be able to carry on the flag when doing inlining).

The downside is that for compilers other than clang (i.e. not providing
`__builtin_memcpy_inline`) the codegen may be worse.

Differential Revision: https://reviews.llvm.org/D128051

libc/src/string/CMakeLists.txt
libc/src/string/memory_utils/memcpy_implementations.h
utils/bazel/llvm-project-overlay/libc/BUILD.bazel

index c4f45e6..a2d89ae 100644 (file)
@@ -279,7 +279,6 @@ function(add_implementation name impl_name)
     ${ARGN})
 
   if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
-    list(APPEND ADD_IMPL_MLLVM_COMPILE_OPTIONS "-combiner-global-alias-analysis")
     # Note that '-mllvm' needs to be prefixed with 'SHELL:' to prevent CMake flag deduplication.
     foreach(opt IN LISTS ADD_IMPL_MLLVM_COMPILE_OPTIONS)
       list(APPEND ADD_IMPL_COMPILE_OPTIONS "SHELL:-mllvm ${opt}")
index 8da4085..3385d40 100644 (file)
@@ -39,11 +39,11 @@ namespace __llvm_libc {
 
 static inline void inline_memcpy(char *__restrict dst,
                                  const char *__restrict src, size_t count) {
+  using namespace __llvm_libc::builtin;
 #if defined(LLVM_LIBC_ARCH_X86)
   /////////////////////////////////////////////////////////////////////////////
   // LLVM_LIBC_ARCH_X86
   /////////////////////////////////////////////////////////////////////////////
-  using namespace __llvm_libc::x86;
 
   // Whether to use only rep;movsb.
   constexpr bool USE_ONLY_REP_MOVSB =
@@ -69,7 +69,7 @@ static inline void inline_memcpy(char *__restrict dst,
 #endif
 
   if (USE_ONLY_REP_MOVSB)
-    return copy<Accelerator>(dst, src, count);
+    return copy<x86::Accelerator>(dst, src, count);
 
   if (count == 0)
     return;
@@ -96,12 +96,11 @@ static inline void inline_memcpy(char *__restrict dst,
   if (count <= REP_MOVS_B_SIZE)
     return copy<Align<_32, Arg::Dst>::Then<Loop<LoopBlockSize>>>(dst, src,
                                                                  count);
-  return copy<Accelerator>(dst, src, count);
+  return copy<x86::Accelerator>(dst, src, count);
 #elif defined(LLVM_LIBC_ARCH_AARCH64)
   /////////////////////////////////////////////////////////////////////////////
   // LLVM_LIBC_ARCH_AARCH64
   /////////////////////////////////////////////////////////////////////////////
-  using namespace __llvm_libc::scalar;
   if (count == 0)
     return;
   if (count == 1)
@@ -127,7 +126,6 @@ static inline void inline_memcpy(char *__restrict dst,
   /////////////////////////////////////////////////////////////////////////////
   // Default
   /////////////////////////////////////////////////////////////////////////////
-  using namespace __llvm_libc::scalar;
   if (count == 0)
     return;
   if (count == 1)
index f40b671..a0d423a 100644 (file)
@@ -829,7 +829,6 @@ libc_function(
     copts = [
         "-fno-builtin-memcpy",
         "-fno-builtin-memmove",
-        "-mllvm -combiner-global-alias-analysis",
         "-mllvm --tail-merge-threshold=0",
     ],
     features = no_sanitize_features,
@@ -845,7 +844,6 @@ libc_function(
     hdrs = ["src/string/memset.h"],
     copts = [
         "-fno-builtin-memset",
-        "-mllvm -combiner-global-alias-analysis",
     ],
     features = no_sanitize_features,
     deps = [
@@ -860,7 +858,6 @@ libc_function(
     hdrs = ["src/string/memmove.h"],
     copts = [
         "-fno-builtin-memmove",
-        "-mllvm -combiner-global-alias-analysis",
     ],
     features = no_sanitize_features,
     deps = [
@@ -876,7 +873,6 @@ libc_function(
     hdrs = ["src/string/memcmp.h"],
     copts = [
         "-fno-builtin-memcmp",
-        "-mllvm -combiner-global-alias-analysis",
     ],
     features = no_sanitize_features,
     deps = [
@@ -908,7 +904,6 @@ libc_function(
     copts = [
         "-fno-builtin-bzero",
         "-fno-builtin-memset",
-        "-mllvm -combiner-global-alias-analysis",
     ],
     features = no_sanitize_features,
     deps = [