[libc-commits] [libc] [libc][sched] Implement `CPU_ZERO`, `CPU_ISSET`, `CPU_SET` macros (PR #131524)

Krishna Pandey via libc-commits libc-commits at lists.llvm.org
Sun Mar 16 12:38:26 PDT 2025


https://github.com/krishna2803 updated https://github.com/llvm/llvm-project/pull/131524

>From 365fe478f157d1754a4db11b2244f3c6189a7227 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 05:16:37 +0530
Subject: [PATCH 01/12] feat: implement CPU_ZERO[_S] macros

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/hdr/types/CMakeLists.txt                 |  8 +++++++
 libc/hdr/types/cpu_set_t.h                    | 22 +++++++++++++++++
 .../llvm-libc-macros/linux/sched-macros.h     |  2 ++
 libc/src/sched/CMakeLists.txt                 |  7 ++++++
 libc/src/sched/linux/sched_setcpuzero.cpp     | 24 +++++++++++++++++++
 libc/src/sched/sched_setcpuzero.h             | 24 +++++++++++++++++++
 9 files changed, 90 insertions(+)
 create mode 100644 libc/hdr/types/cpu_set_t.h
 create mode 100644 libc/src/sched/linux/sched_setcpuzero.cpp
 create mode 100644 libc/src/sched/sched_setcpuzero.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ab1917259519b..a46b30b04aeb6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -910,6 +910,7 @@ if(LLVM_LIBC_FULL_BUILD)
 
     # sched.h entrypoints
     libc.src.sched.__sched_getcpucount
+    libc.src.sched.__sched_setcpuzero
 
     # strings.h entrypoints
     libc.src.strings.strcasecmp_l
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 4b69e43bce37d..5458705a867cc 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -858,6 +858,7 @@ if(LLVM_LIBC_FULL_BUILD)
 
     # sched.h entrypoints
     libc.src.sched.__sched_getcpucount
+    libc.src.sched.__sched_setcpuzero
 
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a29478898fe70..5144f2e8246f4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1028,6 +1028,7 @@ if(LLVM_LIBC_FULL_BUILD)
 
     # sched.h entrypoints
     libc.src.sched.__sched_getcpucount
+    libc.src.sched.__sched_setcpuzero
 
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 84a2647ba664d..c9aa889a44fa4 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -349,3 +349,11 @@ add_proxy_header_library(
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-types.struct_pollfd
 )
+
+add_proxy_header_library(
+  cpu_set_t
+  HDRS
+    cpu_set_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.cpu_set_t
+)
diff --git a/libc/hdr/types/cpu_set_t.h b/libc/hdr/types/cpu_set_t.h
new file mode 100644
index 0000000000000..26aed7592fa2c
--- /dev/null
+++ b/libc/hdr/types/cpu_set_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for cpu_set_t -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
+#define LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/cpu_set_t.h"
+
+#else // Overlay mode
+
+#include <sched.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
diff --git a/libc/include/llvm-libc-macros/linux/sched-macros.h b/libc/include/llvm-libc-macros/linux/sched-macros.h
index ace620049ca0c..d23585c75981f 100644
--- a/libc/include/llvm-libc-macros/linux/sched-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sched-macros.h
@@ -25,5 +25,7 @@
 
 #define CPU_COUNT_S(setsize, set) __sched_getcpucount(setsize, set)
 #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
+#define CPU_ZERO_S(setsize, set) __sched_setcpuzero(setsize, set)
+#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
 
 #endif // LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H
diff --git a/libc/src/sched/CMakeLists.txt b/libc/src/sched/CMakeLists.txt
index a98940c55abd5..293016de2a9f1 100644
--- a/libc/src/sched/CMakeLists.txt
+++ b/libc/src/sched/CMakeLists.txt
@@ -78,3 +78,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.__sched_getcpucount
 )
+
+add_entrypoint_object(
+  __sched_setcpuzero
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.__sched_setcpuzero
+)
diff --git a/libc/src/sched/linux/sched_setcpuzero.cpp b/libc/src/sched/linux/sched_setcpuzero.cpp
new file mode 100644
index 0000000000000..ef8829f999eaa
--- /dev/null
+++ b/libc/src/sched/linux/sched_setcpuzero.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of sched_setcpuzero --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sched/sched_setcpuzero.h"
+
+#include "src/__support/common.h"        // LLVM_LIBC_FUNCTION
+#include "src/__support/macros/config.h" // LLVM_LIBC_FUNCTION
+
+#include "hdr/types/cpu_set_t.h"
+#include "hdr/types/size_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, __sched_setcpuzero,
+                   (const size_t cpuset_size, cpu_set_t *set)) {
+  __builtin_memset(set, 0, cpuset_size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sched/sched_setcpuzero.h b/libc/src/sched/sched_setcpuzero.h
new file mode 100644
index 0000000000000..53a0b4569665f
--- /dev/null
+++ b/libc/src/sched/sched_setcpuzero.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for sched_setcpuzero --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H
+#define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H
+
+#include "src/__support/macros/config.h"
+
+#include "hdr/types/cpu_set_t.h"
+#include "hdr/types/size_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// for internal use in the CPU_ZERO macro
+void __sched_setcpuzero(const size_t cpuset_size, cpu_set_t *set);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H

>From 862d702c148478d7409da25dbeab80b6873d5c4e Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 05:57:08 +0530
Subject: [PATCH 02/12] feat: add entrypoint for __sched_setcpuzero

---
 libc/src/sched/linux/CMakeLists.txt | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index ac95bf85da534..54c8ed66f2d82 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -30,7 +30,7 @@ add_entrypoint_object(
     ../sched_getcpucount.h
   DEPENDS
     libc.include.sched
-  )
+)
 
 add_entrypoint_object(
   sched_yield
@@ -135,3 +135,14 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
+
+add_entrypoint_object(
+  __sched_setcpuzero
+  SRCS
+    sched_setcpuzero.cpp
+  HDRS
+    ../sched_setcpuzero.h
+  DEPENDS
+    libc.hdr.types.cpu_set_t
+    libc.hdr.types.size_t
+)

>From b0af3f5febf91e1dbe7857bc20bc727470dbf087 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:06:55 +0530
Subject: [PATCH 03/12] feat: implement CPU_SETSIZE and NCPUBITS

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/include/llvm-libc-macros/linux/sched-macros.h | 2 ++
 libc/include/llvm-libc-types/cpu_set_t.h           | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/libc/include/llvm-libc-macros/linux/sched-macros.h b/libc/include/llvm-libc-macros/linux/sched-macros.h
index d23585c75981f..f15030fbbab64 100644
--- a/libc/include/llvm-libc-macros/linux/sched-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sched-macros.h
@@ -23,6 +23,8 @@
 #define SCHED_IDLE 5
 #define SCHED_DEADLINE 6
 
+#define CPU_SETSIZE __CPU_SETSIZE
+#define NCPUBITS __NCPUBITS
 #define CPU_COUNT_S(setsize, set) __sched_getcpucount(setsize, set)
 #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
 #define CPU_ZERO_S(setsize, set) __sched_setcpuzero(setsize, set)
diff --git a/libc/include/llvm-libc-types/cpu_set_t.h b/libc/include/llvm-libc-types/cpu_set_t.h
index e7f52597e147e..7af74a31c6932 100644
--- a/libc/include/llvm-libc-types/cpu_set_t.h
+++ b/libc/include/llvm-libc-types/cpu_set_t.h
@@ -9,6 +9,9 @@
 #ifndef LLVM_LIBC_TYPES_CPU_SET_T_H
 #define LLVM_LIBC_TYPES_CPU_SET_T_H
 
+#define __CPU_SETSIZE 1024
+#define __NCPUBITS8 8 * sizeof(unsigned long)
+
 typedef struct {
   // If a processor with more than 1024 CPUs is to be supported in future,
   // we need to adjust the size of this array.

>From 4dc237114fb5bac7235101ca2ca1b5f2ae9594d0 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:09:46 +0530
Subject: [PATCH 04/12] feat: implement CPU_SET[_S] macros

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 .../llvm-libc-macros/linux/sched-macros.h     |  2 ++
 libc/src/sched/CMakeLists.txt                 |  7 +++++
 libc/src/sched/linux/CMakeLists.txt           | 11 +++++++
 libc/src/sched/linux/sched_setcpuset.cpp      | 30 +++++++++++++++++++
 libc/src/sched/sched_setcpuset.h              | 24 +++++++++++++++
 8 files changed, 77 insertions(+)
 create mode 100644 libc/src/sched/linux/sched_setcpuset.cpp
 create mode 100644 libc/src/sched/sched_setcpuset.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a46b30b04aeb6..8b4b491bd28d5 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -911,6 +911,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # sched.h entrypoints
     libc.src.sched.__sched_getcpucount
     libc.src.sched.__sched_setcpuzero
+    libc.src.sched.__sched_setcpuset
 
     # strings.h entrypoints
     libc.src.strings.strcasecmp_l
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5458705a867cc..e96e6b0a1d718 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -859,6 +859,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # sched.h entrypoints
     libc.src.sched.__sched_getcpucount
     libc.src.sched.__sched_setcpuzero
+    libc.src.sched.__sched_setcpuset
 
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5144f2e8246f4..9d4f0646c5259 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1029,6 +1029,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # sched.h entrypoints
     libc.src.sched.__sched_getcpucount
     libc.src.sched.__sched_setcpuzero
+    libc.src.sched.__sched_setcpuset
 
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
diff --git a/libc/include/llvm-libc-macros/linux/sched-macros.h b/libc/include/llvm-libc-macros/linux/sched-macros.h
index f15030fbbab64..2a278fd880178 100644
--- a/libc/include/llvm-libc-macros/linux/sched-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sched-macros.h
@@ -29,5 +29,7 @@
 #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
 #define CPU_ZERO_S(setsize, set) __sched_setcpuzero(setsize, set)
 #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
+#define CPU_SET_S(setsize, set) __sched_setcpuset(setsize, set)
+#define CPU_SET(setsize, set) CPU_SET_S(sizeof(cpt_set_t), set)
 
 #endif // LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H
diff --git a/libc/src/sched/CMakeLists.txt b/libc/src/sched/CMakeLists.txt
index 293016de2a9f1..5f5501b6f3875 100644
--- a/libc/src/sched/CMakeLists.txt
+++ b/libc/src/sched/CMakeLists.txt
@@ -85,3 +85,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.__sched_setcpuzero
 )
+
+add_entrypoint_object(
+  __sched_setcpuset
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.__sched_setcpuset
+)
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index 54c8ed66f2d82..6e10d93e4d66b 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -146,3 +146,14 @@ add_entrypoint_object(
     libc.hdr.types.cpu_set_t
     libc.hdr.types.size_t
 )
+
+add_entrypoint_object(
+  __sched_setcpuset
+  SRCS
+    sched_setcpuset.cpp
+  HDRS
+    ../sched_setcpuset.h
+  DEPENDS
+    libc.hdr.types.cpu_set_t
+    libc.hdr.types.size_t
+)
diff --git a/libc/src/sched/linux/sched_setcpuset.cpp b/libc/src/sched/linux/sched_setcpuset.cpp
new file mode 100644
index 0000000000000..6855839079f7b
--- /dev/null
+++ b/libc/src/sched/linux/sched_setcpuset.cpp
@@ -0,0 +1,30 @@
+//===-- Implementation of sched_setcpuset ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sched/sched_setcpuset.h"
+
+#include "src/__support/common.h"        // LLVM_LIBC_FUNCTION
+#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+
+#include "hdr/types/cpu_set_t.h"
+#include "hdr/types/size_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, __sched_setcpuset,
+                   (int cpu, const size_t cpuset_size, cpu_set_t *set)) {
+  if (cpu / 8 < cpuset_size) {
+    const size_t element_index = cpu / NCPUBITS;
+    const size_t bit_position = cpu % NCPUBITS;
+
+    const unsigned long mask = 1UL << bit_position;
+    set->__bits[element_index] |= mask;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sched/sched_setcpuset.h b/libc/src/sched/sched_setcpuset.h
new file mode 100644
index 0000000000000..38d6cd29e61d4
--- /dev/null
+++ b/libc/src/sched/sched_setcpuset.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for sched_setcpuset ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H
+#define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H
+
+#include "src/__support/macros/config.h"
+
+#include "hdr/types/cpu_set_t.h"
+#include "hdr/types/size_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// for internal use in the CPU_SET macro
+void __sched_setcpuset(int cpu, const size_t cpuset_size, cpu_set_t *set);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H

>From a21734f1d336bc6e0dda1c4bfaae6a08162f8c32 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:10:37 +0530
Subject: [PATCH 05/12] chore: fix typo `LLVM_LIBC_FUNCTION` ->
 `LIBC_NAMESPACE_DECL`

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/src/sched/linux/sched_setcpuzero.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/sched/linux/sched_setcpuzero.cpp b/libc/src/sched/linux/sched_setcpuzero.cpp
index ef8829f999eaa..b8c1aa39eaab5 100644
--- a/libc/src/sched/linux/sched_setcpuzero.cpp
+++ b/libc/src/sched/linux/sched_setcpuzero.cpp
@@ -9,7 +9,7 @@
 #include "src/sched/sched_setcpuzero.h"
 
 #include "src/__support/common.h"        // LLVM_LIBC_FUNCTION
-#include "src/__support/macros/config.h" // LLVM_LIBC_FUNCTION
+#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
 
 #include "hdr/types/cpu_set_t.h"
 #include "hdr/types/size_t.h"

>From dd7f3f60eda1a25f50265fd9d200a0781ee06d8c Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:19:27 +0530
Subject: [PATCH 06/12] chore: fix typo: `int` -> `void`

---
 libc/src/sched/linux/sched_setcpuset.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/sched/linux/sched_setcpuset.cpp b/libc/src/sched/linux/sched_setcpuset.cpp
index 6855839079f7b..9d8a6ca440221 100644
--- a/libc/src/sched/linux/sched_setcpuset.cpp
+++ b/libc/src/sched/linux/sched_setcpuset.cpp
@@ -16,7 +16,7 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(int, __sched_setcpuset,
+LLVM_LIBC_FUNCTION(void, __sched_setcpuset,
                    (int cpu, const size_t cpuset_size, cpu_set_t *set)) {
   if (cpu / 8 < cpuset_size) {
     const size_t element_index = cpu / NCPUBITS;

>From b21ccb36f0005d65f9e834210dc711eefb662a71 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:37:27 +0530
Subject: [PATCH 07/12] feat: implement header for sched_macros

---
 libc/hdr/CMakeLists.txt |  9 +++++++++
 libc/hdr/sched_macros.h | 22 ++++++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 libc/hdr/sched_macros.h

diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index b337a8c9fc2a6..db2dac9ff2822 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -72,6 +72,15 @@ add_proxy_header_library(
     libc.include.fenv
 )
 
+add_proxy_header_library(
+  sched_macros
+  HDRS
+    sched_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.sched
+    libc.include.llvm-libc-macros.sched_macros
+)
+
 add_proxy_header_library(
   signal_macros
   HDRS
diff --git a/libc/hdr/sched_macros.h b/libc/hdr/sched_macros.h
new file mode 100644
index 0000000000000..cfeaa99796786
--- /dev/null
+++ b/libc/hdr/sched_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from sched.h ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SCHED_MACROS_H
+#define LLVM_LIBC_HDR_SCHED_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sched-macros.h"
+
+#else // Overlay mode
+
+#include <sched.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SCHED_MACROS_H

>From 597b8721cf7fe91c0b1dc9fdaed6412a31d730dd Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:41:06 +0530
Subject: [PATCH 08/12] chore: fix fullbuild bugs

---
 libc/src/sched/linux/sched_setcpuset.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/libc/src/sched/linux/sched_setcpuset.cpp b/libc/src/sched/linux/sched_setcpuset.cpp
index 9d8a6ca440221..12786940abfb5 100644
--- a/libc/src/sched/linux/sched_setcpuset.cpp
+++ b/libc/src/sched/linux/sched_setcpuset.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/common.h"        // LLVM_LIBC_FUNCTION
 #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
 
+#include "hdr/sched_macros.h" // NCPUBITS
 #include "hdr/types/cpu_set_t.h"
 #include "hdr/types/size_t.h"
 
@@ -18,9 +19,9 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(void, __sched_setcpuset,
                    (int cpu, const size_t cpuset_size, cpu_set_t *set)) {
-  if (cpu / 8 < cpuset_size) {
-    const size_t element_index = cpu / NCPUBITS;
-    const size_t bit_position = cpu % NCPUBITS;
+  if (static_cast<size_t>(cpu) / 8 < cpuset_size) {
+    const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS;
+    const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;
 
     const unsigned long mask = 1UL << bit_position;
     set->__bits[element_index] |= mask;

>From 96e2378d7041147c606ac962473e397b59615581 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 19:59:28 +0530
Subject: [PATCH 09/12] feat: implement CPU_ISSET[_S] macro and fix CPU_SET[_S]
 macro

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 .../llvm-libc-macros/linux/sched-macros.h     |  6 ++--
 libc/src/sched/CMakeLists.txt                 |  7 ++++
 libc/src/sched/linux/CMakeLists.txt           | 13 ++++++++
 libc/src/sched/linux/sched_getcpuisset.cpp    | 33 +++++++++++++++++++
 libc/src/sched/linux/sched_setcpuset.cpp      |  2 +-
 libc/src/sched/sched_getcpuisset.h            | 24 ++++++++++++++
 9 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 libc/src/sched/linux/sched_getcpuisset.cpp
 create mode 100644 libc/src/sched/sched_getcpuisset.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8b4b491bd28d5..49f6740bdce94 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -912,6 +912,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.sched.__sched_getcpucount
     libc.src.sched.__sched_setcpuzero
     libc.src.sched.__sched_setcpuset
+    libc.src.sched.__sched_getcpuisset
 
     # strings.h entrypoints
     libc.src.strings.strcasecmp_l
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index e96e6b0a1d718..d44937c9fea16 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -860,6 +860,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.sched.__sched_getcpucount
     libc.src.sched.__sched_setcpuzero
     libc.src.sched.__sched_setcpuset
+    libc.src.sched.__sched_getcpuisset
 
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9d4f0646c5259..8e511d25fc8f4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1030,6 +1030,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.sched.__sched_getcpucount
     libc.src.sched.__sched_setcpuzero
     libc.src.sched.__sched_setcpuset
+    libc.src.sched.__sched_getcpuisset
 
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
diff --git a/libc/include/llvm-libc-macros/linux/sched-macros.h b/libc/include/llvm-libc-macros/linux/sched-macros.h
index 2a278fd880178..597789bb4ce29 100644
--- a/libc/include/llvm-libc-macros/linux/sched-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sched-macros.h
@@ -29,7 +29,9 @@
 #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
 #define CPU_ZERO_S(setsize, set) __sched_setcpuzero(setsize, set)
 #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
-#define CPU_SET_S(setsize, set) __sched_setcpuset(setsize, set)
-#define CPU_SET(setsize, set) CPU_SET_S(sizeof(cpt_set_t), set)
+#define CPU_SET_S(cpu, setsize, set) __sched_setcpuset(cpu, setsize, set)
+#define CPU_SET(cpu, setsize, set) CPU_SET_S(cpu, sizeof(cpt_set_t), set)
+#define CPU_ISSET_S(cpu, setsize, set) __sched_getcpuisset(cpu, setsize, set)
+#define CPU_ISSET(cpu, setsize, set) CPU_ISSET_S(cpu, sizeof(cpt_set_t), set)
 
 #endif // LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H
diff --git a/libc/src/sched/CMakeLists.txt b/libc/src/sched/CMakeLists.txt
index 5f5501b6f3875..e6c37d3b0433a 100644
--- a/libc/src/sched/CMakeLists.txt
+++ b/libc/src/sched/CMakeLists.txt
@@ -92,3 +92,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.__sched_setcpuset
 )
+
+add_entrypoint_object(
+  __sched_getcpuisset
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.__sched_getcpuisset
+)
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index 6e10d93e4d66b..c136d3d314cd1 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -154,6 +154,19 @@ add_entrypoint_object(
   HDRS
     ../sched_setcpuset.h
   DEPENDS
+    libc.hdr.sched_macros
+    libc.hdr.types.cpu_set_t
+    libc.hdr.types.size_t
+)
+
+add_entrypoint_object(
+  __sched_getcpuisset
+  SRCS
+    sched_getcpuisset.cpp
+  HDRS
+    ../sched_getcpuisset.h
+  DEPENDS
+    libc.hdr.sched_macros
     libc.hdr.types.cpu_set_t
     libc.hdr.types.size_t
 )
diff --git a/libc/src/sched/linux/sched_getcpuisset.cpp b/libc/src/sched/linux/sched_getcpuisset.cpp
new file mode 100644
index 0000000000000..67185ff126daa
--- /dev/null
+++ b/libc/src/sched/linux/sched_getcpuisset.cpp
@@ -0,0 +1,33 @@
+//===-- Implementation of sched_getcpuisset -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sched/sched_getcpuisset.h"
+
+#include "src/__support/common.h"        // LLVM_LIBC_FUNCTION
+#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+
+#include "hdr/sched_macros.h" // NCPUBITS
+#include "hdr/types/cpu_set_t.h"
+#include "hdr/types/size_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, __sched_getcpuisset,
+                   (int cpu, const size_t cpuset_size, cpu_set_t *set)) {
+  if (static_cast<size_t>(cpu) / 8 < cpuset_size) {
+    const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS;
+    const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;
+
+    const unsigned long mask = 1UL << bit_position;
+    return set->__mask[element_index] & mask;
+  }
+
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sched/linux/sched_setcpuset.cpp b/libc/src/sched/linux/sched_setcpuset.cpp
index 12786940abfb5..03db8453f58e4 100644
--- a/libc/src/sched/linux/sched_setcpuset.cpp
+++ b/libc/src/sched/linux/sched_setcpuset.cpp
@@ -24,7 +24,7 @@ LLVM_LIBC_FUNCTION(void, __sched_setcpuset,
     const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;
 
     const unsigned long mask = 1UL << bit_position;
-    set->__bits[element_index] |= mask;
+    set->__mask[element_index] |= mask;
   }
 }
 
diff --git a/libc/src/sched/sched_getcpuisset.h b/libc/src/sched/sched_getcpuisset.h
new file mode 100644
index 0000000000000..8e6f69c443135
--- /dev/null
+++ b/libc/src/sched/sched_getcpuisset.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for sched_getcpuisset -------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H
+#define LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H
+
+#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
+
+#include "hdr/types/cpu_set_t.h"
+#include "hdr/types/size_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// for internal use in the CPU_ISSET macro
+int __sched_getcpuisset(int cpu, const size_t cpuset_size, cpu_set_t *set);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H

>From 4e33001690fadaebd151a0ef1e747482abb52ea2 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 20:14:05 +0530
Subject: [PATCH 10/12] chore: fix typo `__NCPUBITS8` -> `__NCPUBITS`

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/include/llvm-libc-types/cpu_set_t.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-types/cpu_set_t.h b/libc/include/llvm-libc-types/cpu_set_t.h
index 7af74a31c6932..35fc9144fb98d 100644
--- a/libc/include/llvm-libc-types/cpu_set_t.h
+++ b/libc/include/llvm-libc-types/cpu_set_t.h
@@ -10,7 +10,7 @@
 #define LLVM_LIBC_TYPES_CPU_SET_T_H
 
 #define __CPU_SETSIZE 1024
-#define __NCPUBITS8 8 * sizeof(unsigned long)
+#define __NCPUBITS 8 * sizeof(unsigned long)
 
 typedef struct {
   // If a processor with more than 1024 CPUs is to be supported in future,

>From 9a6ad6715c230f91270fd053b59affea22d81cf0 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 16 Mar 2025 20:23:12 +0530
Subject: [PATCH 11/12] fix: implicit conversion error

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/src/sched/linux/sched_getcpuisset.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/sched/linux/sched_getcpuisset.cpp b/libc/src/sched/linux/sched_getcpuisset.cpp
index 67185ff126daa..3d1ce1780b8ca 100644
--- a/libc/src/sched/linux/sched_getcpuisset.cpp
+++ b/libc/src/sched/linux/sched_getcpuisset.cpp
@@ -24,7 +24,7 @@ LLVM_LIBC_FUNCTION(int, __sched_getcpuisset,
     const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;
 
     const unsigned long mask = 1UL << bit_position;
-    return set->__mask[element_index] & mask;
+    return (set->__mask[element_index] & mask) != 0;
   }
 
   return 0;

>From c94712f13285a9657e35f982e081e4c28ec12b69 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Mon, 17 Mar 2025 01:08:04 +0530
Subject: [PATCH 12/12] chore: add parens

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/include/llvm-libc-types/cpu_set_t.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-types/cpu_set_t.h b/libc/include/llvm-libc-types/cpu_set_t.h
index 35fc9144fb98d..8c6859de5f1d4 100644
--- a/libc/include/llvm-libc-types/cpu_set_t.h
+++ b/libc/include/llvm-libc-types/cpu_set_t.h
@@ -10,7 +10,7 @@
 #define LLVM_LIBC_TYPES_CPU_SET_T_H
 
 #define __CPU_SETSIZE 1024
-#define __NCPUBITS 8 * sizeof(unsigned long)
+#define __NCPUBITS (8 * sizeof(unsigned long))
 
 typedef struct {
   // If a processor with more than 1024 CPUs is to be supported in future,



More information about the libc-commits mailing list