Skip to content

cub, libcu++: advertised properties + their checking in DeviceReduce. - #11577

Merged
griwes merged 19 commits into
NVIDIA:mainfrom
griwes:feature/better-env-diagnostics
Nov 18, 2026
Merged

griwes merged 19 commits into
NVIDIA:mainfrom
griwes:feature/better-env-diagnostics

Conversation

@griwes

@griwes griwes commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Resolves #7797.

An incorrectly implemented environment query can silently select a default in CUB. This PR lets environments advertise the customizations they provide and makes cub::DeviceReduce diagnose broken advertised queries.

cuda::execution::property_keys_t<T> obtains a type's advertised query list. User-defined types supply that list through T::property_keys, using cuda::execution::property_key_list<...>. An entry property_query<Key, Args...> describes a CPO invocation with a const environment and the specified additional argument types; a bare Key is shorthand for a nullary invocation.

The advertisement is a nested typedef, so discovering it does not depend on dispatching another query. Standard-library types keep their metadata under a reserved implementation name, accessed through property_keys_t, to allow a future standard property interface without a source-compatibility conflict.

Metadata is supplied by cuda::std::execution::{prop, env}, cuda::stream_ref, cuda::mr::memory_resource_base, and asynchronous resource references. env combines and deduplicates its components' advertisements while retaining first-matching-member lookup. Validation checks individual advertised contributions so another provider cannot hide a broken advertisement, including in nested and inherited environments.

Validation checks the CPO invocation and instantiates its body without executing the query. Each advertised expression is checked separately so the failing query appears in the instantiation trace. Automatic CUB validation is limited to DeviceReduce: environments with metadata have their advertised customizations checked, while objects without metadata must support a stream or memory-resource query. Working unadvertised customizations remain usable, and unavailable queries retain their defaults.

cuda::checked_env<Queries...>(env) explicitly validates selected CPO invocations and returns a member-query-forwarding adaptor. It can also add metadata to a third-party environment or replace an existing advertised list.

A requested invocation may use the CPO's default when the input does not advertise that customization. If the input advertises a requested customization, its member must be present. The adaptor advertises only the requested expressions backed by supplied members; default-only invocations are checked without being advertised or manufacturing a member. This lets a checked default compose with other environments without shadowing a later provider. Supported member queries outside the checked list remain forwarded.

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@griwes
griwes requested review from a team as code owners September 22, 2026 05:17
@griwes
griwes requested a review from elstehle September 22, 2026 05:17
@griwes
griwes requested a review from pciolkosz September 22, 2026 05:17
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Sep 22, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Sep 22, 2026
@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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 checked execution environments that advertise supported properties and validate queries.
    • Added property metadata for custom environments, stream references, and memory-resource references.
    • DeviceReduce now uses advertised stream and memory-resource properties and reports errors for invalid advertised queries or inputs lacking either property.
    • DeviceReduce accepts stream objects and memory-resource references as environment inputs.
  • Documentation
    • Clarified how to provide streams, memory resources, and custom properties when building execution environments.

The change adds advertised-query metadata and checked environment support to libcudacxx. CUB dispatch validates advertised queries, and DeviceReduce uses validation-aware paths. Tests cover metadata, checked environments, stream and resource inputs, and compile-time diagnostics.

Changes: Environment validation

