Skip to content

Commit 05d3ea7

Browse files
authored
Merge pull request #2007 from omarahmed1111/add-umfInit/umfTearDown-to-urAdapterGet/urAdapterRelease
Add umfInit/umfTearDown to urAdapterGet/urAdapterRelease
2 parents b766009 + 581a0d8 commit 05d3ea7

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@
1212
#include "ur_level_zero.hpp"
1313
#include <iomanip>
1414

15+
// As windows order of unloading dlls is reversed from linux, windows will call
16+
// umfTearDown before it could release umf objects in level_zero, so we call
17+
// umfInit on urAdapterGet and umfAdapterTearDown to enforce the teardown of umf
18+
// after umf objects are destructed.
19+
#if defined(_WIN32)
20+
#include <umf.h>
21+
#endif
22+
1523
// Due to multiple DLLMain definitions with SYCL, Global Adapter is init at
1624
// variable creation.
1725
#if defined(_WIN32)
1826
ur_adapter_handle_t_ *GlobalAdapter = new ur_adapter_handle_t_();
1927
#else
2028
ur_adapter_handle_t_ *GlobalAdapter;
2129
#endif
22-
30+
// This is a temporary workaround on windows, where UR adapter is teardowned
31+
// before the UR loader, which will result in access violation when we use print
32+
// function as the overrided print function was already released with the UR
33+
// adapter.
34+
// TODO: Change adapters to use a common sink class in the loader instead of
35+
// using thier own sink class that inherit from logger::Sink.
2336
class ur_legacy_sink : public logger::Sink {
2437
public:
2538
ur_legacy_sink(std::string logger_name = "", bool skip_prefix = true)
@@ -32,7 +45,11 @@ class ur_legacy_sink : public logger::Sink {
3245
fprintf(stderr, "%s", msg.c_str());
3346
}
3447

35-
~ur_legacy_sink() = default;
48+
~ur_legacy_sink() {
49+
#if defined(_WIN32)
50+
logger::isTearDowned = true;
51+
#endif
52+
};
3653
};
3754

3855
ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
@@ -74,7 +91,14 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
7491
return exceptionToResult(std::current_exception());
7592
}
7693

77-
ur_result_t adapterStateInit() { return UR_RESULT_SUCCESS; }
94+
ur_result_t adapterStateInit() {
95+
96+
#if defined(_WIN32)
97+
umfInit();
98+
#endif
99+
100+
return UR_RESULT_SUCCESS;
101+
}
78102

79103
ur_adapter_handle_t_::ur_adapter_handle_t_()
80104
: logger(logger::get_logger("level_zero")) {
@@ -258,6 +282,7 @@ ur_result_t adapterStateTeardown() {
258282
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
259283
// Global Adapter after refcnt is 0
260284
#if defined(_WIN32)
285+
umfTearDown();
261286
std::atexit(globalAdapterOnDemandCleanup);
262287
#endif
263288

source/common/logger/ur_sinks.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
namespace logger {
1919

20+
#if defined(_WIN32)
21+
inline bool isTearDowned = false;
22+
#endif
23+
2024
class Sink {
2125
public:
2226
template <typename... Args>
@@ -28,7 +32,21 @@ class Sink {
2832
}
2933

3034
format(buffer, fmt, std::forward<Args &&>(args)...);
35+
// This is a temporary workaround on windows, where UR adapter is teardowned
36+
// before the UR loader, which will result in access violation when we use print
37+
// function as the overrided print function was already released with the UR
38+
// adapter.
39+
// TODO: Change adapters to use a common sink class in the loader instead of
40+
// using thier own sink class that inherit from logger::Sink.
41+
#if defined(_WIN32)
42+
if (isTearDowned) {
43+
std::cerr << buffer.str() << "\n";
44+
} else {
45+
print(level, buffer.str());
46+
}
47+
#else
3148
print(level, buffer.str());
49+
#endif
3250
}
3351

3452
void setFlushLevel(logger::Level level) { this->flush_level = level; }

0 commit comments

Comments
 (0)