Skip to content

Use Clang’s generic bit-reversal builtin for host execution - #1

Open
ivanmilevtues wants to merge 5 commits into
mainfrom
feat/bitreverse-generic-builtin
Open

ivanmilevtues wants to merge 5 commits into
mainfrom
feat/bitreverse-generic-builtin

Conversation

@ivanmilevtues

Copy link
Copy Markdown
Member

Motivation

Clang 23 adds __builtin_bitreverseg, which reverses unsigned integers without selecting a width-specific builtin. This implements the host-execution portion of the real upstream request NVIDIA#7779.

Change

  • Prefer the generic builtin in the shared cuda::std::bit_reverse host implementation when feature detection reports it available. cuda::bit_reverse delegates to the same implementation.
  • Preserve existing width-specific and portable fallbacks for other compilers. Device intrinsics and the constant-evaluation path are unchanged.
  • Extend the existing public-API test with every single-bit position and its complement, including 128-bit integers where supported. Expectations come from an independent bit-by-bit reference.

Validation

  • Compiled and ran both the standard and CUDA bit-reversal API tests with Clang 23 and GCC, in C++17 and C++20: eight passing combinations, including runtime and static assertions.
  • Confirmed Clang 23 enables the generic-builtin feature macro.
  • Repository pre-commit checks passed after formatting.
  • No GPU execution, NVCC validation, full CCCL build, or performance benchmark was performed. This does not claim a speedup.

Showcase scope

This PR targets the CodeBoarding fork, not NVIDIA upstream. It is a real, independently reviewable implementation change prepared for a later CodeBoarding demonstration. Automated C++ PR analysis is deferred until the analyzer is released; no new analysis has been generated for this PR.

Use __builtin_bitreverseg when available while retaining existing width-specific and portable fallbacks. Exercise each bit position and its complement across unsigned integer widths.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1989f-4c32-7297-aa93-b60f6e3938ff
Co-authored-by: Amp <amp@ampcode.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T21:02:26.775972Z 4d8e4ef PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Svilen-Stefanov Svilen-Stefanov left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an analysis on this PR, can we add one and maybe do something like eShop here for C++ users?

@ivanmilevtues

Copy link
Copy Markdown
Member Author

/codeboarding

1 similar comment

Copy link
Copy Markdown
Member Author

/codeboarding

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

CodeBoarding review

Status: 0 changed components (no analysed file changed)

See the full change in CodeBoarding.

graph LR
    n_CUDA_Standard_C_Library["CUDA Standard C++ Library"]
    n_Experimental_CUDA_C_Extensions["Experimental CUDA C++ Extensions"]
    n_Parallel_Algorithm_Template_Library["Parallel Algorithm Template Library"]
    n_GPU_Collective_Compute_Primitives["GPU Collective Compute Primitives"]
    n_Python_Compute_and_Task_Bindings["Python Compute and Task Bindings"]
    n_C_Runtime_and_JIT_Interface["C Runtime and JIT Interface"]
    n_Continuous_Integration_and_Documentation["Continuous Integration and Documentation"]
    n_Build_and_Static_Analysis_Tooling["Build and Static Analysis Tooling"]
    n_CUDA_Standard_C_Library -- "references execution disposition types" --> n_Experimental_CUDA_C_Extensions
    n_CUDA_Standard_C_Library -- "invokes parallel algorithms" --> n_Parallel_Algorithm_Template_Library
    n_CUDA_Standard_C_Library -- "uses device runtime primitives" --> n_C_Runtime_and_JIT_Interface
    n_Experimental_CUDA_C_Extensions -- "uses synchronization barriers and views" --> n_CUDA_Standard_C_Library
    n_Experimental_CUDA_C_Extensions -- "dispatches parallel query operations" --> n_Parallel_Algorithm_Template_Library
    n_Experimental_CUDA_C_Extensions -- "invokes device intrinsics and state helpers" --> n_C_Runtime_and_JIT_Interface
    n_Parallel_Algorithm_Template_Library -- "delegates error termination and iterators" --> n_CUDA_Standard_C_Library
    n_Parallel_Algorithm_Template_Library -- "routes memory error handling" --> n_Experimental_CUDA_C_Extensions
    n_GPU_Collective_Compute_Primitives -- "queries device and execution context" --> n_Experimental_CUDA_C_Extensions
    n_GPU_Collective_Compute_Primitives -- "dispatches parallel kernel invocations" --> n_C_Runtime_and_JIT_Interface
    n_C_Runtime_and_JIT_Interface -- "uses standard memory management utilities" --> n_CUDA_Standard_C_Library
    n_C_Runtime_and_JIT_Interface -- "controls stream and task execution" --> n_Experimental_CUDA_C_Extensions
    n_C_Runtime_and_JIT_Interface -- "manages dynamic buffer storage" --> n_Parallel_Algorithm_Template_Library
    n_C_Runtime_and_JIT_Interface -- "applies type trait modernization rules" --> n_Build_and_Static_Analysis_Tooling
    classDef added fill:#1f883d,stroke:#0b5d23,color:#ffffff;
    classDef modified fill:#bf8700,stroke:#7d4e00,color:#ffffff;
    classDef deleted fill:#cf222e,stroke:#82071e,color:#ffffff,stroke-dasharray:5 3;
