Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a more efficient enable_if_t implementation #3071

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libcudacxx/include/cuda/std/__mdspan/layout_stride.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ struct layout_stride
// nvcc cannot deduce this constructor when using _CCCL_REQUIRES
template <
class _IntegralTypes,
enable_if_t<_CCCL_TRAIT(is_convertible, const remove_const_t<_IntegralTypes>&, index_type), int> = 0,
enable_if_t<_CCCL_TRAIT(is_nothrow_constructible, index_type, const remove_const_t<_IntegralTypes>&), int> = 0>
typename enable_if<_CCCL_TRAIT(is_convertible, const remove_const_t<_IntegralTypes>&, index_type), int>::type = 0,
typename enable_if<_CCCL_TRAIT(is_nothrow_constructible, index_type, const remove_const_t<_IntegralTypes>&),
int>::type = 0>
Comment on lines +301 to +303
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Why isn't this using enable_if_t? What bug is worked around here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah layout_stride cannot deduce its arguments if we use the enable_if_t 🤷

That said, we seem to have more issues like this

Copy link
Contributor

@bernhardmgruber bernhardmgruber Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad. Consider adding a comment for those cases, so nobody is tempted to rewrite a enable_if<...>::type into enable_if_t and needs a whole CI run to find out it's a bug.

_LIBCUDACXX_HIDE_FROM_ABI constexpr mapping(
extents_type const& __e, _CUDA_VSTD::array<_IntegralTypes, extents_type::rank()> const& __s) noexcept
# ifndef _CCCL_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
Expand Down Expand Up @@ -327,8 +328,9 @@ struct layout_stride
// nvcc cannot deduce this constructor when using _CCCL_REQUIRES
template <
class _IntegralTypes,
enable_if_t<_CCCL_TRAIT(is_convertible, const remove_const_t<_IntegralTypes>&, index_type), int> = 0,
enable_if_t<_CCCL_TRAIT(is_nothrow_constructible, index_type, const remove_const_t<_IntegralTypes>&), int> = 0>
typename enable_if<_CCCL_TRAIT(is_convertible, const remove_const_t<_IntegralTypes>&, index_type), int>::type = 0,
typename enable_if<_CCCL_TRAIT(is_nothrow_constructible, index_type, const remove_const_t<_IntegralTypes>&),
int>::type = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr mapping(
extents_type const& __e, _CUDA_VSTD::span<_IntegralTypes, extents_type::rank()> const& __s) noexcept
# ifndef _CCCL_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
Expand Down
23 changes: 22 additions & 1 deletion libcudacxx/include/cuda/std/__type_traits/enable_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
// SPDX-FileCopyrightText: Copyright (c) 2023-24 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

Expand Down Expand Up @@ -31,9 +31,30 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT enable_if<true, _Tp>
typedef _Tp type;
};

#if _CCCL_COMPILER(NVHPC) || _CCCL_COMPILER(ICC) || _CCCL_COMPILER(GCC, <, 8)

template <bool _Bp, class _Tp = void>
using enable_if_t _CCCL_NODEBUG_ALIAS = typename enable_if<_Bp, _Tp>::type;

#else // ^^^ standard implementation ^^^ / vvv optimized implementation

// Optimized enable_if_t implementation that does not instantiate a type every time
template <bool>
struct __enable_if_t_impl
{};

template <>
struct __enable_if_t_impl<true>
{
template <class _Tp>
using type = _Tp;
};

template <bool _Bp, class _Tp = void>
using enable_if_t _CCCL_NODEBUG_ALIAS = typename __enable_if_t_impl<_Bp>::template type<_Tp>;

#endif // !_CCCL_COMPILER(NVHPC) && !_CCCL_COMPILER(ICC) && !_CCCL_COMPILER(GCC, <, 8)

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___TYPE_TRAITS_ENABLE_IF_H
Loading