Skip to content

[SYCL] Profile host events only if the submitted queue has profiling enabled #18982

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
merged 9 commits into from
Jun 18, 2025
Merged
34 changes: 17 additions & 17 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,9 @@ event_impl::event_impl(queue_impl &Queue, private_tag)
}

event_impl::event_impl(HostEventState State, private_tag) : MState(State) {
switch (State) {
case HES_Discarded:
case HES_Complete: {
MIsHostEvent = true;
if (State == HES_Discarded || State == HES_Complete)
MIsFlushed = true;
MIsHostEvent = true;
break;
}
case HES_NotComplete: {
MIsProfilingEnabled = true;
MHostProfilingInfo.reset(new HostProfilingInfo());
if (!MHostProfilingInfo)
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Out of host memory " +
codeToString(UR_RESULT_ERROR_OUT_OF_HOST_MEMORY));
}
}
}

void event_impl::setQueue(queue_impl &Queue) {
Expand All @@ -214,8 +200,22 @@ void event_impl::setQueue(queue_impl &Queue) {

void event_impl::setSubmittedQueue(std::weak_ptr<queue_impl> SubmittedQueue) {
MSubmittedQueue = std::move(SubmittedQueue);
if (MHostProfilingInfo) {
if (isHost()) {
if (auto QueuePtr = MSubmittedQueue.lock()) {
// Enable profiling for host events only if the queue where host task was
// submitted has profiling enabled.
MIsProfilingEnabled = QueuePtr->MIsProfilingEnabled;
if (!MIsProfilingEnabled || MState == HES_Discarded ||
MState == HES_Complete)
return;

MHostProfilingInfo.reset(new HostProfilingInfo());
if (!MHostProfilingInfo)
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Out of host memory " +
codeToString(UR_RESULT_ERROR_OUT_OF_HOST_MEMORY));

device_impl &Device = QueuePtr->getDeviceImpl();
MHostProfilingInfo->setDevice(&Device);
}
Expand Down
30 changes: 30 additions & 0 deletions sycl/unittests/queue/GetProfilingInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,33 @@ TEST(GetProfilingInfo, check_command_submission_time_with_host_accessor) {

EXPECT_TRUE(DeviceTimerCalled);
}

// Check that query fails for host task if queue doesn't have profiling
// enabled.
TEST(GetProfilingInfo, check_host_task_profiling_info) {
using namespace sycl;
queue Queue;
event E = Queue.submit([&](handler &cgh) { cgh.host_task([]() {}); });

auto expect_profiling_exception = [&](auto profiling_query) {
try {
std::ignore = profiling_query();
FAIL();
} catch (sycl::exception const &e) {
EXPECT_STREQ(
e.what(),
"Profiling information is unavailable as the queue associated "
"with the event does not have the 'enable_profiling' property.");
}
};

expect_profiling_exception([&] {
return E.get_profiling_info<info::event_profiling::command_submit>();
});
expect_profiling_exception([&] {
return E.get_profiling_info<info::event_profiling::command_start>();
});
expect_profiling_exception([&] {
return E.get_profiling_info<info::event_profiling::command_end>();
});
}
Loading