[libc] Make the errno macro resolve to the thread local variable directly.
authorSiva Chandra Reddy <sivachandra@google.com>
Thu, 3 Mar 2022 10:25:35 +0000 (10:25 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Fri, 4 Mar 2022 17:29:49 +0000 (17:29 +0000)
With modern architectures having a thread pointer and language supporting
thread locals, there is no reason to use a function intermediary to access
the thread local errno value.

The entrypoint corresponding to errno has been replaced with an object
library as there is no formal entrypoint for errno anymore.

Reviewed By: jeffbailey, michaelrj

Differential Revision: https://reviews.llvm.org/D120920

28 files changed:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/api.td
libc/config/linux/x86_64/entrypoints.txt
libc/include/__llvm-libc-common.h
libc/spec/llvm_libc_ext.td
libc/src/__support/CMakeLists.txt
libc/src/__support/FPUtil/CMakeLists.txt
libc/src/__support/File/CMakeLists.txt
libc/src/errno/CMakeLists.txt
libc/src/errno/__errno_location.cpp [deleted file]
libc/src/errno/__errno_location.h [deleted file]
libc/src/errno/errno.cpp [new file with mode: 0644]
libc/src/errno/llvmlibc_errno.h
libc/src/fcntl/linux/CMakeLists.txt
libc/src/math/generic/CMakeLists.txt
libc/src/signal/linux/CMakeLists.txt
libc/src/sys/mman/linux/CMakeLists.txt
libc/src/sys/stat/linux/CMakeLists.txt
libc/src/threads/linux/CMakeLists.txt
libc/src/time/CMakeLists.txt
libc/src/unistd/linux/CMakeLists.txt
libc/test/loader/linux/CMakeLists.txt
libc/test/src/__support/File/CMakeLists.txt
libc/test/src/errno/CMakeLists.txt
libc/test/src/math/CMakeLists.txt
libc/test/src/signal/CMakeLists.txt
libc/test/src/sys/mman/linux/CMakeLists.txt
libc/test/src/threads/CMakeLists.txt

index c80e6be..5bdbced 100644 (file)
@@ -17,9 +17,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.tolower
     libc.src.ctype.toupper
     
-    # errno.h entrypoints
-    libc.src.errno.__errno_location
-
     # fcntl.h entrypoints
     libc.src.fcntl.creat
     libc.src.fcntl.open
index 3841e36..fc20897 100644 (file)
@@ -44,11 +44,8 @@ def NullMacro : MacroDef<"NULL"> {
 
 def ErrnoMacro : MacroDef<"errno"> {
   let Defn = [{
-    #ifdef __cplusplus
-    extern "C"
-    #endif
-    int *__errno_location();
-    #define errno (*__errno_location())
+    extern _Thread_local int __llvmlibc_errno;
+    #define errno __llvmlibc_errno
   }];
 }
 
index 500e9cc..e08c8fd 100644 (file)
@@ -17,9 +17,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.tolower
     libc.src.ctype.toupper
 
-    # errno.h entrypoints
-    libc.src.errno.__errno_location
-
     # fcntl.h entrypoints
     libc.src.fcntl.creat
     libc.src.fcntl.open
index a9e0f69..ce289e5 100644 (file)
@@ -29,6 +29,9 @@
 #undef _Alignof
 #define _Alignof alignof
 
+#undef _Thread_local
+#define _Thread_local thread_local
+
 #else // not __cplusplus
 
 #undef __BEGIN_C_DECLS
index d4a133a..ee62414 100644 (file)
@@ -33,24 +33,8 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> {
       ]
   >;
 
-  HeaderSpec Errno = HeaderSpec<
-      "errno.h",
-      [], // Macros
-      [], // Types
-      [], // Enumerations
-      [
-          FunctionSpec<
-              "__errno_location",
-              RetValSpec<IntPtr>,
-              [ArgSpec<VoidType>]
-
-          >,
-      ]
-  >;
-
   let Headers = [
     String,
     Assert,
-    Errno,
   ];
 }
index f70803d..56d1b07 100644 (file)
@@ -22,7 +22,7 @@ add_header_library(
   DEPENDS
     .ctype_utils
     libc.include.errno
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.__support.CPP.standalone_cpp
 )
 
@@ -43,7 +43,7 @@ add_header_library(
     .ctype_utils
     .high_precision_decimal
     libc.include.errno
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.__support.CPP.standalone_cpp
     libc.src.__support.FPUtil.fputil
 )
index d1a3cf1..555a3f4 100644 (file)
@@ -21,7 +21,7 @@ add_header_library(
     libc.include.fenv
     libc.src.__support.common
     libc.src.__support.CPP.standalone_cpp
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_header_library(
index ddc09b4..d00ccc5 100644 (file)
@@ -5,5 +5,6 @@ add_object_library(
   HDRS
     file.h
   DEPENDS
-    libc.src.errno.__errno_location
+    libc.include.errno
+    libc.src.errno.errno
 )
index eff1c73..e9ddb27 100644 (file)
@@ -1,15 +1,14 @@
 if (LLVM_LIBC_FULL_BUILD)
-add_entrypoint_object(
-  __errno_location
+add_object_library(
+  errno
   SRCS
-    __errno_location.cpp
+    errno.cpp
   HDRS
-    __errno_location.h
     llvmlibc_errno.h
 )
 else()
-add_entrypoint_object(
-  __errno_location
+add_object_library(
+  errno
   SRCS
     dummy_errno.cpp
   HDRS
diff --git a/libc/src/errno/__errno_location.cpp b/libc/src/errno/__errno_location.cpp
deleted file mode 100644 (file)
index b4860eb..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-//===-- Implementation of __errno_location --------------------------------===//
-//
-// 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/errno/__errno_location.h"
-
-#include "src/__support/common.h"
-
-namespace __llvm_libc {
-
-static thread_local int errno = 0;
-
-// __errno_location is not really an entry point but we still want it to behave
-// like an entry point because the errno macro resolves to the C symbol
-// "__errno_location".
-LLVM_LIBC_FUNCTION(int *, __errno_location, ()) { return &errno; }
-
-} // namespace __llvm_libc
diff --git a/libc/src/errno/__errno_location.h b/libc/src/errno/__errno_location.h
deleted file mode 100644 (file)
index 20be3fc..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-//===-- Implementation header for __errno_location --------------*- 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_ERRNO_ERRNO_LOCATION_H
-#define LLVM_LIBC_SRC_ERRNO_ERRNO_LOCATION_H
-
-namespace __llvm_libc {
-
-int *__errno_location();
-
-} // namespace __llvm_libc
-
-#endif // LLVM_LIBC_SRC_ERRNO_ERRNO_LOCATION_H
diff --git a/libc/src/errno/errno.cpp b/libc/src/errno/errno.cpp
new file mode 100644 (file)
index 0000000..84e111b
--- /dev/null
@@ -0,0 +1,9 @@
+//===-- Implementation of __errno_location --------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+thread_local int __llvmlibc_errno = 0;
index d1032a4..5e505ad 100644 (file)
@@ -6,13 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/errno/__errno_location.h"
-
 #ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
 #define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
 
 // Internal code should use this and not use the errno macro from the
 // public header.
-#define llvmlibc_errno (*__llvm_libc::__errno_location())
+extern thread_local int __llvmlibc_errno;
+#define llvmlibc_errno __llvmlibc_errno
 
 #endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
index 17d7b4b..926292c 100644 (file)
@@ -8,7 +8,7 @@ add_entrypoint_object(
     libc.include.errno
     libc.include.fcntl
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -21,7 +21,7 @@ add_entrypoint_object(
     libc.include.errno
     libc.include.fcntl
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -34,5 +34,5 @@ add_entrypoint_object(
     libc.include.errno
     libc.include.fcntl
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index 8777043..cba0879 100644 (file)
@@ -43,7 +43,7 @@ add_object_library(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_object_library(
@@ -65,7 +65,7 @@ add_entrypoint_object(
   DEPENDS
     .sincosf_utils
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
   COMPILE_OPTIONS
     -O3
 )
@@ -79,7 +79,7 @@ add_entrypoint_object(
   DEPENDS
     .sincosf_utils
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.__support.FPUtil.fputil
   COMPILE_OPTIONS
     -O3
@@ -94,7 +94,7 @@ add_entrypoint_object(
   DEPENDS
     .sincosf_utils
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
   COMPILE_OPTIONS
     -O3
 )
index 419146f..c069c9f 100644 (file)
@@ -41,7 +41,7 @@ add_entrypoint_object(
     libc.include.signal
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -55,7 +55,7 @@ add_entrypoint_object(
     libc.include.signal
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -68,7 +68,7 @@ add_entrypoint_object(
   DEPENDS
     libc.include.errno
     libc.include.signal
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -81,7 +81,7 @@ add_entrypoint_object(
   DEPENDS
     libc.include.errno
     libc.include.signal
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -106,7 +106,7 @@ add_entrypoint_object(
   DEPENDS
     libc.include.errno
     libc.include.signal
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -119,5 +119,5 @@ add_entrypoint_object(
   DEPENDS
     libc.include.errno
     libc.include.signal
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index 949d965..0ca65af 100644 (file)
@@ -8,7 +8,7 @@ add_entrypoint_object(
     libc.include.sys_mman
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -21,5 +21,5 @@ add_entrypoint_object(
     libc.include.sys_mman
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index bf92d5f..88d5062 100644 (file)
@@ -9,7 +9,7 @@ add_entrypoint_object(
     libc.include.sys_stat
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -22,5 +22,5 @@ add_entrypoint_object(
     libc.include.sys_stat
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index 6fdca98..f5ac139 100644 (file)
@@ -50,7 +50,7 @@ add_entrypoint_object(
     libc.include.threads
     libc.src.__support.common
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.sys.mman.mmap
   COMPILE_OPTIONS
     -O3
index 7e849cf..514b36c 100644 (file)
@@ -7,7 +7,7 @@ add_object_library(
   DEPENDS
     libc.include.errno
     libc.include.time
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -64,5 +64,5 @@ add_entrypoint_object(
     .time_utils
     libc.include.errno
     libc.include.time
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index 30e073a..f990a82 100644 (file)
@@ -8,7 +8,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -21,7 +21,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -34,7 +34,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -48,7 +48,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -62,7 +62,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -76,7 +76,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -89,5 +89,5 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index 27d808c..36f4ddb 100644 (file)
@@ -70,6 +70,6 @@ add_loader_test(
 #    libc.include.sys_mman
 #    libc.loader.linux.crt1
 #    libc.src.assert.__assert_fail
-#    libc.src.errno.__errno_location
+#    libc.src.errno.errno
 #    libc.src.sys.mman.mmap
 #)
index 485c8e8..2757acc 100644 (file)
@@ -6,6 +6,7 @@ add_libc_unittest(
   SRCS
     file_test.cpp
   DEPENDS
+    libc.include.errno
     libc.include.stdio
     libc.include.stdlib
     libc.src.__support.File.file
index d60ee20..7052073 100644 (file)
@@ -11,5 +11,5 @@ add_libc_unittest(
   SRCS
     errno_test.cpp
   DEPENDS
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
 )
index 0b7ce71..0ca9cb5 100644 (file)
@@ -309,7 +309,7 @@ add_fp_unittest(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.fenv.feclearexcept
     libc.src.fenv.feraiseexcept
     libc.src.fenv.fetestexcept
@@ -329,7 +329,7 @@ add_fp_unittest(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.fenv.feclearexcept
     libc.src.fenv.feraiseexcept
     libc.src.fenv.fetestexcept
@@ -349,7 +349,7 @@ add_fp_unittest(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.fenv.feclearexcept
     libc.src.fenv.feraiseexcept
     libc.src.fenv.fetestexcept
@@ -369,7 +369,7 @@ add_fp_unittest(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.fenv.feclearexcept
     libc.src.fenv.feraiseexcept
     libc.src.fenv.fetestexcept
@@ -389,7 +389,7 @@ add_fp_unittest(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.fenv.feclearexcept
     libc.src.fenv.feraiseexcept
     libc.src.fenv.fetestexcept
@@ -409,7 +409,7 @@ add_fp_unittest(
   DEPENDS
     libc.include.errno
     libc.include.math
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.fenv.feclearexcept
     libc.src.fenv.feraiseexcept
     libc.src.fenv.fetestexcept
index c5baaf7..3274914 100644 (file)
@@ -33,7 +33,7 @@ add_libc_unittest(
     sigprocmask_test.cpp
   DEPENDS
     libc.include.errno
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.signal.raise
     libc.src.signal.sigaddset
     libc.src.signal.sigemptyset
@@ -63,7 +63,7 @@ add_libc_unittest(
   DEPENDS
     libc.include.errno
     libc.include.signal
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.signal.raise
     libc.src.signal.signal
     libc.test.errno_setter_matcher
index c7d3dde..a95f7c8 100644 (file)
@@ -9,7 +9,7 @@ add_libc_unittest(
   DEPENDS
     libc.include.errno
     libc.include.sys_mman
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
     libc.test.errno_setter_matcher
index 2e23623..6ac093c 100644 (file)
@@ -26,7 +26,7 @@ add_libc_unittest(
     thrd_test.cpp
   DEPENDS
     libc.include.threads
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.threads.thrd_create
     libc.src.threads.thrd_join
 )
@@ -39,7 +39,7 @@ add_libc_unittest(
     mtx_test.cpp
   DEPENDS
     libc.include.threads
-    libc.src.errno.__errno_location
+    libc.src.errno.errno
     libc.src.threads.mtx_destroy
     libc.src.threads.mtx_init
     libc.src.threads.mtx_lock