Skip to content

cuda::is_trivially_copyable#8265

Open
fbusato wants to merge 54 commits intoNVIDIA:mainfrom
fbusato:relaxed-type-traits
Open

cuda::is_trivially_copyable#8265
fbusato wants to merge 54 commits intoNVIDIA:mainfrom
fbusato:relaxed-type-traits

Conversation

@fbusato
Copy link
Copy Markdown
Contributor

@fbusato fbusato commented Apr 1, 2026

Description

Followup of the discussion in

The PR introduces cuda::is_trivially_copyable_relaxed to support types that are actually trivially copyable but not recognized by the C++ std::is_trivially_copyable type trait.

The new trait supports:

  • trivially copyable types.
  • extended floating-point types.
  • extended floating-point vector types.
  • Raw arrays, cuda::std::array.
  • cuda::std::pair.
  • cuda::std::tuple.
  • Nested compositions of the above cases.

Potentially affected paths by std::is_trivially_copyable

unsupported cases:

  • cuda/std/__atomic/types/base.h:39
  • cuda/std/__atomic/types/reference.h:39
  • cuda/__memcpy_async/memcpy_async_barrier.h:61
  • cuda/__memcpy_async/memcpy_async_tx.h:58
  • cuda/__container/buffer.h:111
  • cuda/__algorithm/copy.h:73
  • cuda/__algorithm/fill.h:42
  • cuda/std/string_view:147

fallback to slower path:

  • cuda/std/__algorithm/copy.h:102
  • cuda/std/__algorithm/copy_backward.h:50
  • cuda/std/__algorithm/move.h:54
  • cuda/std/__algorithm/move_backward.h:53
  • cuda/std/__bit/bit_cast.h:55
  • cub/device/dispatch/kernels/kernel_histogram.cuh:81
  • cub/detail/uninitialized_copy.cuh:33
  • thrust/type_traits/is_trivially_relocatable.h:213
  • cudax/include/cuda/experimental/__kernel/kernel_ref.cuh:54

Require #8347

@fbusato fbusato self-assigned this Apr 1, 2026
@fbusato fbusato added this to CCCL Apr 1, 2026
@fbusato fbusato added the libcu++ For all items related to libcu++ label Apr 1, 2026
@fbusato fbusato requested a review from a team as a code owner April 1, 2026 19:51
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Apr 1, 2026
@fbusato fbusato requested a review from wmaxey April 1, 2026 19:51
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Apr 1, 2026
@fbusato fbusato requested a review from a team as a code owner April 1, 2026 19:55
@fbusato fbusato requested a review from gonidelis April 1, 2026 19:55
Copy link
Copy Markdown
Contributor

@miscco miscco left a comment

Choose a reason for hiding this comment

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

I am not too excited about this.

We need to be really careful here because the compiler may also break in some of those cases.

I did some experiments in #3183

@github-project-automation github-project-automation Bot moved this from In Review to In Progress in CCCL Apr 1, 2026
@bernhardmgruber
Copy link
Copy Markdown
Contributor

I wanted to start a discussion for some time to add such a trait, but I would really keep this feature internally for now until it is sufficiently baked. I would really like this feature to make thrust::is_trivially_relocatable and THRUST_PROCLAIM_TRIVIALLY_RELOCATABLE obsolete, because those two are often used wrongly, so we should make sure the new trait here can replace it.

Copy link
Copy Markdown
Contributor

@davebayer davebayer left a comment

Choose a reason for hiding this comment

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

I am not a fan of this trait. We bend C++ rules to fix poorly designed nvfp types. We would have to basically change every use of is_trivially_copyable and is_trivially_copy_constructible to this trait to make it work consistently in libcu++.

I don't think this is a good idea, we should rather insist of the nvfp types being fixed.

Comment thread docs/libcudacxx/extended_api/type_traits/is_trivially_copyable_relaxed.rst Outdated
Comment on lines +40 to +41
Users may specialize ``cuda::is_trivially_copyable_relaxed`` for their own types whose memory representation is safe to copy
with ``memcpy`` but that the compiler does not consider trivially copyable.
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.

Isn't this just UB?

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.

not for the types that we care about. Said that, the user could provide an object that triggers UB. I can highlight this point in the documentation but we cannot do anything to explicitly prevent it.

@fbusato
Copy link
Copy Markdown
Contributor Author

fbusato commented Apr 1, 2026

We need to be really careful here because the compiler may also break in some of those cases.

I did some experiments in #3183

Integrated and extended the tests that you point out. Everything works

@fbusato
Copy link
Copy Markdown
Contributor Author

fbusato commented Apr 1, 2026

I wanted to start a discussion for some time to add such a trait, but I would really keep this feature internally for now until it is sufficiently baked. I would really like this feature to make thrust::is_trivially_relocatable and THRUST_PROCLAIM_TRIVIALLY_RELOCATABLE obsolete, because those two are often used wrongly, so we should make sure the new trait here can replace it.

