Skip to content

Commit 081bb70

Browse files
committed
Add host standard library detection
1 parent ebc2dd8 commit 081bb70

File tree

19 files changed

+200
-65
lines changed

19 files changed

+200
-65
lines changed

libcudacxx/include/cuda/__cccl_config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cuda/std/__cccl/exceptions.h> // IWYU pragma: export
2424
#include <cuda/std/__cccl/execution_space.h> // IWYU pragma: export
2525
#include <cuda/std/__cccl/extended_data_types.h> // IWYU pragma: export
26+
#include <cuda/std/__cccl/host_std_lib.h> // IWYU pragma: export
2627
#include <cuda/std/__cccl/os.h> // IWYU pragma: export
2728
#include <cuda/std/__cccl/preprocessor.h> // IWYU pragma: export
2829
#include <cuda/std/__cccl/ptx_isa.h> // IWYU pragma: export

libcudacxx/include/cuda/__complex/get_real_imag.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
#include <cuda/__fwd/complex.h>
2525
#include <cuda/std/__fwd/complex.h>
2626

27-
#if !_CCCL_COMPILER(NVRTC)
28-
# include <complex>
29-
#endif // !_CCCL_COMPILER(NVRTC)
30-
3127
#include <cuda/std/__cccl/prologue.h>
3228

3329
_CCCL_BEGIN_NAMESPACE_CUDA

libcudacxx/include/cuda/std/__cccl/builtin.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <cuda/std/__cccl/attributes.h>
2727
#include <cuda/std/__cccl/extended_data_types.h>
28+
#include <cuda/std/__cccl/host_std_lib.h>
2829

2930
//! This file consolidates all compiler builtin detection for CCCL.
3031
//!
@@ -609,25 +610,20 @@
609610

610611
// Some compilers provide std::move/std::forward/etc as builtins
611612
#if defined(__cplusplus)
612-
// Bring in the feature test macros (needed for std::forward_like)
613-
# if _CCCL_HAS_INCLUDE(<version>) // <version> should be the smallest include possible
614-
# include <version>
615-
# elif !_CCCL_COMPILER(NVRTC)
616-
# include <ciso646> // otherwise go for the smallest possible header
617-
# endif // !_CCCL_COMPILER(NVRTC)
618-
619613
// Bring in the bits of the STL we need
620-
# if defined(_GLIBCXX_VERSION)
614+
# if _CCCL_HOST_STD_LIB(LIBSTDCXX)
621615
# include <bits/move.h> // for move, forward, forward_like, and addressof
622-
# elif defined(_LIBCPP_VERSION)
616+
# elif _CCCL_HOST_STD_LIB(LIBCXX)
623617
# include <__memory/addressof.h>
624618
# include <__utility/as_const.h>
625619
# include <__utility/forward.h>
626-
# include <__utility/forward_like.h>
620+
# if __cpp_lib_forward_like >= 202217L
621+
# include <__utility/forward_like.h>
622+
# endif // __cpp_lib_forward_like >= 202217L
627623
# include <__utility/move.h>
628624
# endif
629625

630-
# if defined(_GLIBCXX_VERSION) || defined(_LIBCPP_VERSION)
626+
# if _CCCL_HOST_STD_LIB(LIBSTDCXX) || _CCCL_HOST_STD_LIB(LIBCXX)
631627
// std::move builtin
632628
# if _CCCL_COMPILER(CLANG, >=, 15) || _CCCL_COMPILER(GCC, >=, 15)
633629
# define _CCCL_HAS_BUILTIN_STD_MOVE() 1
@@ -650,11 +646,10 @@
650646

651647
// std::forward_like builtin
652648
// Leaving out MSVC for now because it is hard for forward-declare std::forward_like.
653-
# if (_CCCL_COMPILER(CLANG, >=, 17) || _CCCL_COMPILER(GCC, >=, 15)) && defined(__cpp_lib_forward_like) \
654-
&& (__cpp_lib_forward_like >= 202217L)
649+
# if (_CCCL_COMPILER(CLANG, >=, 17) || _CCCL_COMPILER(GCC, >=, 15)) && __cpp_lib_forward_like >= 202217L
655650
# define _CCCL_HAS_BUILTIN_STD_FORWARD_LIKE() 1
656651
# endif
657-
# endif // defined(_GLIBCXX_VERSION) || defined(_LIBCPP_VERSION) || defined(_MSVC_STL_VERSION)
652+
# endif // _CCCL_HOST_STD_LIB(LIBSTDCXX) || _CCCL_HOST_STD_LIB(LIBCXX)
658653
#endif // defined(__cplusplus)
659654

660655
#ifndef _CCCL_HAS_BUILTIN_STD_MOVE
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of libcu++, the C++ Standard Library for your entire system,
4+
// under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef __CCCL_HOST_STD_LIB_H
12+
#define __CCCL_HOST_STD_LIB_H
13+
14+
#include <cuda/std/__cccl/compiler.h>
15+
#include <cuda/std/__cccl/preprocessor.h>
16+
#include <cuda/std/__cccl/system_header.h>
17+
18+
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
19+
# pragma GCC system_header
20+
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
21+
# pragma clang system_header
22+
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
23+
# pragma system_header
24+
#endif // no system header
25+
26+
#define _CCCL_HOST_STD_LIB_LIBSTDCXX() 0
27+
#define _CCCL_HOST_STD_LIB_LIBCXX() 0
28+
#define _CCCL_HOST_STD_LIB_STL() 0
29+
30+
// include a minimal header
31+
#if _CCCL_HAS_INCLUDE(<version>)
32+
# include <version>
33+
#elif _CCCL_HAS_INCLUDE(<ciso646>)
34+
# include <ciso646>
35+
#endif // ^^^ _CCCL_HAS_INCLUDE(<ciso646>) ^^^
36+
37+
#if defined(_MSVC_STL_VERSION)
38+
# undef _CCCL_HOST_STD_LIB_STL
39+
# define _CCCL_HOST_STD_LIB_STL() 1
40+
#elif defined(__GLIBCXX__)
41+
# undef _CCCL_HOST_STD_LIB_LIBSTDCXX
42+
# define _CCCL_HOST_STD_LIB_LIBSTDCXX() 1
43+
#elif defined(_LIBCPP_VERSION)
44+
# undef _CCCL_HOST_STD_LIB_LIBCXX
45+
# define _CCCL_HOST_STD_LIB_LIBCXX() 1
46+
#endif // ^^^ _LIBCPP_VERSION ^^^
47+
48+
#define _CCCL_HOST_STD_LIB(_X) _CCCL_HOST_STD_LIB_##_X()
49+
#define _CCCL_HAS_HOST_STD_LIB() \
50+
(_CCCL_HOST_STD_LIB_LIBSTDCXX() || _CCCL_HOST_STD_LIB_LIBCXX() || _CCCL_HOST_STD_LIB_STL())
51+
52+
#endif // __CCCL_HOST_STD_LIB_H

libcudacxx/include/cuda/std/__complex/complex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <cuda/std/__complex/vector_support.h>
2525
#include <cuda/std/__concepts/concept_macros.h>
26+
#include <cuda/std/__fwd/complex.h>
2627
#include <cuda/std/__fwd/get.h>
2728
#include <cuda/std/__tuple_dir/tuple_element.h>
2829
#include <cuda/std/__tuple_dir/tuple_size.h>
@@ -37,8 +38,7 @@
3738

3839
// Compatibility helpers for thrust to convert between `std::complex` and `cuda::std::complex`
3940
#if !_CCCL_COMPILER(NVRTC)
40-
# include <complex>
41-
# include <sstream> // for std::basic_ostringstream
41+
# include <iosfwd>
4242

4343
# define _LIBCUDACXX_ACCESS_STD_COMPLEX_REAL(__c) reinterpret_cast<const _Up(&)[2]>(__c)[0]
4444
# define _LIBCUDACXX_ACCESS_STD_COMPLEX_IMAG(__c) reinterpret_cast<const _Up(&)[2]>(__c)[1]

libcudacxx/include/cuda/std/__complex/nvbf16.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# include <cuda/std/__type_traits/is_constructible.h>
3333

3434
# if !_CCCL_COMPILER(NVRTC)
35-
# include <sstream> // for std::basic_ostringstream
35+
# include <iosfwd>
3636
# endif // !_CCCL_COMPILER(NVRTC)
3737

3838
# include <cuda/std/__cccl/prologue.h>
@@ -158,7 +158,8 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT _CCCL_ALIGNAS(alignof(__nv_bfloat162)) compl
158158
return *this;
159159
}
160160

161-
_CCCL_HOST operator ::std::complex<value_type>() const
161+
template <class _Up = value_type>
162+
_CCCL_HOST operator ::std::complex<_Up>() const
162163
{
163164
return {__repr_.x, __repr_.y};
164165
}
@@ -295,21 +296,21 @@ struct __get_complex_impl<__nv_bfloat16>
295296
};
296297

297298
# if !_CCCL_COMPILER(NVRTC)
298-
template <class _CharT, class _Traits>
299+
template <class _CharT, class _Traits, class _Tp = float>
299300
::std::basic_istream<_CharT, _Traits>&
300301
operator>>(::std::basic_istream<_CharT, _Traits>& __is, complex<__nv_bfloat16>& __x)
301302
{
302-
::std::complex<float> __temp;
303+
::std::complex<_Tp> __temp;
303304
__is >> __temp;
304305
__x = __temp;
305306
return __is;
306307
}
307308

308-
template <class _CharT, class _Traits>
309+
template <class _CharT, class _Traits, class _Tp = float>
309310
::std::basic_ostream<_CharT, _Traits>&
310311
operator<<(::std::basic_ostream<_CharT, _Traits>& __os, const complex<__nv_bfloat16>& __x)
311312
{
312-
return __os << complex<float>{__x};
313+
return __os << complex<_Tp>{__x};
313314
}
314315
# endif // !_CCCL_COMPILER(NVRTC)
315316

libcudacxx/include/cuda/std/__complex/nvfp16.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# include <cuda/std/__type_traits/is_constructible.h>
3333

3434
# if !_CCCL_COMPILER(NVRTC)
35-
# include <sstream> // for std::basic_ostringstream
35+
# include <iosfwd>
3636
# endif // !_CCCL_COMPILER(NVRTC)
3737

3838
# include <cuda/std/__cccl/prologue.h>
@@ -158,7 +158,8 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT _CCCL_ALIGNAS(alignof(__half2)) complex<__ha
158158
return *this;
159159
}
160160

161-
_CCCL_HOST operator ::std::complex<value_type>() const
161+
template <class _Up = value_type>
162+
_CCCL_HOST operator ::std::complex<_Up>() const
162163
{
163164
return {__repr_.x, __repr_.y};
164165
}
@@ -295,20 +296,20 @@ struct __get_complex_impl<__half>
295296
};
296297

297298
# if !defined(_LIBCUDACXX_HAS_NO_LOCALIZATION) && !_CCCL_COMPILER(NVRTC)
298-
template <class _CharT, class _Traits>
299+
template <class _CharT, class _Traits, class _Tp = float>
299300
::std::basic_istream<_CharT, _Traits>& operator>>(::std::basic_istream<_CharT, _Traits>& __is, complex<__half>& __x)
300301
{
301-
::std::complex<float> __temp;
302+
::std::complex<_Tp> __temp;
302303
__is >> __temp;
303304
__x = __temp;
304305
return __is;
305306
}
306307

307-
template <class _CharT, class _Traits>
308+
template <class _CharT, class _Traits, class _Tp = float>
308309
::std::basic_ostream<_CharT, _Traits>&
309310
operator<<(::std::basic_ostream<_CharT, _Traits>& __os, const complex<__half>& __x)
310311
{
311-
return __os << complex<float>{__x};
312+
return __os << complex<_Tp>{__x};
312313
}
313314
# endif // !_LIBCUDACXX_HAS_NO_LOCALIZATION && !_CCCL_COMPILER(NVRTC)
314315

libcudacxx/include/cuda/std/__fwd/allocator.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT allocator;
3030

3131
_CCCL_END_NAMESPACE_CUDA_STD
3232

33+
#if _CCCL_HAS_HOST_STD_LIB()
34+
35+
_CCCL_BEGIN_NAMESPACE_STD
36+
37+
template <class _Tp>
38+
class allocator;
39+
40+
_CCCL_END_NAMESPACE_STD
41+
42+
#endif // _CCCL_HAS_HOST_STD_LIB()
43+
3344
#include <cuda/std/__cccl/epilogue.h>
3445

3546
#endif // _CUDA_STD___FWD_ALLOCATOR_H

libcudacxx/include/cuda/std/__fwd/char_traits.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ struct char_traits<wchar_t>;
4444

4545
_CCCL_END_NAMESPACE_CUDA_STD
4646

47+
#if _CCCL_HAS_HOST_STD_LIB()
48+
49+
_CCCL_BEGIN_NAMESPACE_STD
50+
51+
template <class _CharT>
52+
struct char_traits;
53+
54+
_CCCL_END_NAMESPACE_STD
55+
56+
#endif // _CCCL_HAS_HOST_STD_LIB()
57+
4758
#include <cuda/std/__cccl/epilogue.h>
4859

4960
#endif // _CUDA_STD___FWD_CHAR_TRAITS_H

libcudacxx/include/cuda/std/__fwd/complex.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
# pragma system_header
2121
#endif // no system header
2222

23-
#if !_CCCL_COMPILER(NVRTC)
24-
# include <complex>
25-
#endif // !_CCCL_COMPILER(NVRTC)
26-
2723
#include <cuda/std/__cccl/prologue.h>
2824

25+
#if _CCCL_HAS_HOST_STD_LIB()
26+
27+
_CCCL_BEGIN_NAMESPACE_STD
28+
29+
template <class>
30+
class complex;
31+
32+
_CCCL_END_NAMESPACE_STD
33+
34+
#endif // _CCCL_HAS_HOST_STD_LIB()
35+
2936
_CCCL_BEGIN_NAMESPACE_CUDA_STD
3037

3138
template <class _Tp>

0 commit comments

Comments
 (0)