Layer / File(s) Summary
Property-query metadata contracts
libcudacxx/include/cuda/__execution/property_query.h, libcudacxx/include/cuda/std/__execution/env.h, libcudacxx/include/cuda/__stream/stream_ref.h, libcudacxx/include/cuda/__memory_resource/*, libcudacxx/include/cuda/execution, libcudacxx/test/libcudacxx/cuda/execution/*, libcudacxx/test/libcudacxx/cuda/stream_ref/*, libcudacxx/test/libcudacxx/cuda/memory_resource/get_memory_resource/*, libcudacxx/test/libcudacxx/std/execution/exec.prop/*, docs/libcudacxx/standard_api/execution_library.rst, docs/libcudacxx/extended_api/memory_resource/resource_utilities.rst
Adds property-query types and metadata discovery. Execution environments, streams, and memory resources advertise supported queries. Tests check metadata and query results.
Checked environment adaptor
libcudacxx/include/cuda/__execution/checked_env.h, libcudacxx/test/libcudacxx/cuda/execution/checked_env.*.cpp, docs/libcudacxx/standard_api/execution_library.rst
Adds cuda::checked_env to validate and forward an explicit query list. Tests cover value-backed and reference-backed environments and invalid non-const queries.
Validation-aware DeviceReduce dispatch
cub/cub/detail/env_dispatch.cuh, cub/cub/device/device_reduce.cuh, cub/test/*device_reduce*, cub/test/catch2_test_device_reduce_env_api.cu, cub/benchmarks/bench/reduce/by_key.cu, cub/test/catch2_test_memory_resources.h, docs/cub/developer/device_scope.rst, docs/cub/device_wide.rst, docs/cub/environment.rst
Adds advertised-query lookup and validated dispatch paths. DeviceReduce uses them for reduction, min/max, arg-reduction, and ReduceByKey operations. Tests cover stream and resource inputs and compile-time diagnostics. Documentation describes the environment rules and dispatch behavior.

Assessment against linked issues

Objective Addressed Explanation
Advertise supported queries through execution properties, composed environments, streams, and memory resources [#7797] ✅
Validate advertised environment queries and require unmarked DeviceReduce objects to provide a supported query [#7797] ✅
Provide a helper to specify and validate an explicit query list [#7797] ✅

Suggested reviewers: bernhardmgruber

Change: Feature

Merge Risk: Moderate · previous review df32d

An advertised resource that has non-const allocation methods and is passed through a const environment to validated DeviceReduce dispatch may be rejected. Resolve or confirm this before merging.

Finishing Touches

Resolve merge conflicts

  • Resolve merge conflict in branch feature/better-env-diagnostics
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
ℹ️ Recent review info

⚙️ Run configuration

  • Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 6e00b48c-ebc2-452a-9386-4152814749de
📥 Commits

Reviewing files that changed from the base of the PR and between c18d5b5 and df32dc2.

📒 Files selected for processing (2)

  • cub/cub/detail/env_dispatch.cuh
  • cub/test/test_device_reduce_invalid_object_fail.cu
🚧 Files skipped from review as they are similar to previous changes (2)

  • cub/test/test_device_reduce_invalid_object_fail.cu
  • cub/cub/detail/env_dispatch.cuh

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

@github-actions

This comment has been minimized.

Comment thread cub/cub/detail/env_dispatch.cuh Resolved
Comment thread cub/cub/detail/env_dispatch.cuh Resolved
Comment thread cub/cub/detail/env_dispatch.cuh Resolved
Comment thread cub/cub/detail/env_dispatch.cuh Resolved
@griwes
griwes requested a review from a team as a code owner September 23, 2026 03:21

@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

Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 7ba1e3ba-a3d4-47cc-814e-13f61de96c7d

📥 Commits

Reviewing files that changed from the base of the PR and between d3241f2 and 6bc691d.

📒 Files selected for processing (10)
  • cub/benchmarks/bench/reduce/by_key.cu
  • cub/cub/detail/env_dispatch.cuh
  • cub/test/catch2_test_device_reduce_env_api.cu
  • docs/cub/environment.rst
  • docs/libcudacxx/extended_api/memory_resource/resource_utilities.rst
  • libcudacxx/include/cuda/__memory_resource/any_resource.h
  • libcudacxx/include/cuda/__memory_resource/get_memory_resource.h
  • libcudacxx/include/cuda/__memory_resource/memory_resource_base.h
  • libcudacxx/test/libcudacxx/cuda/execution/property_query.pass.cpp
  • libcudacxx/test/libcudacxx/cuda/memory_resource/get_memory_resource/get_memory_resource.pass.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
  • docs/libcudacxx/extended_api/memory_resource/resource_utilities.rst
  • docs/cub/environment.rst
  • cub/cub/detail/env_dispatch.cuh

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

@github-actions

This comment has been minimized.

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

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · important: Restrict property_keys to asynchronous resource references. · any_resource.h:732-740

libcudacxx/include/cuda/__memory_resource/any_resource.h:732-740
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

important: Restrict property_keys to asynchronous resource references.

synchronous_resource_ref aliases basic_resource_ref<_ResourceKind::_Synchronous, ...>, but basic_resource_ref unconditionally advertises get_memory_resource_t. The PR's own get_memory_resource.pass.cpp:70 requires synchronous_resource_ref<> to have no property_keys, so this change makes that test fail to compile. Expose the metadata and query(get_memory_resource_t) only for _ResourceKind::_Asynchronous.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 93e82c11-2ad7-46ae-bb87-02c3b523cb6e

📥 Commits

Reviewing files that changed from the base of the PR and between 6bc691d and 7c63160.

📒 Files selected for processing (3)
  • cub/cub/detail/env_dispatch.cuh
  • cub/test/catch2_test_memory_resources.h
  • libcudacxx/include/cuda/__execution/checked_env.h

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

@github-actions

This comment has been minimized.

Comment thread cub/benchmarks/bench/reduce/by_key.cu Outdated
reduction_op_t{},
static_cast<OffsetT>(elements),
alloc);
cuda::mr::resource_ref<>{alloc});

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.

Q: Why do we need to change "user side" code?

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.

This is because the nature of get_memory_resource is... extremely weird. For some reason we decided that it should accept owning memory resources (#8404). But owning memory resources cannot possibly follow the env-like path through a query, because the env-like path is const qualified, and dealing with const-qualified memory resources is not very useful; so get_memory_resource got a non-const qualified overload to catch these... ...but that will never pass the env-like validation, because the env-like path REQUIRES environments to be const.

That change also led to some of the pstl code doing... const casts casting the constness of a reference away. It's a big mess. If at all possible, I'd like to remove the code added by the previous PR entirely, because the ownership semantics get very tangled very quickly.

For the code specifically here in the benchmark, cub_bench_env already does equivalent wrapping.

There are changes I can make in the general mechanism introduced in this PR to make this change unnecessary, buuuuuut I do not actually know if those are desired at all. I can special case the object matching a memory resource concept and in that case just accept it - but that would further blur the lines here I think; if we want to say that this is the environment based overload, we really should actually accept only environments, and not environments + some random assortment of other types that have explicit carveouts in the protocol.

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.

So, the PR here can introduce an API break?

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.

It does, but the cases where it does, the implementation is actually just ignoring the argument, so turning that into a compiler error is arguably a fix.

Comment thread cub/cub/detail/env_dispatch.cuh Outdated
Comment on lines +81 to +83
static_assert(::cuda::std::__is_callable_v<::cuda::get_stream_t, const EnvT&>
|| ::cuda::std::__is_callable_v<::cuda::mr::get_memory_resource_t, const EnvT&>,
"An object passed as a DeviceReduce environment must provide a stream or a memory resource.");

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: this is fine as a first step. We also have functions taking an env but also a temporary storage pointer and size&, so for those we would only check for it being a stream. Nothing needs to be done about this in this PR.

Comment thread cub/cub/detail/env_dispatch.cuh Resolved
@github-actions

This comment has been minimized.

@griwes

griwes commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 7ca6d9e

@github-actions

This comment has been minimized.

@griwes

griwes commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 968ed9d

@github-actions

This comment has been minimized.

@griwes

griwes commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

This PR doesn't have the means to resolve all cases similar to the snippet #7797; there can be arbitrary misspelling. I believe providing a tool to help avoid them is the goal here.

I believe the CPO callable vs query callable issue has now been resolved, narrowly within the mechanisms of this PR. @gonidelis please re-review.

@griwes
griwes requested a review from gonidelis November 3, 2026 20:19
@griwes

griwes commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 37555bb

@github-actions

This comment has been minimized.

@gonidelis

Copy link
Copy Markdown
Member

fix the conflicts otherwise lgtm

@github-project-automation github-project-automation Bot moved this from In Progress to In Review in CCCL Nov 14, 2026
@griwes

griwes commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test e75c885

@github-actions

This comment has been minimized.

@griwes

griwes commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 319860b

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@griwes

griwes commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test e91ff91

@griwes

griwes commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 7bdccf0

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 24m: Pass: 100%/342 | Total: 3d 11h | Max: 1h 24m | Hits: 96%/782370

See results here.

@griwes
griwes merged commit fef6555 into NVIDIA:main Nov 18, 2026
371 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Nov 18, 2026
@griwes
griwes deleted the feature/better-env-diagnostics branch November 19, 2026 00:35
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]: Improve Environment Diagnostic

5 participants