Skip to content

Commit

Permalink
Workaround wrong codegen with ASAN+std::simd
Browse files Browse the repository at this point in the history
This is a workaround for GCC > 11 generating an aligned store to an unaligned address when calling placement new on std::simd, when ASAN is enabled.

Fixes: alpaka-group#777
  • Loading branch information
bernhardmgruber committed Sep 28, 2023
1 parent 1ce1e9d commit 55f27e0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions include/llama/View.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "mapping/One.hpp"

#include <type_traits>
#if __has_include(<experimental/simd>)
# include <experimental/simd>
#endif

namespace llama
{
Expand Down Expand Up @@ -87,6 +90,24 @@ namespace llama
LeafRecordCoords<typename Mapping::RecordDim>,
mp_bind_front<internal::IsComputed, Mapping>::template fn>::value;

namespace internal
{
template<typename T>
void valueInit(T* p)
{
new(p) T{};
}

#if defined(__GNUG__) && __GNUC__ > 11 && defined(__SANITIZE_ADDRESS__) && defined(__AVX512F__)
// ASAN messes with placement new and GCC generates an aligned store to an unaligned address with AVX512
template<typename T>
void valueInit(std::experimental::native_simd<T>* p)
{
std::memset(p, 0, sizeof(T));
}
#endif
} // namespace internal

template<typename Mapping, typename BlobType, typename Accessor, std::size_t... RCs>
LLAMA_FN_HOST_ACC_INLINE void constructField(
View<Mapping, BlobType, Accessor>& view,
Expand All @@ -106,7 +127,7 @@ namespace llama
else if constexpr(
std::is_lvalue_reference_v<RefType> && !std::is_const_v<std::remove_reference_t<RefType>>)
{
new(&view(ai)) FieldType{};
internal::valueInit(&view(ai));
}
}
else
Expand All @@ -119,7 +140,7 @@ namespace llama
else if constexpr(
std::is_lvalue_reference_v<RefType> && !std::is_const_v<std::remove_reference_t<RefType>>)
{
new(&view(ai)(rc)) FieldType{};
internal::valueInit(&view(ai)(rc));
}
}
}
Expand Down

0 comments on commit 55f27e0

Please sign in to comment.