Skip to content

[libcu++] Fix default make_shared_resource construction#9044

Merged
bdice merged 4 commits into
NVIDIA:mainfrom
bdice:fix-default-make-shared-resource
May 17, 2026
Merged

[libcu++] Fix default make_shared_resource construction#9044
bdice merged 4 commits into
NVIDIA:mainfrom
bdice:fix-default-make-shared-resource

Conversation

@bdice
Copy link
Copy Markdown
Contributor

@bdice bdice commented May 16, 2026

Description

closes #9043

Adds coverage for zero-argument cuda::mr::make_shared_resource<T>() with a default-constructible resource, and fixes shared_resource<T>(in_place_type<T>) so it constructs a T even when no constructor arguments are provided.

The fix preserves null default construction for __shared_block_ptr<T> by adding an explicit in-place payload construction path.

Tested with:

CCCL_BUILD_INFIX=opencode cmake --preset libcudacxx-cpp17 -DCMAKE_CUDA_ARCHITECTURES=native
cmake --build /home/coder/cccl/build/opencode/libcudacxx-cpp17 --target libcudacxx.test.cuda.memory_resource.shared_resource -j
ctest --test-dir /home/coder/cccl/build/opencode/libcudacxx-cpp17 -R '^libcudacxx\.test\.cuda\.memory_resource\.shared_resource$' --output-on-failure

Checklist

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

@bdice bdice requested a review from a team as a code owner May 16, 2026 00:10
@bdice bdice requested a review from wmaxey May 16, 2026 00:10
@github-project-automation github-project-automation Bot moved this to Todo in CCCL May 16, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL May 16, 2026
@bdice bdice self-assigned this May 16, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 4391aa1f-0416-4b71-a055-a96bc3b4c6be

📥 Commits

Reviewing files that changed from the base of the PR and between 2981aac and 3b1aa56.

📒 Files selected for processing (1)
  • libcudacxx/include/cuda/__memory_resource/shared_block_ptr.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • libcudacxx/include/cuda/__memory_resource/shared_block_ptr.h

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Enabled direct in-place construction of shared resources with forwarded arguments for building payloads.
  • Bug Fixes

    • Fixed argument forwarding during shared resource initialization to correctly preserve value categories.
  • Tests

    • Added coverage ensuring default construction occurs exactly once and that allocation returns a valid pointer.

suggestion:

Walkthrough

Adds an in-place constructor to __shared_block_ptr and updates shared_resource argument forwarding so make_shared_resource() with no arguments default-constructs T. Adds a test verifying a single construction and non-null allocate_sync.

Changes

In-place construction support for shared_resource

Layer / File(s) Summary
Add in-place constructor to __shared_block_ptr
libcudacxx/include/cuda/__memory_resource/shared_block_ptr.h
Added include for cuda/std/__utility/in_place.h and new constructor overload explicit __shared_block_ptr(::cuda::std::in_place_type_t<_Payload>, _Args&&... __args) that allocates a control block and forwards arguments to payload construction.
Update shared_resource argument forwarding
libcudacxx/include/cuda/__memory_resource/shared_resource.h
In-place constructor's __block_ initialization changed to use ::cuda::std::forward<_Args>(__args)... with explicit template parameter instead of bare __args..., preserving value categories through the forwarding chain.
Test default construction behavior
libcudacxx/test/libcudacxx/cuda/memory_resource/shared_resource.cu
Added default_constructible_resource test helper with static construction counter and sync memory operations. New test verifies make_shared_resource<default_constructible_resource>() constructs payload exactly once and returns non-null allocation.

Assessment against linked issues

Objective Addressed Explanation
Default-construct T when make_shared_resource<T>() called with zero arguments [#9043]
Prevent null dereference assertion in assertion-enabled builds [#9043]
Construct payload exactly once, not zero times [#9043]

Possibly related PRs

  • NVIDIA/cccl#8802: Touches shared ref-counting machinery and is related to the shared-block construction semantics corrected here.

Suggested reviewers

  • davebayer

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 16m: Pass: 100%/116 | Total: 2d 13h | Max: 2h 14m | Hits: 81%/419684

See results here.

@bdice bdice merged commit 2a660dc into NVIDIA:main May 17, 2026
135 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL May 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Successfully created backport PR for branch/3.4.x:

Comment on lines +71 to 84
//! @brief Constructs a new control block, forwarding arguments to the payload.
template <class... _Args>
_CCCL_HOST_API explicit __shared_block_ptr(::cuda::std::in_place_type_t<_Payload>, _Args&&... __args)
: __block_(new __block_t(::cuda::std::forward<_Args>(__args)...))
{}

//! @brief Constructs a new control block, forwarding arguments to the payload.
_CCCL_TEMPLATE(class _Arg, class... _Rest)
_CCCL_REQUIRES((!::cuda::std::is_same_v<::cuda::std::remove_cvref_t<_Arg>, __shared_block_ptr>) )
_CCCL_REQUIRES(
(!::cuda::std::is_same_v<::cuda::std::remove_cvref_t<_Arg>, __shared_block_ptr>)
&& (!::cuda::std::is_same_v<::cuda::std::remove_cvref_t<_Arg>, ::cuda::std::in_place_type_t<_Payload>>) )
_CCCL_HOST_API explicit __shared_block_ptr(_Arg&& __arg, _Rest&&... __rest)
: __block_(new __block_t(::cuda::std::forward<_Arg>(__arg), ::cuda::std::forward<_Rest>(__rest)...))
{}
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 believe the additional constraint should not be necessary. We should probably just require everybody to use in_place

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.

Happy to do this in a follow-up but we need to land the backport first to get CI unblocked for RAPIDS.

GCC 7 jobs failed without the additional constraint (ambiguous with two forwarding constructors).

rapids-bot Bot pushed a commit to rapidsai/rmm that referenced this pull request May 18, 2026
There is a bug in CCCL's `make_shared_resource<T>()` that doesn't work with types that have no constructor arguments. NVIDIA/cccl#9043

NVIDIA/cccl#9044 fixes it, but we need a workaround to unblock CI until that is backported (NVIDIA/cccl#9047) and rapids-cmake is updated.

Authors:
  - Bradley Dice (https://github.com/bdice)

Approvers:
  - David Wendt (https://github.com/davidwendt)

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

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

make_shared_resource<T>() default-constructs an empty shared_resource after shared_block_ptr rewrite

3 participants