12
12
#include " ur_level_zero.hpp"
13
13
#include < iomanip>
14
14
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
+
15
23
// Due to multiple DLLMain definitions with SYCL, Global Adapter is init at
16
24
// variable creation.
17
25
#if defined(_WIN32)
18
26
ur_adapter_handle_t_ *GlobalAdapter = new ur_adapter_handle_t_();
19
27
#else
20
28
ur_adapter_handle_t_ *GlobalAdapter;
21
29
#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.
23
36
class ur_legacy_sink : public logger ::Sink {
24
37
public:
25
38
ur_legacy_sink (std::string logger_name = " " , bool skip_prefix = true )
@@ -32,7 +45,11 @@ class ur_legacy_sink : public logger::Sink {
32
45
fprintf (stderr, " %s" , msg.c_str ());
33
46
}
34
47
35
- ~ur_legacy_sink () = default ;
48
+ ~ur_legacy_sink () {
49
+ #if defined(_WIN32)
50
+ logger::isTearDowned = true ;
51
+ #endif
52
+ };
36
53
};
37
54
38
55
ur_result_t initPlatforms (PlatformVec &platforms) noexcept try {
@@ -74,7 +91,14 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
74
91
return exceptionToResult (std::current_exception ());
75
92
}
76
93
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
+ }
78
102
79
103
ur_adapter_handle_t_::ur_adapter_handle_t_ ()
80
104
: logger(logger::get_logger(" level_zero" )) {
@@ -258,6 +282,7 @@ ur_result_t adapterStateTeardown() {
258
282
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
259
283
// Global Adapter after refcnt is 0
260
284
#if defined(_WIN32)
285
+ umfTearDown ();
261
286
std::atexit (globalAdapterOnDemandCleanup);
262
287
#endif
263
288
0 commit comments