Skip to content

Commit 575758c

Browse files
committed
[SYCL] Provide initialization for variable
As far as the compiler is concerned, the variable `x` is uninitialized on some control-flow paths and is passed to the function `(ex|in)clusive_scan_over_group`. In terms of LLVM, the function parameter that `x` is passed to is likely marked `noundef`. This can lead the compiler to take advantage of `undef` rules and assume that the control path in which `x` is defined must happen (otherwise it'd be undefined), and it can essentially remove the guarding `i < N` around the memory accesses. This is, of course, dangerous. Though, in practice, this is often avoided because the scan function is inlined before LLVM makes that assumption, this should not be relied upon. I've chosen to zero-initialize it. The value of `init` cannot be used because it may be out of bounds for the type pointed to by `InPtr`, which can introduce new UB into the program.
1 parent 7afc2d0 commit 575758c

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

sycl/include/sycl/group_algorithm.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ joint_exclusive_scan(Group g, InPtr first, InPtr last, OutPtr result, T init,
843843
return ((v + divisor - 1) / divisor) * divisor;
844844
};
845845
typename std::remove_const<typename detail::remove_pointer<InPtr>::type>::type
846-
x;
846+
x = {};
847847
T carry = init;
848848
for (ptrdiff_t chunk = 0; chunk < roundup(N, stride); chunk += stride) {
849849
ptrdiff_t i = chunk + offset;
@@ -1034,7 +1034,7 @@ joint_inclusive_scan(Group g, InPtr first, InPtr last, OutPtr result,
10341034
return ((v + divisor - 1) / divisor) * divisor;
10351035
};
10361036
typename std::remove_const<typename detail::remove_pointer<InPtr>::type>::type
1037-
x;
1037+
x = {};
10381038
T carry = init;
10391039
for (ptrdiff_t chunk = 0; chunk < roundup(N, stride); chunk += stride) {
10401040
ptrdiff_t i = chunk + offset;

0 commit comments

Comments
 (0)