Skip to content

Commit 8eb00d7

Browse files
author
Alexander Batashev
authored
[SYCL] Fix plugin deinit order (#3024)
Scheduler may hold references to active queues, which upon destruction may call PI APIs. To avoid potential clashes, acquire a reference on plugins and only perform tear down after scheduler is gone.
1 parent d532e62 commit 8eb00d7

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

sycl/source/detail/global_handler.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,27 @@ GlobalHandler::getDeviceFilterList(const std::string &InitValue) {
115115
}
116116

117117
void shutdown() {
118-
for (plugin &Plugin : GlobalHandler::instance().getPlugins()) {
119-
// PluginParameter is reserved for future use that can control
120-
// some parameters in the plugin tear-down process.
121-
// Currently, it is not used.
122-
void *PluginParameter = nullptr;
123-
Plugin.call_nocheck<PiApiKind::piTearDown>(PluginParameter);
124-
Plugin.unload();
118+
// First, release resources, that may access plugins.
119+
GlobalHandler::instance().MScheduler.reset(nullptr);
120+
GlobalHandler::instance().MProgramManager.reset(nullptr);
121+
GlobalHandler::instance().MPlatformCache.reset(nullptr);
122+
123+
// Call to GlobalHandler::instance().getPlugins() initializes plugins. If
124+
// user application has loaded SYCL runtime, and never called any APIs,
125+
// there's no need to load and unload plugins.
126+
if (GlobalHandler::instance().MPlugins) {
127+
for (plugin &Plugin : GlobalHandler::instance().getPlugins()) {
128+
// PluginParameter is reserved for future use that can control
129+
// some parameters in the plugin tear-down process.
130+
// Currently, it is not used.
131+
void *PluginParameter = nullptr;
132+
Plugin.call_nocheck<PiApiKind::piTearDown>(PluginParameter);
133+
Plugin.unload();
134+
}
135+
GlobalHandler::instance().MPlugins.reset(nullptr);
125136
}
126137

138+
// Release the rest of global resources.
127139
delete &GlobalHandler::instance();
128140
}
129141

sycl/source/detail/global_handler.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ class GlobalHandler {
5959

6060
private:
6161
friend void shutdown();
62+
6263
// Constructor and destructor are declared out-of-line to allow incomplete
6364
// types as template arguments to unique_ptr.
6465
GlobalHandler();
6566
~GlobalHandler();
6667

6768
SpinLock MFieldsLock;
6869

70+
// Do not forget to update shutdown() function if needed.
6971
std::unique_ptr<Scheduler> MScheduler;
7072
std::unique_ptr<ProgramManager> MProgramManager;
7173
std::unique_ptr<Sync> MSync;

0 commit comments

Comments
 (0)