[AMDGPU] Place global constructors in .init_array and .fini_array
authorJoseph Huber <jhuber6@vols.utk.edu>
Thu, 27 Apr 2023 13:00:01 +0000 (08:00 -0500)
committerJoseph Huber <jhuber6@vols.utk.edu>
Sat, 29 Apr 2023 13:40:19 +0000 (08:40 -0500)
commita1da7461571cf1763136e22a018a20a271bb70b9
tree26340b3a33e825c661aeb9ce52c6bc969013a34e
parentbc37be1855773c1dcf8c6bf577a096a81fd58652
[AMDGPU] Place global constructors in .init_array and .fini_array

For the GPU, we emit external kernels that call the initializers and
constructors, however if we had a persistent kernel like in the `_start`
kernel for the `libc` project, we could initialize the standard way of
calling constructors. This patch adds new global variables containing
pointers to the constructors to be called. If these are placed in the
`.init_array` and `.fini_array` sections, then the backend will handle
them specially. The linker will then provide the `__init_array_` and
`__fini_array_` sections to traverse them. An implementation would look
like this.

```
extern uintptr_t __init_array_start[];
extern uintptr_t __init_array_end[];
extern uintptr_t __fini_array_start[];
extern uintptr_t __fini_array_end[];

using InitCallback = void(int, char **, char **);
using FiniCallback = void(void);

extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel]] void
_start(int argc, char **argv, char **envp) {
  uint64_t init_array_size = __init_array_end - __init_array_start;
  for (uint64_t i = 0; i < init_array_size; ++i)
    reinterpret_cast<InitCallback *>(__init_array_start[i])(argc, argv, env);
  uint64_t fini_array_size = __fini_array_end - __fini_array_start;
  for (uint64_t i = 0; i < fini_array_size; ++i)
    reinterpret_cast<FiniCallback *>(__fini_array_start[i])();
}
```

Reviewed By: yaxunl

Differential Revision: https://reviews.llvm.org/D149340
llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
llvm/test/CodeGen/AMDGPU/lower-ctor-dtor-constexpr-alias.ll
llvm/test/CodeGen/AMDGPU/lower-ctor-dtor-existing.ll
llvm/test/CodeGen/AMDGPU/lower-ctor-dtor.ll