Skip to content

[SYCL] Fix library destructor priority value #3054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sycl/doc/GlobalObjectsInRuntime.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ destruction of nested `std::unique_ptr`s.

### Linux

On Linux DPC++ runtime uses `__attribute__((destructor))` property with maximum
possible priority value 65535. This approach does not guarantee, that
`GlobalHandler` destructor is the last thing to run, as user code may contain
a similar function with the same priority value.
On Linux DPC++ runtime uses `__attribute__((destructor))` property with low
priority value 110. This approach does not guarantee, that `GlobalHandler`
destructor is the last thing to run, as user code may contain a similar function
with the same priority value. At the same time, users may specify priorities
within [101, 109] range in order to run destructor after SYCL runtime has been
de-initialized. A destructor without specific priority value is going to be
executed before runtime shutdown mechanisms.

Another approach would be to leak global objects. This would guarantee user,
that global objects live long enough. But some global objects allocate heap
Expand Down
8 changes: 5 additions & 3 deletions sycl/source/detail/global_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
#else
// Setting maximum priority on destructor ensures it runs after all other global
// destructors.
__attribute__((destructor(65535))) static void syclUnload() { shutdown(); }
// Setting low priority on destructor ensures it runs after all other global
// destructors. Priorities 0-100 are reserved by the compiler. The priority
// value 110 allows SYCL users to run their destructors after runtime library
// deinitialization.
__attribute__((destructor(110))) static void syclUnload() { shutdown(); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we document it somewhere what priority are for what? And how it interacts with no priority specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I conducted a small experiment:

#include <iostream>

__attribute__((destructor(101))) static void syclUnload101() {
    std::cout << "101\n";
}

__attribute__((destructor(102))) static void syclUnload102() {
    std::cout << "102\n";
}

__attribute__((destructor(110))) static void syclUnload110() {
    std::cout << "110\n";
}

__attribute__((destructor(105))) static void syclUnload105() {
    std::cout << "105\n";
}

__attribute__((destructor)) static void syclUnload() {
    std::cout << "default\n";
}

int main() {
    std::cout << "Hi\n";
    return 0;
}

The result is the same for both GCC and Clang:

Hi
default
110
105
102
101

#endif
} // namespace detail
} // namespace sycl
Expand Down