1111#include " adapter.hpp"
1212#include " ur_level_zero.hpp"
1313
14+ ur_adapter_handle_t_ *Adapter;
15+
1416ur_result_t initPlatforms (PlatformVec &platforms) noexcept try {
1517 uint32_t ZeDriverCount = 0 ;
1618 ZE2UR_CALL (zeDriverGet, (&ZeDriverCount, nullptr ));
@@ -37,8 +39,7 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
3739ur_result_t adapterStateInit () { return UR_RESULT_SUCCESS; }
3840
3941ur_adapter_handle_t_::ur_adapter_handle_t_ () {
40-
41- Adapter.PlatformCache .Compute = [](Result<PlatformVec> &result) {
42+ PlatformCache.Compute = [](Result<PlatformVec> &result) {
4243 static std::once_flag ZeCallCountInitialized;
4344 try {
4445 std::call_once (ZeCallCountInitialized, []() {
@@ -52,7 +53,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
5253 }
5354
5455 // initialize level zero only once.
55- if (Adapter. ZeResult == std::nullopt ) {
56+ if (Adapter-> ZeResult == std::nullopt ) {
5657 // Setting these environment variables before running zeInit will enable
5758 // the validation layer in the Level Zero loader.
5859 if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
@@ -71,20 +72,20 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
7172 // We must only initialize the driver once, even if urPlatformGet() is
7273 // called multiple times. Declaring the return value as "static" ensures
7374 // it's only called once.
74- Adapter. ZeResult = ZE_CALL_NOCHECK (zeInit, (ZE_INIT_FLAG_GPU_ONLY));
75+ Adapter-> ZeResult = ZE_CALL_NOCHECK (zeInit, (ZE_INIT_FLAG_GPU_ONLY));
7576 }
76- assert (Adapter. ZeResult !=
77+ assert (Adapter-> ZeResult !=
7778 std::nullopt ); // verify that level-zero is initialized
7879 PlatformVec platforms;
7980
8081 // Absorb the ZE_RESULT_ERROR_UNINITIALIZED and just return 0 Platforms.
81- if (*Adapter. ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
82+ if (*Adapter-> ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
8283 result = std::move (platforms);
8384 return ;
8485 }
85- if (*Adapter. ZeResult != ZE_RESULT_SUCCESS) {
86+ if (*Adapter-> ZeResult != ZE_RESULT_SUCCESS) {
8687 urPrint (" zeInit: Level Zero initialization failure\n " );
87- result = ze2urResult (*Adapter. ZeResult );
88+ result = ze2urResult (*Adapter-> ZeResult );
8889 return ;
8990 }
9091
@@ -97,8 +98,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
9798 };
9899}
99100
100- ur_adapter_handle_t_ Adapter{};
101-
102101ur_result_t adapterStateTeardown () {
103102 bool LeakFound = false ;
104103
@@ -203,11 +202,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
203202 // /< adapters available.
204203) {
205204 if (NumEntries > 0 && Adapters) {
206- std::lock_guard<std::mutex> Lock{Adapter.Mutex };
207- if (Adapter.RefCount ++ == 0 ) {
208- adapterStateInit ();
205+ if (Adapter) {
206+ std::lock_guard<std::mutex> Lock{Adapter->Mutex };
207+ if (Adapter->RefCount ++ == 0 ) {
208+ adapterStateInit ();
209+ }
210+ *Adapters = Adapter;
211+ } else {
212+ return UR_RESULT_ERROR_UNINITIALIZED;
209213 }
210- *Adapters = &Adapter;
211214 }
212215
213216 if (NumAdapters) {
@@ -218,17 +221,24 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
218221}
219222
220223UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease (ur_adapter_handle_t ) {
221- std::lock_guard<std::mutex> Lock{Adapter.Mutex };
222- if (--Adapter.RefCount == 0 ) {
223- return adapterStateTeardown ();
224+ // Check first if the Adapter pointer is valid
225+ if (Adapter) {
226+ std::lock_guard<std::mutex> Lock{Adapter->Mutex };
227+ if (--Adapter->RefCount == 0 ) {
228+ return adapterStateTeardown ();
229+ }
224230 }
225231
226232 return UR_RESULT_SUCCESS;
227233}
228234
229235UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain (ur_adapter_handle_t ) {
230- std::lock_guard<std::mutex> Lock{Adapter.Mutex };
231- Adapter.RefCount ++;
236+ if (Adapter) {
237+ std::lock_guard<std::mutex> Lock{Adapter->Mutex };
238+ Adapter->RefCount ++;
239+ } else {
240+ return UR_RESULT_ERROR_UNINITIALIZED;
241+ }
232242
233243 return UR_RESULT_SUCCESS;
234244}
@@ -257,7 +267,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
257267 case UR_ADAPTER_INFO_BACKEND:
258268 return ReturnValue (UR_ADAPTER_BACKEND_LEVEL_ZERO);
259269 case UR_ADAPTER_INFO_REFERENCE_COUNT:
260- return ReturnValue (Adapter. RefCount .load ());
270+ return ReturnValue (Adapter-> RefCount .load ());
261271 default :
262272 return UR_RESULT_ERROR_INVALID_ENUMERATION;
263273 }
0 commit comments