Skip to content

[libcu++] Add machinery for iterator unwrapping - #12418

Merged
miscco merged 6 commits into
NVIDIA:mainfrom
miscco:iteraor_unwrapping
Nov 12, 2026
Merged

miscco merged 6 commits into
NVIDIA:mainfrom
miscco:iteraor_unwrapping

Conversation

@miscco

@miscco miscco commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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).

@miscco
miscco requested review from a team as code owners November 4, 2026 09:38
@miscco
miscco requested review from elstehle and pciolkosz November 4, 2026 09:38
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Nov 4, 2026
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Nov 4, 2026
@miscco miscco linked an issue Nov 4, 2026 that may be closed by this pull request
1 task done
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

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.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

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.

Changes: Contiguous iterator support

Layer / File(s) Summary
Contiguous traversal detection
libcudacxx/include/cuda/std/__iterator/iterator_traits.h, libcudacxx/test/libcudacxx/libcxx/iterators/has_contiguous_traversal.compile.pass.cpp
__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.
Adaptor behavior validation
libcudacxx/test/libcudacxx/cuda/iterators/contiguous_iterator_adaptor.pass.cpp, thrust/testing/contiguous_iterator_adaptor.cu, thrust/testing/is_contiguous_iterator.cu, thrust/testing/trivial_sequence.cu, thrust/testing/type_traits.cu
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
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/cccl
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 5060e9b
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 86b55f1c-a4e4-4c00-b987-051fda58f08f
Commits

Reviewing files that changed from the base of the PR and between 3aeeb38 and 56d2dc8.

Files selected for processing: 5

  • libcudacxx/include/cuda/__iterator/contiguous_iterator_adaptor.h
  • libcudacxx/include/cuda/__iterator/transform_iterator.h
  • libcudacxx/test/libcudacxx/cuda/iterators/contiguous_iterator_adaptor.pass.cpp
  • thrust/testing/contiguous_iterator_adaptor.cu
  • thrust/thrust/iterator/transform_iterator.h

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

@davebayer davebayer left a comment

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.

lgtm

@github-actions

This comment has been minimized.

Comment thread thrust/thrust/detail/pointer.h Resolved
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.
@miscco
miscco force-pushed the iteraor_unwrapping branch from aac5b1a to 599906d Compare November 9, 2026 09:32

@coderabbitai coderabbitai Bot left a comment

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.

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.

Source: Learnings

thrust/thrust/iterator/detail/normal_iterator.h: 1

101-103: Trivial · Functional Correctness · Quick win

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.

Review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/cccl
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 89e1909
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: bf44c9d1-b41a-4eed-9503-1eee203ea75f
Commits

Reviewing files that changed from the base of the PR and between aac5b1a and 599906d.

Files selected for processing: 8

  • libcudacxx/include/cuda/__iterator/contiguous_iterator_adaptor.h
  • libcudacxx/include/cuda/__iterator/transform_iterator.h
  • libcudacxx/include/cuda/std/__iterator/wrap_iter.h
  • libcudacxx/test/libcudacxx/cuda/iterators/contiguous_iterator_adaptor.pass.cpp
  • thrust/testing/contiguous_iterator_adaptor.cu
  • thrust/thrust/detail/pointer.h
  • thrust/thrust/iterator/detail/normal_iterator.h
  • thrust/thrust/iterator/transform_iterator.h

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

@miscco

miscco commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 599906d

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 2

Review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/cccl
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 5060e9b
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: e98fea3d-0ca6-43bd-acb4-22c02be2b212
Commits

Reviewing files that changed from the base of the PR and between 599906d and be84c8d.

Files selected for processing: 6

  • libcudacxx/include/cuda/__iterator/transform_iterator.h
  • libcudacxx/include/cuda/std/__iterator/wrap_iter.h
  • thrust/thrust/detail/pointer.h
  • thrust/thrust/device_ptr.h
  • thrust/thrust/iterator/detail/normal_iterator.h
  • thrust/thrust/iterator/transform_iterator.h

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

Comment thread thrust/thrust/detail/pointer.h Resolved
Comment thread thrust/thrust/device_ptr.h Resolved
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->.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/cccl
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 5060e9b
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 44ad9958-2752-423f-8e16-cc748632fe2c
Commits

Reviewing files that changed from the base of the PR and between be84c8d and 3aeeb38.

Files selected for processing: 9

  • libcudacxx/include/cuda/__iterator/contiguous_iterator_adaptor.h
  • libcudacxx/include/cuda/__iterator/transform_iterator.h
  • libcudacxx/test/libcudacxx/cuda/iterators/contiguous_iterator_adaptor.pass.cpp
  • thrust/testing/contiguous_iterator_adaptor.cu
  • thrust/testing/is_contiguous_iterator.cu
  • thrust/testing/trivial_sequence.cu
  • thrust/testing/type_traits.cu
  • thrust/thrust/iterator/detail/normal_iterator.h
  • thrust/thrust/iterator/transform_iterator.h
Files with no reviewable changes: 1

  • thrust/thrust/iterator/detail/normal_iterator.h

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.
@github-actions

This comment has been minimized.

…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.
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 3h 45m: Pass: 100%/291 | Total: 8d 17h | Max: 3h 45m | Hits: 51%/2181615

See results here.

Comment on lines +117 to +120
//! @brief True when `_Iter` is an iterator adaptor over contiguous storage, which unwraps to a pointer and can be
//! rebuilt over a copy of the storage behind it.
template <class _Iter>
_CCCL_CONCEPT __iter_can_unwrap = _CCCL_REQUIRES_EXPR((_Iter)) //

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: 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

Comment on lines +50 to +51
//! @brief Rebuilds the stored pointer in place over the storage at @p ptr.
//! @param[in] ptr The address of the storage that replaces the storage this iterator points to.

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.

Remark: I wouldn't use Doxygen comments for internal helpers, just an ordinary one.

@bernhardmgruber bernhardmgruber left a comment

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.

Let's apply the above two comments in a follow-up.

@miscco
miscco merged commit 15f5d7e into NVIDIA:main Nov 12, 2026
313 checks passed
@miscco
miscco deleted the iteraor_unwrapping branch November 12, 2026 16:04
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[FEA]: PoC fancy iterator unpacking facility

3 participants