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)
1826ur_adapter_handle_t_ *GlobalAdapter = new ur_adapter_handle_t_();
1927#else
2028ur_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.
2336class ur_legacy_sink : public logger ::Sink {
2437public:
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
3855ur_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
79103ur_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
0 commit comments