[libc] Remove the optional arguments for NVPTX constructors (#69536)
[llvm-project.git] / libc / startup / gpu / nvptx / start.cpp
blob77fe921be4e029e66df2b49f085de3e2d847fb13
1 //===-- Implementation of crt for nvptx -----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/GPU/utils.h"
10 #include "src/__support/RPC/rpc_client.h"
11 #include "src/stdlib/atexit.h"
12 #include "src/stdlib/exit.h"
14 extern "C" int main(int argc, char **argv, char **envp);
16 namespace LIBC_NAMESPACE {
18 extern "C" {
19 // Nvidia's 'nvlink' linker does not provide these symbols. We instead need
20 // to manually create them and update the globals in the loader implememtation.
21 uintptr_t *__init_array_start [[gnu::visibility("protected")]];
22 uintptr_t *__init_array_end [[gnu::visibility("protected")]];
23 uintptr_t *__fini_array_start [[gnu::visibility("protected")]];
24 uintptr_t *__fini_array_end [[gnu::visibility("protected")]];
27 // Nvidia requires that the signature of the function pointers match. This means
28 // we cannot support the extended constructor arguments.
29 using InitCallback = void(void);
30 using FiniCallback = void(void);
32 static void call_init_array_callbacks(int, char **, char **) {
33 size_t init_array_size = __init_array_end - __init_array_start;
34 for (size_t i = 0; i < init_array_size; ++i)
35 reinterpret_cast<InitCallback *>(__init_array_start[i])();
38 static void call_fini_array_callbacks() {
39 size_t fini_array_size = __fini_array_end - __fini_array_start;
40 for (size_t i = fini_array_size; i > 0; --i)
41 reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
44 } // namespace LIBC_NAMESPACE
46 extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
47 _begin(int argc, char **argv, char **env) {
48 // We want the fini array callbacks to be run after other atexit
49 // callbacks are run. So, we register them before running the init
50 // array callbacks as they can potentially register their own atexit
51 // callbacks.
52 LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks);
53 LIBC_NAMESPACE::call_init_array_callbacks(argc, argv, env);
56 extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
57 _start(int argc, char **argv, char **envp, int *ret) {
58 // Invoke the 'main' function with every active thread that the user launched
59 // the _start kernel with.
60 __atomic_fetch_or(ret, main(argc, argv, envp), __ATOMIC_RELAXED);
63 extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
64 _end(int retval) {
65 // To finis the execution we invoke all the callbacks registered via 'atexit'
66 // and then exit with the appropriate return value.
67 LIBC_NAMESPACE::exit(retval);