Commit a3521bc
authored
Add wide_stores support for MXFP4 (4wave) GEMM (llvm backend) (#1282)
## Wide epilogue stores for bf16 MXFP4 GEMM via `v_permlane16_swap`
In MXFP4 GEMM kernels with bf16 output, the MMA accumulator layout
(`F32_16x16x128_F8F6F4`) distributes results across threads in a
non-contiguous pattern each thread holds 4 values spanning 4 rows but
only 1 column. Without optimization, the epilogue emits 192 scalar
`buffer_store_short` (2 bytes each), underutilizing memory bandwidth.
This PR adds a `wide_stores` that swaps MFMA operands (B as LHS, A as
RHS) and uses `v_permlane16_swap_b32` to coalesce epilogue stores into
`buffer_store_dwordx4` (16 bytes each), reducing store count from 192 to
48.
### How it works
**Swapped MFMA operands (`wide_stores=True`):** The kernel computes C^T
= B × A^T (and stores C) instead of C = A × B^T, making the
accumulator's 4-contiguous values align with the output memory's
stride-1 dimension. The source/target write syntax maps `Register[N, M]`
to `Memory[M, N]` so the output is C [M, N] directly.
**`wide_store_coalescing` pass:** An always-on graph pass that
identifies eligible bf16 global Write nodes using the source/target
dimension remapping pattern (indicating swapped operands). Only tags
writes that satisfy all conditions: global address space, bf16 dtype,
and source/target syntax. Safe to run unconditionally, non-`wide_stores`
kernels are unaffected.
**`_write_permlane_pack_to_global` codegen:** At code generation time,
tagged writes emit `v_permlane16_swap_b32` to exchange each thread's 4
bf16 values (packed as 2 i32 dwords) with a partner lane 16 positions
apart. Both lane halves write identical 8-bf16 vectors to the same
address (benign duplicate store), avoiding divergent control flow. The
buffer descriptor's `valid_bytes` handles out-of-bounds suppression for
dynamic shapes.
### Assembly comparison (shape 1024×3072×8192, block 256×192×256)
| Instruction | Baseline | Wide Stores | Change |
|---|---|---|---|
| `buffer_store_short` | 192 | 0 | eliminated |
| `buffer_store_dwordx4` | 0 | 48 | 16B/store |
| `v_permlane16_swap` | 0 | 96 | lane exchange |
| `v_cvt_pk_bf16_f32` | 192 | 96 | 2x reduction |
| Total asm lines | 12,115 | 9,322 | 23% fewer |
LLVM now emits `v_cvt_pk_bf16_f32` natively (bf16 softening has been
fixed upstream), so the paired f32->bf16 conversion is a single
instruction.
Compute-bound shapes (large K) show 6-9% gains; small-K shapes show
13-27% gains
| Shape (M, N, K) | Baseline TFLOPS | Wide Stores TFLOPS | Speedup |
|---|---|---|---|
| (315904, 384, 1792) | 1169.51 | 1557.86 | 1.332x |
| (20480, 1152, 28928) | 2714.36 | 3015.93 | 1.111x |
| (5888, 1920, 3328) | 1582.35 | 2104.81 | 1.330x |
| (46336, 2688, 8448) | 2495.16 | 2940.50 | 1.179x |
| (512, 3072, 428288) | 590.71 | 648.15 | 1.097x |
| (256, 4224, 102656) | 409.79 | 458.95 | 1.120x |
| (512, 4224, 55808) | 794.47 | 893.25 | 1.124x |
| (4864, 4608, 2560) | 1435.43 | 2123.30 | 1.479x |
| (4864, 4608, 5888) | 2083.50 | 2669.39 | 1.281x |
| (4608, 4992, 7424) | 2227.52 | 2813.12 | 1.263x |
| (2048, 5376, 63232) | 3112.73 | 3405.23 | 1.094x |
| (2048, 5760, 6400) | 2230.97 | 2690.92 | 1.206x |
| (3584, 6912, 548608) | 3272.81 | 3531.95 | 1.079x |
| (4864, 7680, 7936) | 2406.89 | 3036.57 | 1.262x |
| (13056, 10368, 15360) | 2936.09 | 3352.67 | 1.142x |
| (14848, 12672, 10496) | 2781.61 | 3254.58 | 1.170x |
| (1792, 19968, 60928) | 3115.72 | 3392.02 | 1.089x |
---------
Signed-off-by: xintin <gaurav.verma@amd.com>1 parent 5186495 commit a3521bc
9 files changed
Lines changed: 678 additions & 125 deletions
File tree
- examples/python
- lit_tests/kernel/wave
- tests/kernel
- wave_lang/kernel
- compiler/wave_codegen
- wave
- templates
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
24 | 21 | | |
| 22 | + | |
25 | 23 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | 24 | | |
| 25 | + | |
30 | 26 | | |
| 27 | + | |
31 | 28 | | |
32 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
33 | 38 | | |
34 | 39 | | |
35 | | - | |
36 | | - | |
37 | 40 | | |
38 | 41 | | |
| 42 | + | |
| 43 | + | |
39 | 44 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
424 | 425 | | |
425 | 426 | | |
426 | 427 | | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
427 | 466 | | |
428 | 467 | | |
429 | 468 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| 307 | + | |
307 | 308 | | |
308 | 309 | | |
309 | 310 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
1028 | 1029 | | |
1029 | 1030 | | |
1030 | 1031 | | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
1031 | 1088 | | |
1032 | 1089 | | |
1033 | 1090 | | |
| |||
0 commit comments