the actual issue is that the user can extend the trait to custom types. It needs to be public

@fbusato
Copy link
Copy Markdown
Contributor Author

fbusato commented Apr 1, 2026

I am not a fan of this trait. We bend C++ rules to fix poorly designed nvfp types. We would have to basically change every use of is_trivially_copyable and is_trivially_copy_constructible to this trait to make it work consistently in libcu++.
I don't think this is a good idea, we should rather insist of the nvfp types being fixed.

there are no plan for that. nvfp types are not trivially copyable for optimization purposes.

@jrhemstad
Copy link
Copy Markdown
Collaborator

we should rather insist of the nvfp types being fixed.

This isn't going to happen and we shouldn't delude ourselves into thinking it ever will.

@fbusato fbusato moved this from In Progress to In Review in CCCL Apr 1, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@fbusato fbusato moved this from In Progress to Blocked in CCCL Apr 14, 2026
@fbusato fbusato moved this from Blocked to In Progress in CCCL Apr 28, 2026
@fbusato fbusato moved this from In Progress to Todo in CCCL Apr 28, 2026
@github-actions

This comment has been minimized.

@fbusato fbusato requested review from a team as code owners April 30, 2026 20:10
@fbusato fbusato requested a review from miscco April 30, 2026 20:21
@fbusato
Copy link
Copy Markdown
Contributor Author

fbusato commented Apr 30, 2026

  • entirely refactored the implementation relying on __aggregate_all_of_v.
  • replaced cuda::std::is_trivially_copyable with cuda::is_trivially_copyable in all CCCL library paths (but not in tests/benchmarks)

@@ -0,0 +1,323 @@
//===----------------------------------------------------------------------===//
Copy link
Copy Markdown
Contributor Author

@fbusato fbusato Apr 30, 2026

Choose a reason for hiding this comment

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

I'm inclined to remove this file because it is very similar with bit_cast.pass.cpp

@github-actions

This comment has been minimized.

} // namespace cuda

``cuda::is_trivially_copyable_v`` trait evaluates if a type can be copied by copying its underlying bytes.
It extends ``cuda::std::is_trivially_copyable`` to also recognize CUDA extended floating-point vector types as trivially copyable.
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 also mention whether it is legal for users to specialize the trait, and give an example of how to do that if so. Given the definition above it implies that users should specialize is_trivially_copyable_v instead of is_trivially_copyable, usually it is the other way around.

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.

specializing is_trivially_copyable_v could be too far. The only reason we are proving it is to overcome CUDA data type limitations. I don't see a good motivation for user to specialize it

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.

@miscco @davebayer let me know if you have a different opinion

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.

I don't see a good motivation for user to specialize it

Even so, I still think the definition snippet should show the "usual" way these traits are defined, i.e.

template <typename T>
struct my_trait : bool_constant<...> { };

template <typename T>
inline constexper bool my_trait_v = my_trait<T>::value;

Even if that is not technically true, we don't want to give the impression that specializing the _v form is what actually matters.

``cuda::is_trivially_copyable_v`` trait evaluates if a type can be copied by copying its underlying bytes.
It extends ``cuda::std::is_trivially_copyable`` to also recognize CUDA extended floating-point vector types as trivially copyable.

``cuda::is_trivially_copyable_v<T>`` relies on ``cuda::std::is_trivially_copyable`` but adds support for CUDA-specific types.
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.

If it is legal to specialize, should elaborate whether the user should specialize cuda::std::is_trivially_copyable or cuda::is_trivially_copyable

Comment thread docs/libcudacxx/extended_api/type_traits/is_trivially_copyable.rst Outdated
Comment thread docs/libcudacxx/extended_api/type_traits/is_trivially_copyable.rst
Comment thread libcudacxx/include/cuda/__type_traits/is_trivially_copyable.h
Comment thread libcudacxx/include/cuda/__type_traits/is_trivially_copyable.h
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.

Important: can you add tests that the trait works even when it is user-specialized?

Comment thread libcudacxx/include/cuda/std/__bit/bit_cast.h
``cuda::is_trivially_copyable_v`` trait evaluates if a type can be copied by copying its underlying bytes.
It extends ``cuda::std::is_trivially_copyable`` to also recognize CUDA extended floating-point vector types as trivially copyable.

``cuda::is_trivially_copyable_v<T>`` relies on ``cuda::std::is_trivially_copyable`` but adds support for CUDA-specific types.
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.

Note: This sentence and the previous one seem a bit redundant

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

😬 CI Workflow Results

🟥 Finished in 3h 26m: Pass: 97%/437 | Total: 19d 02h | Max: 3h 25m | Hits: 44%/599270

See results here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libcu++ For all items related to libcu++

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

8 participants