xref: /llvm-project/libc/startup/gpu/amdgpu/start.cpp (revision 666a3f4ed4f62a9b1b732dae6a34a66d31217563)
1 //===-- Implementation of crt for amdgpu ----------------------------------===//
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 // FIXME: Factor this out into common logic so we don't need to stub it here.
21 void teardown_main_tls() {}
22 
23 DataEnvironment app;
24 
25 extern "C" uintptr_t __init_array_start[];
26 extern "C" uintptr_t __init_array_end[];
27 extern "C" uintptr_t __fini_array_start[];
28 extern "C" uintptr_t __fini_array_end[];
29 
30 using InitCallback = void(int, char **, char **);
31 using FiniCallback = void(void);
32 
33 static void call_init_array_callbacks(int argc, char **argv, char **env) {
34   size_t init_array_size = __init_array_end - __init_array_start;
35   for (size_t i = 0; i < init_array_size; ++i)
36     reinterpret_cast<InitCallback *>(__init_array_start[i])(argc, argv, env);
37 }
38 
39 static void call_fini_array_callbacks() {
40   size_t fini_array_size = __fini_array_end - __fini_array_start;
41   for (size_t i = fini_array_size; i > 0; --i)
42     reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
43 }
44 
45 } // namespace LIBC_NAMESPACE_DECL
46 
47 extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel,
48              clang::amdgpu_flat_work_group_size(1, 1),
49              clang::amdgpu_max_num_work_groups(1)]] void
50 _begin(int argc, char **argv, char **env) {
51   __atomic_store_n(&LIBC_NAMESPACE::app.env_ptr,
52                    reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED);
53   // We want the fini array callbacks to be run after other atexit
54   // callbacks are run. So, we register them before running the init
55   // array callbacks as they can potentially register their own atexit
56   // callbacks.
57   LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks);
58   LIBC_NAMESPACE::call_init_array_callbacks(argc, argv, env);
59 }
60 
61 extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel]] void
62 _start(int argc, char **argv, char **envp, int *ret) {
63   // Invoke the 'main' function with every active thread that the user launched
64   // the _start kernel with.
65   __atomic_fetch_or(ret, main(argc, argv, envp), __ATOMIC_RELAXED);
66 }
67 
68 extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel,
69              clang::amdgpu_flat_work_group_size(1, 1),
70              clang::amdgpu_max_num_work_groups(1)]] void
71 _end(int retval) {
72   // Only a single thread should call `exit` here, the rest should gracefully
73   // return from the kernel. This is so only one thread calls the destructors
74   // registred with 'atexit' above.
75   LIBC_NAMESPACE::exit(retval);
76 }
77