diff --git a/sycl/source/detail/event_impl.cpp b/sycl/source/detail/event_impl.cpp index fe1f63899798c..2bf1233042e6a 100644 --- a/sycl/source/detail/event_impl.cpp +++ b/sycl/source/detail/event_impl.cpp @@ -186,23 +186,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) { @@ -215,14 +201,23 @@ void event_impl::setQueue(queue_impl &Queue) { MIsDefaultConstructed = false; } +void event_impl::initHostProfilingInfo() { + assert(isHost() && "This must be a host event"); + assert(MState == HES_NotComplete && + "Host event must be incomplete to initialize profiling info"); + + std::shared_ptr QueuePtr = MSubmittedQueue.lock(); + assert(QueuePtr && "Queue must be valid to initialize host profiling info"); + assert(QueuePtr->MIsProfilingEnabled && "Queue must have profiling enabled"); + + MIsProfilingEnabled = true; + MHostProfilingInfo = std::make_unique(); + device_impl &Device = QueuePtr->getDeviceImpl(); + MHostProfilingInfo->setDevice(&Device); +} + void event_impl::setSubmittedQueue(std::weak_ptr SubmittedQueue) { MSubmittedQueue = std::move(SubmittedQueue); - if (MHostProfilingInfo) { - if (std::shared_ptr QueuePtr = MSubmittedQueue.lock()) { - device_impl &Device = QueuePtr->getDeviceImpl(); - MHostProfilingInfo->setDevice(&Device); - } - } } void *event_impl::instrumentationProlog(std::string &Name, int32_t StreamID, @@ -621,12 +616,7 @@ bool event_impl::isCompleted() { info::event_command_status::complete; } -void event_impl::setCommand(void *Cmd) { - MCommand = Cmd; - auto TypedCommand = static_cast(Cmd); - if (TypedCommand) - MIsHostEvent = TypedCommand->getWorkerContext() == nullptr; -} +void event_impl::setCommand(void *Cmd) { MCommand = Cmd; } } // namespace detail } // namespace _V1 diff --git a/sycl/source/detail/event_impl.hpp b/sycl/source/detail/event_impl.hpp index 79d66f336a4f0..8ff54777db83b 100644 --- a/sycl/source/detail/event_impl.hpp +++ b/sycl/source/detail/event_impl.hpp @@ -378,6 +378,9 @@ class event_impl : public std::enable_shared_from_this { return MEvent && MQueue.expired() && !MIsEnqueued && !MCommand; } + // Initializes the host profiling info for the event. + void initHostProfilingInfo(); + protected: // When instrumentation is enabled emits trace event for event wait begin and // returns the telemetry event generated for the wait diff --git a/sycl/source/detail/scheduler/commands.cpp b/sycl/source/detail/scheduler/commands.cpp index ff354642834c9..40246b26d4b9f 100644 --- a/sycl/source/detail/scheduler/commands.cpp +++ b/sycl/source/detail/scheduler/commands.cpp @@ -1954,8 +1954,15 @@ ExecCGCommand::ExecCGCommand( : Command(CommandType::RUN_CG, Queue, CommandBuffer, Dependencies), MEventNeeded(EventNeeded), MCommandGroup(std::move(CommandGroup)) { if (MCommandGroup->getType() == detail::CGType::CodeplayHostTask) { - MEvent->setSubmittedQueue( - static_cast(MCommandGroup.get())->MQueue); + queue_impl *SubmitQueue = + static_cast(MCommandGroup.get())->MQueue.get(); + assert(SubmitQueue && + "Host task command group must have a valid submit queue"); + + MEvent->setSubmittedQueue(SubmitQueue->weak_from_this()); + // Initialize host profiling info if the queue has profiling enabled. + if (SubmitQueue->MIsProfilingEnabled) + MEvent->initHostProfilingInfo(); } if (MCommandGroup->getType() == detail::CGType::ProfilingTag) MEvent->markAsProfilingTagEvent(); diff --git a/sycl/unittests/queue/GetProfilingInfo.cpp b/sycl/unittests/queue/GetProfilingInfo.cpp index 0e1d3a405232a..b7551907e310d 100644 --- a/sycl/unittests/queue/GetProfilingInfo.cpp +++ b/sycl/unittests/queue/GetProfilingInfo.cpp @@ -345,3 +345,34 @@ 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; + [[maybe_unused]] unittest::UrMock<> Mock; + 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(); + }); +}