Files
metal2vulkan/docs
Anees Iqbal 0a82834fd2 rt: a kernel that does not observe its threadgroup partition runs its grid in fitted tiles, with no division
A kernel that does not observe its threadgroup partition reads only thread_position_in_grid and
threads_per_grid, so only the set of positions matters. Its partial-threadgroups variant used to
answer Metal's partial threadgroups in rt's tiles (256x1, or the grid's width rounded down to a
power of two): each thread rebuilt its position by UDiv/UMod of its index by the threads left, and
tiles fitted only by width launched 1.68x to 4x the grid's threads on 3D grids of 8x8
threadgroups. On the RX 550 such rows ran up to 3.67x the old runtime's time.

On Device::from_queries' profile under the identity domains (ProfileFacts' new ObliviousEdge,
Grid there, Partial on Device::new's, whose modules stay byte-equal), such a variant states
RuntimeFact::GridTiles and runs its grid in tiles:
- Under MultiRegion 1 its workgroup of GRID_TILE_THREADS (256) is one tile whose power-of-two
  sides the region's nominal size gives. Its position is ((base + WorkgroupId) << s) +
  (idx & m.x, (idx >> s.x) & m.y, idx >> (s.x + s.y)), with m = L - 1, s = bitCount(m), and idx =
  LocalInvocationIndex: masks and shifts of uniform values, no division, nothing new pushed.
- Where the region is flagged NONUNIFORM, an invocation past the grid returns before the body.
  On the RX 550 the driver folds the flag into the exit's exec mask, so the three compares issue
  on every wave; the prologue to that exit is 10 VALU (7 for the position, 3 compares) and 18
  SALU, where the old runtime's is 14 VALU. One pipeline and one form: no second pipeline for
  whole grids.
- Under MultiRegion 0 (an indirect dispatch) it runs at its pipeline's size, as a plain kernel:
  the specialization folds the tile and the exit away.

pipeline::grid_tile(threads) picks the tile from the grid's extents alone, with no device input:
sides 2^a, 2^b, 2^c with a + b + c = 8, the first in one total order over those 45 tiles:
1. the fewest threads launched;
2. then the fewest tiles partly past the grid;
3. then the shallowest (z side 1 first);
4. then, where the grid has two rows or more, the height nearest 2 (|b - 1| smallest);
5. then the widest.
A one-row grid takes 256x1 by its waste. Why this order: among tiles of equal waste the shape
measured within 3% of the best everywhere is 128x2. On the UHD 630, the slowest GPU, 256x1 was
first on every image row and 64x4 cost 5% to 10%; 128x2 stayed within 3% of 256x1 on every such
row. On the 780M avoiding wide tiles helped (sampling 1.028 of the old runtime), and on the RX 550
having more than one row did (texture_write 1.024). The measured rows are its budgets: the RX 550's
21 rows of 3D grids of 8x8 threadgroups, waves, sample_down and sample_up, and the texture
rows on the 780M, 3090 and RX 550.

grid_tile's paths:
- Where the powers of two that divide the extents reach 256 it takes the first tile that
  divides the grid.
- Where one tile holds the grid it takes the first tile that holds it.
- Otherwise it tries the 45 tiles in the order, computing each tile's counts as it goes. It
  passes over a depth 2^c whose bound, ceil(z / 2^c) * ceil(x*y / 2^(8 - c)), exceeds the fewest
  tiles found so far.
24x24x252 and 8x8x360 take 8x8x4 with no waste, 8x8x22 8x8x4 (1.09x), 304x304 16x16,
1024x1024 128x2, 5600x4200 32x8, a row 256x1 and a column 1x256.

rt runs every direct dispatch of such a kernel on one VkPipeline (256x1x1, MultiRegion 1, made
with the state), each region carrying its tile. The indirect pipeline is made at the state's
first indirect dispatch, a first-use wait, and is counted in the pipeline gauge. The
translator's binder plans a GridTiles kernel's Threads and Threadgroups dispatches the same way,
and a ThreadgroupRange at its own size with MultiRegion 0.

Tests:
- lower::dispatch: the rule's table (whole and ragged square, wide, tall, deep, one-row and
  one-tile grids, each with its tile), and grid_tile against the order taken over every tile on
  12,850 grids.
- engine_identity: on the interpreter, ragged-in-3D, whole, z-ragged, row and column grids store
  each of Metal's positions once and nothing past the grid; an indirect dispatch runs at its own
  size; from_queries' module divides by no run-time value, Device::new's still does.
- rt translate: the narrow threadgroup dispatch now shares the tiles' pipeline. The tiles test's
  indirect dispatch makes one more pipeline by one first-use wait, and every gauge, pipelines
  included, is zero at the device's drop.

SEMANTICS_VERSION 2166; ARTIFACT_FORMAT_VERSION 533 (the new fact).
2026-11-17 16:30:42 -08:00
..