Skip to content

Commit dc30fa6

Browse files
committed
[libc][fix] Call GPU destructors in the correct order
Summary: I was mistakenly iterating the list backwards. Regular semantics puts both arrays in priority order but the destructors are called backwards.
1 parent 129a91e commit dc30fa6

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

libc/startup/gpu/amdgpu/start.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ static void call_init_array_callbacks(int argc, char **argv, char **env) {
3737

3838
static void call_fini_array_callbacks() {
3939
size_t fini_array_size = __fini_array_end - __fini_array_start;
40-
for (size_t i = 0; i < fini_array_size; ++i)
41-
reinterpret_cast<FiniCallback *>(__fini_array_start[i])();
40+
for (size_t i = fini_array_size; i > 0; --i)
41+
reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
4242
}
4343

4444
} // namespace LIBC_NAMESPACE

libc/startup/gpu/nvptx/start.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ static void call_init_array_callbacks(int argc, char **argv, char **env) {
3535

3636
static void call_fini_array_callbacks() {
3737
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])();
38+
for (size_t i = fini_array_size; i > 0; --i)
39+
reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
4040
}
4141

4242
} // namespace LIBC_NAMESPACE

libc/utils/gpu/loader/nvptx/Loader.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ Expected<void *> get_ctor_dtor_array(const void *image, const size_t size,
8383
// for destructors.
8484
llvm::sort(ctors, [](auto x, auto y) { return x.second < y.second; });
8585
llvm::sort(dtors, [](auto x, auto y) { return x.second < y.second; });
86-
llvm::reverse(dtors);
8786

8887
// Allocate host pinned memory to make these arrays visible to the GPU.
8988
CUdeviceptr *dev_memory = reinterpret_cast<CUdeviceptr *>(allocator(

0 commit comments

Comments
 (0)