https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/240330

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


>From a66a1216122d25730fbab11e65c2ecae79a7a6f6 Mon Sep 17 00:00:00 2001
From: c8ef <[email protected]>
Date: Mon, 9 Nov 2026 18:58:13 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for pwrite

This PR move pwrite behind the centralized syscall wrappers, following 
libc/docs/dev/syscall_wrapper_refactor.md.
---
 .../linux/syscall_wrappers/CMakeLists.txt     | 17 +++++++
 .../OSUtil/linux/syscall_wrappers/pwrite.h    | 51 +++++++++++++++++++
 libc/src/unistd/linux/CMakeLists.txt          | 10 ++--
 libc/src/unistd/linux/pwrite.cpp              | 35 ++++---------
 .../llvm-project-overlay/libc/BUILD.bazel     | 23 ++++++++-
 5 files changed, 105 insertions(+), 31 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/pwrite.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt 
b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index e7450be872876c..f318c5bedb82a0 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1898,6 +1898,23 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  pwrite
+  HDRS
+    pwrite.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.hdr.types.off_t
+    libc.hdr.types.size_t
+    libc.hdr.types.ssize_t
+    libc.include.sys_syscall
+    libc.src.__support.CPP.bit
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   rt_sigaction
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/pwrite.h 
b/libc/src/__support/OSUtil/linux/syscall_wrappers/pwrite.h
new file mode 100644
index 00000000000000..c82c44c3bd165c
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/pwrite.h
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 pwrite.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PWRITE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PWRITE_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "src/__support/CPP/bit.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<ssize_t> pwrite(int fd, const void *buf, size_t count,
+                                    off_t offset) {
+  if constexpr (sizeof(long) == sizeof(uint32_t) &&
+                sizeof(off_t) == sizeof(uint64_t)) {
+    // This is a 32-bit system with a 64-bit offset, offset must be split.
+    const uint64_t bits = cpp::bit_cast<uint64_t>(offset);
+    const uint32_t lo = bits & UINT32_MAX;
+    const uint32_t hi = bits >> 32;
+    const long offset_low = cpp::bit_cast<long>(static_cast<long>(lo));
+    const long offset_high = cpp::bit_cast<long>(static_cast<long>(hi));
+    return syscall_checked<ssize_t>(SYS_pwrite64, fd, buf, count, offset_low,
+                                    offset_high);
+  } else {
+    return syscall_checked<ssize_t>(SYS_pwrite64, fd, buf, count, offset);
+  }
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PWRITE_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt 
b/libc/src/unistd/linux/CMakeLists.txt
index a1ba5562504694..8a028572af79b1 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -773,11 +773,11 @@ add_entrypoint_object(
     libc.hdr.types.off_t
     libc.hdr.types.size_t
     libc.hdr.types.ssize_t
-    libc.hdr.fcntl_macros
-    libc.hdr.stdint_proxy
-    libc.include.unistd
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.unistd_macros
+    libc.src.__support.OSUtil.linux.syscall_wrappers.pwrite
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
     libc.src.errno.errno
 )
 
diff --git a/libc/src/unistd/linux/pwrite.cpp b/libc/src/unistd/linux/pwrite.cpp
index 7672063dcab986..de6100712324df 100644
--- a/libc/src/unistd/linux/pwrite.cpp
+++ b/libc/src/unistd/linux/pwrite.cpp
@@ -1,47 +1,34 @@
-//===-- Linux implementation of pwrite 
------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 pwrite.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/pwrite.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/pwrite.h"
 #include "src/__support/common.h"
-
-#include "hdr/stdint_proxy.h" // For uint64_t.
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, pwrite,
                    (int fd, const void *buf, size_t count, off_t offset)) {
 
-  ssize_t ret;
-  if constexpr (sizeof(long) == sizeof(uint32_t) &&
-                sizeof(off_t) == sizeof(uint64_t)) {
-    // This is a 32-bit system with a 64-bit offset, offset must be split.
-    const uint64_t bits = cpp::bit_cast<uint64_t>(offset);
-    const uint32_t lo = bits & UINT32_MAX;
-    const uint32_t hi = bits >> 32;
-    const long offset_low = cpp::bit_cast<long>(static_cast<long>(lo));
-    const long offset_high = cpp::bit_cast<long>(static_cast<long>(hi));
-    ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pwrite64, fd, buf, count,
-                                                offset_low, offset_high);
-  } else {
-    ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pwrite64, fd, buf, count,
-                                                offset);
-  }
-
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::pwrite(fd, buf, count, offset);
+  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 bf9804e080d695..b90c54e791d131 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3476,6 +3476,26 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_pwrite",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/pwrite.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_cpp_bit",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":hdr_stdint_proxy",
+        ":types_off_t",
+        ":types_size_t",
+        ":types_ssize_t",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_kill",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/kill.h"],
@@ -19027,9 +19047,8 @@ libc_function(
         ":__support_common",
         ":__support_libc_errno",
         ":__support_macros_config",
-        ":__support_osutil_syscall",
+        ":__support_osutil_linux_syscall_wrappers_pwrite",
         ":errno",
-        ":hdr_stdint_proxy",
         ":hdr_unistd_macros",
         ":types_off_t",
         ":types_size_t",

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to