Commit bc358d8
authored
feat(rocke): quad_perm crosslane intrinsic + kernarg-packer precompile (#12070)
ISSUE ID : AICK-1513
## What this is
PR 1 of a 4-PR stack that splits the GDN gfx950 work (originally one
51-file PR) into reviewable layers. This is the foundation layer: it
adds a new crosslane hardware primitive and a launch-path speedup, with
**no GDN code** and no dependency on the other three PRs.
Stack (bottom → top): **quad_perm (this)** → dispatch.core hoist → GDN
decode → GDN prefill.
## What changed
**1. `quad_perm` — an intra-quad DPP permute intrinsic, added to both
engines.**
`quad_perm(data, [p0,p1,p2,p3])` lets lane `4q+i` read `data` from lane
`4q+perm[i]` on the VALU (via `v_mov_b32_dpp` /
`llvm.amdgcn.update.dpp.i32`), with no LDS crossbar and no `lgkmcnt`
wait. Also adds `warp_shuffle_xor_quad`, a fast path for the two
xor-masks that stay inside a quad: masks 1 and 2 lower to `quad_perm`,
and **any other mask is rejected** — callers needing a wider mask use
`warp_shuffle_xor`, which goes through `ds_swizzle`.
- Python engine: `core/ir.py` (builder + validation),
`core/lower_hip.py`, `core/lower_llvm.py`.
- C++ engine (live): `core/ir/ir_flow.cpp`, `core/ir/core_types.cpp`
(opcode + purity), `core/lower_hip/lower_hip_mma.cpp`,
`core/lower_llvm/crosslane.cpp`, `include/rocke/ir.h`.
- Coverage: Python `tests/test_rocke.py`, C++
`tests/core/future_intrinsic_lowering.cpp`, and cross-engine parity
`tests/instances/parity/target_intrinsics_emit.{c,py}` (identical IR
from both lowerers).
**2. Kernarg-packer precompile — a launch-path speedup for every
kernel.**
`runtime/packing.py` gains `compile_packer(signature)`;
`runtime/launcher.py` compiles the packer once at module-load and calls
it per launch instead of re-deriving the layout each time.
Byte-identical to `pack_args`.
What it removes is **kernarg layout reconstruction** — the
offset/alignment walk, the per-argument type dispatch, and the
format-string assembly. It is *not* a saving on format compilation:
CPython's `struct` module already caches recently used format strings,
so re-packing the same format is a cache lookup, not a recompile.
## Review round 1 — what changed since the first push
Four items were raised by @yraparti and @tenpercent. All four are
addressed.
**1. Lowerers accepted out-of-range `ctrl` (`bffb5fd911f`).** All four
`quad_perm` lowering sites masked the control word with `0xFF` instead
of validating it. `ctrl` packs four two-bit lane selectors (`p0 | p1<<2
| p2<<4 | p3<<6`), so `0..255` is the whole legal range — and masking
silently rewrote malformed IR into a *different, valid* permute: `256`
became `0` (`[0,0,0,0]`, a lane-0 broadcast) and `-1` became `255`
(`[3,3,3,3]`). A wrong reduction then computed wrong numbers instead of
failing.
The builders already validate selectors, but IR reaching a lowerer by
another route (deserialized, rewritten by a pass, hand-built) skips
them. Now rejected in the Python lowerers (`ValueError`) and the C++
ones (`ROCKE_ERR_VALUE`), with the mask dropped so the check cannot be
bypassed. Tests on both sides are **mutation-verified**: with the mask
restored, the Python subtests and the four C++ assertions fail.
**2. Wave-size semantics undocumented (`cfeec0a1315`, `8cea213942f`).**
`quad_perm` makes no wave-size assumption, and both engines now say so:
the control word applies within every four-lane group, and four divides
both 32 and 64, so a lane never addresses outside its own quad. Wave
size changes only the *number* of quads (8 in wave32, 16 in wave64).
Deliberately scoped — wave-size-independent is **not**
architecture-independent. The op still needs DPP-capable hardware; the
useful point is that base-DPP `quad_perm` is available on CDNA where
`dpp_xor`'s RDNA-only `row_xmask` is not. Also recorded: the op has no
lane targeting (the control is broadcast to every quad, row/bank masks
fixed at `15, 15`), so selecting a subset of quads is the caller's job.
A first draft of this docstring cited `dpp_xor` as an op whose partner
lane can leave the wave; `8cea213942f` corrects that. `dpp_xor` caps
`xor_mask` at `1..15` and its partner stays inside a 16-lane row, which
also divides both wave sizes. The accurate counterexample is
`warp_shuffle_xor` at `lane_xor = 32`.
**3. "The dominant Python cost" was unbacked (`786f6c827b0`).** Correct:
a packing-only microbenchmark cannot establish a share of launcher
overhead. Both sites now describe the mechanism instead of a magnitude
-- the packer precomputes the fixed argument layout once per launcher,
so a launch does not rebuild the offset table, re-dispatch on argument
types, or re-assemble the format string. `struct` already caches
recently used format strings, so the saving is that surrounding work
rather than the format compile itself.
The profiler written to measure the share now lives in its own PR
(#12168, draft), and performance claims are deferred until its gate is
fixed: its chunk-size-sensitivity check is a ratio, so a chunk size
large enough to saturate both comparison windows accepts a
back-pressured run.
**4. PR description misdescribed `warp_shuffle_xor_quad`.** It said the
helper "leaves wider masks on `ds_swizzle`", implying a dispatcher. It
is a specialist: masks other than 1 and 2 raise. Corrected in the
What-changed section above.
## Scope note (disclosed)
Item 2 is functionally independent of item 1. It is kept here rather
than split for one concrete reason: its byte-identity test
(`test_compile_packer_matches_pack_args_byte_for_byte`) lives in
`tests/test_rocke.py`, the same file that holds the `quad_perm` tests —
splitting would put two PRs on one test file. Reviewers should weigh the
packer's **global blast radius** (it changes the launch path for every
kernel), not only its decode benefit.
## Why it's safe
- No kernel IR changes — this only adds an opcode and a runtime fast
path.
- `quad_perm` is validated at the builder (selectors ∈ 0..3, i32-only)
**and now at every lowerer** (`ctrl` ∈ 0..255), and is marked pure in
both engines.
- `llvm.amdgcn.update.dpp.i32` is an already-shipped overload (used by
`mov_dpp8`/`ds_swizzle`), not a new intrinsic surface.
- The packer is asserted byte-identical to the existing `pack_args`.
## Why it's first in the stack
GDN decode's in-quad xor-butterfly reduction lowers to `quad_perm` (3
call sites), so this must land before the decode PR. It touches only
`platform/`, so it reviews without any GDN context.
## Verification (run, not planned)
Both engines built and exercised locally on this diff:
- Python, with the C++ extension importable: `pytest tests/test_rocke.py
-k "quad_perm or warp_shuffle_xor_quad or compile_packer"` → **8 passed,
0 skipped** (the cross-engine assertions no longer skip). Full file:
**291 passed, 14 skipped, 43 subtests**.
- C++ engine: `rocke_future_intrinsic_lowering` → **31 case(s) OK**.
- Cross-engine byte-identity: `tools/check_byte_identity.py --only
target_intrinsics` → **GREEN, configs=8, bad=0** — Python and C++ emit
identical `.ll`.
- `test_rocke_ci_static.py` **5 passed**.
- Formatters: `black` clean; `clang-format` applied (local binary is
v20; repo pre-commit pins v18.1.4, so CI may adjust whitespace).
GPU numerical execution of `quad_perm` itself and the full all-family
byte-identity gate were **not** run here.
## Stacking / base
#11807 has merged, and this PR is now based directly on `develop`.1 parent 9342d1b commit bc358d8
14 files changed
Lines changed: 575 additions & 2 deletions
File tree
- dnn-providers/hip-kernel-provider/rocke/platform
- cpp
- core
- ir
- lower_hip
- lower_llvm
- include/rocke
- python/rocke
- core
- runtime
- tests
- core
- instances/parity
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
581 | 581 | | |
582 | 582 | | |
583 | 583 | | |
| 584 | + | |
584 | 585 | | |
585 | 586 | | |
586 | 587 | | |
| |||
826 | 827 | | |
827 | 828 | | |
828 | 829 | | |
| 830 | + | |
829 | 831 | | |
830 | 832 | | |
831 | 833 | | |
| |||
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
185 | 211 | | |
186 | 212 | | |
187 | 213 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
330 | 353 | | |
331 | 354 | | |
332 | 355 | | |
| |||
1346 | 1369 | | |
1347 | 1370 | | |
1348 | 1371 | | |
| 1372 | + | |
1349 | 1373 | | |
1350 | 1374 | | |
1351 | 1375 | | |
| |||
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
328 | 352 | | |
329 | 353 | | |
330 | 354 | | |
| |||
1050 | 1074 | | |
1051 | 1075 | | |
1052 | 1076 | | |
| 1077 | + | |
1053 | 1078 | | |
1054 | 1079 | | |
1055 | 1080 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| 363 | + | |
363 | 364 | | |
364 | 365 | | |
365 | 366 | | |
| |||
1137 | 1138 | | |
1138 | 1139 | | |
1139 | 1140 | | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
1140 | 1150 | | |
1141 | 1151 | | |
1142 | 1152 | | |
| |||
Lines changed: 59 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2845 | 2845 | | |
2846 | 2846 | | |
2847 | 2847 | | |
| 2848 | + | |
| 2849 | + | |
| 2850 | + | |
| 2851 | + | |
| 2852 | + | |
| 2853 | + | |
| 2854 | + | |
| 2855 | + | |
| 2856 | + | |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
| 2862 | + | |
| 2863 | + | |
| 2864 | + | |
| 2865 | + | |
| 2866 | + | |
| 2867 | + | |
| 2868 | + | |
| 2869 | + | |
| 2870 | + | |
| 2871 | + | |
| 2872 | + | |
| 2873 | + | |
| 2874 | + | |
| 2875 | + | |
| 2876 | + | |
| 2877 | + | |
| 2878 | + | |
| 2879 | + | |
| 2880 | + | |
| 2881 | + | |
| 2882 | + | |
| 2883 | + | |
| 2884 | + | |
| 2885 | + | |
| 2886 | + | |
| 2887 | + | |
| 2888 | + | |
| 2889 | + | |
| 2890 | + | |
| 2891 | + | |
| 2892 | + | |
| 2893 | + | |
| 2894 | + | |
| 2895 | + | |
| 2896 | + | |
| 2897 | + | |
| 2898 | + | |
| 2899 | + | |
| 2900 | + | |
| 2901 | + | |
| 2902 | + | |
| 2903 | + | |
| 2904 | + | |
| 2905 | + | |
2848 | 2906 | | |
2849 | 2907 | | |
2850 | 2908 | | |
| |||
4477 | 4535 | | |
4478 | 4536 | | |
4479 | 4537 | | |
| 4538 | + | |
4480 | 4539 | | |
4481 | 4540 | | |
4482 | 4541 | | |
| |||
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1971 | 1971 | | |
1972 | 1972 | | |
1973 | 1973 | | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
1974 | 1990 | | |
1975 | 1991 | | |
1976 | 1992 | | |
| |||
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3800 | 3800 | | |
3801 | 3801 | | |
3802 | 3802 | | |
| 3803 | + | |
| 3804 | + | |
| 3805 | + | |
| 3806 | + | |
| 3807 | + | |
| 3808 | + | |
| 3809 | + | |
| 3810 | + | |
| 3811 | + | |
| 3812 | + | |
| 3813 | + | |
| 3814 | + | |
| 3815 | + | |
| 3816 | + | |
| 3817 | + | |
| 3818 | + | |
| 3819 | + | |
| 3820 | + | |
| 3821 | + | |
| 3822 | + | |
3803 | 3823 | | |
3804 | 3824 | | |
3805 | 3825 | | |
| |||
Lines changed: 10 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| |||
420 | 420 | | |
421 | 421 | | |
422 | 422 | | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
423 | 431 | | |
424 | 432 | | |
425 | 433 | | |
| |||
436 | 444 | | |
437 | 445 | | |
438 | 446 | | |
439 | | - | |
| 447 | + | |
440 | 448 | | |
441 | 449 | | |
442 | 450 | | |
| |||
Lines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
84 | 145 | | |
85 | 146 | | |
86 | 147 | | |
| |||
0 commit comments