Skip to content

Commit

Permalink
Try and make everything work on different compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Sep 17, 2024
1 parent 1566ccb commit f4feb1a
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions libcudacxx/include/cuda/std/__cccl/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,65 +54,77 @@
#endif // !CCCL_ENABLE_DEVICE_ASSERTIONS

//! Use the different standard library implementations to implement host side asserts
#ifdef CCCL_ENABLE_HOST_ASSERTIONS
# if __has_include(<yvals_core.h>) // MSVC uses _STL_VERIFY from <yvals_core.h>
# include <yvals_core.h>
# define _CCCL_ASSERT_HOST(expression, message) _STL_VERIFY(expression, message)
# elif __has_include(<__assert>) // libc++ uses _LIBCPP_ASSERT from <__assert>
# include <__assert>
# define _CCCL_ASSERT_HOST(expression, message) _LIBCPP_ASSERT(expression, message)
# elif __has_include(<bits/c++config.h>) // libstdc++ uses __glibcxx_assert from <bits/c++config.h
# include <bits/c++config.h>
# if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 12
//! _CCCL_ASSERT_IMPL_HOST should never be used directly
#if __has_include(<yvals_core.h>) // MSVC uses _STL_VERIFY from <yvals_core.h>
# include <yvals_core.h>
# define _CCCL_ASSERT_IMPL_HOST(expression, message) _STL_VERIFY(expression, message)
#elif __has_include(<__assert>) // libc++ uses _LIBCPP_ASSERT from <__assert>
# include <__assert>
# define _CCCL_ASSERT_IMPL_HOST(expression, message) _LIBCPP_ASSERT(expression, message)
#elif __has_include(<bits/c++config.h>) // libstdc++ uses __glibcxx_assert from <bits/c++config.h
# include <bits/c++config.h>
# if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 12
// libstdc++ does not fully qualify its use of `__is_constant_evaluated`
// It was introduced in the assert handling in 5e8a30d
// libstdc++ : Redefine __glibcxx_assert to work in C++ 23 constexpr
_LIBCUDACXX_BEGIN_NAMESPACE_STD
using ::std::__is_constant_evaluated;
_LIBCUDACXX_END_NAMESPACE_STD
# endif // _GLIBCXX_RELEASE >= 12
# define _CCCL_ASSERT_HOST(expression, message) \
_CCCL_NV_DIAG_SUPPRESS(3060) __glibcxx_assert(expression); \
_CCCL_NV_DIAG_DEFAULT(3060)
# else // ^^^ libstdc++ ^^^ / vvv Unknown standard library vvv
# error "Unknown host standard library used."
# endif // Unknown standard library
# endif // _GLIBCXX_RELEASE >= 12
//! libstdc++ uses `is_constant_evaluated` in its assert definition which triggers a warning in non constexpr functions
# define _CCCL_ASSERT_IMPL_HOST(expression, message) \
_CCCL_NV_DIAG_SUPPRESS(3060) __glibcxx_assert(expression); \
_CCCL_NV_DIAG_DEFAULT(3060)
#else // ^^^ libstdc++ ^^^ / vvv Unknown standard library vvv
# error "Unknown host standard library used."
#endif // Unknown standard library

//! Use internal nvcc implementation on device or the host library for other cuda compilers
//! _CCCL_ASSERT_IMPL_DEVICE should never be used directly
#if defined(_CCCL_CUDA_COMPILER_NVCC) //! Use __assert_fail to implement device side asserts
# include <assert.h>
# define _CCCL_ASSERT_IMPL_DEVICE(expression, message) \
__builtin_expect(static_cast<bool>(expression), 1) ? (void) 0 : __assert_fail(message, __FILE__, __LINE__, __func__)
#elif defined(_CCCL_CUDA_COMPILER)
# define _CCCL_ASSERT_IMPL_DEVICE(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message)
#else // ^^^ _CCCL_CUDA_COMPILER ^^^ / vvv !_CCCL_CUDA_COMPILER vvv
# define _CCCL_ASSERT_IMPL_DEVICE(expression, message) ((void) 0)
#endif // !_CCCL_CUDA_COMPILER

//! _CCCL_ASSERT_HOST is enabled conditionally depending on CCCL_ENABLE_HOST_ASSERTIONS
#ifdef CCCL_ENABLE_HOST_ASSERTIONS
# define _CCCL_ASSERT_HOST(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message)
#else // ^^^ CCCL_ENABLE_HOST_ASSERTIONS ^^^ / vvv !CCCL_ENABLE_HOST_ASSERTIONS vvv
# define _CCCL_ASSERT_HOST(expression, message) ((void) 0)
#endif // !CCCL_ENABLE_HOST_ASSERTIONS

//! Use internal nvcc implementation on device or the host library for other cuda compilers
//! _CCCL_ASSERT_DEVICE is enabled conditionally depending on CCCL_ENABLE_DEVICE_ASSERTIONS
#ifdef CCCL_ENABLE_DEVICE_ASSERTIONS
# if defined(_CCCL_CUDA_COMPILER_NVCC) //! Use __assert_fail to implement device side asserts
# include <assert.h>
# define _CCCL_ASSERT_DEVICE(expression, message) \
__builtin_expect(static_cast<bool>(expression), 1) \
? (void) 0 \
: __assert_fail(message, __FILE__, __LINE__, __func__)
# elif defined(_CCCL_CUDA_COMPILER)
# define _CCCL_ASSERT_DEVICE(expression, message) _CCCL_ASSERT_HOST(expression, message)
# endif // _CCCL_CUDA_COMPILER
# define _CCCL_ASSERT_DEVICE(expression, message) _CCCL_ASSERT_IMPL_DEVICE(expression, message)
#else // ^^^ CCCL_ENABLE_DEVICE_ASSERTIONS ^^^ / vvv !CCCL_ENABLE_DEVICE_ASSERTIONS vvv
# define _CCCL_ASSERT_DEVICE(expression, message) ((void) 0)
#endif // !CCCL_ENABLE_DEVICE_ASSERTIONS

//! _CCCL_VERIFY is an unconditionally enabled assertion that is reserved for the most important checks
#if defined(_CCCL_CUDA_COMPILER_NVHPC) // NVHPC needs to use NV_IF_TARGET
//! _CCCL_VERIFY is enabled unconditionally and reserved for critical checks that are required to always be on
//! _CCCL_ASSERT is enabled conditionally depending on CCCL_ENABLE_HOST_ASSERTIONS and CCCL_ENABLE_DEVICE_ASSERTIONS
#if defined(_CCCL_CUDA_COMPILER_NVHPC) // NVHPC needs to use NV_IF_TARGET instead of __CUDA_ARCH__
# define _CCCL_VERIFY(expression, message) \
NV_IF_ELSE_TARGET( \
NV_IS_DEVICE, (_CCCL_ASSERT_IMPL_DEVICE(expression, message);), (_CCCL_ASSERT_IMPL_HOST(expression, message);))
# define _CCCL_ASSERT(expression, message) \
NV_IF_ELSE_TARGET( \
NV_IS_DEVICE, (_CCCL_ASSERT_DEVICE(expression, message);), (_CCCL_ASSERT_HOST(expression, message);))
#else // ^^^ _CCCL_CUDA_COMPILER_NVHPC ^^^ / vvv !_CCCL_CUDA_COMPILER_NVHPC vvv
#elif defined(_CCCL_CUDA_COMPILER)
# ifdef __CUDA_ARCH__
# define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_DEVICE(expression, message)
# define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_DEVICE(expression, message)
# define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_DEVICE(expression, message)
# else // ^^^ __CUDA_ARCH__ ^^^ / vvv !__CUDA_ARCH__ vvv
# define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_HOST(expression, message)
# define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message)
# define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_HOST(expression, message)
# endif // !__CUDA_ARCH__
#endif // !_CCCL_CUDA_COMPILER_NVHPC

#if defined(CCCL_ENABLE_HOST_ASSERTIONS) || defined(CCCL_ENABLE_DEVICE_ASSERTIONS)
# define _CCCL_ASSERT(expression, message) _CCCL_VERIFY(expression, message)
#else // ^^^ CCCL_ENABLE_HOST_ASSERTIONS || CCCL_ENABLE_DEVICE_ASSERTIONS ^^^ / vvv No assertions vvv
# define _CCCL_ASSERT(expression, message) ((void) 0)
#endif // No assertions
#else // ^^^ _CCCL_CUDA_COMPILER ^^^ / vvv !_CCCL_CUDA_COMPILER vvv
# define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message)
# define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_HOST(expression, message)
#endif // !_CCCL_CUDA_COMPILER

#endif // __CCCL_ASSERT_H

0 comments on commit f4feb1a

Please sign in to comment.