Loading

download artifacts · run 42613379981

Consolidate the leading- and trailing-zero fallbacks around popcount while retaining compiler and device intrinsics.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1c083-fef7-73f1-b175-43fb1b9be831
Co-authored-by: Amp <amp@ampcode.com>
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

CodeBoarding review

Status: 5 changed components

See the full change in CodeBoarding.

graph LR
    n_CUDA_Standard_C_Library["CUDA Standard C++ Library"]
    n_Experimental_CUDA_C_Extensions["Experimental CUDA C++ Extensions"]
    n_Parallel_Algorithm_Template_Library["Parallel Algorithm Template Library"]
    n_GPU_Collective_Compute_Primitives["GPU Collective Compute Primitives"]
    n_Python_Compute_and_Task_Bindings["Python Compute and Task Bindings"]
    n_C_Runtime_and_JIT_Interface["C Runtime and JIT Interface"]
    n_Continuous_Integration_and_Documentation["Continuous Integration and Documentation"]
    n_Build_and_Static_Analysis_Tooling["Build and Static Analysis Tooling"]
    n_CUDA_Standard_C_Library -- "references execution disposition types" --> n_Experimental_CUDA_C_Extensions
    n_CUDA_Standard_C_Library -- "invokes parallel algorithms" --> n_Parallel_Algorithm_Template_Library
    n_CUDA_Standard_C_Library -- "uses device runtime primitives" --> n_C_Runtime_and_JIT_Interface
    n_Experimental_CUDA_C_Extensions -- "uses synchronization barriers and views" --> n_CUDA_Standard_C_Library
    n_Experimental_CUDA_C_Extensions -- "dispatches parallel query operations" --> n_Parallel_Algorithm_Template_Library
    n_Experimental_CUDA_C_Extensions -- "invokes device intrinsics and state helpers" --> n_C_Runtime_and_JIT_Interface
    n_Parallel_Algorithm_Template_Library -- "delegates error termination and iterators" --> n_CUDA_Standard_C_Library
    n_Parallel_Algorithm_Template_Library -- "routes memory error handling" --> n_Experimental_CUDA_C_Extensions
    n_GPU_Collective_Compute_Primitives -- "queries device and execution context" --> n_Experimental_CUDA_C_Extensions
    n_GPU_Collective_Compute_Primitives -- "dispatches parallel kernel invocations" --> n_C_Runtime_and_JIT_Interface
    n_C_Runtime_and_JIT_Interface -- "uses standard memory management utilities" --> n_CUDA_Standard_C_Library
    n_C_Runtime_and_JIT_Interface -- "controls stream and task execution" --> n_Experimental_CUDA_C_Extensions
    n_C_Runtime_and_JIT_Interface -- "manages dynamic buffer storage" --> n_Parallel_Algorithm_Template_Library
    n_C_Runtime_and_JIT_Interface -- "applies type trait modernization rules" --> n_Build_and_Static_Analysis_Tooling
    classDef added fill:#1f883d,stroke:#0b5d23,color:#ffffff;
    classDef modified fill:#bf8700,stroke:#7d4e00,color:#ffffff;
    classDef deleted fill:#cf222e,stroke:#82071e,color:#ffffff,stroke-dasharray:5 3;
    class n_CUDA_Standard_C_Library,n_Parallel_Algorithm_Template_Library,n_C_Runtime_and_JIT_Interface modified;
Loading

download artifacts · run 42647988120

ivanmilevtues and others added 3 commits November 9, 2026 15:27
Compose lazy bit-reversed indices with permutation iterators and the existing copy backend. Support explicit execution policies and inferred iterator systems, with CPU and GPU coverage and an FFT-style reordering example.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1c083-fef7-73f1-b175-43fb1b9be831
Co-authored-by: Amp <amp@ampcode.com>
Replace the duplicated items-per-thread calculation with the existing CUB helper, preserving behavior.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1c083-fef7-73f1-b175-43fb1b9be831
Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants