Skip to content

Commit 92bdb0f

Browse files
author
Brox Chen
authored
[SYCL] improve operator forwarding in annotated_ref (#12140)
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
1 parent fed32a8 commit 92bdb0f

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

sycl/doc/extensions/experimental/sycl_ext_oneapi_annotated_ptr.asciidoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ T operator=(const annotated_ref& other) const;
617617
a|
618618
Equivalent to:
619619
```c++
620-
T tmp = other; // Reads from memory
621-
// with annotations
620+
T tmp = other.operator T(); // Reads from memory
621+
// with annotations
622622
*this = tmp; // Writes to memory
623623
// with annotations
624624
return T;
@@ -642,8 +642,8 @@ Return result by value.
642642
Available only if the corresponding assignment operator OP is available for `T` taking a type of `O`.
643643
Equivalent to:
644644
```c++
645-
T tmp = *this; // Reads from memory
646-
// with annotations
645+
T tmp = this->operator T(); // Reads from memory
646+
// with annotations
647647
tmp OP std::forward<O>(a);
648648
*this = tmp; // Writes to memory
649649
// with annotations
@@ -665,8 +665,8 @@ Return result by value.
665665
Available only if the corresponding assignment operator OP is available for `T`.
666666
Equivalent to:
667667
```c++
668-
T tmp = *this; // Reads from memory
669-
// with annotations
668+
T tmp = this->operator T(); // Reads from memory
669+
// with annotations
670670
T tmp2 = b; // Reads from memory
671671
// with annotations
672672
tmp OP b;

sycl/include/sycl/ext/oneapi/experimental/annotated_ptr/annotated_ptr.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,23 @@ class annotated_ref<T, detail::properties_t<Props...>> {
120120

121121
template <class O, class P>
122122
T operator=(const annotated_ref<O, P> &Ref) const {
123-
O t2 = Ref;
123+
O t2 = Ref.operator O();
124124
return *this = t2;
125125
}
126126

127127
// propagate compound operators
128128
#define PROPAGATE_OP(op) \
129129
template <class O, typename = std::enable_if_t<!detail::is_ann_ref_v<O>>> \
130130
T operator op(O &&rhs) const { \
131-
T t = *this; \
131+
T t = this->operator T(); \
132132
t op std::forward<O>(rhs); \
133133
*this = t; \
134134
return t; \
135135
} \
136136
template <class O, class P> \
137137
T operator op(const annotated_ref<O, P> &rhs) const { \
138-
T t = *this; \
139-
O t2 = rhs; \
138+
T t = this->operator T(); \
139+
O t2 = rhs.operator T(); \
140140
t op t2; \
141141
*this = t; \
142142
return t; \
@@ -158,12 +158,12 @@ class annotated_ref<T, detail::properties_t<Props...>> {
158158
template <class O> \
159159
friend auto operator op(O &&a, const annotated_ref &b) \
160160
->decltype(std::forward<O>(a) op std::declval<T>()) { \
161-
return std::forward<O>(a) op T(b); \
161+
return std::forward<O>(a) op b.operator T(); \
162162
} \
163163
template <class O, typename = std::enable_if_t<!detail::is_ann_ref_v<O>>> \
164164
friend auto operator op(const annotated_ref &a, O &&b) \
165165
->decltype(std::declval<T>() op std::forward<O>(b)) { \
166-
return T(a) op std::forward<O>(b); \
166+
return a.operator T() op std::forward<O>(b); \
167167
}
168168
PROPAGATE_OP(+)
169169
PROPAGATE_OP(-)
@@ -190,7 +190,7 @@ class annotated_ref<T, detail::properties_t<Props...>> {
190190
#define PROPAGATE_OP(op) \
191191
template <typename O = T> \
192192
auto operator op() const->decltype(op std::declval<O>()) { \
193-
return op O(*this); \
193+
return op this->operator O(); \
194194
}
195195
PROPAGATE_OP(+)
196196
PROPAGATE_OP(-)
@@ -200,29 +200,29 @@ class annotated_ref<T, detail::properties_t<Props...>> {
200200

201201
// Propagate inc/dec operators
202202
T operator++() const {
203-
T t = *this;
203+
T t = this->operator T();
204204
++t;
205205
*this = t;
206206
return t;
207207
}
208208

209209
T operator++(int) const {
210-
T t1 = *this;
210+
T t1 = this->operator T();
211211
T t2 = t1;
212212
t2++;
213213
*this = t2;
214214
return t1;
215215
}
216216

217217
T operator--() const {
218-
T t = *this;
218+
T t = this->operator T();
219219
--t;
220220
*this = t;
221221
return t;
222222
}
223223

224224
T operator--(int) const {
225-
T t1 = *this;
225+
T t1 = this->operator T();
226226
T t2 = t1;
227227
t2--;
228228
*this = t2;

sycl/test-e2e/Annotated_arg_ptr/annotated_ptr.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ template <typename T> struct MyThirdStruct {
6767
float operator==(const T &rhs) const { return data == rhs.data ? 3.0 : 1.0; }
6868
};
6969

70+
template <typename T> struct MyFourthStruct {
71+
T p;
72+
73+
template <typename T2> MyFourthStruct(const T2 &p_) : p(p_) {}
74+
75+
template <typename T2> void operator=(const T2 &p_) {}
76+
77+
int operator+(const int &rhs) const { return 0; }
78+
int operator+=(const int &rhs) const { return 0; }
79+
};
80+
7081
MySecondStruct::operator MyStruct<int>() const { return MyStruct<int>(0); }
7182

7283
#define BINARY_OP(op) \
@@ -181,6 +192,11 @@ int main() {
181192

182193
auto *r10 = malloc_shared<int>(1, Q);
183194

195+
auto *r11 = malloc_shared<MyFourthStruct<int>>(1, Q);
196+
annotated_ptr r11_ptr{r11};
197+
auto r11_add = *r11_ptr + 1;
198+
*r11_ptr += 1;
199+
184200
// testing return type of operators
185201
int o1 = 0;
186202
float o2 = 1.5;

0 commit comments

Comments
 (0)