From dc30fa6aca4bcd275082a6fc27ce287f3444ce30 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Thu, 9 Nov 2023 09:21:52 -0600 Subject: [PATCH] [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. --- libc/startup/gpu/amdgpu/start.cpp | 4 ++-- libc/startup/gpu/nvptx/start.cpp | 4 ++-- libc/utils/gpu/loader/nvptx/Loader.cpp | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libc/startup/gpu/amdgpu/start.cpp b/libc/startup/gpu/amdgpu/start.cpp index 89b0be208d2e..9d7f04c10b48 100644 --- a/libc/startup/gpu/amdgpu/start.cpp +++ b/libc/startup/gpu/amdgpu/start.cpp @@ -37,8 +37,8 @@ static void call_init_array_callbacks(int argc, char **argv, char **env) { static void call_fini_array_callbacks() { size_t fini_array_size = __fini_array_end - __fini_array_start; - for (size_t i = 0; i < fini_array_size; ++i) - reinterpret_cast(__fini_array_start[i])(); + for (size_t i = fini_array_size; i > 0; --i) + reinterpret_cast(__fini_array_start[i - 1])(); } } // namespace LIBC_NAMESPACE diff --git a/libc/startup/gpu/nvptx/start.cpp b/libc/startup/gpu/nvptx/start.cpp index 1ff187a57778..2c09430c5971 100644 --- a/libc/startup/gpu/nvptx/start.cpp +++ b/libc/startup/gpu/nvptx/start.cpp @@ -35,8 +35,8 @@ static void call_init_array_callbacks(int argc, char **argv, char **env) { static void call_fini_array_callbacks() { size_t fini_array_size = __fini_array_end - __fini_array_start; - for (size_t i = 0; i < fini_array_size; ++i) - reinterpret_cast(__fini_array_start[i])(); + for (size_t i = fini_array_size; i > 0; --i) + reinterpret_cast(__fini_array_start[i - 1])(); } } // namespace LIBC_NAMESPACE diff --git a/libc/utils/gpu/loader/nvptx/Loader.cpp b/libc/utils/gpu/loader/nvptx/Loader.cpp index e920b65a7e10..5388f287063b 100644 --- a/libc/utils/gpu/loader/nvptx/Loader.cpp +++ b/libc/utils/gpu/loader/nvptx/Loader.cpp @@ -83,7 +83,6 @@ Expected get_ctor_dtor_array(const void *image, const size_t size, // for destructors. llvm::sort(ctors, [](auto x, auto y) { return x.second < y.second; }); llvm::sort(dtors, [](auto x, auto y) { return x.second < y.second; }); - llvm::reverse(dtors); // Allocate host pinned memory to make these arrays visible to the GPU. CUdeviceptr *dev_memory = reinterpret_cast(allocator(