Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / startup / gpu / nvptx / start.cpp
blob1ff187a577789f14a77bdb0d51b2e1ac2b8b1ef3
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 using InitCallback = void(int, char **, char **);
28 using FiniCallback = void(void);
30 static void call_init_array_callbacks(int argc, char **argv, char **env) {
31 size_t init_array_size = __init_array_end - __init_array_start;
32 for (size_t i = 0; i < init_array_size; ++i)
33 reinterpret_cast<InitCallback *>(__init_array_start[i])(argc, argv, env);
36 static void call_fini_array_callbacks() {
37 size_t fini_array_size = __fini_array_end - __fini_array_start;
38 for (size_t i = 0; i < fini_array_size; ++i)
39 reinterpret_cast<FiniCallback *>(__fini_array_start[i])();
42 } // namespace LIBC_NAMESPACE
44 extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
45 _begin(int argc, char **argv, char **env) {
46 // We want the fini array callbacks to be run after other atexit
47 // callbacks are run. So, we register them before running the init
48 // array callbacks as they can potentially register their own atexit
49 // callbacks.
50 LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks);
51 LIBC_NAMESPACE::call_init_array_callbacks(argc, argv, env);
54 extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
55 _start(int argc, char **argv, char **envp, int *ret) {
56 // Invoke the 'main' function with every active thread that the user launched
57 // the _start kernel with.
58 __atomic_fetch_or(ret, main(argc, argv, envp), __ATOMIC_RELAXED);
61 extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
62 _end(int retval) {
63 // To finis the execution we invoke all the callbacks registered via 'atexit'
64 // and then exit with the appropriate return value.
65 LIBC_NAMESPACE::exit(retval);