Repository navigation
[SYCL][E2E][Doc] Test external semaphores on regular command lists #23399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
uditagarwal97
merged 3 commits into
intel:sycl
from
crystarm:fix/external-semaphore-regular-cl
Oct 19, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
sycl/test-e2e/bindless_images/dx12_interop/external_semaphore_regular_cl.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // REQUIRES: aspect-ext_oneapi_external_semaphore_import, aspect-usm_shared_allocations | ||
| // REQUIRES: aspect-ext_intel_device_info_luid | ||
| // REQUIRES: windows, level_zero | ||
|
|
||
| // Regular command-list external semaphore support requires this driver | ||
| // version. | ||
| // REQUIRES-INTEL-DRIVER: win: 101.9030 | ||
|
|
||
| // XFAIL: windows && run-mode && gpu-intel-gen12 | ||
| // XFAIL-TRACKER: https://github.com/intel/llvm/issues/23249 | ||
|
|
||
| // RUN: %{build} %link-directx -o %t.exe %if target-spir %{ -Wno-ignored-attributes %} | ||
| // RUN: env UR_L0_BATCH_SIZE=8 %{run} %t.exe | ||
|
|
||
| // Verify that external semaphore wait and signal operations work on an | ||
| // in-order queue backed by regular (non-immediate) command lists. | ||
|
|
||
| #include "d3d12_setup.hpp" | ||
| #include <chrono> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <sycl/atomic_ref.hpp> | ||
| #include <sycl/detail/core.hpp> | ||
| #include <sycl/ext/intel/info/device.hpp> | ||
| #include <sycl/ext/oneapi/bindless_images.hpp> | ||
| #include <sycl/properties/queue_properties.hpp> | ||
| #include <sycl/usm.hpp> | ||
| #include <thread> | ||
|
|
||
| #define WIN32_LEAN_AND_MEAN | ||
| #include <windows.h> | ||
|
|
||
| namespace syclexp = sycl::ext::oneapi::experimental; | ||
|
|
||
| D3D12Context createSyclD3D12Context(const sycl::device &device) { | ||
| auto syclLuid = device.get_info<sycl::ext::intel::info::device::luid>(); | ||
| static_assert(sizeof(LUID) == sizeof(syclLuid)); | ||
| LUID adapterLuid{}; | ||
| std::memcpy(&adapterLuid, syclLuid.data(), sizeof(adapterLuid)); | ||
|
|
||
| ComPtr<IDXGIFactory4> factory; | ||
| ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&factory)), | ||
| "Failed to create DXGI factory"); | ||
|
|
||
| ComPtr<IDXGIAdapter1> adapter; | ||
| ThrowIfFailed(factory->EnumAdapterByLuid(adapterLuid, IID_PPV_ARGS(&adapter)), | ||
| "Failed to find the DXGI adapter for the SYCL device"); | ||
| return createD3D12Context(adapter.Get()); | ||
| } | ||
|
|
||
| [[noreturn]] void failWithoutCleanup(const std::string &message) { | ||
| std::cerr << message << std::endl; | ||
| std::_Exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| using MarkerAtomicRef = | ||
| sycl::atomic_ref<int, sycl::memory_order::relaxed, | ||
| sycl::memory_scope::system, | ||
| sycl::access::address_space::global_space>; | ||
|
|
||
| bool waitForMarker(int *marker) { | ||
| const auto deadline = | ||
| std::chrono::steady_clock::now() + std::chrono::seconds(5); | ||
| while (MarkerAtomicRef(*marker).load() == 0) { | ||
| if (std::chrono::steady_clock::now() >= deadline) | ||
| return false; | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool remainsIncomplete(const sycl::event &event) { | ||
| const auto deadline = | ||
| std::chrono::steady_clock::now() + std::chrono::milliseconds(100); | ||
| do { | ||
| if (event.get_info<sycl::info::event::command_execution_status>() == | ||
| sycl::info::event_command_status::complete) | ||
| return false; | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| } while (std::chrono::steady_clock::now() < deadline); | ||
| return true; | ||
| } | ||
|
|
||
| bool waitForCompletion(const sycl::event &event) { | ||
| const auto deadline = | ||
| std::chrono::steady_clock::now() + std::chrono::seconds(5); | ||
| while (event.get_info<sycl::info::event::command_execution_status>() != | ||
| sycl::info::event_command_status::complete) { | ||
| if (std::chrono::steady_clock::now() >= deadline) | ||
| return false; | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| sycl::queue q{ | ||
| {sycl::property::queue::in_order{}, | ||
| sycl::ext::intel::property::queue::no_immediate_command_list{}}}; | ||
| auto device = q.get_device(); | ||
| auto context = q.get_context(); | ||
|
|
||
| D3D12Context d3dCtx = createSyclD3D12Context(device); | ||
| D3D12ExportableFence extFence = createExportableFence(d3dCtx); | ||
|
|
||
| auto semDesc = | ||
| syclexp::external_semaphore_descriptor<syclexp::resource_win32_handle>{ | ||
| extFence.sharedHandle, | ||
| syclexp::external_semaphore_handle_type::win32_nt_dx12_fence}; | ||
| syclexp::external_semaphore syclSem = | ||
| syclexp::import_external_semaphore(semDesc, device, context); | ||
| int *marker = sycl::malloc_shared<int>(1, q); | ||
| MarkerAtomicRef(*marker).store(0); | ||
|
|
||
| try { | ||
| const uint64_t D3DSignalValue = extFence.fenceValue + 1; | ||
| q.single_task([=]() { MarkerAtomicRef(*marker).store(1); }); | ||
| sycl::event waitEvent = | ||
| q.ext_oneapi_wait_external_semaphore(syclSem, D3DSignalValue); | ||
|
|
||
| if (!waitForMarker(marker)) | ||
| failWithoutCleanup( | ||
| "The external semaphore wait did not submit the preceding batch."); | ||
| if (!remainsIncomplete(waitEvent)) | ||
| failWithoutCleanup( | ||
| "The external semaphore wait completed before D3D12 signaled it."); | ||
|
|
||
| signalExportableFence(d3dCtx, extFence); | ||
| if (!waitForCompletion(waitEvent)) | ||
| failWithoutCleanup( | ||
| "The external semaphore wait did not complete after D3D12 signaled " | ||
| "it."); | ||
|
|
||
| const uint64_t SyclSignalValue = D3DSignalValue + 1; | ||
| q.ext_oneapi_signal_external_semaphore(syclSem, SyclSignalValue); | ||
|
|
||
| ThrowIfFailed(extFence.fence->SetEventOnCompletion(SyclSignalValue, | ||
| d3dCtx.fenceEvent), | ||
| "Failed to wait for shared fence"); | ||
| constexpr DWORD TimeoutMs = 5000; | ||
| DWORD waitResult = WaitForSingleObject(d3dCtx.fenceEvent, TimeoutMs); | ||
| if (waitResult == WAIT_TIMEOUT) | ||
| failWithoutCleanup( | ||
| "Timed out waiting for the regular command-list semaphore signal."); | ||
| if (waitResult != WAIT_OBJECT_0) | ||
| failWithoutCleanup("Waiting for the external semaphore failed with " | ||
| "error " + | ||
| std::to_string(GetLastError())); | ||
|
|
||
| q.wait_and_throw(); | ||
| } catch (const std::exception &e) { | ||
| failWithoutCleanup(e.what()); | ||
| } | ||
|
|
||
| sycl::free(marker, q); | ||
| syclexp::release_external_semaphore(syclSem, device, context); | ||
| cleanupExportableFence(extFence); | ||
| if (d3dCtx.fenceEvent) | ||
| CloseHandle(d3dCtx.fenceEvent); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Suggestion: With this change the spec no longer restricts external semaphore use to immediate command lists. The new tests, however, need
REQUIRES-INTEL-DRIVER: lin: 39758, win: 101.9030. On older L0 drivers, a semaphore wait on a regular command list could hang silently, and nothing in the spec or the runtime warns the user. Could you add an implementation note here (or a runtime check or diagnostic in the L0 adapter) saying that regular command list support depends on the backend or driver? That way users on older drivers aren't caught out.