Skip to content

Commit c43a90f

Browse files
author
Brox Chen
authored
[SYCL] fix a behavior mismatch in ann_ptr operator (#11904)
Fixed two issues: 1. For compound operators, there is an error in the specs. The `tmp = tmp Op rhs` statement is wrong since Op is defined as `+=` and for `+=` it will be `tmp = tmp += rhs`. Correct the format 2. For compound operators and inc/dec operators, the old implementation is using the non-compound operator which is wrong. i.e. If T is a customer type with only `+=` operator being defined, when applying `+=` on `annotated_ref<T>`, the previous implementation errors out since there is no `+` defined in T. 3. Added the missing `const` to inc/dec operators
1 parent 98989d6 commit c43a90f

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

sycl/doc/extensions/experimental/sycl_ext_oneapi_annotated_ptr.asciidoc

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,10 @@ class annotated_ref {
528528
T operator=(const annotated_ref&) const;
529529
// OP is: +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=
530530
T operatorOP(const T &) const;
531-
T operator++();
532-
T operator++(int);
533-
T operator--();
534-
T operator--(int);
531+
T operator++() const;
532+
T operator++(int) const;
533+
T operator--() const;
534+
T operator--(int) const;
535535
};
536536
} // namespace sycl::ext::oneapi::experimental
537537
```
@@ -556,7 +556,7 @@ annotations when the object is loaded from memory.
556556
a|
557557
[source,c++]
558558
----
559-
T operator=(const T &);
559+
T operator=(const T &) const;
560560
----
561561
|
562562
Writes an object of type `T` to the location referenced by this wrapper,
@@ -566,14 +566,14 @@ applying the annotations when the object is stored to memory.
566566
a|
567567
[source,c++]
568568
----
569-
T operator=(const annotated_ref& other);
569+
T operator=(const annotated_ref& other) const;
570570
----
571571
a|
572572
Equivalent to:
573573
```c++
574-
tmp T = other; // Reads from memory
574+
T tmp = other; // Reads from memory
575575
// with annotations
576-
*this = T; // Writes to memory
576+
*this = tmp; // Writes to memory
577577
// with annotations
578578
return T;
579579
```
@@ -583,18 +583,18 @@ Does not rebind the reference!
583583
a|
584584
[source,c++]
585585
----
586-
T operatorOP(const T &);
586+
T operatorOP(const T &) const;
587587
----
588588
a|
589589
Where [code]#OP# is: [code]#pass:[+=]#, [code]#-=#,[code]#*=#, [code]#/=#, [code]#%=#, [code]#+<<=+#, [code]#>>=#, [code]#&=#, [code]#\|=#, [code]#^=#.
590590

591591
Compound assignment operators. Return result by value.
592-
Available only if the corresponding non-compound operator is available for `T`.
592+
Available only if the corresponding assignment operator OP is available for `T`.
593593
Equivalent to:
594594
```c++
595595
T tmp = *this; // Reads from memory
596596
// with annotations
597-
tmp = tmp OP val;
597+
tmp OP val;
598598
*this = tmp; // Writes to memory
599599
// with annotations
600600
return tmp;
@@ -603,14 +603,16 @@ return tmp;
603603
a|
604604
[source,c++]
605605
----
606-
T operator++();
607-
T operator++(int);
608-
T operator--();
609-
T operator--(int);
606+
T operator++() const;
607+
T operator++(int) const;
608+
T operator--() const;
609+
T operator--(int) const;
610610
----
611611
|
612-
increment and decrement operator of annotated_ref. The annotations are applied
613-
when the object `T` is loaded and stored to the memory.
612+
Increment and decrement operator of annotated_ref. Increment/Decrement the object `T`
613+
referenced by this wrapper via ``T``'s Increment/Decrement operator.
614+
615+
The annotations are applied when the object `T` is loaded and stored to the memory.
614616
|===
615617

616618
== Issues related to `annotated_ptr`

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ namespace experimental {
3232

3333
namespace {
3434
#define PROPAGATE_OP(op) \
35-
T operator op##=(const T &rhs) const { return *this = *this op rhs; }
35+
T operator op##=(const T &rhs) const { \
36+
T t = *this; \
37+
t op## = rhs; \
38+
*this = t; \
39+
return t; \
40+
}
3641

3742
// compare strings on compile time
3843
constexpr bool compareStrs(const char *Str1, const char *Str2) {
@@ -114,22 +119,36 @@ class annotated_ref<T, detail::properties_t<Props...>> {
114119
PROPAGATE_OP(<<)
115120
PROPAGATE_OP(>>)
116121

117-
T operator++() { return *this += 1; }
118-
119-
T operator++(int) {
120-
const T t = *this;
121-
*this = (t + 1);
122+
T operator++() const {
123+
T t = *this;
124+
++t;
125+
*this = t;
122126
return t;
123127
}
124128

125-
T operator--() { return *this -= 1; }
129+
T operator++(int) const {
130+
T t1 = *this;
131+
T t2 = t1;
132+
t2++;
133+
*this = t2;
134+
return t1;
135+
}
126136

127-
T operator--(int) {
128-
const T t = *this;
129-
*this = (t - 1);
137+
T operator--() const {
138+
T t = *this;
139+
--t;
140+
*this = t;
130141
return t;
131142
}
132143

144+
T operator--(int) const {
145+
T t1 = *this;
146+
T t2 = t1;
147+
t2--;
148+
*this = t2;
149+
return t1;
150+
}
151+
133152
template <class T2, class P2> friend class annotated_ptr;
134153
};
135154

0 commit comments

Comments
 (0)