Skip to content

Commit

Permalink
Backport is_constant_evaluated
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Nov 25, 2024
1 parent 5bc7448 commit 0be3f3c
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 44 deletions.
5 changes: 3 additions & 2 deletions libcudacxx/include/cuda/std/__algorithm/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX14 bool __dispatch_memmove(_Up* __r
return false;
#endif

if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return false;
}
Expand Down Expand Up @@ -114,7 +114,8 @@ _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX14 pair<_Tp*, _Up*> __copy(_Tp* __f
{
return {__last, __result + __n};
}
if ((!__cccl_is_constant_evaluated() && __first < __result) || __constexpr_tail_overlap(__first, __result, __last))
if ((!_CUDA_VSTD::is_constant_evaluated() && __first < __result)
|| __constexpr_tail_overlap(__first, __result, __last))
{
for (ptrdiff_t __i = __n; __i > 0; --__i)
{
Expand Down
2 changes: 1 addition & 1 deletion libcudacxx/include/cuda/std/__bit/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ struct __bit_array
_LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX14 explicit __bit_array(difference_type __s)
: __size_(__s)
{
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
{
Expand Down
8 changes: 4 additions & 4 deletions libcudacxx/include/cuda/std/__memory/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT allocator : private __non_trivial_if<!_CCCL_
__throw_bad_array_new_length();
}
#if defined(_CCCL_HAS_CONSTEXPR_ALLOCATION)
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return ::std::allocator<_Tp>{}.allocate(__n);
}
Expand All @@ -150,7 +150,7 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT allocator : private __non_trivial_if<!_CCCL_
_LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20_ALLOCATION void deallocate(_Tp* __p, size_t __n) noexcept
{
#if defined(_CCCL_HAS_CONSTEXPR_ALLOCATION)
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return ::std::allocator<_Tp>{}.deallocate(__p, __n);
}
Expand Down Expand Up @@ -231,7 +231,7 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT allocator<const _Tp>
{
__throw_bad_array_new_length();
}
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
}
Expand All @@ -250,7 +250,7 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT allocator<const _Tp>

_LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 void deallocate(const _Tp* __p, size_t __n) noexcept
{
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
::operator delete(const_cast<_Tp*>(__p));
}
Expand Down
8 changes: 4 additions & 4 deletions libcudacxx/include/cuda/std/__memory/construct_at.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ construct_at(_Tp* __location, _Args&&... __args)
{
_CCCL_ASSERT(__location != nullptr, "null pointer given to construct_at");
// Need to go through `std::construct_at` as that is the explicitly blessed function
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return ::std::construct_at(__location, _CUDA_VSTD::forward<_Args>(__args)...);
}
Expand All @@ -131,7 +131,7 @@ construct_at(_Tp* __location, _Args&&... __args)
{
_CCCL_ASSERT(__location != nullptr, "null pointer given to construct_at");
// Need to go through `std::construct_at` as that is the explicitly blessed function
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return ::std::construct_at(__location, _CUDA_VSTD::forward<_Args>(__args)...);
}
Expand All @@ -150,7 +150,7 @@ __construct_at(_Tp* __location, _Args&&... __args)
_CCCL_ASSERT(__location != nullptr, "null pointer given to construct_at");
#if _CCCL_STD_VER >= 2020
// Need to go through `std::construct_at` as that is the explicitly blessed function
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return ::std::construct_at(__location, _CUDA_VSTD::forward<_Args>(__args)...);
}
Expand All @@ -167,7 +167,7 @@ __construct_at(_Tp* __location, _Args&&... __args)
_CCCL_ASSERT(__location != nullptr, "null pointer given to construct_at");
#if _CCCL_STD_VER >= 2020
// Need to go through `std::construct_at` as that is the explicitly blessed function
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
return ::std::construct_at(__location, _CUDA_VSTD::forward<_Args>(__args)...);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ template <
_LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 _Out*
__uninitialized_allocator_copy_impl(_Alloc&, _In* __first1, _In* __last1, _Out* __first2)
{
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
while (__first1 != __last1)
{
Expand Down Expand Up @@ -650,7 +650,7 @@ template <class _Alloc,
_LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 _Iter2
__uninitialized_allocator_move_if_noexcept(_Alloc&, _Iter1 __first1, _Iter1 __last1, _Iter2 __first2)
{
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
while (__first1 != __last1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ _LIBCUDACXX_HIDE_FROM_ABI constexpr bool is_constant_evaluated() noexcept
{
return _CCCL_BUILTIN_IS_CONSTANT_EVALUATED();
}

_LIBCUDACXX_HIDE_FROM_ABI constexpr bool __cccl_is_constant_evaluated() noexcept
{
return _CCCL_BUILTIN_IS_CONSTANT_EVALUATED();
}
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool __cccl_default_is_constant_evaluated() noexcept
{
return _CCCL_BUILTIN_IS_CONSTANT_EVALUATED();
}
#else // ^^^ _CCCL_BUILTIN_IS_CONSTANT_EVALUATED ^^^ / vvv !_CCCL_BUILTIN_IS_CONSTANT_EVALUATED vvv
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool __cccl_is_constant_evaluated() noexcept
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool is_constant_evaluated() noexcept
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion libcudacxx/include/cuda/std/detail/libcxx/include/__string
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT char_traits<char>
{
#if _CCCL_COMPILER(GCC, <, 13)
// absurd workaround for GCC "internal compiler error: in cxx_eval_array_reference"
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
;
#endif
#if defined(_CCCL_BUILTIN_STRLEN)
Expand Down
4 changes: 2 additions & 2 deletions libcudacxx/include/cuda/std/detail/libcxx/include/complex
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)

#if _CCCL_STD_VER > 2011 && defined(_CCCL_BUILTIN_IS_CONSTANT_EVALUATED)
// Avoid floating point operations that are invalid during constant evaluation
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
Expand Down Expand Up @@ -652,7 +652,7 @@ operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)

#if _CCCL_STD_VER > 2011 && defined(_CCCL_BUILTIN_IS_CONSTANT_EVALUATED)
// Avoid floating point operations that are invalid during constant evaluation
if (__cccl_is_constant_evaluated())
if (_CUDA_VSTD::is_constant_evaluated())
{
bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// <cuda/std/type_traits>

// __cccl_is_constant_evaluated()
// _CUDA_VSTD::is_constant_evaluated()

// returns false when there's no constant evaluation support from the compiler.
// as well as when called not in a constexpr context
Expand All @@ -21,14 +21,14 @@

int main(int, char**)
{
ASSERT_SAME_TYPE(decltype(cuda::std::__cccl_is_constant_evaluated()), bool);
ASSERT_NOEXCEPT(cuda::std::__cccl_is_constant_evaluated());
ASSERT_SAME_TYPE(decltype(cuda::std::is_constant_evaluated()), bool);
ASSERT_NOEXCEPT(cuda::std::is_constant_evaluated());

#if defined(_CCCL_BUILTIN_IS_CONSTANT_EVALUATED)
static_assert(cuda::std::__cccl_is_constant_evaluated(), "");
static_assert(cuda::std::is_constant_evaluated(), "");
#endif

bool p = cuda::std::__cccl_is_constant_evaluated();
bool p = cuda::std::is_constant_evaluated();
assert(!p);

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ __host__ __device__ TEST_CONSTEXPR_CXX14 bool test()
assert(j == 75);

test<int, random_access_iterator<int*>, random_access_iterator<int*>>();
if (!cuda::std::__cccl_is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
if (!cuda::std::is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
{
test<int, random_access_iterator<int*>, int*>();
test<int, int*, random_access_iterator<int*>>();
test<int, int*, int*>();
}

test<MoveOnly, random_access_iterator<int*>, random_access_iterator<MoveOnly*>>();
if (!cuda::std::__cccl_is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
if (!cuda::std::is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
{
test<MoveOnly, random_access_iterator<int*>, MoveOnly*>();
test<MoveOnly, int*, random_access_iterator<MoveOnly*>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ __host__ __device__ TEST_CONSTEXPR_CXX14 bool test()
assert(j == 75);

test<int, random_access_iterator<int*>, random_access_iterator<int*>>();
if (!cuda::std::__cccl_is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
if (!cuda::std::is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
{
test<int, random_access_iterator<int*>, int*>();
test<int, int*, random_access_iterator<int*>>();
test<int, int*, int*>();
}

test<MoveOnly, random_access_iterator<int*>, random_access_iterator<MoveOnly*>>();
if (!cuda::std::__cccl_is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
if (!cuda::std::is_constant_evaluated()) // This breaks some compilers due to excessive constant folding
{
test<MoveOnly, random_access_iterator<int*>, MoveOnly*>();
test<MoveOnly, int*, random_access_iterator<MoveOnly*>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ __host__ __device__ constexpr void test()
{
test_default<T>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test_copy_move<T>();
test_size<T>();
Expand All @@ -308,7 +308,7 @@ __host__ __device__ constexpr bool test()
test<ThrowingMoveAssignment>();

// Due to reinterpret_cast within the destructor a on trivially destructible type cannot be constexpr at all
if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivialDestructor>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ __host__ __device__ constexpr bool test()
test<int>();
test<Trivial>();

if (!cuda::std::__cccl_is_constant_evaluated())
if (!cuda::std::is_constant_evaluated())
{
test<NonTrivial>();
test<NonTrivialDestructor>();
Expand Down
2 changes: 1 addition & 1 deletion libcudacxx/test/support/test_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@

#if TEST_HAS_BUILTIN(__builtin_is_constant_evaluated) || _CCCL_COMPILER(GCC, >=, 9) \
|| (_CCCL_COMPILER(MSVC) && _MSC_VER > 1924 && _CCCL_CUDACC_AT_LEAST(11, 3))
# define TEST_IS_CONSTANT_EVALUATED() _CUDA_VSTD::__cccl_is_constant_evaluated()
# define TEST_IS_CONSTANT_EVALUATED() cuda::std::is_constant_evaluated()
#else
# define TEST_IS_CONSTANT_EVALUATED() false
#endif
Expand Down

0 comments on commit 0be3f3c

Please sign in to comment.