[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for pipe (PR #240328)

via libc-commits libc-commits at lists.llvm.org
Mon Nov 9 03:00:44 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

This PR move pipe behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.


---
Full diff: https://github.com/llvm/llvm-project/pull/240328.diff


5 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+12) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/pipe.h (+39) 
- (modified) libc/src/unistd/linux/CMakeLists.txt (+5-3) 
- (modified) libc/src/unistd/linux/pipe.cpp (+11-13) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+16-1) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 65b0aaa04a4629..10cb0173ed9a07 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -840,6 +840,18 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  pipe
+  HDRS
+    pipe.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   pipe2
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/pipe.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/pipe.h
new file mode 100644
index 00000000000000..2a29124172419b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/pipe.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for pipe.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PIPE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PIPE_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> pipe(int pipefd[2]) {
+#ifdef SYS_pipe
+  return syscall_checked<int>(SYS_pipe, pipefd);
+#elif defined(SYS_pipe2)
+  return syscall_checked<int>(SYS_pipe2, pipefd, 0);
+#else
+#error "pipe and pipe2 syscalls are not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PIPE_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 36ac888e563a54..c8a7f3d32cf43a 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -722,9 +722,11 @@ add_entrypoint_object(
   HDRS
     ../pipe.h
   DEPENDS
-    libc.include.unistd
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.pipe
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.__support.macros.sanitizer
     libc.src.errno.errno
 )
 
diff --git a/libc/src/unistd/linux/pipe.cpp b/libc/src/unistd/linux/pipe.cpp
index 9b0c97439dd6f6..bb2dce313b2c36 100644
--- a/libc/src/unistd/linux/pipe.cpp
+++ b/libc/src/unistd/linux/pipe.cpp
@@ -1,36 +1,34 @@
-//===-- Linux implementation of pipe --------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of pipe.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/pipe.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/pipe.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/sanitizer.h" // for LIBC_MSAN_UNPOISON
-#include <sys/syscall.h>                    // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, pipe, (int pipefd[2])) {
-#ifdef SYS_pipe
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe,
-                                              reinterpret_cast<long>(pipefd));
-#elif defined(SYS_pipe2)
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(
-      SYS_pipe2, reinterpret_cast<long>(pipefd), 0);
-#endif
+  auto result = linux_syscalls::pipe(pipefd);
   LIBC_MSAN_UNPOISON(pipefd, sizeof(int) * 2);
-  if (ret < 0) {
-    libc_errno = -ret;
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 8a5abe7d94e258..16139b31b3fbe7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4613,6 +4613,21 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_pipe",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/pipe.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_pkey_alloc",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h"],
@@ -18937,7 +18952,7 @@ libc_function(
         ":__support_libc_errno",
         ":__support_macros_config",
         ":__support_macros_sanitizer",
-        ":__support_osutil_syscall",
+        ":__support_osutil_linux_syscall_wrappers_pipe",
         ":errno",
     ],
 )

``````````

</details>


https://github.com/llvm/llvm-project/pull/240328


More information about the libc-commits mailing list