Skip to content

[UR] Lazily initialize GlobalAdapter#21490

Open
uditagarwal97 wants to merge 4 commits intosyclfrom
private/udit/global_adapter
Open

[UR] Lazily initialize GlobalAdapter#21490
uditagarwal97 wants to merge 4 commits intosyclfrom
private/udit/global_adapter

Conversation

@uditagarwal97
Copy link
Contributor

@uditagarwal97 uditagarwal97 commented Mar 11, 2026

Problem
ur_win_proxy_loader loads L0 adapter DLL in its DLLMain. Global variables are initialized during DLL loading and so is GlobalAdapter - which tries to initialize L0 driver (zeInit). zeInit may spawn threads and it's illegal to spawn threads directly or indirectly from DLLMain.
This is causing deadlock with some L0 driver versions.

Proposed Solution
Lazily initialize GlobalAdapter, just like what we do on Linux.

@uditagarwal97 uditagarwal97 self-assigned this Mar 11, 2026
Comment on lines +31 to +41
Sum.combine(In[NDIt.get_global_linear_id()]);
});
});
}).wait_and_throw();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is incorrect as buffer destructor is not blocking (because of no-writeback) and there's no q.wait(). In its current state, the test is flakily segfaulting because of race between kernel execution and application termination. Adding q.wait_and_throw() fixes the race.
After fixing the race, the test fails deterministically because of L0 leak check due to same reason as in #20852

Comment on lines +5 to +6
// https://github.com/intel/llvm/issues/21520
// RUN-IF: level_zero && linux, env SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=0 UR_L0_V2_FORCE_BATCHED=1 %{run} %t2.out
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabling this batched submission test on Windows because there seems to be a race between the batch being enqueued and L0 loader teardown. See #21520

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a deadlock on Windows caused by the Level Zero adapter eagerly initializing GlobalAdapter during DLL loading (DLLMain), which could spawn threads—an illegal operation within DLLMain. The fix makes Windows use the same lazy initialization path that Linux already uses.

Changes:

  • Remove the #ifdef _WIN32 eager allocation of GlobalAdapter in adapter.cpp, unifying initialization to the existing lazy path in urAdapterGet.
  • Broaden test UNSUPPORTED markers and add wait_and_throw() in reduction_resource_leak_dw.cpp to address Windows test failures.
  • Convert a %if conditional RUN line to RUN-IF with a linux restriction in buffer.cpp to skip a force-batched submission test on Windows.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
unified-runtime/source/adapters/level_zero/adapter.cpp Remove Windows-specific eager GlobalAdapter allocation, unify to lazy init
sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp Broaden UNSUPPORTED to all Windows (not just v2 adapter) and add wait_and_throw()
sycl/test-e2e/Basic/buffer/buffer.cpp Restrict force-batched submission test to Linux using RUN-IF syntax

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a deadlock on Windows caused by eager initialization of GlobalAdapter during DLL loading. The ur_adapter_handle_t_ constructor calls zeInit, which may spawn threads—something that is illegal from DLLMain. The fix makes GlobalAdapter initialization lazy on Windows, matching the existing Linux behavior.

Changes:

  • Removed the Windows-specific eager GlobalAdapter initialization in adapter.cpp, relying on the existing lazy initialization in urAdapterGet.
  • Adjusted test expectations: broadened the UNSUPPORTED marker for reduction_resource_leak_dw.cpp on Windows, added .wait_and_throw() to avoid resource leaks.
  • Changed buffer.cpp to restrict a force-batched submission test to Linux only using RUN-IF, with a TODO tracking Windows enablement.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
unified-runtime/source/adapters/level_zero/adapter.cpp Removes Windows-specific eager GlobalAdapter allocation, unifying with lazy init path
sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp Broadens UNSUPPORTED to all Windows (not just v2 adapter) and adds wait_and_throw()
sycl/test-e2e/Basic/buffer/buffer.cpp Restricts force-batched submission test to Linux via RUN-IF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants