| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "config/gpu/app.h" |
| 10 | #include "src/__support/GPU/utils.h" |
| 11 | #include "src/__support/RPC/rpc_client.h" |
| 12 | #include "src/__support/macros/config.h" |
| 13 | #include "src/stdlib/atexit.h" |
| 14 | #include "src/stdlib/exit.h" |
| 15 | |
| 16 | extern "C" int main(int argc, char **argv, char **envp); |
| 17 | |
| 18 | namespace LIBC_NAMESPACE_DECL { |
| 19 | |
| 20 | DataEnvironment app; |
| 21 | |
| 22 | // FIXME: Factor this out into common logic so we don't need to stub it here. |
| 23 | void teardown_main_tls() {} |
| 24 | |
| 25 | extern "C" { |
| 26 | // Nvidia's 'nvlink' linker does not provide these symbols. We instead need |
| 27 | // to manually create them and update the globals in the loader implememtation. |
| 28 | uintptr_t *__init_array_start [[gnu::visibility("protected" )]]; |
| 29 | uintptr_t *__init_array_end [[gnu::visibility("protected" )]]; |
| 30 | uintptr_t *__fini_array_start [[gnu::visibility("protected" )]]; |
| 31 | uintptr_t *__fini_array_end [[gnu::visibility("protected" )]]; |
| 32 | } |
| 33 | |
| 34 | // Nvidia requires that the signature of the function pointers match. This means |
| 35 | // we cannot support the extended constructor arguments. |
| 36 | using InitCallback = void(void); |
| 37 | using FiniCallback = void(void); |
| 38 | |
| 39 | static void call_init_array_callbacks(int, char **, char **) { |
| 40 | size_t init_array_size = __init_array_end - __init_array_start; |
| 41 | for (size_t i = 0; i < init_array_size; ++i) |
| 42 | reinterpret_cast<InitCallback *>(__init_array_start[i])(); |
| 43 | } |
| 44 | |
| 45 | static void call_fini_array_callbacks() { |
| 46 | size_t fini_array_size = __fini_array_end - __fini_array_start; |
| 47 | for (size_t i = fini_array_size; i > 0; --i) |
| 48 | reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])(); |
| 49 | } |
| 50 | |
| 51 | } // namespace LIBC_NAMESPACE_DECL |
| 52 | |
| 53 | extern "C" [[gnu::visibility("protected" ), clang::nvptx_kernel]] void |
| 54 | _begin(int argc, char **argv, char **env) { |
| 55 | __atomic_store_n(&LIBC_NAMESPACE::app.env_ptr, |
| 56 | reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED); |
| 57 | |
| 58 | // We want the fini array callbacks to be run after other atexit |
| 59 | // callbacks are run. So, we register them before running the init |
| 60 | // array callbacks as they can potentially register their own atexit |
| 61 | // callbacks. |
| 62 | LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks); |
| 63 | LIBC_NAMESPACE::call_init_array_callbacks(argc, argv, env); |
| 64 | } |
| 65 | |
| 66 | extern "C" [[gnu::visibility("protected" ), clang::nvptx_kernel]] void |
| 67 | _start(int argc, char **argv, char **envp, int *ret) { |
| 68 | // Invoke the 'main' function with every active thread that the user launched |
| 69 | // the _start kernel with. |
| 70 | __atomic_fetch_or(ret, main(argc, argv, envp), __ATOMIC_RELAXED); |
| 71 | } |
| 72 | |
| 73 | extern "C" [[gnu::visibility("protected" ), clang::nvptx_kernel]] void |
| 74 | _end(int retval) { |
| 75 | // To finis the execution we invoke all the callbacks registered via 'atexit' |
| 76 | // and then exit with the appropriate return value. |
| 77 | LIBC_NAMESPACE::exit(retval); |
| 78 | } |
| 79 | |