Skip to content

Commit

Permalink
Fix _LIBCUDACXX_UNREACHABLE for old MSVC
Browse files Browse the repository at this point in the history
MSVC2017 / 2019 have issues properly determining that `__assume(0)` is indeed meant to mean unreachable code and complains about missing return statement sometimes.

We can work around this by wrapping it into a `__declspec(noreturn)` function 🤷

Addresses nvbug4359466
  • Loading branch information
miscco committed Nov 17, 2023
1 parent 931dc67 commit f5dccff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
22 changes: 0 additions & 22 deletions libcudacxx/include/cuda/std/detail/libcxx/include/array
Original file line number Diff line number Diff line change
Expand Up @@ -467,19 +467,13 @@ struct _LIBCUDACXX_TEMPLATE_VIS array<_Tp, 0>
{
_LIBCUDACXX_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
_LIBCUDACXX_UNREACHABLE();
#if defined(_LIBCUDACXX_COMPILER_MSVC_2017)
return *data();
#endif // _LIBCUDACXX_COMPILER_MSVC_2017
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 const_reference
operator[](size_type) const noexcept
{
_LIBCUDACXX_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
_LIBCUDACXX_UNREACHABLE();
#if defined(_LIBCUDACXX_COMPILER_MSVC_2017)
return *data();
#endif // _LIBCUDACXX_COMPILER_MSVC_2017
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 reference at(size_type)
Expand All @@ -498,43 +492,27 @@ struct _LIBCUDACXX_TEMPLATE_VIS array<_Tp, 0>
_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 reference front() noexcept
{
_LIBCUDACXX_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
#if defined(_LIBCUDACXX_COMPILER_MSVC)
return *data();
#else // ^^^ _LIBCUDACXX_COMPILER_MSVC ^^^ / vvv !_LIBCUDACXX_COMPILER_MSVC vvv
_LIBCUDACXX_UNREACHABLE();
#endif // !_LIBCUDACXX_COMPILER_MSVC
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 const_reference
front() const noexcept
{
_LIBCUDACXX_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
#if defined(_LIBCUDACXX_COMPILER_MSVC)
return *data();
#else // ^^^ _LIBCUDACXX_COMPILER_MSVC ^^^ / vvv !_LIBCUDACXX_COMPILER_MSVC vvv
_LIBCUDACXX_UNREACHABLE();
#endif // !_LIBCUDACXX_COMPILER_MSVC
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 reference back() noexcept
{
_LIBCUDACXX_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
#if defined(_LIBCUDACXX_COMPILER_MSVC)
return *data();
#else // ^^^ _LIBCUDACXX_COMPILER_MSVC ^^^ / vvv !_LIBCUDACXX_COMPILER_MSVC vvv
_LIBCUDACXX_UNREACHABLE();
#endif // !_LIBCUDACXX_COMPILER_MSVC
}

_LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 const_reference
back() const noexcept
{
_LIBCUDACXX_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
#if defined(_LIBCUDACXX_COMPILER_MSVC)
return *data();
#else // ^^^ _LIBCUDACXX_COMPILER_MSVC ^^^ / vvv !_LIBCUDACXX_COMPILER_MSVC vvv
_LIBCUDACXX_UNREACHABLE();
#endif // !_LIBCUDACXX_COMPILER_MSVC
}
};

Expand Down
8 changes: 6 additions & 2 deletions libcudacxx/include/cuda/std/detail/libcxx/include/cstdlib
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ void *aligned_alloc(size_t alignment, size_t size); // C11
# define _LIBCUDACXX_UNREACHABLE() __builtin_unreachable()
#endif // CUDACC above 11.4
#else // ^^^ __CUDA_ARCH__ ^^^ / vvv !__CUDA_ARCH__ vvv
#if defined(_LIBCUDACXX_COMPILER_MSVC)
# define _LIBCUDACXX_UNREACHABLE() __assume(false)
#if defined(_LIBCUDACXX_COMPILER_MSVC_2017)
template<class = void>
_LIBCUDACXX_INLINE_VISIBILITY __declspec(noreturn) void __unreachable_fallback() { __assume(0); }
# define _LIBCUDACXX_UNREACHABLE() __unreachable_fallback()
#elif defined(_LIBCUDACXX_COMPILER_MSVC)
# define _LIBCUDACXX_UNREACHABLE() __assume(0)
#elif defined(_LIBCUDACXX_COMPILER_GCC) || __has_builtin(__builtin_unreachable)
# define _LIBCUDACXX_UNREACHABLE() __builtin_unreachable()
#else // Other compilers
Expand Down

0 comments on commit f5dccff

Please sign in to comment.