Skip to content

Commit

Permalink
Update the unsafe implicit conversion error message in MDRangePolicy (k…
Browse files Browse the repository at this point in the history
…okkos#6855)

* Updated the error message in MDRangePolicy about unsafe implicit conversions

* Addressed clang warnings
  • Loading branch information
ldh4 committed Mar 5, 2024
1 parent 9feb104 commit c3c8a70
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
19 changes: 12 additions & 7 deletions core/src/KokkosExp_MDRangePolicy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,20 @@ is_less_than_value_initialized_variable(T arg) {

// Checked narrowing conversion that calls abort if the cast changes the value
template <class To, class From>
constexpr To checked_narrow_cast(From arg) {
constexpr To checked_narrow_cast(From arg, std::size_t idx) {
constexpr const bool is_different_signedness =
(std::is_signed<To>::value != std::is_signed<From>::value);
auto const ret = static_cast<To>(arg);
if (static_cast<From>(ret) != arg ||
(is_different_signedness &&
is_less_than_value_initialized_variable(arg) !=
is_less_than_value_initialized_variable(ret))) {
Kokkos::abort("unsafe narrowing conversion");
auto msg =
"Kokkos::MDRangePolicy bound type error: an unsafe implicit conversion "
"is performed on a bound (" +
std::to_string(arg) + ") in dimension (" + std::to_string(idx) +
"), which may not preserve its original value.\n";
Kokkos::abort(msg.c_str());
}
return ret;
}
Expand All @@ -102,9 +107,9 @@ constexpr Array to_array_potentially_narrowing(const U (&init)[M]) {
// std::transform(std::begin(init), std::end(init), a.data(),
// [](U x) { return static_cast<T>(x); });
// except that std::transform is not constexpr.
for (auto x : init) {
*ptr++ = checked_narrow_cast<T>(x);
(void)checked_narrow_cast<IndexType>(x); // see note above
for (std::size_t i = 0; i < M; ++i) {
*ptr++ = checked_narrow_cast<T>(init[i], i);
(void)checked_narrow_cast<IndexType>(init[i], i); // see note above
}
return a;
}
Expand All @@ -122,8 +127,8 @@ constexpr NVCC_WONT_LET_ME_CALL_YOU_Array to_array_potentially_narrowing(
constexpr std::size_t N = a.size();
static_assert(M <= N);
for (std::size_t i = 0; i < M; ++i) {
a[i] = checked_narrow_cast<T>(other[i]);
(void)checked_narrow_cast<IndexType>(other[i]); // see note above
a[i] = checked_narrow_cast<T>(other[i], i);
(void)checked_narrow_cast<IndexType>(other[i], i); // see note above
}
return a;
}
Expand Down
13 changes: 8 additions & 5 deletions core/unit_test/TestMDRangePolicyConstructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ TEST(TEST_CATEGORY_DEATH, policy_bounds_unsafe_narrowing_conversions) {
using Policy = Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>,
Kokkos::IndexType<unsigned>>;

std::string msg =
"Kokkos::MDRangePolicy bound type error: an unsafe implicit conversion "
"is "
"performed on a bound (-1) in dimension (0), which may not preserve its "
"original value.\n";
std::string expected = std::regex_replace(msg, std::regex("\\(|\\)"), "\\$&");

::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH(
{
(void)Policy({-1, 0}, {2, 3});
},
"unsafe narrowing conversion");
ASSERT_DEATH({ (void)Policy({-1, 0}, {2, 3}); }, expected);
}

TEST(TEST_CATEGORY_DEATH, policy_invalid_bounds) {
Expand Down

0 comments on commit c3c8a70

Please sign in to comment.