← Back to Pull requests

KernelInterface: more sub-group communication (shuffles, votes, constant width) #831

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 03, 2026 22:03 UTC
Author

Adds the sub-group primitives that Molly.jl's CUDA kernels use (ext/MollyCUDAExt.jl), so that they can be written portably on top of KernelInterface. This is a companion to #830 (@groupreduce/@subgroupreduce).

New device functions

| Function | Requirement | Molly.jl use |
|---|---|---|
| shfl(val, lane) | supports_shuffle(backend, T) | rotating j-atom data around the warp in the kernel without a neighbor list |
| shfl_up(val, offset) | supports_shuffle(backend, T) | building block for sub-group scans (its neighbor finder builds a prefix count by hand) |
| shfl_xor(val, mask) | supports_shuffle(backend, T) | butterfly all-reduce (min/max/\|) of tile masks |
| sub_group_any(pred) / sub_group_all(pred) | supports_subgroups | uniform skip of pair evaluations (vote_any_sync) |
| sub_group_ballot(pred)::UInt64 | supports_subgroups, width ≤ 64 | interaction bitmasks; count_ones/trailing_zeros on the result |

  • Struct shuffles: backends implement the shuffles for primitive types only, with signatures that only match those. A generic fallback shuffles isbits structs and tuples field by field (e.g. SVector, Unitful quantities). For such types, supports_shuffle checks their fields. Molly currently gets this by overriding CUDA.jl's internal shfl_recurse.

  • Contract change: supports_shuffle and the backend implementation notes now cover all four shuffles.
  • Warp size

    get_max_sub_group_size() is now required to be a compile-time constant of the generated code. Loops over lanes and shuffle butterflies get specialized for it, which every backend can provide since kernels are already compiled for a fixed width (sub_group_size(backend)). For type-level decisions (e.g. a UInt32 vs UInt64 mask) the docs point to passing sub_group_size(backend) from the host.

    POCL

  • Shuffles: implemented with SPIRVIntrinsics' sub_group_shuffle/sub_group_shuffle_xor. Out-of-range lanes give an unspecified value instead of an InexactError.

  • Votes: implemented with SPIRVIntrinsics' sub_group_any/sub_group_all/sub_group_ballot (cl_khr_subgroups, cl_khr_subgroup_ballot).

  • Constant width: finish_module! replaces loads of __spirv_BuiltInSubgroupMaxSize with the width the kernel is compiled for (intel_reqd_sub_group_size), before optimization.
  • Depends on JuliaGPU/OpenCL.jl#526, which adds the votes and unchecked shuffle lanes to SPIRVIntrinsics. Until it's released, this PR takes SPIRVIntrinsics from that branch:

  • through [sources] in Project.toml;

  • explicitly on Julia 1.10 CI, which ignores [sources];

  • in the Buildkite jobs, where the OpenCL job used to develop SPIRVIntrinsics from OpenCL.jl's ka-0.10 branch.
  • The branch has the LLVM 10 upgrade that ka-0.10 lacks. Before merging, these should be replaced by a compat bound on the release; all of them are marked TODO.

    Built on the above (fallbacks, backends may override)

    Added after a survey of packages that use warp operations: KomaMRI, KernelIntrinsics/KernelForge, AcceleratedKernels#93, ParallelStencil, ClimaCore, IntervalMDP, …

    | Function | Fallback |
    |---|---|
    | shuffles of other primitive types (Bool, Char, 64-bit types on Metal, …) | shuffled as UInt32 words. Backends now have to support UInt32 natively. |
    | shfl(val, lane, width), shfl_down/up/xor(val, x, width) | segments of width lanes with CUDA's semantics (reads from outside the segment return the own value), built on shfl |
    | sub_group_match_any(val)::UInt64 | the lanes with the same value (===), found group by group with shfl + sub_group_ballot. CUDA could use match.any.sync |
    | sub_group_any/all(pred, width), sub_group_ballot(pred, width), sub_group_match_any(val, width) | votes within segments of width lanes (masks have a bit per lane of the segment), built on the ballot/match of the whole sub-group. Together with the shuffles with a width, this lets e.g. 32-lane tiles run on 64-wide sub-groups |
    | sub_group_reduce(op, val) | ordered tree with shfl_down, then broadcast with shfl. Backends can dispatch on typeof(op) for native reductions (Metal simd_sum, SPIR-V GroupNonUniformIAdd, CUDA redux.sync) |
    | sub_group_scan(op, val) | inclusive Hillis-Steele scan with shfl_up |

    The docs now also say how partial sub-groups behave: lanes without a work-item give unspecified shuffle values, and the votes, match, reduce and scan only take the existing work-items into account.

    The new tests are in small functions of their own, subgroup_communication_testsuite and helpers, with concrete loops. An earlier version iterated over heterogeneous tuples of functions and passed the resulting union-of-singletons value through KI.@launch, whose GC.@preserve then hit JuliaLang/julia#63482: on 1.12 and 1.13, codegen emits a null gc_preserve_begin operand, and LLVM segfaults. That is fixed on master by #63483, but the backport to 1.12/1.13 is still pending.

    Guarantees added after porting Molly, KomaMRI and ParallelStencil

    Follows from a cross-backend investigation with experiments on CUDA, AMD (wave32 and wave64), PoCL, rusticl and Intel's CPU OpenCL runtime.

  • Sub-group layout. If a work-group is 1-D, or its x extent is a multiple of the sub-group width W, sub-groups are formed from consecutive work-items, x fastest: sub-group (lin - 1) ÷ W + 1, lane (lin - 1) % W + 1.

  • - This holds on CUDA (documented), AMD, PoCL, rusticl and Intel's CPU runtime. Intel's CPU runtime forms sub-groups per row for other shapes.
    - Still to check on Metal and Intel GPUs.
    - It lets kernels with e.g. (32, 8) work-groups (ParallelStencil's default) rely on the layout.
  • shfl_down/shfl_up past the sub-group width return the work-item's own value, as CUDA's do. That makes them consistent with the shuffles with a width.

  • - Cost: free on CUDA and Metal, a select on AMD and SPIR-V.
    - shfl_xor requires mask < W.
  • Faster sub_group_reduce fallback. It is now an ordered shfl_xor butterfly over the constant width, so it unrolls and needs no broadcast in full sub-groups. KomaMRI measured the previous fallback at ~40% of a reduction-heavy kernel on CUDA. All sub-groups run the same shuffles. sub_group_scan also loops over the constant width.

  • POCL limitations:

  • - PoCL implements sub-group operations with work-group barriers, so all sub-groups of a work-group have to run the same sub-group operations. This is now documented in POCLBackend.
    - Until pocl_standalone_jll includes the PoCL fix (JuliaPackaging/Yggdrasil#15001), POCL overrides the reduce/scan fallbacks with a loop it compiles correctly in bounds-checked kernels.

    For backend packages

    KernelInterface 0.4 is still unreleased, so this extends its contract. CUDA.jl, AMDGPU.jl, oneAPI.jl, Metal.jl and OpenCL.jl will need to:

  • implement shfl, shfl_up, shfl_xor, sub_group_any, sub_group_all and sub_group_ballot;

  • narrow their shfl_down overrides to the primitive types, so that struct shuffles reach the fallback;

  • return a constant from get_max_sub_group_size (e.g. 32 % T on CUDA).
  • One open question: sub_group_ballot is required for widths ≤ 64. If a backend can't support ballot, it could get its own capability query instead.

    Tests

  • KI testsuite:

  • - rotation via shfl, shfl_up lanes and a shfl_xor butterfly all-reduce for every supported type
    - struct shuffles and supports_shuffle for structs
    - any/all/ballot with several predicate patterns
    - get_max_sub_group_size returns the width
  • Stub tests: the shuffle fallback throws for primitive types.

  • POCL test: the width is a constant in the IR.

  • Results: the full test suite passes locally on POCL (KI on POCL: 568/568).
  • 🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: maleadt Review duration: 4751m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.52) × (1 − Review Confidence (0.50)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 70%
    50%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #831"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 50.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 50.2% · Lower = rushed or shallow review
    50.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.9% · Safe to merge
    25.9%