80 lines · cpp
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-exception6//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);17extern "C" void __cxa_finalize(void *dso);18 19namespace LIBC_NAMESPACE_DECL {20 21// FIXME: Factor this out into common logic so we don't need to stub it here.22void teardown_main_tls() {}23 24// FIXME: Touch this symbol to force this to be linked in statically.25volatile void *dummy = &LIBC_NAMESPACE::rpc::client;26 27DataEnvironment app;28 29extern "C" uintptr_t __init_array_start[];30extern "C" uintptr_t __init_array_end[];31extern "C" uintptr_t __fini_array_start[];32extern "C" uintptr_t __fini_array_end[];33 34using InitCallback = void(int, char **, char **);35using FiniCallback = void(void);36 37static void call_init_array_callbacks(int argc, char **argv, char **env) {38 size_t init_array_size = __init_array_end - __init_array_start;39 for (size_t i = 0; i < init_array_size; ++i)40 reinterpret_cast<InitCallback *>(__init_array_start[i])(argc, argv, env);41}42 43static void call_fini_array_callbacks() {44 size_t fini_array_size = __fini_array_end - __fini_array_start;45 for (size_t i = fini_array_size; i > 0; --i)46 reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();47}48 49} // namespace LIBC_NAMESPACE_DECL50 51extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel,52 clang::amdgpu_flat_work_group_size(1, 1),53 clang::amdgpu_max_num_work_groups(1)]] void54_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 // We want the fini array callbacks to be run after other atexit58 // callbacks are run. So, we register them before running the init59 // array callbacks as they can potentially register their own atexit60 // callbacks.61 LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks);62 LIBC_NAMESPACE::call_init_array_callbacks(argc, argv, env);63}64 65extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel]] void66_start(int argc, char **argv, char **envp, int *ret) {67 // Invoke the 'main' function with every active thread that the user launched68 // the _start kernel with.69 __atomic_fetch_or(ret, main(argc, argv, envp), __ATOMIC_RELAXED);70}71 72extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel,73 clang::amdgpu_flat_work_group_size(1, 1),74 clang::amdgpu_max_num_work_groups(1)]] void75_end() {76 // Only a single thread should call the destructors registred with 'atexit'.77 // The loader utility will handle the actual exit and return code cleanly.78 __cxa_finalize(nullptr);79}80