# AMD MFMA instructions on CDNA3 and CDNA4 (MI300X, MI355X)

Canonical: https://xerj.org/science/amd-mfma-instructions
Updated: 2026-11-22

MFMA shapes, cycles and FLOPs for MI300X and MI355X, HIP intrinsics, real compiled gfx942 and gfx950 ISA, AGPRs, spills and an occupancy calculator.
SHORT ANSWER · UPDATED 2026-11-22
MFMA instructions are how CDNA GPUs do matrix math: one 64-lane wavefront computes a small D = A × B + C block per instruction. On MI300X (gfx942) the FP16 instruction v_mfma_f32_16x16x16_f16 takes 16 cycles for 8,192 FLOPs. In HIP you call it with __builtin_amdgcn_mfma_f32_16x16x16f16. MI355X (gfx950) adds double-K FP16 and BF16 MFMAs and scaled FP8, FP6 and FP4 MFMAs.
Key number: 2,048 FLOPs per CU per cycle × 304 CUs × 2.1 GHz = 1,307.4 TFLOPS, exactly AMD's FP16 peak for MI300X. (as of 2026-11-22 · sources: [AMD Matrix Instruction Calculator](https://github.com/ROCm/amd_matrix_instruction_calculator), [AMD MI300X](https://www.amd.com/en/products/accelerators/instinct/mi300/mi300x.html))
Last checked: 2026-11-22. All assembly on this page was compiled with clang 21.1.8 for gfx942 and gfx950. It was compiled, not run on a GPU.

TL;DR Pick the MFMA shape that matches your data type and tile, keep its operands in the right lanes, and watch three numbers the compiler gives you for free: registers per lane (VGPRs plus AGPRs), LDS bytes per workgroup and occupancy. The calculator below turns those into waves per SIMD with the same formula LLVM uses.

> Interactive calculator: open the HTML page.

PASTE THIS TO YOUR AI AGENT. IT DOES THE REST
Act as a GPU kernel engineer. Read https://xerj.org/science/amd-mfma-instructions and https://xerj.org/llms.txt. Install XERJ and index this repository so you can search it with citations. Compile my HIP kernels with -save-temps and -Rpass-analysis=kernel-resource-usage for gfx942 and gfx950. List each kernel's VGPRs, AGPRs, spills, scratch, LDS bytes and occupancy, and show the v_mfma and s_waitcnt lines of its inner loop.

