Skip to content

Commit fd0595d

Browse files
bernhardmgrubermiscco
authored andcommitted
Fix proclaim_copyable_arguments for lambdas (NVIDIA#2833)
Co-authored-by: Michael Schellenberger Costa <[email protected]>
1 parent e5268b2 commit fd0595d

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

cudax/cmake/cudaxBuildCompilerTargets.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function(cudax_build_compiler_targets)
4747
if("Clang" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
4848
# stf heavily uses host device lambdas which break on clang due to a warning about the implicitly
4949
# deleted copy constructor
50+
# TODO(bgruber): remove this when NVBug 4980157 is resolved
5051
append_option_if_available("-Wno-deprecated-copy" cxx_compile_options)
5152
endif()
5253

libcudacxx/include/cuda/__functional/address_stability.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ _CCCL_INLINE_VAR constexpr bool proclaims_copyable_arguments_v = proclaims_copya
4848
template <typename F>
4949
struct __callable_permitting_copied_arguments : F
5050
{
51+
#if _CCCL_STD_VER <= 2014
52+
template <typename G>
53+
_LIBCUDACXX_HIDE_FROM_ABI constexpr __callable_permitting_copied_arguments(G&& g)
54+
: F(::cuda::std::forward<G>(g))
55+
{}
56+
#endif // _CCCL_STD_VER <= 2014
57+
5158
using F::operator();
5259
};
5360

@@ -61,9 +68,9 @@ struct proclaims_copyable_arguments<__callable_permitting_copied_arguments<F>> :
6168
//! @see proclaims_copyable_arguments
6269
template <typename F>
6370
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr auto
64-
proclaim_copyable_arguments(F f) -> __callable_permitting_copied_arguments<F>
71+
proclaim_copyable_arguments(F&& f) -> __callable_permitting_copied_arguments<::cuda::std::decay_t<F>>
6572
{
66-
return __callable_permitting_copied_arguments<F>{_CUDA_VSTD::move(f)};
73+
return {::cuda::std::forward<F>(f)};
6774
}
6875

6976
// Specializations for libcu++ function objects are provided here to not pull this include into `<cuda/std/...>` headers
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
target_compile_options(${test_target} PRIVATE $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>: --extended-lambda>)
2+
3+
# this check is actually not correct, because we must check the host compiler, not the CXX compiler.
4+
# We rely on that those are usually the same ;)
5+
if ("Clang" STREQUAL "${CMAKE_CXX_COMPILER_ID}" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13)
6+
# When clang >= 13 is used as host compiler, we get the following warning:
7+
# nvcc_internal_extended_lambda_implementation:312:22: error: definition of implicit copy constructor for '__nv_hdl_wrapper_t<false, true, false, __nv_dl_tag<void (*)(), &TestAddressStabilityLambda, 2>, int (const int &)>' is deprecated because it has a user-declared copy assignment operator [-Werror,-Wdeprecated-copy]
8+
# 312 | __nv_hdl_wrapper_t & operator=(const __nv_hdl_wrapper_t &in) = delete;
9+
# | ^
10+
# Let's suppress it until NVBug 4980157 is resolved.
11+
target_compile_options(${test_target} PRIVATE $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>: -Wno-deprecated-copy>)
12+
endif ()

thrust/testing/address_stability.cu

+37
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,40 @@ void TestAddressStabilityUserDefinedFunctionObject()
8383
static_assert(proclaims_copyable_arguments<decltype(proclaim_copyable_arguments(my_plus<const int&&>{}))>::value, "");
8484
}
8585
DECLARE_UNITTEST(TestAddressStabilityUserDefinedFunctionObject);
86+
87+
void TestAddressStabilityLambda()
88+
{
89+
using ::cuda::proclaim_copyable_arguments;
90+
using ::cuda::proclaims_copyable_arguments;
91+
92+
{
93+
auto l = [](const int& i) {
94+
return i + 2;
95+
};
96+
static_assert(!proclaims_copyable_arguments<decltype(l)>::value, "");
97+
auto pr_l = proclaim_copyable_arguments(l);
98+
ASSERT_EQUAL(pr_l(3), 5);
99+
static_assert(proclaims_copyable_arguments<decltype(pr_l)>::value, "");
100+
}
101+
102+
{
103+
auto l = [] _CCCL_DEVICE(const int& i) {
104+
return i + 2;
105+
};
106+
static_assert(!proclaims_copyable_arguments<decltype(l)>::value, "");
107+
auto pr_device_l = proclaim_copyable_arguments(l);
108+
(void) &pr_device_l;
109+
static_assert(proclaims_copyable_arguments<decltype(pr_device_l)>::value, "");
110+
}
111+
112+
{
113+
auto l = [] _CCCL_HOST_DEVICE(const int& i) {
114+
return i + 2;
115+
};
116+
static_assert(!proclaims_copyable_arguments<decltype(l)>::value, "");
117+
auto pr_l = proclaim_copyable_arguments(l);
118+
ASSERT_EQUAL(pr_l(3), 5);
119+
static_assert(proclaims_copyable_arguments<decltype(pr_l)>::value, "");
120+
}
121+
}
122+
DECLARE_UNITTEST(TestAddressStabilityLambda);

0 commit comments

Comments
 (0)