Skip to content

Commit

Permalink
Try to work better with nvhpc
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Sep 16, 2024
1 parent 807f633 commit 8e3ed7e
Showing 1 changed file with 40 additions and 47 deletions.
87 changes: 40 additions & 47 deletions libcudacxx/include/cuda/std/__cccl/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <cuda/std/__cccl/diagnostic.h>

#include <nv/target>

#if defined(_DEBUG) || defined(DEBUG)
# ifndef _CCCL_ENABLE_DEBUG_MODE
# define _CCCL_ENABLE_DEBUG_MODE
Expand Down Expand Up @@ -51,60 +53,51 @@
# endif // CCCL_ENABLE_ASSERTIONS
#endif // !CCCL_ENABLE_DEVICE_ASSERTIONS

//! Use internal nvcc implementation on device or the host library for clang-cuda
#ifdef __CUDA_ARCH__
# 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_IMPL(expression, message) \
__builtin_expect(static_cast<bool>(expression), 1) \
? (void) 0 \
: __assert_fail(message, __FILE__, __LINE__, __func__)
# else // ^^^ _CCCL_CUDA_COMPILER_NVCC ^^^ / vvv !_CCCL_CUDA_COMPILER_NVCC vvv
# if __has_include(<__assert>) // libc++ uses _LIBCPP_ASSERT from <__assert>
# include <__assert>
# define _CCCL_ASSERT_IMPL(expression, message) _LIBCPP_ASSERT(expression, message)
# else // 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_IMPL(expression, message) __glibcxx_assert(expression)
# endif // libstdc++
# endif // !_CCCL_CUDA_COMPILER_NVCC
# else // ^^^ CCCL_ENABLE_DEVICE_ASSERTIONS ^^^ / vvv !CCCL_ENABLE_DEVICE_ASSERTIONS vvv
# define _CCCL_ASSERT_IMPL
# endif // !CCCL_ENABLE_DEVICE_ASSERTIONS
#endif // __CUDA_ARCH__

//! Use the different standard library implementations to implement host side asserts
#ifndef __CUDA_ARCH__
# 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_IMPL(expression, message) _STL_VERIFY(expression, message)
# elif __has_include(<__assert>) // libc++ uses _LIBCPP_ASSERT from <__assert>
# include <__assert>
# define _CCCL_ASSERT_IMPL(expression, message) _LIBCPP_ASSERT(expression, message)
# else // libstdc++ uses __glibcxx_assert from <bits/c++config.h
# include <bits/c++config.h>
# if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 12
#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
// 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_IMPL(expression, message) __glibcxx_assert(expression)
# endif // libstdc++
# endif // !CCCL_ENABLE_HOST_ASSERTIONS
#endif // !__CUDA_ARCH__
# endif // _GLIBCXX_RELEASE >= 12
# define _CCCL_ASSERT_HOST(expression, message) __glibcxx_assert(expression)
# else // ^^^ libstdc++ ^^^ / vvv Unknown standard library vvv
# error "Unknown host standard library used."
# endif // Unknown standard library
#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
#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
#else // ^^^ CCCL_ENABLE_DEVICE_ASSERTIONS ^^^ / vvv !CCCL_ENABLE_DEVICE_ASSERTIONS vvv
# define _CCCL_ASSERT_DEVICE(expression, message) ((void) 0)
#endif // !CCCL_ENABLE_DEVICE_ASSERTIONS

//! Use the right assert in the right situation
#define _CCCL_ASSERT_IMPL(expression, message) \
NV_IF_ELSE_TARGET( \
NV_IS_DEVICE, (_CCCL_ASSERT_DEVICE(expression, message);), (_CCCL_ASSERT_HOST(expression, message);))

//! _CCCL_VERIFY is an unconditionally enabled assertion that is reserved for the most important checks
//! Some compilers warn about `is_constant_evaluated()` in a non constexpr function, so silence that right away
Expand Down

0 comments on commit 8e3ed7e

Please sign in to comment.