Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bound checks for each dimension of mdspan #3065

Merged
merged 20 commits into from
Dec 10, 2024
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions libcudacxx/include/cuda/std/__mdspan/mdspan.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
# pragma system_header
#endif // no system header

#include <cuda/std/__algorithm/all_of.h>
#include <cuda/std/__mdspan/compressed_pair.h>
#include <cuda/std/__mdspan/default_accessor.h>
#include <cuda/std/__mdspan/extents.h>
Expand All @@ -64,6 +65,7 @@
#include <cuda/std/__type_traits/is_convertible.h>
#include <cuda/std/__type_traits/is_default_constructible.h>
#include <cuda/std/__type_traits/is_nothrow_constructible.h>
#include <cuda/std/__type_traits/is_signed.h>
#include <cuda/std/__type_traits/rank.h>
#include <cuda/std/__type_traits/remove_all_extents.h>
#include <cuda/std/__type_traits/remove_cv.h>
Expand Down Expand Up @@ -108,28 +110,45 @@ class mdspan
return (__self.rank() > 0)
&& __MDSPAN_FOLD_OR((__self.__mapping_ref().extents().template __extent<_Idxs>() == index_type(0)));
}

template <class... _SizeTypes>
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI static constexpr bool
__check_index(_Extents const& exts, _SizeTypes... __indices)
{
index_type __zero = 0; // avoid warning:186 (ICC/NVCC) pointless comparison of unsigned integer with zero
array<bool, sizeof...(_SizeTypes)> __res{((is_signed_v<index_type> && static_cast<index_type>(__indices) < __zero)
|| static_cast<index_type>(__indices) >= exts.extent(_Idxs))...};
_LIBCUDACXX_UNUSED_VAR(__zero * 2);
fbusato marked this conversation as resolved.
Show resolved Hide resolved
return _CUDA_VSTD::all_of(__res.begin(), __res.end(), [](bool __v) {
return __v;
});
}
fbusato marked this conversation as resolved.
Show resolved Hide resolved

template <class... _SizeTypes>
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI static constexpr index_type
__index(mdspan const& __self, _SizeTypes... __indices) noexcept
{
_CCCL_ASSERT(__check_index(__self.__mapping_ref().extents(), __indices...),
"cuda::std::mdspan subscript out of range!");
const index_type __res = __self.__mapping_ref()(index_type(__indices)...);
_CCCL_ASSERT(__res < __self.__mapping_ref().required_span_size(), "cuda::std::mdspan subscript out of range!");
return __res;
}
template <class _SizeType, size_t _Np>
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI static constexpr index_type
__index(mdspan const& __self, const _CUDA_VSTD::array<_SizeType, _Np>& __indices) noexcept
{
_CCCL_ASSERT(__check_index(__self.__mapping_ref().extents(), __indices[_Idxs]...),
"cuda::std::mdspan subscript out of range!");
const index_type __res = __self.__mapping_ref()(__indices[_Idxs]...);
_CCCL_ASSERT(__res < __self.__mapping_ref().required_span_size(), "cuda::std::mdspan subscript out of range!");
return __res;
}
template <class _SizeType, size_t _Np>
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI static constexpr index_type
__index(mdspan const& __self, const _CUDA_VSTD::span<_SizeType, _Np>& __indices) noexcept
{
_CCCL_ASSERT(__check_index(__self.__mapping_ref().extents(), __indices[_Idxs]...),
"cuda::std::mdspan subscript out of range!");
const index_type __res = __self.__mapping_ref()(__indices[_Idxs]...);
_CCCL_ASSERT(__res < __self.__mapping_ref().required_span_size(), "cuda::std::mdspan subscript out of range!");
return __res;
}
};
Expand Down
Loading