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
16extern "C" int main(int argc, char **argv, char **envp);
17
18namespace LIBC_NAMESPACE_DECL {
19
20DataEnvironment app;
21
22// FIXME: Factor this out into common logic so we don't need to stub it here.
23void teardown_main_tls() {}
24
25extern "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.
28uintptr_t *__init_array_start [[gnu::visibility("protected")]];
29uintptr_t *__init_array_end [[gnu::visibility("protected")]];
30uintptr_t *__fini_array_start [[gnu::visibility("protected")]];
31uintptr_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.
36using InitCallback = void(void);
37using FiniCallback = void(void);
38
39static 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
45static 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
53extern "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
66extern "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
73extern "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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libc/startup/gpu/nvptx/start.cpp