[SYCL] Fix marray ~operator for bool data type#22440
Conversation
|
@uditagarwal97 could you please provide a bit of background info for this change? Thanks! |
~operator for bool data type
There was a problem hiding this comment.
Pull request overview
This PR fixes the behavior of the bitwise NOT (~) operator for sycl::marray<bool, N>, ensuring it produces a proper boolean inversion instead of returning values that always convert to true after bitwise negation.
Changes:
- Adjust
marray’soperator~implementation to correctly invertboolelements in both the ext-vector and scalar paths. - Add a new end-to-end test covering multiple
marray<bool, N>sizes to exercise both implementation paths (including theNumElements == 3non-ext-vector case).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| sycl/include/sycl/marray.hpp | Fixes operator~ for marray<bool, N> by normalizing results to boolean inversion in both ext-vector and non-ext-vector implementations. |
| sycl/test-e2e/Basic/built-ins/marray_bitwise_not_bool.cpp | Adds an E2E regression test validating ~ on marray<bool, N> across several sizes and value patterns. |
iclsrc
left a comment
There was a problem hiding this comment.
The fix is correct: ~uint8_t(1) = 0xFE, and static_cast<bool>(0xFE) is true, so the original storeVecResult path produced the wrong result for marray<bool>. Both the ext_vector and non-vector paths are now logically correct. One minor readability suggestion on the ext_vector path.
| if constexpr (std::is_same_v<DataT, bool>) { | ||
| for (size_t I = 0; I < NumElements; ++I) | ||
| Ret[I] = ResVec[I] & 1; |
There was a problem hiding this comment.
🔵 Suggestion: The ResVec[I] & 1 expression relies on a subtle property — that ~(0 or 1) always flips the LSB — and it also wastes the ResVec = ~LhsVec computation on line 459 (the compiler will likely optimize it away, but it's dead work). Consider !LhsVec[I] instead, which mirrors the non-vector path's !Lhs[I] pattern and is immediately readable without needing to reason about bitwise complement:
| if constexpr (std::is_same_v<DataT, bool>) { | |
| for (size_t I = 0; I < NumElements; ++I) | |
| Ret[I] = ResVec[I] & 1; | |
| if constexpr (std::is_same_v<DataT, bool>) { | |
| for (size_t I = 0; I < NumElements; ++I) | |
| Ret[I] = !LhsVec[I]; |
@slawekptak I've updated the PR description. |
We use uint8_t for bool when using
ext_vector_typeon device and~uint8_t(1) = 0xFE, andstatic_cast<bool>(0xFE)is true, so the originalstoreVecResultpath produced the wrong result formarray<bool>Fixes CMPLRLLVM-76430