You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[libcu++] Add machinery for iterator unwrapping - #12418
Adds machinery so that cuda::std::copy, move, sort and friends can unwrap fancy iterators (adaptors such as transform_iterator, and thrust pointers) to their underlying contiguous storage, perform the operation on raw pointers, and rebuild the original iterator type from the result. This enables more efficient loads for adaptor-wrapped contiguous data.
Design
__unwrap_iterator_adaptor peels iterator adaptors through base() until it reaches a contiguous iterator (cuda::std::to_address succeeds) or an iterator that adapts nothing further.
__rebind_iterator_adaptor rebuilds an iterator in place over a different T*, keeping its type. Adaptors provide a void __rebind(T*) member that rebinds their stored iterator in place; raw pointers and other contiguous iterators are assigned (or constructed) from the new pointer.
__iter_can_unwrap / __iter_can_rebind describe when this is possible. __is_contiguous_iterator_or_adaptor_v is true for transform iterators over contiguous storage. Output and input/output transform iterators are not contiguous adaptors because they cannot be rebuilt over a copy of their storage.
__wrap_iter gains a __rebind member.
Contiguous storage detection
Contiguous storage is detected through pointer_traits::to_address (via __has_cuda_std_to_address) or a raw pointer. A contiguous iterator tag, or a cuda::std::to_address that only works through operator->, is not enough: reverse_iterator, deque and list iterators satisfy the latter, but unwrapping them to a pointer would make adaptor-wrapped reverse iterators look like forward storage.
Thrust pointers
This PR no longer marks thrust::pointer, thrust::device_ptr or thrust::detail::normal_iterator as having contiguous traversal (__has_contiguous_traversal). Those specializations made cuda::std::copy and similar algorithms unwrap a device_ptr to a raw pointer and use memmove on the device address, which is wrong when the call is made from host code. Thrust pointers are still recognized as contiguous storage through their pointer_traits::to_address.
Tests
libcudacxx/test/libcudacxx/cuda/iterators/contiguous_iterator_adaptor.pass.cpp: adaptor trait and unwrap/rebind checks, including nested transform iterators, a pointer_traits opt-in for an addressable iterator, and negative cases (reverse_iterator, counting iterators, output adaptors).
thrust/testing/contiguous_iterator_adaptor.cu: thrust storage and transform adaptors, split into independent test cases with STATIC_REQUIRE checks.
thrust/testing/is_contiguous_iterator.cu, trivial_sequence.cu, type_traits.cu: updated to check __can_to_address instead of the removed contiguous-traversal specializations.
Verified locally: the thrust contiguous_iterator_adaptor, is_contiguous_iterator, type_traits and trivial_sequence tests build and pass (cpp20, CUDA 12.9). The libcu++ contiguous_iterator_adaptor.pass.cpp was compiled and run directly with nvcc (exit 0); it has not been run through lit yet.
Open points / follow-ups
None outstanding from this PR. __tagged_ptr is intentionally out of scope (it holds a basic_any vtable pointer, not iterator storage).
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
New Features
Added support for identifying contiguous iterators and accessing their underlying storage where supported.
Compatible iterator adaptors can now be rebound to pointer storage while preserving their transformations.
Expanded contiguous-iterator support across CUDA standard-library and Thrust iterators, including nested adaptors and const iterators. Non-contiguous iterators remain distinguishable and are not treated as contiguous.
The changes add contiguous traversal detection and utilities to unwrap and rebind supported iterator adaptors. libcu++ and Thrust adaptors gain rebinding support. Tests cover trait detection, nested adaptors, and non-contiguous iterators.
__has_contiguous_traversal now checks for the contiguous iterator concept. Compile-time assertions cover pointers, custom iterators, non-iterators, and selected standard-library iterators.
Adaptor unwrapping and rebinding libcudacxx/include/cuda/__iterator/contiguous_iterator_adaptor.h, libcudacxx/include/cuda/__iterator/transform_iterator.h, libcudacxx/include/cuda/std/__iterator/wrap_iter.h, thrust/thrust/iterator/..., libcudacxx/include/cuda/iterator
The new utilities detect contiguous storage, unwrap eligible iterators, and determine whether adaptors can be rebound. libcu++ and Thrust adaptors add rebinding operations. The CUDA iterator umbrella header exports the utilities.
Tests cover pointer and nested transform adaptors, Thrust iterators, and non-contiguous cases. Additional assertions check addressability and contiguous traversal traits.
Suggested reviewers:jacobfaib
Change: Feature
Merge Risk: Minimal · previous review 56d2d
The previously reported rebinding failure is fixed, and no merge-blocking issue remains.
Security architecture risk: Low · up to 56d2d
The inspected changes preserve built-in adaptor state and type restrictions, and no new attacker-facing or privileged execution path was established. Downstream usage and custom adaptor behavior remain incompletely covered.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
inferred — The demonstrated scope is an in-process iterator contract spanning libcu++ and Thrust, including host/device pointer representations. Rebinding changes a stored pointer representation; it does not allocate storage or establish its lifetime, bounds or address-space accessibility.
Trust Boundaries and Controls
observed — Built-in eligibility uses type and pointer-conversion constraints, not runtime memory-authority checks. Negative assertions reject incompatible const rebinding, reverse-iterator eligibility and output-adaptor eligibility. These checks should not be interpreted as validating arbitrary pointer provenance.
Resilience and Maintainability Implications
observed — Custom member rebinding is accepted through expression validity. The helper provides no transactional rollback or non-throwing guarantee for arbitrary hooks. This limits the proven failure-containment contract, but no concrete security-relevant runtime failure was established in built-in consumers.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
Fancy iterators over contiguous storage are not contiguous, so algorithms miss the fast copy. Unwrap peels iterator adaptors through their base down to a contiguous iterator, and rebind rebuilds a transform iterator over a copy of that storage, replacing the contiguous iterator with a pointer and keeping the functors. Only adaptors that can be rebuilt satisfy __iter_can_unwrap, which excludes transform output and input output iterators. A contiguous iterator unwraps to its address, even if it also has a base.
The reason will be displayed to describe this comment to others. Learn more.
Nitpick comments: 2
thrust/thrust/detail/pointer.h: 1
436-436: Trivial · Maintainability & Code Quality · Low value
suggestion: Change the #endif comment to // !_CCCL_DOXYGEN_INVOKED. The matching #if is #ifndef _CCCL_DOXYGEN_INVOKED. The repository convention is that an annotated #endif repeats the exact condition text. libcudacxx/include/cuda/__iterator/transform_iterator.h already uses #endif // !_CCCL_DOXYGEN_INVOKED for the same guard.
Based on learnings: for annotated #else/#endif, the comment must repeat the exact verbatim condition text from the corresponding #if.
suggestion: Test the algorithm unwrap and rewrap path.
Add tests for normal_iterator<int*> and normal_iterator<device_ptr<T>>. Check that cuda::std::__unwrap_iter returns the expected address and that cuda::std::copy returns the correctly advanced iterator. The adaptor tests do not cover algorithm rewrapping.
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
Fancy iterators over contiguous storage are not contiguous, so algorithms miss the fast copy. Unwrap peels iterator adaptors through their base down to a contiguous iterator, and rebind rebuilds a transform iterator over a copy of that storage, replacing the contiguous iterator with a pointer and keeping the functors. Only adaptors that can be rebuilt satisfy __iter_can_unwrap, which excludes transform output and input output iterators. A contiguous iterator unwraps to its address, even if it also has a base.
…o_address
Drop the __has_contiguous_traversal specializations for thrust::pointer,
thrust::device_ptr and thrust::detail::normal_iterator. Marking these as
contiguous made cuda::std::copy, move, sort and friends unwrap them to raw
pointers, so device memory was accessed with host memcpy/memmove.
Contiguous storage for the adaptor machinery is now determined by
pointer_traits::to_address (via __has_cuda_std_to_address), or by a raw
pointer. A contiguous iterator tag or a cuda::std::to_address that only goes
through operator-> is not enough, since reverse, deque and list iterators
satisfy it.
Tests are updated to check __can_to_address and the adaptor traits instead of
the removed trait. The libcu++ test gains a pointer_traits opt-in for an
addressable iterator and checks that contiguous_iterator<int*> no longer
unwraps through operator->.
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
__rebind_iterator_adaptor now rebuilds the iterator in place, and the
transform_iterator __rebind members mutate the stored iterator instead of
returning a new one. This matches __wrap_iter and normal_iterator, which
already rebind in place.
Tests rebind named iterators and check the same object afterwards.
…age tests
Use __has_cuda_std_to_address instead of __can_to_address where the
tests assert that an iterator exposes contiguous storage through
pointer_traits. Mark the __unwrapped_iterator_t and
__unwrapped_iter_value_t aliases _CCCL_NODEBUG for the clang-tidy
cccl-nodebug-on-aliases check.
The reason will be displayed to describe this comment to others. Learn more.
Important: Please try to find better names for the concept __iter_can_unwrap and the helpers above. We already have thrust::unwrap_contiguous_iterator and thrust::try_unwrap_contiguous_iterator and the functions here sound like they would do something similar. Please sufficiently distinguish the functions here. E.g. __iter_can_unwrap_and_rebind etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds machinery so that
cuda::std::copy,move,sortand friends can unwrap fancy iterators (adaptors such astransform_iterator, and thrust pointers) to their underlying contiguous storage, perform the operation on raw pointers, and rebuild the original iterator type from the result. This enables more efficient loads for adaptor-wrapped contiguous data.Design
__unwrap_iterator_adaptorpeels iterator adaptors throughbase()until it reaches a contiguous iterator (cuda::std::to_addresssucceeds) or an iterator that adapts nothing further.__rebind_iterator_adaptorrebuilds an iterator in place over a differentT*, keeping its type. Adaptors provide avoid __rebind(T*)member that rebinds their stored iterator in place; raw pointers and other contiguous iterators are assigned (or constructed) from the new pointer.__iter_can_unwrap/__iter_can_rebinddescribe when this is possible.__is_contiguous_iterator_or_adaptor_vis true for transform iterators over contiguous storage. Output and input/output transform iterators are not contiguous adaptors because they cannot be rebuilt over a copy of their storage.__wrap_itergains a__rebindmember.Contiguous storage detection
Contiguous storage is detected through
pointer_traits::to_address(via__has_cuda_std_to_address) or a raw pointer. A contiguous iterator tag, or acuda::std::to_addressthat only works throughoperator->, is not enough:reverse_iterator,dequeandlistiterators satisfy the latter, but unwrapping them to a pointer would make adaptor-wrapped reverse iterators look like forward storage.Thrust pointers
This PR no longer marks
thrust::pointer,thrust::device_ptrorthrust::detail::normal_iteratoras having contiguous traversal (__has_contiguous_traversal). Those specializations madecuda::std::copyand similar algorithms unwrap adevice_ptrto a raw pointer and usememmoveon the device address, which is wrong when the call is made from host code. Thrust pointers are still recognized as contiguous storage through theirpointer_traits::to_address.Tests
libcudacxx/test/libcudacxx/cuda/iterators/contiguous_iterator_adaptor.pass.cpp: adaptor trait and unwrap/rebind checks, including nested transform iterators, apointer_traitsopt-in for an addressable iterator, and negative cases (reverse_iterator, counting iterators, output adaptors).thrust/testing/contiguous_iterator_adaptor.cu: thrust storage and transform adaptors, split into independent test cases withSTATIC_REQUIREchecks.thrust/testing/is_contiguous_iterator.cu,trivial_sequence.cu,type_traits.cu: updated to check__can_to_addressinstead of the removed contiguous-traversal specializations.Verified locally: the thrust
contiguous_iterator_adaptor,is_contiguous_iterator,type_traitsandtrivial_sequencetests build and pass (cpp20, CUDA 12.9). The libcu++contiguous_iterator_adaptor.pass.cppwas compiled and run directly with nvcc (exit 0); it has not been run through lit yet.Open points / follow-ups
__tagged_ptris intentionally out of scope (it holds abasic_anyvtable pointer, not iterator storage).