Skip to content

Commit ecfffbf

Browse files
[libc][OSUtil] refactor quick_exit to be an object library everywhere (#85955)
The usage of __builtin_unreachable after calls to quick_exit were distressing. If a function is properly marked [[noreturn]] then __builtin_unreachable is not necessary. Looking into this further, we seem to have header only implementations for CPU targets. The inline nature of these functions is curious; we're going to exit, it doesn't matter if we need to pay the call of a function or not. If we just make these functions have distinct TUs rather than be header only, we can clean up the cmake rules for quick_exit which were different between CPU and GPU. Remove darwin support for quick_exit. This isn't being tested, and we can bring it back when necessary.
1 parent d18bee2 commit ecfffbf

File tree

15 files changed

+32
-115
lines changed

15 files changed

+32
-115
lines changed

libc/src/__support/OSUtil/CMakeLists.txt

+7-20
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,10 @@ if(NOT TARGET ${target_os_util})
88
return()
99
endif()
1010

11-
# The OSUtil is an object library in GPU mode.
12-
if(NOT LIBC_TARGET_OS_IS_GPU)
13-
add_header_library(
14-
osutil
15-
HDRS
16-
io.h
17-
quick_exit.h
18-
syscall.h
19-
DEPENDS
20-
${target_os_util}
21-
)
22-
else()
23-
add_object_library(
24-
osutil
25-
ALIAS
26-
${target_os_util}
27-
DEPENDS
28-
${target_os_util}
29-
)
30-
endif()
11+
add_object_library(
12+
osutil
13+
ALIAS
14+
${target_os_util}
15+
DEPENDS
16+
${target_os_util}
17+
)

libc/src/__support/OSUtil/baremetal/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
add_header_library(
1+
add_object_library(
22
baremetal_util
3+
SRCS
4+
quick_exit.cpp
35
HDRS
46
io.h
5-
quick_exit.h
67
DEPENDS
78
libc.src.__support.common
89
libc.src.__support.CPP.string_view

libc/src/__support/OSUtil/baremetal/quick_exit.h renamed to libc/src/__support/OSUtil/baremetal/quick_exit.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_QUICK_EXIT_H
10-
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_QUICK_EXIT_H
11-
12-
namespace LIBC_NAMESPACE {
9+
#include "src/__support/OSUtil/quick_exit.h"
1310

1411
// This is intended to be provided by the vendor.
15-
extern "C" void __llvm_libc_quick_exit(int status);
12+
[[noreturn]] extern "C" void __llvm_libc_quick_exit(int status);
1613

17-
void quick_exit(int status) { __llvm_libc_quick_exit(status); }
14+
namespace LIBC_NAMESPACE {
1815

19-
} // namespace LIBC_NAMESPACE
16+
[[noreturn]] void quick_exit(int status) { __llvm_libc_quick_exit(status); }
2017

21-
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_QUICK_EXIT_H
18+
} // namespace LIBC_NAMESPACE

libc/src/__support/OSUtil/darwin/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ add_header_library(
88
darwin_util
99
HDRS
1010
io.h
11-
quick_exit.h
1211
syscall.h
1312
DEPENDS
1413
.${LIBC_TARGET_ARCHITECTURE}.darwin_util

libc/src/__support/OSUtil/darwin/quick_exit.h

-26
This file was deleted.

libc/src/__support/OSUtil/gpu/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_object_library(
44
quick_exit.cpp
55
io.cpp
66
HDRS
7-
quick_exit.h
87
io.h
98
DEPENDS
109
libc.src.__support.common

libc/src/__support/OSUtil/gpu/quick_exit.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_GPU_QUICK_EXIT_H
10-
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_GPU_QUICK_EXIT_H
11-
12-
#include "quick_exit.h"
9+
#include "src/__support/OSUtil/quick_exit.h"
1310

1411
#include "src/__support/RPC/rpc_client.h"
1512
#include "src/__support/macros/properties/architectures.h"
1613

1714
namespace LIBC_NAMESPACE {
1815

19-
void quick_exit(int status) {
16+
[[noreturn]] void quick_exit(int status) {
2017
// We want to first make sure the server is listening before we exit.
2118
rpc::Client::Port port = rpc::client.open<RPC_EXIT>();
2219
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
@@ -29,5 +26,3 @@ void quick_exit(int status) {
2926
}
3027

3128
} // namespace LIBC_NAMESPACE
32-
33-
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GPU_QUICK_EXIT_H

libc/src/__support/OSUtil/gpu/quick_exit.h

-18
This file was deleted.

libc/src/__support/OSUtil/linux/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ endif()
44

55
add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
66

7-
add_header_library(
7+
add_object_library(
88
linux_util
9+
SRCS
10+
quick_exit.cpp
911
HDRS
1012
io.h
11-
quick_exit.h
1213
syscall.h
1314
DEPENDS
1415
.${LIBC_TARGET_ARCHITECTURE}.linux_${LIBC_TARGET_ARCHITECTURE}_util

libc/src/__support/OSUtil/linux/quick_exit.h renamed to libc/src/__support/OSUtil/linux/quick_exit.cpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_QUICK_EXIT_H
10-
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_QUICK_EXIT_H
11-
12-
#include "syscall.h" // For internal syscall function.
13-
149
#include "src/__support/common.h"
15-
10+
#include "syscall.h" // For internal syscall function.
1611
#include <sys/syscall.h> // For syscall numbers.
1712

1813
namespace LIBC_NAMESPACE {
@@ -22,7 +17,7 @@ namespace LIBC_NAMESPACE {
2217
#ifdef LIBC_TARGET_ARCH_IS_X86
2318
__attribute__((no_stack_protector))
2419
#endif
25-
LIBC_INLINE void
20+
__attribute__((noreturn)) void
2621
quick_exit(int status) {
2722
for (;;) {
2823
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit_group, status);
@@ -31,5 +26,3 @@ quick_exit(int status) {
3126
}
3227

3328
} // namespace LIBC_NAMESPACE
34-
35-
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_QUICK_EXIT_H

libc/src/__support/OSUtil/quick_exit.h

+4-11
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,10 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
1010
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
1111

12-
#include "src/__support/macros/properties/architectures.h"
12+
namespace LIBC_NAMESPACE {
1313

14-
#if defined(LIBC_TARGET_ARCH_IS_GPU)
15-
#include "gpu/quick_exit.h"
16-
#elif defined(__APPLE__)
17-
#include "darwin/quick_exit.h"
18-
#elif defined(__linux__)
19-
#include "linux/quick_exit.h"
20-
#elif defined(__ELF__)
21-
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
22-
#include "baremetal/quick_exit.h"
23-
#endif
14+
[[noreturn]] void quick_exit(int status);
15+
16+
}
2417

2518
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H

libc/src/stdlib/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,11 @@ add_entrypoint_object(
414414
CXX_STANDARD
415415
20 # For constinit of the atexit callback list.
416416
DEPENDS
417-
libc.src.__support.fixedvector
417+
libc.src.__support.CPP.new
418+
libc.src.__support.OSUtil.osutil
418419
libc.src.__support.blockstore
420+
libc.src.__support.fixedvector
419421
libc.src.__support.threads.mutex
420-
libc.src.__support.CPP.new
421422
)
422423

423424
add_entrypoint_object(

libc/src/stdlib/_Exit.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414
namespace LIBC_NAMESPACE {
1515

16-
LLVM_LIBC_FUNCTION(void, _Exit, (int status)) {
16+
[[noreturn]] LLVM_LIBC_FUNCTION(void, _Exit, (int status)) {
1717
quick_exit(status);
18-
__builtin_unreachable();
1918
}
2019

2120
} // namespace LIBC_NAMESPACE

libc/src/stdlib/exit.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ extern "C" void __cxa_finalize(void *);
1414

1515
namespace LIBC_NAMESPACE {
1616

17-
LLVM_LIBC_FUNCTION(void, exit, (int status)) {
17+
[[noreturn]] LLVM_LIBC_FUNCTION(void, exit, (int status)) {
1818
__cxa_finalize(nullptr);
1919
quick_exit(status);
20-
__builtin_unreachable();
2120
}
2221

2322
} // namespace LIBC_NAMESPACE

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

+1-4
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,7 @@ libc_support_library(
10021002
libc_support_library(
10031003
name = "__support_osutil_quick_exit",
10041004
hdrs = ["src/__support/OSUtil/quick_exit.h"],
1005-
textual_hdrs = [
1006-
"src/__support/OSUtil/linux/quick_exit.h",
1007-
#TODO: add support for GPU quick_exit (isn't just in a header.)
1008-
],
1005+
srcs = ["src/__support/OSUtil/linux/quick_exit.cpp"],
10091006
deps = [
10101007
":__support_osutil_syscall",
10111008
],

0 commit comments

Comments
 (0)