Skip to content

No safe path for writes whose uniqueness is guaranteed by the algorithm, not a ThreadIndex #584

Closed

Description

@honeyspoon

Summary

DisjointSlice has two access paths with nothing between them:

proof required bounds checked
get_mut(ThreadIndex) a ThreadIndex yes
get_unchecked_mut(usize) none no

An algorithm whose write uniqueness comes from somewhere other than a ThreadIndex has no index to offer, so it must drop to get_unchecked_mut and lose the bounds check as well as the proof — even though the bounds are often trivially checkable.

The gap, concretely

Warp reduction. Only lane 0 writes, and warp indices are unique by construction. There is no ThreadIndex for "the warp I belong to":

#[kernel]
pub fn warp_sums(input: DisjointSlice<f32>, mut out: DisjointSlice<f32>, n: usize) {
    let gid = thread::index_1d().get();
    let mut v = if gid < n { unsafe { *input.get_unchecked(gid) } } else { 0.0 };
    v = warp::reduce_sum_f32(u32::MAX, v);

    if warp::lane_id() == 0 {
        let warp_idx = gid / 32;
        // Uniqueness holds: one write per warp, warp_idx distinct per warp.
        // But there is no ThreadIndex for warp_idx, so this is fully unchecked --
        // an out-of-range warp_idx is UB rather than a rejected write.
        unsafe { *out.get_unchecked_mut(warp_idx) = v; }
    }
}

Scatter with known-unique destinations. A permutation guarantees uniqueness, but the destination index comes from data, not from the thread index:

// `perm` is a permutation, so each dst is written exactly once -- but that fact
// lives in the caller's head, and the index is unchecked either way.
let dst = unsafe { *perm.get_unchecked(gid) };
unsafe { *out.get_unchecked_mut(dst) = value; }

In both cases the disjointness obligation is real and unavoidable. What is avoidable is throwing away the bounds check at the same time.

Why an unsafe convenience method is not the answer

I proposed exactly that in #572 (now closed): unsafe fn write(&mut self, idx, value) -> bool and unsafe fn read(&self, idx) -> Option<T>, both bounds-checked, both T: Copy.

The argument for it was that its obligations are a strict subset of get_unchecked_mut's (bounds discharged, disjointness still on the caller), so it is monotonically safer than the path callers use today. That still seems true — but it is an escape hatch, not a fix. It makes the unsound path slightly less unsound rather than making the sound path expressible, and it grows the API surface with a third access method whose safety contract users must read carefully to distinguish from the other two.

Notably, the returned bool/Option invites exactly the wrong reading: it looks like the safety signal and is not.

What a real solution looks like

A witness for uniqueness established by other means, so these writes can be safe rather than merely checked. The warp-reduction case would mint one from "I am lane 0 of warp w", and the API would accept it where it accepts a ThreadIndex today.

The hard part, and the reason I am filing this rather than proposing it: #515 shows where such a witness must not live. That PR added a per-call Uniform<T> witness and was closed because

safe code can select between two Uniform<u32> values under a thread-varying condition, or use different strides at two call sites on one slice, and two threads then hold &mut to the same element (stride 5 at (1,0) and stride 10 at (0,5) both resolve element 5).

So a uniqueness witness cannot be a per-call parameter either — safe code could vary it between calls on the same slice and reintroduce the aliasing. The transferable rule from that rejection is that a witness must be bound to the domain over which the invariant holds, and for write-disjointness that domain is the slice's whole lifetime, not one call.

Which suggests the witness belongs in the slice type — a DisjointSlice variant whose construction records how uniqueness is established (thread index, warp index, permutation) and which then hands out safe checked accessors for that scheme.

Open design questions

  • Can the warp-index case be expressed as an ordinary ThreadIndex over a coarser index space, making this a special case of DisjointSlice cannot express a thread owning N contiguous elements #583 rather than a new mechanism?
  • For the scatter case, is a data-dependent permutation ever provable, or is that irreducibly unsafe? If irreducible, the honest fix may be documentation pointing users at get_unchecked_mut with a worked disjointness argument, rather than a new method.
  • Should get_unchecked_mut's docs state the bounds obligation more prominently? Today the escape hatch is the only option for these patterns, and nothing signposts that.

Activity

  1. added a commit that references this issue on Aug 5, 2026
    20c1db0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions