diff --git a/sycl/source/detail/event_impl.cpp b/sycl/source/detail/event_impl.cpp index ea8964bb727de..3b338c2a1260d 100644 --- a/sycl/source/detail/event_impl.cpp +++ b/sycl/source/detail/event_impl.cpp @@ -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) { @@ -214,8 +200,22 @@ void event_impl::setQueue(queue_impl &Queue) { void event_impl::setSubmittedQueue(std::weak_ptr 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); } diff --git a/sycl/unittests/queue/GetProfilingInfo.cpp b/sycl/unittests/queue/GetProfilingInfo.cpp index 0e1d3a405232a..71b36e5b89dd9 100644 --- a/sycl/unittests/queue/GetProfilingInfo.cpp +++ b/sycl/unittests/queue/GetProfilingInfo.cpp @@ -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(); + }); + expect_profiling_exception([&] { + return E.get_profiling_info(); + }); + expect_profiling_exception([&] { + return E.get_profiling_info(); + }); +}