Skip to content

Commit ea1949d

Browse files
committed
[CUDA] add wrapper header for libc++'s __utlility/declval.h
Since llvm#116709 more libc++ code relies on std::declval() and it broke some CUDA compilations. The new wrapper adds GPU-side overloads for the declval() helper functions which allows it to continue working when used from CUDA sources.
1 parent 09f7cab commit ea1949d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

clang/lib/Headers/CMakeLists.txt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ set(cuda_wrapper_bits_files
347347
cuda_wrappers/bits/basic_string.tcc
348348
)
349349

350+
set(cuda_wrapper_utility_files
351+
cuda_wrappers/__utility/declval.h
352+
)
353+
350354
set(ppc_wrapper_files
351355
ppc_wrappers/mmintrin.h
352356
ppc_wrappers/xmmintrin.h
@@ -443,8 +447,9 @@ endfunction(clang_generate_header)
443447

444448
# Copy header files from the source directory to the build directory
445449
foreach( f ${files} ${cuda_wrapper_files} ${cuda_wrapper_bits_files}
446-
${ppc_wrapper_files} ${openmp_wrapper_files} ${zos_wrapper_files} ${hlsl_files}
447-
${llvm_libc_wrapper_files} ${llvm_offload_wrapper_files})
450+
${cuda_wrapper_utility_files} ${ppc_wrapper_files} ${openmp_wrapper_files}
451+
${zos_wrapper_files} ${hlsl_files} ${llvm_libc_wrapper_files}
452+
${llvm_offload_wrapper_files})
448453
copy_header_to_output_dir(${CMAKE_CURRENT_SOURCE_DIR} ${f})
449454
endforeach( f )
450455

@@ -553,7 +558,7 @@ add_header_target("arm-common-resource-headers" "${arm_common_files};${arm_commo
553558
# Architecture/platform specific targets
554559
add_header_target("arm-resource-headers" "${arm_only_files};${arm_only_generated_files}")
555560
add_header_target("aarch64-resource-headers" "${aarch64_only_files};${aarch64_only_generated_files}")
556-
add_header_target("cuda-resource-headers" "${cuda_files};${cuda_wrapper_files};${cuda_wrapper_bits_files}")
561+
add_header_target("cuda-resource-headers" "${cuda_files};${cuda_wrapper_files};${cuda_wrapper_bits_files};${cuda_wrapper_utility_files}")
557562
add_header_target("hexagon-resource-headers" "${hexagon_files}")
558563
add_header_target("hip-resource-headers" "${hip_files}")
559564
add_header_target("loongarch-resource-headers" "${loongarch_files}")
@@ -600,6 +605,11 @@ install(
600605
DESTINATION ${header_install_dir}/cuda_wrappers/bits
601606
COMPONENT clang-resource-headers)
602607

608+
install(
609+
FILES ${cuda_wrapper_utility_files}
610+
DESTINATION ${header_install_dir}/cuda_wrappers/__utility
611+
COMPONENT clang-resource-headers)
612+
603613
install(
604614
FILES ${ppc_wrapper_files}
605615
DESTINATION ${header_install_dir}/ppc_wrappers
@@ -663,6 +673,12 @@ install(
663673
EXCLUDE_FROM_ALL
664674
COMPONENT cuda-resource-headers)
665675

676+
install(
677+
FILES ${cuda_wrapper_utility_files}
678+
DESTINATION ${header_install_dir}/cuda_wrappers/__utility
679+
EXCLUDE_FROM_ALL
680+
COMPONENT cuda-resource-headers)
681+
666682
install(
667683
FILES ${cuda_files}
668684
DESTINATION ${header_install_dir}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __CUDA_WRAPPERS_UTILITY_DECLVAL_H__
2+
#define __CUDA_WRAPPERS_UTILITY_DECLVAL_H__
3+
4+
#include_next <__utility/declval.h>
5+
6+
// The stuff below is the exact copy of the <__utility/declval.h>,
7+
// but with __device__ attribute applied to the functions, so it works on a GPU.
8+
9+
_LIBCPP_BEGIN_NAMESPACE_STD
10+
11+
// Suppress deprecation notice for volatile-qualified return type resulting
12+
// from volatile-qualified types _Tp.
13+
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
14+
template <class _Tp>
15+
__attribute__((device))
16+
_Tp&& __declval(int);
17+
template <class _Tp>
18+
__attribute__((device))
19+
_Tp __declval(long);
20+
_LIBCPP_SUPPRESS_DEPRECATED_POP
21+
22+
template <class _Tp>
23+
__attribute__((device))
24+
_LIBCPP_HIDE_FROM_ABI decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT {
25+
static_assert(!__is_same(_Tp, _Tp),
26+
"std::declval can only be used in an unevaluated context. "
27+
"It's likely that your current usage is trying to extract a value from the function.");
28+
}
29+
30+
_LIBCPP_END_NAMESPACE_STD
31+
#endif // __CUDA_WRAPPERS_UTILITY_DECLVAL_H__

0 commit comments

Comments
 (0)