[libc-commits] [libc] dc30fa6 - [libc][fix] Call GPU destructors in the correct order
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Thu Nov 9 07:23:31 PST 2023
Author: Joseph Huber
Date: 2023-11-09T09:22:41-06:00
New Revision: dc30fa6aca4bcd275082a6fc27ce287f3444ce30
URL: https://github.com/llvm/llvm-project/commit/dc30fa6aca4bcd275082a6fc27ce287f3444ce30
DIFF: https://github.com/llvm/llvm-project/commit/dc30fa6aca4bcd275082a6fc27ce287f3444ce30.diff
LOG: [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.
Added:
Modified:
libc/startup/gpu/amdgpu/start.cpp
libc/startup/gpu/nvptx/start.cpp
libc/utils/gpu/loader/nvptx/Loader.cpp
Removed:
################################################################################
diff --git a/libc/startup/gpu/amdgpu/start.cpp b/libc/startup/gpu/amdgpu/start.cpp
index 89b0be208d2e7fd..9d7f04c10b48820 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<FiniCallback *>(__fini_array_start[i])();
+ for (size_t i = fini_array_size; i > 0; --i)
+ reinterpret_cast<FiniCallback *>(__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 1ff187a577789f1..2c09430c5971ef0 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<FiniCallback *>(__fini_array_start[i])();
+ for (size_t i = fini_array_size; i > 0; --i)
+ reinterpret_cast<FiniCallback *>(__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 e920b65a7e10cce..5388f287063b7f9 100644
--- a/libc/utils/gpu/loader/nvptx/Loader.cpp
+++ b/libc/utils/gpu/loader/nvptx/Loader.cpp
@@ -83,7 +83,6 @@ Expected<void *> 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<CUdeviceptr *>(allocator(
More information about the libc-commits
mailing list