Skip to content

Commit bf7c84c

Browse files
[SYCL] Simplify storePlainArg to avoid alias violations (#14344)
The helper function storePlainArg in handler and jit_compiler reinterpret-casts pointers to memory in vectors of char in order to store arguments in them. However, this violates strict aliasing and is unnecessary as the resulting pointers are immediately converted to void* after all calls to the function. As such, this patch simplfies these implementations to always return void* and use memcpy to avoid the alias violation. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent deeb664 commit bf7c84c

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

sycl/include/sycl/handler.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,10 @@ class __SYCL_EXPORT handler {
489489
handler(std::shared_ptr<ext::oneapi::experimental::detail::graph_impl> Graph);
490490

491491
/// Stores copy of Arg passed to the CGData.MArgsStorage.
492-
template <typename T, typename F = typename std::remove_const_t<
493-
typename std::remove_reference_t<T>>>
494-
F *storePlainArg(T &&Arg) {
492+
template <typename T> void *storePlainArg(T &&Arg) {
495493
CGData.MArgsStorage.emplace_back(sizeof(T));
496-
auto Storage = reinterpret_cast<F *>(CGData.MArgsStorage.back().data());
497-
*Storage = Arg;
494+
void *Storage = static_cast<void *>(CGData.MArgsStorage.back().data());
495+
std::memcpy(Storage, &Arg, sizeof(T));
498496
return Storage;
499497
}
500498

@@ -691,7 +689,7 @@ class __SYCL_EXPORT handler {
691689
}
692690

693691
template <typename T> void setArgHelper(int ArgIndex, T &&Arg) {
694-
auto StoredArg = static_cast<void *>(storePlainArg(Arg));
692+
void *StoredArg = storePlainArg(Arg);
695693

696694
if (!std::is_same<cl_mem, T>::value && std::is_pointer<T>::value) {
697695
MArgs.emplace_back(detail::kernel_param_kind_t::kind_pointer, StoredArg,
@@ -703,7 +701,7 @@ class __SYCL_EXPORT handler {
703701
}
704702

705703
void setArgHelper(int ArgIndex, sampler &&Arg) {
706-
auto StoredArg = static_cast<void *>(storePlainArg(Arg));
704+
void *StoredArg = storePlainArg(Arg);
707705
MArgs.emplace_back(detail::kernel_param_kind_t::kind_sampler, StoredArg,
708706
sizeof(sampler), ArgIndex);
709707
}

sycl/source/detail/jit_compiler.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,6 @@ detectIdenticalParameter(std::vector<Param> &Params, ArgDesc Arg) {
468468
return Params.end();
469469
}
470470

471-
template <typename T, typename F = typename std::remove_const_t<
472-
typename std::remove_reference_t<T>>>
473-
F *storePlainArg(std::vector<std::vector<char>> &ArgStorage, T &&Arg) {
474-
ArgStorage.emplace_back(sizeof(T));
475-
auto Storage = reinterpret_cast<F *>(ArgStorage.back().data());
476-
*Storage = Arg;
477-
return Storage;
478-
}
479-
480471
void *storePlainArgRaw(std::vector<std::vector<char>> &ArgStorage, void *ArgPtr,
481472
size_t ArgSize) {
482473
ArgStorage.emplace_back(ArgSize);
@@ -485,6 +476,11 @@ void *storePlainArgRaw(std::vector<std::vector<char>> &ArgStorage, void *ArgPtr,
485476
return Storage;
486477
}
487478

479+
template <typename T>
480+
void *storePlainArg(std::vector<std::vector<char>> &ArgStorage, T &&Arg) {
481+
return storePlainArgRaw(ArgStorage, &Arg, sizeof(T));
482+
}
483+
488484
static ParamIterator preProcessArguments(
489485
std::vector<std::vector<char>> &ArgStorage, ParamIterator Arg,
490486
PromotionMap &PromotedAccs,
@@ -648,10 +644,10 @@ updatePromotedArgs(const ::jit_compiler::SYCLKernelInfo &FusedKernelInfo,
648644
Req, Promotion::Local)
649645
: 0;
650646
range<3> AccessRange{1, 1, LocalSize};
651-
auto *RangeArg = storePlainArg(FusedArgStorage, AccessRange);
647+
void *RangeArg = storePlainArg(FusedArgStorage, AccessRange);
652648
// Use all-zero as the offset
653649
id<3> AcessOffset{0, 0, 0};
654-
auto *OffsetArg = storePlainArg(FusedArgStorage, AcessOffset);
650+
void *OffsetArg = storePlainArg(FusedArgStorage, AcessOffset);
655651

656652
// Override the arguments.
657653
// 1. Override the pointer with a std-layout argument with 'nullptr' as

0 commit comments

Comments
 (0)