Skip to content

Use __builtin_bswapg in cuda::std::byteswap when available - #9785

Merged
Jacobfaib merged 1 commit into
NVIDIA:mainfrom
Functionhx:fix/bswapg-builtin
Jul 10, 2026
Merged

Use __builtin_bswapg in cuda::std::byteswap when available#9785
Jacobfaib merged 1 commit into
NVIDIA:mainfrom
Functionhx:fix/bswapg-builtin

Conversation

@Functionhx

Copy link
Copy Markdown
Contributor

Fixes #7778.

@Functionhx
Functionhx requested a review from a team as a code owner July 10, 2026 10:14
@Functionhx
Functionhx requested a review from Jacobfaib July 10, 2026 10:14
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jul 10, 2026
@copy-pr-bot

copy-pr-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jul 10, 2026
if constexpr (sizeof(_Integer) > 1)
{
#if defined(_CCCL_BUILTIN_BSWAPG)
return static_cast<_Integer>(_CCCL_BUILTIN_BSWAPG(::cuda::std::__to_unsigned_like(__val)));

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.

__builtin_bswapg is supposed to support any integer, do we still need the cast to unsigned?

Also on this note, we should add support for the sized builtins as well, __builtin_bswapg was only added in clang 22 (not sure if gcc even has it to begin with). Before that, __builtin_bswap{16, 32, 64} and possibly __builtin_bswap128 have existed for longer.

@Jacobfaib

Copy link
Copy Markdown
Contributor

/ok to test

@copy-pr-bot

copy-pr-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

/ok to test

@Jacobfaib, there was an error processing your request: E1

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/

@Jacobfaib

Copy link
Copy Markdown
Contributor

/ok to test bb4ef02

@github-actions

This comment has been minimized.

@Functionhx

Copy link
Copy Markdown
Contributor Author

Will do — I'll verify the unsigned cast is redundant and add the sized builtin fallback. Thanks for the review.

@Functionhx
Functionhx force-pushed the fix/bswapg-builtin branch from bb4ef02 to 198d99a Compare July 10, 2026 16:38
@Jacobfaib

Copy link
Copy Markdown
Contributor

/ok to test 198d99a

#if defined(_CCCL_BUILTIN_BSWAPG)
return static_cast<_Integer>(_CCCL_BUILTIN_BSWAPG(__val));
#else // ^^^ _CCCL_BUILTIN_BSWAPG ^^^ / vvv !_CCCL_BUILTIN_BSWAPG vvv
if constexpr (sizeof(_Integer) == 2)

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.

Don't hardcode the size here, I would say sizeof(_Integer) == sizeof(::cuda::std::uint16_t) since that is what we ultimately care about in the end.

if constexpr (sizeof(_Integer) == 2)
{
# if defined(_CCCL_BUILTIN_BSWAP16)
return static_cast<_Integer>(_CCCL_BUILTIN_BSWAP16(static_cast<uint16_t>(__val)));

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.

Suggested change
return static_cast<_Integer>(_CCCL_BUILTIN_BSWAP16(static_cast<uint16_t>(__val)));
return static_cast<_Integer>(_CCCL_BUILTIN_BSWAP16(static_cast<::cuda::std::uint16_t>(__val)));

Unfortunately we must fully qualify all types and calls like this. It's not so strictly required for types, but better safe than sorry

@Functionhx

Copy link
Copy Markdown
Contributor Author

Addressed both follow-up comments in 708003e: replaced the hardcoded size checks with comparisons against the corresponding fully qualified fixed-width types, and fully qualified the cast types in all three sized-builtin branches. Local pre-commit and the targeted std/numerics/bit/byteswap.pass.cpp lit test both pass.

Comment thread libcudacxx/include/cuda/std/__bit/byteswap.h Outdated
Signed-off-by: Yuchen Fan <functionhx@gmail.com>
@Functionhx
Functionhx force-pushed the fix/bswapg-builtin branch from 708003e to 98b083b Compare July 10, 2026 17:08
@Jacobfaib

Copy link
Copy Markdown
Contributor

/ok to test 98b083b

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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: 59b56197-3676-4122-a9bd-67e8adccaaa8

📥 Commits

Reviewing files that changed from the base of the PR and between 0fa6cea and 98b083b.

📒 Files selected for processing (1)
  • libcudacxx/include/cuda/std/__bit/byteswap.h

📝 Walkthrough

Summary by CodeRabbit

  • Performance
    • Improved byte-swapping for supported integral types by using an optimized compiler-provided operation when available.
    • Preserved existing fallback behavior across supported compilation environments.

Walkthrough

cuda::std::byteswap detects builtin_bswapg, disables it for NVCC device compilation, and uses it for integral types larger than one byte when available.

Changes

Byteswap builtin integration

Layer / File(s) Summary
Builtin selection and byteswap dispatch
libcudacxx/include/cuda/std/__bit/byteswap.h
Preprocessor detection and device guards add _CCCL_BUILTIN_BSWAPG; byteswap uses the builtin for multi-byte integers and retains the existing fallback.

Assessment against linked issues

Objective Addressed Explanation
Use __builtin_bswapg in cuda::std::byteswap [#7778]

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

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 52m: Pass: 100%/120 | Total: 1d 21h | Max: 1h 19m | Hits: 100%/356615

See results here.

@Jacobfaib
Jacobfaib merged commit a6d373c into NVIDIA:main Jul 10, 2026
139 of 140 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Jul 10, 2026
@Jacobfaib

Copy link
Copy Markdown
Contributor

Thanks for the patch!

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

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[FEA]: Use __builtin_bswapg in cuda::std::byteswap

2 participants