[libc-commits] [libc] [llvm] [libc] Allow each function can have extra attributes by defining LLVM_LIBC_FUNCTION_ATTR_func macro. (PR #116160)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 20 11:09:33 PST 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/116160

>From 44637f4624e7c49055e5f417830cc0eb57b24e28 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Wed, 13 Nov 2024 23:14:28 -0500
Subject: [PATCH 1/4] [libc] Allow each fucntion can have extra attributes by
 defining LLVM_LIBC_FUNCTION_ATTR_func macro.

---
 libc/src/__support/common.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 48c773fa02c176..489ca41770e998 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -21,9 +21,24 @@
 #define LLVM_LIBC_FUNCTION_ATTR
 #endif
 
+// Allow each function `func` can have extra attributes specified by defining:
+// `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with
+// "LLVM_LIBC_EMPTY,"
+//
+// For example:
+// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, __attribute__((weak))
+#define LLVM_LIBC_EMPTY
+
+#define GET_SECOND(first, second, ...) second
+#define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY, )
+
+#define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
+#define EXPAND_ATTR(name) LLVM_LIBC_ATTR(name)
+
 // MacOS needs to be excluded because it does not support aliasing.
 #if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)                           \
+  EXPAND_ATTR(name)                                                            \
   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
       __##name##_impl__ __asm__(#name);                                        \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \

>From ed162f5748644f669da644e326920c7e7d48341d Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Tue, 19 Nov 2024 17:18:50 -0500
Subject: [PATCH 2/4] Address comments.

---
 libc/src/__support/common.h                      | 13 +++++++------
 .../libc/libc_build_rules.bzl                    | 16 +++++++++++-----
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 489ca41770e998..582b53f056b9f0 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -21,24 +21,25 @@
 #define LLVM_LIBC_FUNCTION_ATTR
 #endif
 
-// Allow each function `func` can have extra attributes specified by defining:
+// Allow each function `func` to have extra attributes specified by defining:
 // `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with
-// "LLVM_LIBC_EMPTY,"
+// "LLVM_LIBC_EMPTY, "
 //
-// For example:
-// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, __attribute__((weak))
+// For examples:
+// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, [[gnu::weak]]
+// #define LLVM_LIBC_FUNCTION_ATTR_memchr LLVM_LIBC_EMPTY, [[gnu::weak]]       \
+//                                        [[gnu::visibility("default")]]
 #define LLVM_LIBC_EMPTY
 
 #define GET_SECOND(first, second, ...) second
 #define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY, )
 
 #define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
-#define EXPAND_ATTR(name) LLVM_LIBC_ATTR(name)
 
 // MacOS needs to be excluded because it does not support aliasing.
 #if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
 #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)                           \
-  EXPAND_ATTR(name)                                                            \
+  LLVM_LIBC_ATTR(name)                                                         \
   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
       __##name##_impl__ __asm__(#name);                                        \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
index 9dc25f95b8e3f1..e95b65397c66ef 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -113,11 +113,17 @@ def libc_function(
 
     # This second target is the llvm libc C function with either a default or hidden visibility.
     # All other functions are hidden.
-    func_attrs = ["__attribute__((visibility(\"default\")))"]
-    if weak:
-        func_attrs = func_attrs + ["__attribute__((weak))"]
-    local_defines = local_defines or ["LIBC_COPT_PUBLIC_PACKAGING"]
-    local_defines = local_defines + ["LLVM_LIBC_FUNCTION_ATTR='%s'" % " ".join(func_attrs)]
+    global_func_attrs = [
+        "[[gnu::visibility("default")]]",
+    ]
+    func_attrs = [
+        "LLVM_LIBC_FUNCTION_ATTR_" + name + "='LLVM_LIBC_EMPTY, [[gnu::weak]]'",
+    ] if weak else []
+        
+    local_defines = (local_defines
+                    + ["LIBC_COPT_PUBLIC_PACKAGING"]
+                    + ["LLVM_LIBC_FUNCTION_ATTR='%s'" % " ".join(global_func_attrs)]
+                    + func_attrs)
     _libc_library(
         name = name,
         hidden = True,

>From 7a1a3c5a77a6dcf4f7886a5805e10f9893e45871 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 19 Nov 2024 23:14:21 +0000
Subject: [PATCH 3/4] Fix bazel build.

---
 utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
index e95b65397c66ef..1f372a1a09c80c 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -59,7 +59,7 @@ def libc_function(
         srcs,
         weak = False,
         copts = None,
-        local_defines = None,
+        local_defines = [],
         **kwargs):
     """Add target for a libc function.
 
@@ -108,16 +108,17 @@ def libc_function(
         name = libc_internal_target(name),
         srcs = srcs,
         copts = copts,
+        local_defines = local_defines,
         **kwargs
     )
 
     # This second target is the llvm libc C function with either a default or hidden visibility.
     # All other functions are hidden.
     global_func_attrs = [
-        "[[gnu::visibility("default")]]",
+        "[[gnu::visibility(\"default\")]]",
     ]
     func_attrs = [
-        "LLVM_LIBC_FUNCTION_ATTR_" + name + "='LLVM_LIBC_EMPTY, [[gnu::weak]]'",
+        "LLVM_LIBC_FUNCTION_ATTR_" + name + "=\"LLVM_LIBC_EMPTY, [[gnu::weak]]\"",
     ] if weak else []
         
     local_defines = (local_defines

>From fa25a750b450046a7c04fb99ea37e5fdd920c7dc Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 20 Nov 2024 18:15:05 +0000
Subject: [PATCH 4/4] Address comments.

---
 libc/src/__support/common.h                                | 2 +-
 utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl | 7 ++-----
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 582b53f056b9f0..79803a346f692e 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -32,7 +32,7 @@
 #define LLVM_LIBC_EMPTY
 
 #define GET_SECOND(first, second, ...) second
-#define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY, )
+#define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY)
 
 #define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
 
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
index 1f372a1a09c80c..ddc97119d30165 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -114,16 +114,13 @@ def libc_function(
 
     # This second target is the llvm libc C function with either a default or hidden visibility.
     # All other functions are hidden.
-    global_func_attrs = [
-        "[[gnu::visibility(\"default\")]]",
-    ]
     func_attrs = [
-        "LLVM_LIBC_FUNCTION_ATTR_" + name + "=\"LLVM_LIBC_EMPTY, [[gnu::weak]]\"",
+        "LLVM_LIBC_FUNCTION_ATTR_" + name + "='LLVM_LIBC_EMPTY, [[gnu::weak]]'",
     ] if weak else []
         
     local_defines = (local_defines
                     + ["LIBC_COPT_PUBLIC_PACKAGING"]
-                    + ["LLVM_LIBC_FUNCTION_ATTR='%s'" % " ".join(global_func_attrs)]
+                    + ["LLVM_LIBC_FUNCTION_ATTR='[[gnu::visibility(\"default\")]]'"]
                     + func_attrs)
     _libc_library(
         name = name,



More information about the libc-commits mailing list