You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[SYCL] Trick GCC to avoid strict pointer aliasing warnings (#1203)
The problem is that gcc < 7.2 emit the following warning:
```
warning: dereferencing type-punned pointer will break stric t-aliasing rules [-Wstrict-aliasing]
(*(T *)m_Mem).~T();
^
```
Interesting, that this only happens with `-O2` optimization level
Replaced C-style casts with C++ `reinterpret_cast` and this is actually
quite a hack, because according to the docs [1]:
> the resulting pointer may only be dereferenced safely if allowed by
> the type aliasing rules
Type aliasing rules allow to represent any object as `char *`, but not
the way around, i.e. array of characters cannot be reinterpreted as an
object.
`std::memcpy` also doesn't work here, because there is no requirement
that `T` is trivially copyable.
Another way to trick a compiler is to save pointer returned from
placement new: it already has type `T *`, so, we can store it within the
class and avoid casts. Hovewer, this is also a tricky thing, because since
`m_Mem` and this new pointer point to different types, compiler could
assume that they don't alias (when they actually point to the same memory
location) and perform some undesired transformations.
[1]: https://en.cppreference.com/w/cpp/language/reinterpret_cast
Signed-off-by: Alexey Sachkov <[email protected]>
0 commit comments