Files
reims-vgpu/scripts/scattered-struct/README.md
T
Anees IqbalandClaude 2acd5e229e ♻️ A dispatch's two extents had a name but not a type, and one arm read them as six offset literals
`crates/reims-vgpu/src/runtime/compute_exec/mod.rs`. No behavior change.

`DispatchDims` was `type DispatchDims = (u32, u32, u32, u32, u32, u32, bool)` —
a name over an anonymous seven-tuple. Four arms of `resolve_dispatch_dims`
constructed it positionally and two callers destructured it, so the grid and
threadgroup extents existed as six interchangeable `u32`s everywhere between.

Now a struct of two `Extent3`s and a flag. `decode::compute::Size3` could not
be reused for the halves: it is `u64` on the wire, and the resolve narrows each
component through `u32_dim`, which is where an out-of-range dimension becomes
the named refusal `compute_grid_dim_range` rather than a substitution.

The `DispatchThreadsIndirect` arm is why this was worth doing. It read:

    Ok((
        u32_dim(ld32(&raw[0..]) as u64)?,
        u32_dim(ld32(&raw[4..]) as u64)?,
        u32_dim(ld32(&raw[8..]) as u64)?,
        u32_dim(ld32(&raw[12..]) as u64)?,
        u32_dim(ld32(&raw[16..]) as u64)?,
        u32_dim(ld32(&raw[20..]) as u64)?,
        true,
    ))

Six calls differing only by an offset literal, spanning the two halves of
`MTLDispatchThreadsIndirectArguments`. Now `Extent3::from_indirect(&raw, 0)`
and `Extent3::from_indirect(&raw, 12)`: one stride expression, one base offset
per half. A transposition among those six literals dispatches a *valid* grid of
the wrong shape — no refusal, nothing to log — which is the same silent class
as `81146ef`'s blend halves.

`Extent3::from_wire` collapses the three other arms' `u32_dim(cmd.grid.x)?, …`
ladders the same way.

Tests: new `dispatch_threads_indirect_reads_both_extents_from_the_buffer`
drives that arm with six distinct values (11, 22, 33 / 44, 55, 66) and sets
`threads_per_threadgroup` on the wire to 99s that appear nowhere in the buffer,
because this is the one arm that must *not* read the threadgroup from the wire.
Verified non-vacuous by swapping the two `from_indirect` base offsets: fails
with "left: Extent3 { x: 44, y: 55, z: 66 }, right: Extent3 { x: 11, y: 22,
z: 33 }".

`resolve_indirect_threadgroups_from_buffer` stops asserting a flat septuple and
now asserts the two extents separately — as a septuple it could not say which
half came from the buffer and which from the wire, which is that arm's entire
subject.

The `scattered-struct` README's standing finding is updated: producer done,
consumer (`compute_core`, `compute_encode_on_encoder`) still open and why.

Verification: `cargo test -p reims-vgpu --no-default-features --features
backend-vulkan,host-window -- --test-threads=1` — 1365 lib (up one) plus all
integration tests pass. Clippy clean with `-D warnings` on the Vulkan arm and
on `backend-metal` via `--target aarch64-apple-darwin`. `cargo fmt --all
--check` silent.

Not verified: no live VM boot. The indirect-dispatch arms need a guest issuing
`dispatchThreadgroupsWithIndirectBuffer:`, which the desktop workload does not,
so the synthetic tests are the coverage either way.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-05 05:05:36 +03:00