This page goes one level below the [AMD GPU optimization guide](https://xerj.org/science/amd-gpu-optimization), which covers profiling, libraries, vLLM and system tuning.

## CDNA3 MFMA shapes, cycles and FLOPs

We ran AMD's Matrix Instruction Calculator (commit 94b1f16) with `-a CDNA3 -i <instruction> -d` for each row. The last column multiplies its per-CU rate by MI300X's 304 CUs and 2.1 GHz peak clock. Every result matches AMD's published MI300X peak for that type.

| Instruction (CDNA3) | FLOPs | Cycles | FLOPs per CU per cycle | × 304 CUs × 2.1 GHz (TFLOPS) | Registers A / B / D |

| v_mfma_f32_16x16x16_f16 | 8,192 | 16 | 2,048 | 1,307.4 | 2 / 2 / 4 |

| v_mfma_f32_32x32x8_f16 | 16,384 | 32 | 2,048 | 1,307.4 | 2 / 2 / 16 |

| v_mfma_f32_16x16x16_bf16 | 8,192 | 16 | 2,048 | 1,307.4 | 2 / 2 / 4 |

| v_mfma_f32_16x16x32_fp8_fp8 | 16,384 | 16 | 4,096 | 2,614.9 | 2 / 2 / 4 |

| v_mfma_f32_32x32x16_fp8_fp8 | 32,768 | 32 | 4,096 | 2,614.9 | 2 / 2 / 16 |

| v_mfma_i32_16x16x32_i8 | 16,384 ops | 16 | 4,096 ops | 2,614.9 TOPS | 2 / 2 / 4 |

| v_mfma_f32_16x16x8_xf32 | 4,096 | 16 | 1,024 | 653.7 | 2 / 2 / 4 |

| v_mfma_f32_16x16x4_f32 | 2,048 | 32 | 256 | 163.4 | 1 / 1 / 4 |

| v_mfma_f64_16x16x4_f64 | 2,048 | 32 | 256 | 163.4 | 2 / 2 / 8 |

Read the name as `v_mfma_<accumulator type>_<M>x<N>x<K>_<input type>`. FLOPs = 2 × M × N × K. FLOPs per CU per cycle = FLOPs ÷ cycles × 4, because each CU has 4 SIMDs that each run one wavefront's MFMA. The calculator also reports that `v_mfma_f32_16x16x16_f16` can co-execute with VALU work for 12 of its 16 cycles, which is where address math and conversions should go.

```
git clone https://github.com/ROCm/amd_matrix_instruction_calculator
cd amd_matrix_instruction_calculator && pip install -r requirements.txt
python3 matrix_calculator.py -a CDNA3 -L                                   # list instructions
python3 matrix_calculator.py -a CDNA3 -i v_mfma_f32_16x16x16_f16 -d        # cycles, FLOPs, registers
python3 matrix_calculator.py -a CDNA3 -i v_mfma_f32_16x16x16_f16 -A --matrix-layout
```

## Operand layout for v_mfma_f32_16x16x16_f16

From the calculator's matrix layout output. Lane l holds four FP16 values of A and B and four FP32 values of D:

| Operand | Lane l holds | Lane 0 | Lane 16 | Lane 63 |

| A (16×16, 2 VGPRs) | row l % 16, k = 4·(l / 16) to 4·(l / 16) + 3 | A[0][0..3] | A[0][4..7] | A[15][12..15] |

| B (16×16, 2 VGPRs) | column l % 16, k = 4·(l / 16) to 4·(l / 16) + 3 | B[0..3][0] | B[4..7][0] | B[12..15][15] |

| D (16×16, 4 registers) | column l % 16, rows 4·(l / 16) to 4·(l / 16) + 3 | D[0..3][0] | D[4..7][0] | D[12..15][15] |

The [LDS-tiled GEMM in the guide](https://xerj.org/science/amd-gpu-optimization#code) uses exactly this mapping: `i = lane & 15`, `kq = (lane >> 4) * 4`.

## HIP intrinsics

Signatures from clang's BuiltinsAMDGPU.td. The last three arguments are the cbsz, abid and blgp modifiers; pass 0 for a plain multiply.

```
typedef _Float16 half4  __attribute__((ext_vector_type(4)));
typedef _Float16 half8  __attribute__((ext_vector_type(8)));
typedef float    float4_ __attribute__((ext_vector_type(4)));
typedef float    float16_ __attribute__((ext_vector_type(16)));
typedef __bf16   bf16x8  __attribute__((ext_vector_type(8)));

// mai-insts: CDNA GPUs, including gfx942 and gfx950
float4_  __builtin_amdgcn_mfma_f32_16x16x16f16(half4 a, half4 b, float4_ c, int cbsz, int abid, int blgp);
float16_ __builtin_amdgcn_mfma_f32_32x32x8f16 (half4 a, half4 b, float16_ c, int cbsz, int abid, int blgp);
// fp8-insts (gfx942, gfx950). A and B each pack 8 fp8 values into 64 bits
float4_  __builtin_amdgcn_mfma_f32_16x16x32_fp8_fp8(int64_t a, int64_t b, float4_ c, int cbsz, int abid, int blgp);
// gfx950-insts only
float4_  __builtin_amdgcn_mfma_f32_16x16x32_f16(half8 a, half8 b, float4_ c, int cbsz, int abid, int blgp);
float4_  __builtin_amdgcn_mfma_f32_16x16x32_bf16(bf16x8 a, bf16x8 b, float4_ c, int cbsz, int abid, int blgp);
// plus __builtin_amdgcn_mfma_scale_f32_16x16x128_f8f6f4 for scaled FP8, FP6 and FP4
```

Calling a gfx950 builtin while compiling for gfx942 fails at compile time. We checked: clang 21.1.8 reports `'__builtin_amdgcn_mfma_f32_16x16x32_f16' needs target feature gfx950-insts`. Guard such code with the target macros or build separate kernels per target.

## CDNA4 (gfx950) changes

- Matrix throughput for 16-bit and smaller types doubles: 4,096 FLOPs per CU per clock for BF16 on MI350 Series, per AMD's ROCm guide. 4,096 × 256 CUs × 2.4 GHz is about 2.5 PFLOPS on MI355X.
- FP64 matrix drops to 128 FLOPs per CU per clock (256 on MI300X), and TF32 moves to software emulation through BF16.
- FP8 uses the OCP formats, and MXFP8, MXFP6 and MXFP4 are native. For FP16 and BF16, AMD recommends `v_mfma_f32_16x16x32` with BLOCK_K 64; for MXFP4, `v_mfma_scale_f32_16x16x128_f8f6f4` with BLOCK_K 256.
- LDS grows to 160 KiB per CU with 64 banks, and data can load directly from L1 to LDS.

A gfx950-only MFMA, compiled with clang 21.1.8:

```
#include 
typedef _Float16 half8 __attribute__((ext_vector_type(8)));
typedef float    float4_ __attribute__((ext_vector_type(4)));

// gfx950 (CDNA4) only: one MFMA with K = 32 per instruction for fp16.
// Operand layout per lane is defined in the CDNA4 ISA reference.
__global__ __launch_bounds__(64)
void mfma_16x16x32_f16(const half8* a, const half8* b, float4_* d) {
const int l = threadIdx.x;
float4_ acc = {0.f, 0.f, 0.f, 0.f};
acc = __builtin_amdgcn_mfma_f32_16x16x32_f16(a[l], b[l], acc, 0, 0, 0);
d[l] = acc;
}
```

```
$ clang -x hip -O3 --offload-arch=gfx950 --offload-device-only -S mfma_gfx950.hip
global_load_dwordx4 v[0:3], v8, s[4:5]
global_load_dwordx4 v[4:7], v8, s[6:7]
s_waitcnt vmcnt(0)
v_mfma_f32_16x16x32_f16 a[0:3], v[0:3], v[4:7], 0
s_nop 7
global_store_dwordx4 v8, a[0:3], s[2:3]
; NumVgprs: 9   NumAgprs: 4   Occupancy: 8
```

Note the `s_nop 7`: the compiler inserts wait states before reading the MFMA result. In a real kernel, independent MFMAs or loads fill that gap instead.
The per-lane operand layout for the K = 32 instructions is defined in the CDNA4 ISA reference; AMD's calculator covers CDNA1 to CDNA3, so we do not reproduce the CDNA4 layout here.

## Read the resource usage the compiler reports

Three ways, all run for this page on the LDS GEMM kernel from the guide (gfx942):

```
$ hipcc -O3 --offload-arch=gfx942 -Rpass-analysis=kernel-resource-usage -c gemm_mfma_lds.hip
Function Name: _Z13gemm_f16_mfmaPKDF16_S0_Pfiii
TotalSGPRs: 18
VGPRs: 24
AGPRs: 4
ScratchSize [bytes/lane]: 0
Dynamic Stack: False
Occupancy [waves/SIMD]: 7
SGPRs Spill: 0
VGPRs Spill: 0
LDS Size [bytes/block]: 9216
```

```
$ hipcc -O3 --offload-arch=gfx942 -save-temps -c gemm_mfma_lds.hip
$ grep -E "; (NumVgprs|NumAgprs|TotalNumVgprs|ScratchSize|Occupancy)" gemm_mfma_lds-hip-amdgcn-amd-amdhsa-gfx942.s
; NumVgprs: 24
; NumAgprs: 4
; TotalNumVgprs: 28
; ScratchSize: 0
; Occupancy: 7
```

```
$ llvm-readelf --notes gemm_gfx942.co      # code object metadata
- .agpr_count:     4
.group_segment_fixed_size: 9216
.max_flat_workgroup_size: 256
.private_segment_fixed_size: 0
.sgpr_count:     18
.vgpr_count:     28
.vgpr_spill_count: 0
.wavefront_size: 64
```

In the metadata, `.vgpr_count` is the total (VGPRs plus AGPRs, here 24 + 4 = 28). `.group_segment_fixed_size` is LDS bytes per workgroup and `.private_segment_fixed_size` is scratch bytes per lane.

## AGPRs, spills and waves_per_eu

CDNA3 and CDNA4 give each lane up to 512 registers, shared between architected VGPRs and AGPRs, allocated in groups of 8. More registers per lane means fewer waves per SIMD. To see the trade, we compiled one kernel where a wavefront holds a TM × TN grid of 32×32 MFMA accumulators (16 registers each):

```
#include 
typedef _Float16 half4 __attribute__((ext_vector_type(4)));
typedef float    float16_ __attribute__((ext_vector_type(16)));

// One wavefront accumulates a TMxTN grid of 32x32 MFMA tiles in registers.
// TM*TN*16 accumulator registers per lane: 2x2 -> 64, 4x2 -> 128, 4x4 -> 256.
#ifndef TM
#define TM 4
#endif
#ifndef TN
#define TN 4
#endif
#ifdef WPE
__attribute__((amdgpu_waves_per_eu(WPE)))
#endif
__global__ __launch_bounds__(256)
void wave_tile(const half4* __restrict__ A, const half4* __restrict__ B,
float16_* __restrict__ C, int ksteps) {
const int l = threadIdx.x & 63;
const int w = (threadIdx.x >> 6) + blockIdx.x * 4;
float16_ acc[TM][TN] = {};
for (int k = 0; k 
| Accumulator grid | waves_per_eu | VGPRs | AGPRs | Spilled VGPRs | Scratch bytes | Occupancy |

| 2×2 (64 accumulator registers) | default | 16 | 64 | 0 | 0 | 6 |

| 4×2 (128) | default | 20 | 128 | 0 | 0 | 3 |

| 4×4 (256) | default | 24 | 256 | 0 | 0 | 1 |

| 4×4 (256) | 2 | 256 | 0 | 66 | 140 | 2 |

clang 21.1.8, `-O3 --offload-arch=gfx942`, `__launch_bounds__(256)`, with `-DTM -DTN -DWPE` set per row. Compiled, not run.
Asking for 2 waves per SIMD capped the kernel at 256 registers, so the compiler moved everything into VGPRs and spilled 66 of them to scratch. Whether 2 waves with spills beats 1 wave without depends on the kernel, so measure both. AMD's guide gives the rule of thumb for Triton: raise `waves_per_eu` when VGPR use sits just above an occupancy boundary, for example 170 VGPRs (rounded to 176) gives 2 waves, and trimming to 168 gives 3.
A detail that cost us time and may save you some: clang releases before 25 accept `__attribute__((launch_bounds(...)))` but ignore it, so ROCm's own hip_runtime header maps `__launch_bounds__` to `amdgpu_flat_work_group_size` and `amdgpu_waves_per_eu`. If you compile with a bare clang and your own macros, copy that mapping, or the compiler assumes 1,024 threads per workgroup.

## 16×16 or 32×32

AMD's ROCm guide reports that on MI300X the 16×16 MFMA usually beats 32×32, even for large GEMMs, and credits better power efficiency. Both have the same peak rate per CU in the table above; 32×32 needs 16 accumulator registers per lane instead of 4. In Triton, `matrix_instr_nonkdim=16` selects 16×16. On MI350 Series, benchmark both.

## How XERJ fits

XERJ is a new way for AI to search for data. Index your kernels together with the ISA dumps the compiler writes, and an agent can answer "which kernels spill" or "where is the s_waitcnt before this v_mfma" with the file and line.

## FAQ

### What is an MFMA instruction?

MFMA (matrix fused multiply add) is the CDNA matrix core instruction. One wavefront of 64 lanes computes D = A × B + C for a small block, for example 16×16×16 in FP16 with FP32 accumulation, in a fixed number of cycles.

### How many FLOPs does one MFMA do on MI300X?

v_mfma_f32_16x16x16_f16 does 8,192 FLOPs in 16 cycles, 2,048 FLOPs per CU per cycle. Times 304 CUs at 2.1 GHz that is 1,307.4 TFLOPS, AMD's FP16 peak for MI300X.

### Which MFMA instructions are new on gfx950 (MI355X)?

CDNA4 adds FP16 and BF16 MFMAs with twice the K per instruction, such as v_mfma_f32_16x16x32_f16, and scaled MFMAs for FP8, FP6 and FP4 such as v_mfma_scale_f32_16x16x128_f8f6f4.

### What are AGPRs?

Accumulation registers. On CDNA3 and CDNA4 each lane has up to 512 registers split between architected VGPRs and AGPRs, and MFMA accumulators can live in AGPRs. The compiler reports both counts.

### Should I use 16x16 or 32x32 MFMA on MI300X?

AMD's ROCm guide says 16x16 MFMA usually outperforms 32x32 on MI300X, even for large GEMMs. In Triton set matrix_instr_nonkdim=16. On MI350 Series, benchmark both.

## Evidence

- Cycles, FLOPs, registers and operand layouts for CDNA3 MFMA. Source: [AMD Matrix Instruction Calculator](https://github.com/ROCm/amd_matrix_instruction_calculator), commit 94b1f16 (2026-11-11), run on 2026-11-22
- MI300X 304 CUs, 2,100 MHz, FP16 1,307.4 TFLOPS, FP8 2,614.9 TFLOPS. Source: [AMD MI300X](https://www.amd.com/en/products/accelerators/instinct/mi300/mi300x.html)
- MI355X 256 CUs, 2,400 MHz, BF16 2.5 PFLOPS. Source: [AMD MI355X](https://www.amd.com/en/products/accelerators/instinct/mi350/mi355x.html)
- CDNA4 4,096 and 128 FLOPs per CU per clock, OCP FP8, recommended MFMA and BLOCK_K, 16×16 vs 32×32, waves_per_eu, occupancy rule. Source: [ROCm MI300 Series and MI350 Series workload optimization](https://rocm.docs.amd.com/projects/ai-ecosystem/en/latest/optimization/workload-optimization.html)
- Builtin signatures and target features. Source: [clang BuiltinsAMDGPU.td](https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Basic/BuiltinsAMDGPU.td)
- VGPR granule 8 and the waves formula. Source: [LLVM AMDGPUBaseInfo.cpp](https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp)
- __launch_bounds__ mapping for clang before 25. Source: [ROCm amd_hip_runtime.h](https://github.com/ROCm/rocm-systems/blob/develop/projects/clr/hipamd/include/hip/amd_detail/amd_hip_runtime.h)
- LDS banks and per-CU sizes. Sources: [HIP hardware implementation](https://rocm.docs.amd.com/projects/HIP/en/latest/understand/hardware_implementation.html), [ROCm GPU specifications](https://rocm.docs.amd.com/en/latest/reference/gpu-arch-specs.html)
- ISA references. Sources: [CDNA3 ISA](https://www.amd.com/content/dam/amd/en/documents/instinct-tech-docs/instruction-set-architectures/amd-instinct-mi300-cdna3-instruction-set-architecture.pdf), [CDNA4 ISA](https://www.amd.com/content/dam/amd/en/documents/instinct-tech-docs/instruction-set-architectures/amd-instinct-cdna4-instruction-set-architecture.pdf)

## Related

- [AMD GPU optimization guide: MI300X, MI325X, MI355X](https://xerj.org/science/amd-gpu-optimization)
- [Vector index RAM calculator](https://xerj.org/answers/vector-index-ram-calculator)
- [What is syntax-aware search?](https://xerj.org/science/syntax-aware-search)
