[libc-commits] [libc] [libc] Remove unnecessary call in memfunction dispatchers (PR #75800)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Mon Dec 18 07:59:17 PST 2023


https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/75800

>From 8a7a186e9472da22536a329b46d2846aecb1176f Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Mon, 18 Dec 2023 14:31:57 +0000
Subject: [PATCH 1/2] [libc] Remove unnecessary call in memfunction dispatchers

Before this patch the compiler could generate unncessary calls to the selected implementation.
https://clang.llvm.org/docs/AttributeReference.html#flatten
---
 libc/src/string/memory_utils/inline_bcmp.h    | 3 ++-
 libc/src/string/memory_utils/inline_bzero.h   | 6 ++++--
 libc/src/string/memory_utils/inline_memcmp.h  | 3 ++-
 libc/src/string/memory_utils/inline_memcpy.h  | 4 ++--
 libc/src/string/memory_utils/inline_memmove.h | 8 ++++----
 5 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/libc/src/string/memory_utils/inline_bcmp.h b/libc/src/string/memory_utils/inline_bcmp.h
index b1c981d859e022..bbbcf581df3de4 100644
--- a/libc/src/string/memory_utils/inline_bcmp.h
+++ b/libc/src/string/memory_utils/inline_bcmp.h
@@ -32,7 +32,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LIBC_INLINE int inline_bcmp(const void *p1, const void *p2, size_t count) {
+__attribute__((flatten)) LIBC_INLINE int
+inline_bcmp(const void *p1, const void *p2, size_t count) {
   return static_cast<int>(LIBC_SRC_STRING_MEMORY_UTILS_BCMP(
       reinterpret_cast<CPtr>(p1), reinterpret_cast<CPtr>(p2), count));
 }
diff --git a/libc/src/string/memory_utils/inline_bzero.h b/libc/src/string/memory_utils/inline_bzero.h
index ed83cab68b2fd1..68fa48a3b4d21b 100644
--- a/libc/src/string/memory_utils/inline_bzero.h
+++ b/libc/src/string/memory_utils/inline_bzero.h
@@ -16,11 +16,13 @@
 
 namespace LIBC_NAMESPACE {
 
-LIBC_INLINE static void inline_bzero(Ptr dst, size_t count) {
+__attribute__((flatten)) LIBC_INLINE static void inline_bzero(Ptr dst,
+                                                              size_t count) {
   inline_memset(dst, 0, count);
 }
 
-LIBC_INLINE static void inline_bzero(void *dst, size_t count) {
+__attribute__((flatten)) LIBC_INLINE static void inline_bzero(void *dst,
+                                                              size_t count) {
   inline_bzero(reinterpret_cast<Ptr>(dst), count);
 }
 
diff --git a/libc/src/string/memory_utils/inline_memcmp.h b/libc/src/string/memory_utils/inline_memcmp.h
index d88d43691daee8..58c6bd405a5b6c 100644
--- a/libc/src/string/memory_utils/inline_memcmp.h
+++ b/libc/src/string/memory_utils/inline_memcmp.h
@@ -33,7 +33,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LIBC_INLINE int inline_memcmp(const void *p1, const void *p2, size_t count) {
+__attribute__((flatten)) LIBC_INLINE int
+inline_memcmp(const void *p1, const void *p2, size_t count) {
   return static_cast<int>(LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP(
       reinterpret_cast<CPtr>(p1), reinterpret_cast<CPtr>(p2), count));
 }
diff --git a/libc/src/string/memory_utils/inline_memcpy.h b/libc/src/string/memory_utils/inline_memcpy.h
index a92bf4ddf881d5..35d8cec5d04926 100644
--- a/libc/src/string/memory_utils/inline_memcpy.h
+++ b/libc/src/string/memory_utils/inline_memcpy.h
@@ -40,8 +40,8 @@
 
 namespace LIBC_NAMESPACE {
 
-LIBC_INLINE void inline_memcpy(void *__restrict dst, const void *__restrict src,
-                               size_t count) {
+__attribute__((flatten)) LIBC_INLINE void
+inline_memcpy(void *__restrict dst, const void *__restrict src, size_t count) {
   LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY(reinterpret_cast<Ptr>(dst),
                                       reinterpret_cast<CPtr>(src), count);
 }
diff --git a/libc/src/string/memory_utils/inline_memmove.h b/libc/src/string/memory_utils/inline_memmove.h
index 30c2c3ddbf1bb7..fbcd7a6b10df17 100644
--- a/libc/src/string/memory_utils/inline_memmove.h
+++ b/libc/src/string/memory_utils/inline_memmove.h
@@ -49,14 +49,14 @@ LIBC_INLINE constexpr bool inline_memmove_no_small_size(void *, const void *,
   return false;
 }
 
-LIBC_INLINE bool inline_memmove_small_size(void *dst, const void *src,
-                                           size_t count) {
+__attribute__((flatten)) LIBC_INLINE bool
+inline_memmove_small_size(void *dst, const void *src, size_t count) {
   return LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE_SMALL_SIZE(
       reinterpret_cast<Ptr>(dst), reinterpret_cast<CPtr>(src), count);
 }
 
-LIBC_INLINE void inline_memmove_follow_up(void *dst, const void *src,
-                                          size_t count) {
+__attribute__((flatten)) LIBC_INLINE void
+inline_memmove_follow_up(void *dst, const void *src, size_t count) {
   LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE_FOLLOW_UP(
       reinterpret_cast<Ptr>(dst), reinterpret_cast<CPtr>(src), count);
 }

>From 87aa43b02cca2dd6b5ff4132a7a6d770a098dbf1 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Mon, 18 Dec 2023 15:59:03 +0000
Subject: [PATCH 2/2] Use [[gnu::flatten]] instead of __attribute__((flatten))

---
 libc/src/string/memory_utils/inline_bcmp.h    | 4 ++--
 libc/src/string/memory_utils/inline_bzero.h   | 6 ++----
 libc/src/string/memory_utils/inline_memcmp.h  | 4 ++--
 libc/src/string/memory_utils/inline_memcpy.h  | 2 +-
 libc/src/string/memory_utils/inline_memmove.h | 4 ++--
 5 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/libc/src/string/memory_utils/inline_bcmp.h b/libc/src/string/memory_utils/inline_bcmp.h
index bbbcf581df3de4..69aa2acf75be47 100644
--- a/libc/src/string/memory_utils/inline_bcmp.h
+++ b/libc/src/string/memory_utils/inline_bcmp.h
@@ -32,8 +32,8 @@
 
 namespace LIBC_NAMESPACE {
 
-__attribute__((flatten)) LIBC_INLINE int
-inline_bcmp(const void *p1, const void *p2, size_t count) {
+[[gnu::flatten]] LIBC_INLINE int inline_bcmp(const void *p1, const void *p2,
+                                             size_t count) {
   return static_cast<int>(LIBC_SRC_STRING_MEMORY_UTILS_BCMP(
       reinterpret_cast<CPtr>(p1), reinterpret_cast<CPtr>(p2), count));
 }
diff --git a/libc/src/string/memory_utils/inline_bzero.h b/libc/src/string/memory_utils/inline_bzero.h
index 68fa48a3b4d21b..d760baca9d0f4d 100644
--- a/libc/src/string/memory_utils/inline_bzero.h
+++ b/libc/src/string/memory_utils/inline_bzero.h
@@ -16,13 +16,11 @@
 
 namespace LIBC_NAMESPACE {
 
-__attribute__((flatten)) LIBC_INLINE static void inline_bzero(Ptr dst,
-                                                              size_t count) {
+[[gnu::flatten]] LIBC_INLINE static void inline_bzero(Ptr dst, size_t count) {
   inline_memset(dst, 0, count);
 }
 
-__attribute__((flatten)) LIBC_INLINE static void inline_bzero(void *dst,
-                                                              size_t count) {
+[[gnu::flatten]] LIBC_INLINE static void inline_bzero(void *dst, size_t count) {
   inline_bzero(reinterpret_cast<Ptr>(dst), count);
 }
 
diff --git a/libc/src/string/memory_utils/inline_memcmp.h b/libc/src/string/memory_utils/inline_memcmp.h
index 58c6bd405a5b6c..1fdc13973c6462 100644
--- a/libc/src/string/memory_utils/inline_memcmp.h
+++ b/libc/src/string/memory_utils/inline_memcmp.h
@@ -33,8 +33,8 @@
 
 namespace LIBC_NAMESPACE {
 
-__attribute__((flatten)) LIBC_INLINE int
-inline_memcmp(const void *p1, const void *p2, size_t count) {
+[[gnu::flatten]] LIBC_INLINE int inline_memcmp(const void *p1, const void *p2,
+                                               size_t count) {
   return static_cast<int>(LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP(
       reinterpret_cast<CPtr>(p1), reinterpret_cast<CPtr>(p2), count));
 }
diff --git a/libc/src/string/memory_utils/inline_memcpy.h b/libc/src/string/memory_utils/inline_memcpy.h
index 35d8cec5d04926..c88fd55eff645f 100644
--- a/libc/src/string/memory_utils/inline_memcpy.h
+++ b/libc/src/string/memory_utils/inline_memcpy.h
@@ -40,7 +40,7 @@
 
 namespace LIBC_NAMESPACE {
 
-__attribute__((flatten)) LIBC_INLINE void
+[[gnu::flatten]] LIBC_INLINE void
 inline_memcpy(void *__restrict dst, const void *__restrict src, size_t count) {
   LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY(reinterpret_cast<Ptr>(dst),
                                       reinterpret_cast<CPtr>(src), count);
diff --git a/libc/src/string/memory_utils/inline_memmove.h b/libc/src/string/memory_utils/inline_memmove.h
index fbcd7a6b10df17..3cbc3e0b0a528d 100644
--- a/libc/src/string/memory_utils/inline_memmove.h
+++ b/libc/src/string/memory_utils/inline_memmove.h
@@ -49,13 +49,13 @@ LIBC_INLINE constexpr bool inline_memmove_no_small_size(void *, const void *,
   return false;
 }
 
-__attribute__((flatten)) LIBC_INLINE bool
+[[gnu::flatten]] LIBC_INLINE bool
 inline_memmove_small_size(void *dst, const void *src, size_t count) {
   return LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE_SMALL_SIZE(
       reinterpret_cast<Ptr>(dst), reinterpret_cast<CPtr>(src), count);
 }
 
-__attribute__((flatten)) LIBC_INLINE void
+[[gnu::flatten]] LIBC_INLINE void
 inline_memmove_follow_up(void *dst, const void *src, size_t count) {
   LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE_FOLLOW_UP(
       reinterpret_cast<Ptr>(dst), reinterpret_cast<CPtr>(src), count);



More information about the libc-commits mailing list