145 lines
7.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# scattered-struct
Finds values that travel as loose parameters when a type for them already
exists, or obviously wants to.
```sh
scripts/scattered-struct/scattered-struct.sh [crate-src-dir ...]
# defaults: crates/reims-vgpu/src and crates/reims-vgpu-wire/src
```
## Why this finds things clippy cannot
`clippy::too_many_arguments` fires on arity alone, so it is silent on a
four-parameter function that should take one struct and loud on a nine-parameter
one that legitimately takes nine. It also cannot see the thing that makes these
dangerous, which is not the *count* but the **run of same-typed neighbours**: a
signature ending `volume, cube, arrayed, one_dim` accepts all 24 permutations of
those four `bool`s and the compiler objects to none of them. Every commit this
script was written out of found the same shape, and in each the type that should
have travelled was already declared somewhere in the crate.
Three report classes.
### A type built out of the parameters passed to build it
A function whose body constructs a struct literal using four or more of its own
parameters in shorthand. The body's own words say the values belong together;
the signature took them apart. The fix is to move the construction to the call
site and pass the type.
The reason it is worth doing beyond tidiness: a caller that needs one field to
differ then says so by name — `SampledKey { format: resident_color(bgra),
..SampledKey::of(r) }` — where a positional call could only express it by
putting a different expression in the eighth slot, which nothing reading the
call would notice.
### An enum arm flattened into loose primitives
A `match` whose arms are tuples of bare literals. The enum is the type; the
tuple is where it stopped being one. A run of these over one enum in one file is
the finding — a lone arm is usually something else.
This class is the more serious of the two, because flattening an *n*-state enum
into *k* booleans manufactures `2^k − n` states that the type could not express.
Every consumer downstream then has to decide what the extra states mean, and
they will not all decide the same way: five functions inheriting one such
flattening had four different readings of the state that could not occur.
### A run of adjacent parameters of one primitive type
Four or more neighbouring parameters sharing one primitive type. Every
permutation of the run compiles and no call site can object, so the run is the
hazard whether or not a type is waiting for it.
**The finding is a run repeated across a family**, not a long run on its own.
`mapping_write`'s five rect functions all took `origin_x, origin_y, width,
height` and four of them added `bpp`; that repetition says the rectangle is a
thing the module keeps re-spelling.
Sort the report by how many *signatures* share a run before sorting by run
length — but do not stop there. A run that looks like an SDK's parameter list
still needs its *callers* checked: `compute_core`'s six dispatch dimensions
read as a mirror of `dispatchThreadgroups:threadsPerThreadgroup:` and were
triaged away here on that basis, until the producer turned out to be a function
returning an anonymous seven-tuple over a type that already existed. See the
standing findings below. The SDK-mirror exemption is about where a value is
*going*, and this class is about where it has been.
## What a hit can legitimately be
The report points at code; it does not convict it.
- **The parameters are transformed on the way in.** `batch_append` puts seven of
its eight parameters into an `OpenBatch`, but `dset` becomes a collection and
`sampled_retains` is moved, and its other arm — the joiner — uses three of the
seven and discards the rest. Passing a built `OpenBatch` would hand the joiner
a value it exists to not need.
- **The struct is the function's output, not its input.** `registry_ensure` and
`fill_render_pso_key` are named for producing the thing they build. A
constructor assembling its own fields from parameters is what a constructor
is; the smell is only present when *callers* already hold the assembled value.
- **The enum arm carries genuinely unrelated values.** Two literals that happen
to share an arm are not a flattened type. Look for the same arity in every arm
and a struct built immediately after the `match`.
- **The run mirrors an API this code does not own, *and arrives loose*.** A run
reproducing an SDK call's parameter list is contract fidelity, and grouping it
would put a translation step between the decoded values and the call that
consumes them. Both halves are required: `compute_core` satisfies the first
and fails the second, which is why it is a standing finding below rather than
an example here.
The class that is always worth fixing is the last one: **callers that all hold
the same source value and spell its fields out**. Grep the call sites before
deciding — if every one of them reads `f(x.a, x.b, x.c, x.d)`, the type is
already there and the signature is the only thing that disagrees.
## Standing findings
Recorded so the next reader starts from the triage rather than the raw report.
- **`mapping_write`'s rectangle** — *done*, see the commit that introduced
`SurfaceWindow` and `Rect`. Worth keeping the method: the 29 call sites were
rewritten by a script that split each argument list at top-level commas and
regrouped by position, rather than by hand. Hand-editing 29 sites where four
of the arguments are same-typed `u32`s is how a crossing gets introduced by
the very commit that exists to prevent one.
- **`translate::blend::state`** — *done*. Two triples where a half-swap returns
`Ok` with the wrong channels blended, so there is nothing for the fail
channel to say. Now takes the decoded `&PipelineColorAttachment`.
- **The compute dispatch dimensions** — open, and a correction to this file's
own earlier triage. `compute_core` and `compute_encode_on_encoder` were
called SDK mirrors here because `grid_x, grid_y, grid_z, tg_x, tg_y, tg_z`
reproduces `dispatchThreadgroups:threadsPerThreadgroup:`. That is true of the
*Metal call at the bottom*, but not of how the values arrive:
`compute_exec::resolve_dispatch_dims_reported` returns
`(u32, u32, u32, u32, u32, u32, bool)` — an anonymous seven-tuple destructured
by two callers — and `decode::compute::Size3` already exists as the type for
each half. So this is the `Point` shape after all, with an SDK-shaped call
only at the far end.
**The producer half is done.** `DispatchDims` was a type alias over
`(u32, u32, u32, u32, u32, u32, bool)` — it had a name but no type — and is
now a struct of two `Extent3`s. `Size3` itself could not be reused: it is
`u64` on the wire and the resolve narrows each component through `u32_dim`.
The consumer half is still open: `compute_core` and
`compute_encode_on_encoder` take the six as loose `u32`s. Left for a session
that can do better than clippy on it — their bodies are `backend-metal`,
which is Apple-only, so a Linux host can cross-compile and lint them but
cannot run their tests. Note also that `dispatch_kind` and `dispatch_type`
sit inside the same run while belonging to neither extent, which is part of
why it reads as eight interchangeable `u32`s.
## Known limits
Regex over lines, not a parser. It requires a struct literal's fields in
shorthand on their own lines, so `Foo { a: a, b: b }` and single-line literals
are invisible; and it reads only the first qualifying literal per function. A
clean report is weak evidence, a hit is strong evidence. Two filters exist
because their absence produced nonsense rather than noise: `) -> Status {` reads
as a struct literal to any regex this simple, and the field scan must stop at
the literal's own closing brace or it collects every shorthand field in the
function — that one reported eleven matching fields for a six-parameter
function.