Skip to content
Merged
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
24 changes: 22 additions & 2 deletions libcudacxx/include/cuda/__utility/__basic_any/basic_any_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT __basic_any : __basic_any_base<_Interface>
//! @post `__other.has_value() == false` and `has_value()` is `true` if and
//! only if `__other.has_value()` was `true`.
_CCCL_TEMPLATE(class _OtherInterface)
_CCCL_REQUIRES((!::cuda::std::same_as<_OtherInterface, _Interface>)
_CCCL_REQUIRES((!::cuda::std::same_as<_OtherInterface&, _Interface&>)
_CCCL_AND __any_convertible_to<__basic_any<_OtherInterface>, __basic_any>)
_CCCL_API __basic_any(__basic_any<_OtherInterface>&& __other)
{
Expand All @@ -160,13 +160,33 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT __basic_any : __basic_any_base<_Interface>
//! `__icopyable<>`.
//! @post `has_value() == __other.has_value()`.
_CCCL_TEMPLATE(class _OtherInterface)
_CCCL_REQUIRES((!::cuda::std::same_as<_OtherInterface, _Interface>)
_CCCL_REQUIRES((!::cuda::std::same_as<_OtherInterface&, _Interface&>)
_CCCL_AND __any_convertible_to<__basic_any<_OtherInterface> const&, __basic_any>)
_CCCL_API __basic_any(__basic_any<_OtherInterface> const& __other)
{
__convert_from(__other);
}

//! @brief Non-template converting constructors from the corresponding
//! reference type `__basic_any<_Interface&>`. These enable implicit
//! conversion from proxy types (e.g. Cython's __Pyx_FakeReference)
//! that wrap a `__basic_any<_Interface&>` and provide
//! `operator __basic_any<_Interface&>&()`, since non-template parameters
//! participate in implicit conversion sequences.
//! @pre `_Interface` must extend `__icopyable<>` (converting a ref to
//! a value requires copying the referenced object).
template <bool _Copyable = __copyable, ::cuda::std::enable_if_t<_Copyable, int> = 0>
_CCCL_API __basic_any(__basic_any<_Interface&> const& __other)
{
__convert_from(__other);
}

template <bool _Copyable = __copyable, ::cuda::std::enable_if_t<_Copyable, int> = 0>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be _CCCL_REQUIRES?

@bdice bdice Apr 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, this was intentional. The __basic_any class uses raw enable_if_t instead of _CCCL_TEMPLATE/_CCCL_REQUIRES to maintain ABI compatibility across C++17 and C++20. The _CCCL_REQUIRES macro expands to a requires-clause in C++20 but to additional SFINAE template parameters in C++17, which produces different mangled names depending on the dialect. This caused runtime failures (vtable cast assertions) when any_resource was used across DSO boundaries compiled with different -std= flags. See #7397 for the original bug report and #7401 / #7405 for the fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we only needed to use enable_if for the functions whose ptrs actually end up in the pseudo-vtable (the ones marked _CCCL_PUBLIC_API). for these constructors, _CCCL_REQUIRES would not cause the ABI issue described in #7397, and it would be consistent with the other constructors in this class.

_CCCL_API __basic_any(__basic_any<_Interface&>&& __other)
{
__convert_from(__other);
}

#if _CCCL_COMPILER(CLANG, <, 12) || _CCCL_COMPILER(GCC, <, 11)
// Older versions of clang and gcc need help disambiguating between
// __basic_any<__ireference<I>> and __basic_any<I&>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,83 @@ TEST_CASE("regression test for NVIDIA/cccl#8037", "[container][resource]")
{
STATIC_REQUIRE(cuda::std::move_constructible<my_resource_wrapper>);
}

// Minimal proxy that wraps a value and provides operator T&(), mimicking
// Cython's __Pyx_FakeReference<T> which wraps intermediate expression results.
template <typename T>
struct value_proxy
{
T value;

template <typename... Args>
explicit value_proxy(Args&&... args)
: value(std::forward<Args>(args)...)
{}

operator T&()
{
return value;
}
};

// See https://github.com/NVIDIA/cccl/issues/8316
TEST_CASE("any_resource from proxy-wrapped resource_ref", "[container][resource]")
{
host_device_resource mr;
cuda::mr::resource_ref<cuda::mr::host_accessible> ref{mr};

SECTION("direct construction from resource_ref")
{
cuda::mr::any_resource<cuda::mr::host_accessible> any{ref};
// Extra parens prevent Catch2's expression decomposition, which triggers
// an nvcc bug with auto NTTP deduction in __satisfies during ADL.
CHECK((any == ref));
Comment thread
ericniebler marked this conversation as resolved.
Resolved
}

# if !_CCCL_CUDA_COMPILER(NVCC, <, 12, 9)
// nvcc before CTK 12.9 has a bug where auto NTTP deduction failures in
// __satisfies are hard errors instead of SFINAE during overload resolution.
Comment on lines +384 to +386

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just raising a flag here. It seems like CUDA 12.0 didn't work with these changes. I wasn't able to come to a good workaround, but maybe someone else will have ideas. This is the MRE (sadly not very minimal): https://gist.github.com/bdice/0704486a9d32bceb782acd931c823123

I can't give a Godbolt reproducer link because it requires the changes from this PR...

@ericniebler ericniebler Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems reasonable to exclude the tests that trigger a since-fixed compiler bug. particularly one that is hard to work around, like this one.

EDIT: i wonder if it is the same issue that i worked around here: https://github.com/NVIDIA/cccl/blob/main/libcudacxx/include/cuda/__utility/__basic_any/basic_any_value.h#L254-L255

SECTION("construction from lvalue proxy-wrapped resource_ref")
{
value_proxy<cuda::mr::resource_ref<cuda::mr::host_accessible>> proxy{mr};
cuda::mr::any_resource<cuda::mr::host_accessible> any{proxy};
CHECK((any == ref));
}

SECTION("construction from rvalue proxy-wrapped resource_ref")
{
cuda::mr::any_resource<cuda::mr::host_accessible> any{
value_proxy<cuda::mr::resource_ref<cuda::mr::host_accessible>>{mr}};
CHECK((any == ref));
}
# endif // !_CCCL_CUDA_COMPILER(NVCC, <, 12, 9)
}

TEST_CASE("any_synchronous_resource from proxy-wrapped synchronous_resource_ref", "[container][resource]")
{
host_device_resource mr;
cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible> ref{mr};

SECTION("direct construction from synchronous_resource_ref")
{
cuda::mr::any_synchronous_resource<cuda::mr::host_accessible> any{ref};
CHECK((any == ref));
}

# if !_CCCL_CUDA_COMPILER(NVCC, <, 12, 9)
SECTION("construction from lvalue proxy-wrapped synchronous_resource_ref")
{
value_proxy<cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible>> proxy{mr};
cuda::mr::any_synchronous_resource<cuda::mr::host_accessible> any{proxy};
CHECK((any == ref));
}

SECTION("construction from rvalue proxy-wrapped synchronous_resource_ref")
{
cuda::mr::any_synchronous_resource<cuda::mr::host_accessible> any{
value_proxy<cuda::mr::synchronous_resource_ref<cuda::mr::host_accessible>>{mr}};
CHECK((any == ref));
}
# endif // !_CCCL_CUDA_COMPILER(NVCC, <, 12, 9)
}
#endif // __CUDA_ARCH__
Loading