Skip to content

Commit b80f13e

Browse files
authored
[SYCL] Fix SegFault when accessor passed to reduction is destroyed early (intel#3498)
The error may happen when the copy of the accessor passed to ONEAPI::reduction is destroyed right after being passed to reduction. The fix is simple - to create a new copy of the given accessor. Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent 0c0f4c5 commit b80f13e

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

sycl/include/CL/sycl/ONEAPI/reduction.hpp

+14-28
Original file line numberDiff line numberDiff line change
@@ -508,30 +508,24 @@ class reduction_impl : private reduction_impl_base {
508508
}
509509

510510
/// Constructs reduction_impl when the identity value is statically known.
511-
// Note that aliasing constructor was used to initialize MRWAcc to avoid
512-
// destruction of the object referenced by the parameter Acc.
513511
template <
514512
typename _T = T,
515513
enable_if_t<IsKnownIdentityOp<_T, BinaryOperation>::value> * = nullptr>
516514
reduction_impl(rw_accessor_type &Acc)
517-
: MRWAcc(shared_ptr_class<rw_accessor_type>(
518-
shared_ptr_class<rw_accessor_type>{}, &Acc)),
519-
MIdentity(getIdentity()), InitializeToIdentity(false) {
515+
: MRWAcc(new rw_accessor_type(Acc)), MIdentity(getIdentity()),
516+
InitializeToIdentity(false) {
520517
if (Acc.get_count() != 1)
521518
throw sycl::runtime_error("Reduction variable must be a scalar.",
522519
PI_INVALID_VALUE);
523520
}
524521

525522
/// Constructs reduction_impl when the identity value is statically known.
526-
// Note that aliasing constructor was used to initialize MDWAcc to avoid
527-
// destruction of the object referenced by the parameter Acc.
528523
template <
529524
typename _T = T,
530525
enable_if_t<IsKnownIdentityOp<_T, BinaryOperation>::value> * = nullptr>
531526
reduction_impl(dw_accessor_type &Acc)
532-
: MDWAcc(shared_ptr_class<dw_accessor_type>(
533-
shared_ptr_class<dw_accessor_type>{}, &Acc)),
534-
MIdentity(getIdentity()), InitializeToIdentity(true) {
527+
: MDWAcc(new dw_accessor_type(Acc)), MIdentity(getIdentity()),
528+
InitializeToIdentity(true) {
535529
if (Acc.get_count() != 1)
536530
throw sycl::runtime_error("Reduction variable must be a scalar.",
537531
PI_INVALID_VALUE);
@@ -567,15 +561,12 @@ class reduction_impl : private reduction_impl_base {
567561

568562
/// Constructs reduction_impl when the identity value is statically known,
569563
/// and user still passed the identity value.
570-
// Note that aliasing constructor was used to initialize MRWAcc to avoid
571-
// destruction of the object referenced by the parameter Acc.
572564
template <
573565
typename _T = T,
574566
enable_if_t<IsKnownIdentityOp<_T, BinaryOperation>::value> * = nullptr>
575567
reduction_impl(rw_accessor_type &Acc, const T & /*Identity*/, BinaryOperation)
576-
: MRWAcc(shared_ptr_class<rw_accessor_type>(
577-
shared_ptr_class<rw_accessor_type>{}, &Acc)),
578-
MIdentity(getIdentity()), InitializeToIdentity(false) {
568+
: MRWAcc(new rw_accessor_type(Acc)), MIdentity(getIdentity()),
569+
InitializeToIdentity(false) {
579570
if (Acc.get_count() != 1)
580571
throw sycl::runtime_error("Reduction variable must be a scalar.",
581572
PI_INVALID_VALUE);
@@ -592,13 +583,14 @@ class reduction_impl : private reduction_impl_base {
592583
// list of known operations does not break the existing programs.
593584
}
594585

586+
/// Constructs reduction_impl when the identity value is statically known,
587+
/// and user still passed the identity value.
595588
template <
596589
typename _T = T,
597590
enable_if_t<IsKnownIdentityOp<_T, BinaryOperation>::value> * = nullptr>
598591
reduction_impl(dw_accessor_type &Acc, const T & /*Identity*/, BinaryOperation)
599-
: MDWAcc(shared_ptr_class<dw_accessor_type>(
600-
shared_ptr_class<dw_accessor_type>{}, &Acc)),
601-
MIdentity(getIdentity()), InitializeToIdentity(true) {
592+
: MDWAcc(new dw_accessor_type(Acc)), MIdentity(getIdentity()),
593+
InitializeToIdentity(true) {
602594
if (Acc.get_count() != 1)
603595
throw sycl::runtime_error("Reduction variable must be a scalar.",
604596
PI_INVALID_VALUE);
@@ -632,30 +624,24 @@ class reduction_impl : private reduction_impl_base {
632624
}
633625

634626
/// Constructs reduction_impl when the identity value is unknown.
635-
// Note that aliasing constructor was used to initialize MRWAcc to avoid
636-
// destruction of the object referenced by the parameter Acc.
637627
template <
638628
typename _T = T,
639629
enable_if_t<!IsKnownIdentityOp<_T, BinaryOperation>::value> * = nullptr>
640630
reduction_impl(rw_accessor_type &Acc, const T &Identity, BinaryOperation BOp)
641-
: MRWAcc(shared_ptr_class<rw_accessor_type>(
642-
shared_ptr_class<rw_accessor_type>{}, &Acc)),
643-
MIdentity(Identity), MBinaryOp(BOp), InitializeToIdentity(false) {
631+
: MRWAcc(new rw_accessor_type(Acc)), MIdentity(Identity), MBinaryOp(BOp),
632+
InitializeToIdentity(false) {
644633
if (Acc.get_count() != 1)
645634
throw sycl::runtime_error("Reduction variable must be a scalar.",
646635
PI_INVALID_VALUE);
647636
}
648637

649638
/// Constructs reduction_impl when the identity value is unknown.
650-
// Note that aliasing constructor was used to initialize MDWAcc to avoid
651-
// destruction of the object referenced by the parameter Acc.
652639
template <
653640
typename _T = T,
654641
enable_if_t<!IsKnownIdentityOp<_T, BinaryOperation>::value> * = nullptr>
655642
reduction_impl(dw_accessor_type &Acc, const T &Identity, BinaryOperation BOp)
656-
: MDWAcc(shared_ptr_class<dw_accessor_type>(
657-
shared_ptr_class<dw_accessor_type>{}, &Acc)),
658-
MIdentity(Identity), MBinaryOp(BOp), InitializeToIdentity(true) {
643+
: MDWAcc(new dw_accessor_type(Acc)), MIdentity(Identity), MBinaryOp(BOp),
644+
InitializeToIdentity(true) {
659645
if (Acc.get_count() != 1)
660646
throw sycl::runtime_error("Reduction variable must be a scalar.",
661647
PI_INVALID_VALUE);

0 commit comments

Comments
 (0)