[libc] Make the errno macro resolve to the thread local variable directly.

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
This commit is contained in:
Siva Chandra Reddy 2022-03-03 10:25:35 +00:00
parent fa8293bbc7
commit dd33f9cdef
28 changed files with 67 additions and 120 deletions

View 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

View 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
}];
}

View 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

View 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

View 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,
];
}

View 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
)

View 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(

View File

@ -5,5 +5,6 @@ add_object_library(
HDRS
file.h
DEPENDS
libc.src.errno.__errno_location
libc.include.errno
libc.src.errno.errno
)

View 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

View File

@ -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

View File

@ -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

9
libc/src/errno/errno.cpp Normal file
View File

@ -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;

View 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

View 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
)

View 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
)

View 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
)

View 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
)

View 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
)

View 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

View 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
)

View 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
)

View 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
#)

View 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

View File

@ -11,5 +11,5 @@ add_libc_unittest(
SRCS
errno_test.cpp
DEPENDS
libc.src.errno.__errno_location
libc.src.errno.errno
)

View 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

View 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

View 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

View 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