Skip to content

[NFCI][SYCL] Prefer raw ptr/ref for queue_impl #18874

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

Closed
wants to merge 1 commit into from

Conversation

aelovikov-intel
Copy link
Contributor

Continuation of the refactoring in
#18715
#18748

MState.store(HES_Complete);
}

// TODO: comment about https://github.com/intel/llvm/pull/14370
event_impl::event_impl(std::nullptr_t) : MQueue{}, MIsProfilingEnabled{true} {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sergey-semenov , @KseniyaTikhomirova any idea/preferences on how I should change the ctors here?

I think it's very surprising that (before my changes)
event_impl e() and event_impl e(nullptr) behave/are implemented so differently.

Copy link
Contributor

Choose a reason for hiding this comment

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

seems to be not applicable anymore, doesn't it? I don't see this constructor in the current mainline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I've split that part of the change into dedicated #18907 that has landed already.

@@ -121,7 +120,7 @@ class Command {
};

Command(
CommandType Type, QueueImplPtr Queue,
CommandType Type, queue_impl *Queue,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sergey-semenov or @intel/sycl-graphs-reviewers , do you know if Queue must be non-null for any of the subclasses?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really an expert on the scheduler, but I'd expect the ExecCGCommand subclass to expect the queue to be non-null as MQueue will be used to enqueue work.

Looking at ExecCGCommand::enqueueImpQueue in scheduler.cpp there are a few places with assert(MQueue

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it must be not-null for all of them. even host task.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even if we're submitting to the graph and not actual queue?

Copy link
Contributor

Choose a reason for hiding this comment

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

graph has queue as input parameter for begin_recording API https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/experimental/sycl_ext_oneapi_graph.asciidoc#84-graph,
so I expect graph commands to have corresponding queue too.
Although it is better to have a confirmation from graph extension authors or check yourself.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, since sycl buffers are separated into initialization commands and copy commands in the scheduler, we make sure that those have completed when it comes to finalize graph. We've got a bit of info on that in the last 3 paragraphs of https://github.com/intel/llvm/blob/sycl/sycl/doc/design/CommandGraph.md#scheduler-integration

Copy link
Contributor

@KseniyaTikhomirova KseniyaTikhomirova left a comment

Choose a reason for hiding this comment

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

I think at this point we need to clearly document somewhere why & how it is guaranteed to have valid (alive) raw pointer to Queue among so many objects, what object controls its lifetime and what destruction order we have.

@@ -248,7 +248,7 @@ void *event_impl::instrumentationProlog(std::string &Name, int32_t StreamID,
// queue is available with the wait events. We check to see if the
// TraceEvent is available in the Queue object.
void *TraceEvent = nullptr;
if (QueueImplPtr Queue = MQueue.lock()) {
if (auto Queue = MQueue.lock()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to get rid of QueueImplPtr? When it comes to code reading & understanding it is much easier to understand what type variable "Queue" is if it is declared as specific type. In this case I do not see any benefits in usage of "auto".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copying from my reply in #18983 (comment):

This alias was created because we had almost all APIs using it. The idea behind the ongoing refactoring is to have as little APIs operating on shared_ptr as possible (and avoid accidental copies as a byproduct). As such, having the alias in future won't be improving readability but reducing it, because the usage of that alias would be very rare.

As such, I'm simply removing the aliases from the headers that were [mostly] cleaned of excessive shared_ptr usage.

Copy link
Contributor

@KseniyaTikhomirova KseniyaTikhomirova Jun 16, 2025

Choose a reason for hiding this comment

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

I would suggest to use shared_ptr explicitly here. I am ok with alias removal but I don't like its replacement with auto.

@@ -559,9 +558,8 @@ class Scheduler {
/// \return a command that represents command group execution and a bool
/// indicating whether this command should be enqueued to the graph
/// processor right away or not.
Command *addCG(std::unique_ptr<detail::CG> CommandGroup,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer method with similar names (addCG) to access same variable type of Queue. It is confusing when one method accepts reference while another - raw pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I disagree. The distinction (that one interface has nullptr as valid input and the othder doesn't) is important, and I want to know it in advance and verified by the compiler through type system.

@@ -650,9 +650,12 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
// for in order ones.
void revisitUnenqueuedCommandsState(const EventImplPtr &CompletedHostTask);

static ContextImplPtr getContext(const QueueImplPtr &Queue) {
static ContextImplPtr getContext(queue_impl *Queue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

getContextImplPtr is const method. Why can't we declare Queue as pointer to const queue_impl then? To emphasize that the object doesn't change. Applicable to other places where it is possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const std::shared_ptr<queue_impl> & only said that the shared pointer didn't change, it provided no such guarantees about queue_impl. We can cleanup const-correctness in a separate refactoring once this one is finished, but we'll first have to agree how exactly we want to treat it (e.g. should we return const queue_impl or queue_impl from const handler_impl?).

Copy link
Contributor

Choose a reason for hiding this comment

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

I was not referring to the old impl, I saw that we have const for pointer only, but since we have another type now - probably we can do better.

@@ -121,7 +120,7 @@ class Command {
};

Command(
CommandType Type, QueueImplPtr Queue,
CommandType Type, queue_impl *Queue,
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it must be not-null for all of them. even host task.

@aelovikov-intel aelovikov-intel deleted the queue_impl branch June 16, 2025 21:13
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.

3 participants