Skip to content

[SYCL] improve operator forwarding in annotated_ref #12140

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 5 commits into from
Dec 12, 2023

Conversation

broxigarchen
Copy link
Contributor

Consider the following case

template <typename T>
class annotated_ref {
    operator T() {}
};

template <typename T>
class A {
    template <typename T2>
    A (T2& p) {}
};

annotated_ref<A> a;
auto b = A(a);  // the operator T() from annotated_ref get shadowed by A's constructor

The fix tried to resolve this problem by making operator T() invocation to be more clear

@broxigarchen broxigarchen requested a review from a team as a code owner December 11, 2023 19:33
@broxigarchen broxigarchen changed the title improve operator forwarding [SYCL] improve operator forwarding in annotated_ref Dec 11, 2023
Comment on lines 123 to 124
O t2 = Ref.operator T();
return *this = t2;
Copy link
Contributor

Choose a reason for hiding this comment

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

Style question: Does someone care whether this is instead

Suggested change
O t2 = Ref.operator T();
return *this = t2;
O t2 = *(Ref.m_Ptr);
return *this = t2;

or

Suggested change
O t2 = Ref.operator T();
return *this = t2;
return *this = *(Ref.m_Ptr);;

or

Suggested change
O t2 = Ref.operator T();
return *this = t2;
return *this = Ref.operator T();

Copy link
Contributor Author

@broxigarchen broxigarchen Dec 11, 2023

Choose a reason for hiding this comment

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

O t2 = *(Ref.m_Ptr); // this is wrong, we need the conversion to have annotation
return *this = *(Ref.m_Ptr);; // same as above

return *this = Ref.operator T(); // this is ok, I have no preference

Copy link
Contributor

Choose a reason for hiding this comment

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

you're right. If someone prefers we avoid the .operator syntax we could move the annotated load into a private method in change this to

Suggested change
O t2 = Ref.operator T();
return *this = t2;
return *this = Ref.load();

It might even make sense to add this to the public interface (and add it to the spec). Because user code could have the same problem and appreciate not to have to use .operator in that case.

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 will keep the .operator for now

Copy link
Contributor

Choose a reason for hiding this comment

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

@Pennycook @gmlueck Do you think we should add load (or different name like get, ...) so users don't have the same problem if they have a type with conflicting conversion operator? If so we can do that in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, I'm not even sure I understand what these few lines are supposed to do. Could you give an example of where somebody might need to use this, outside of the definition of this operator?

Copy link
Contributor

@rolandschulz rolandschulz Dec 13, 2023

Choose a reason for hiding this comment

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

void f(annotated_ptr<MyT, ....> p) {
   MyT v = *p;
}

This works for any reasonable type. The operator* returns an annotated_ref and the assignment triggers the implicit conversion which does the annotated load. The problem is if the type is badly written and has an un/under-constrained conversion constructor e.g.:

struct MyT {
    template<class T> MyT(T&&) { ...}
};

Now that gets used rather than the implicit conversion from annotated_ref which is the intention. If a user has to deal with this kind of type (or wants to program generically very defensively that any such weird type would work) they then would instead write:

void f(annotated_ptr<MyT, ....> p) {
   MyT v = (*p).load();
}

Given that this doesn't rely on implicit conversion, there is no way for the type to interfere.
Without adding that member the user would in this case need to write:

void f(annotated_ptr<MyT, ....> p) {
   MyT v = (*p).operator MyT();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for the explanation. I agree that adding a member function would improve usability.

My preference would be to call it load(), because that's aligned with atomic_ref (which presumably has the same problem, since it also exposes an implicit conversion to T?). For symmetry and consistency we should define store(T) as well.

Copy link
Contributor

@maarquitos14 maarquitos14 left a comment

Choose a reason for hiding this comment

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

LGTM.

@broxigarchen
Copy link
Contributor Author

@intel/llvm-gatekeepers Hi can you please help to merge this? Thanks!

@aelovikov-intel aelovikov-intel merged commit 92bdb0f into intel:sycl Dec 12, 2023
againull pushed a commit that referenced this pull request Jan 11, 2024
Refactor annotated_arg operator forwarding regarding to
#11971 and
#12140

1. add binary and unary operator forwarding. We don't need inc/dec and
compound here since SYCL kernel arg are const member and cannot be
modified
2. replace the old impl with hidden friend operators

---------

Co-authored-by: Wang, Di5 <di5.wang@intel.com>
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.

5 participants