Skip to content

Commit 52c8ca5

Browse files
[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 <alexey.sachkov@intel.com>
1 parent 6c41afe commit 52c8ca5

File tree

0 file changed

+0
-0
lines changed

    0 file changed

    +0
    -0
    lines changed

    0 commit comments

    Comments
     (0)