Skip to content

[Rock] Add i4 (iu4 16x16x32) WMMA GEMM support for gfx12#2417

Open
rhway666 wants to merge 2 commits into
ROCm:developfrom
rhway666:iu4-gemm
Open

[Rock] Add i4 (iu4 16x16x32) WMMA GEMM support for gfx12#2417
rhway666 wants to merge 2 commits into
ROCm:developfrom
rhway666:iu4-gemm

Conversation

@rhway666

@rhway666 rhway666 commented Jun 22, 2026

Copy link
Copy Markdown

Adds native i4 (iu4) integer GEMM codegen for gfx12/RDNA4: an i4×i4→i32 GEMM
now lowers through the rock dialect to wmma_i32_16x16x32_iu4 (mirrors the
existing i8 wmma_i32_16x16x16_iu8 path). Distinct from prior i4 work (#1584 is
i4×f16 mixed-precision; #1682 is i4 vector loads) — this is the native
iu4×iu4 integer WMMA path.

Tracking issue for i4 follow-ups (autotuner, verifier, conv/other-arch,
signedness, full tuning short-list): #2418

Assisted-by: Claude (Anthropic)

Motivation

The i4 WMMA type id (I4_To_I32_TyId) and getRetType (i4→i32) already exist
upstream, but i4 was never wired to an instruction. This connects it: the
instruction map, the frontend (-t i4), the type gates (gfx12-only), the
tuning-key string, and a tuning config. iu4 uses K=32 and inputVectorLen=16
(vs i8's 16/8) because a 4-bit element packs at twice the K depth of an 8-bit
element. Gated to gfx12 (gfx11 rejects i4).

Technical Details

Files (8):

  • Dialect/Rock/IR/WmmaInsnGroup.cpp — gfx12 insn map {I4_To_I32_TyId, 32} → wmma_i32_16x16x32_iu4; select chooses K=32 for i4.
  • tools/rocmlir-gen/rocmlir-gen.cpp — accept -t i4; default i4 output to i32.
  • IR/RockOps.td — allow I<4> in GemmInputTypes.
  • IR/RockDialect.cppverifyGemmTypes accepts i4 only on gfx12 (RDNA4).
  • Tuning/ParamLookupTable.cpp — map i4 to "i4" in the tuning key.
  • Tuning/QuickTuningPerfconfigs.inc — a gfx1201 i4 tuning config.
  • README.md — list i4 in the -t/--dtype examples.
  • test/Dialect/Rock/wmma_gemm_i4.mlir — lowering check + gfx11 negative check.

Notes for reviewers:

  • Signedness: matches the existing integer WMMA path; this PR does not add a
    signedness selector (tracked as a shared integer-WMMA follow-up in the issue).
  • K selection: select picks K=32 for i4 via a ternary since i4 is the only
    K≠16 type on gfx12 today; mirrors the gfx1250 per-type-K style and can be
    refactored to if/else-if if more K variants are added on gfx12.
  • Tuning (note on a generated file): QuickTuningPerfconfigs.inc is generated
    by quickTuningGen.py --update from sweep TSVs. The i4 entry is required,
    not a perf nicety: the quick-tuning lookup report_fatal_errors on a miss
    (Tuning parameters not found for key gfx1201_gemm_i4) and falls back only by
    same op+dtype across architectures — so i4 has no i8 fallback and no other arch
    carries gemm_i4. Without it, i4 GEMM does not lower. The autotuner
    (rocmlir-tuning-driver) cannot yet tune i4 (its applicability pipeline rejects
    all i4 configs), so the entry is placed by hand and a future --update
    would drop it (flagged inline). The single config is correctness-verified and
    was chosen as the most robust single config across the i8 tier1-gemm shapes
    (offline rocprofv3 sweep on gfx1201); those numbers are provisional and not
    CI-verified. A full per-shape short-list will be produced data-driven via
    quickTuningGen.py --update (not hand-edited) once i4 is wired into the
    autotuner — tracked in the issue.

Known limitation (out of scope, follow-up): the --verifier cpu/gpu
random-data auto-compare paths still need i4 narrow-type handling — (1)
rock.transform's affine index-map must be rewritten for the packed (i8) layout;
(2) the workgroup-LDS memref<…xi4, #gpu.workgroup> alloc + vector.store of
vector<Nxi4> must be packed consistently. This does not affect the generated
kernel (validated below); it's a separate narrow-type follow-up.

Test Plan

  • Lit test (mlir/test/Dialect/Rock/wmma_gemm_i4.mlir, CI-runnable):
    rocmlir-gen … -t i4 -wmma=on --arch gfx1201 | rocmlir-driver … lowers to
    amdgpu.wmma 16x16x32 … vector<16xi4> and
    rocdl.wmma.i32.16x16x32.iu4 … (vector<2xi32>, vector<2xi32>, vector<8xi32>) -> vector<8xi32>; a gfx1100 i4 GEMM is rejected.
  • Hardware run on AMD Radeon AI PRO R9700 (gfx1201, ROCm 7.2): the generated
    i4 kernel compiles to a gfx1201 binary and executes.
  • iu4 instruction independently checked via a standalone HIP microkernel.

Test Result

  • Lit test passes (both lowering checks + the gfx11 negative check).
  • Hardware: all-ones inputs give C[m][n] = Σ_k 1·1 = K — verified output == K
    for K ∈ {32, 128, 256}
    on gfx1201.
  • iu4 16x16x32 microkernel: all-ones → 32 across all 256 accumulator slots
    (bit-exact).
  • clang-format clean (git clang-format --diff is empty).

Submission Checklist

rocMLIR already scaffolds the i4 WMMA type id (I4_To_I32_TyId) and i4->i32
accumulation in getRetType, but never wires it to an instruction. This adds the
missing gfx12 path so an i4 GEMM lowers to wmma_i32_16x16x32_iu4, mirroring the
i8 16x16x16 path. iu4 uses K=32 and inputVectorLen 16 (vs i8's K=16/8) because a
4-bit element packs at twice the K depth of i8.

- WmmaInsnGroup: add the gfx12 {I4,32} -> wmma_i32_16x16x32_iu4 map entry and
  select K=32 for i4.
- rocmlir-gen: accept "-t i4" and default its output type to i32.
- RockOps.td: allow I<4> in GemmInputTypes.
- RockDialect: verifyGemmTypes accepts i4 only on gfx12 (RDNA4); gfx11 rejects.
- ParamLookupTable: map i4 to "i4" in the tuning lookup key.
- QuickTuningPerfconfigs: add a hand-placed gfx1201 i4 quick-tuning config
  (the autotuner cannot yet tune i4; see the inline note and PR description).
- README: list i4 in the -t/--dtype examples.
- test/Dialect/Rock/wmma_gemm_i4.mlir: lowering check + gfx11 negative check.

Assisted-by: Claude (Anthropic)
Copilot AI review requested due to automatic review settings June 22, 2026 19:46
@rhway666 rhway666 requested a review from causten as a code owner June 22, 2026 19:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR wires up native i4 (iu4) integer WMMA GEMM codegen on gfx12/RDNA4, enabling i4×i4→i32 GEMM to lower through rock to the wmma_i32_16x16x32_iu4 instruction, and adds the supporting frontend, verification, tuning-key, perfconfig, and test updates.

Changes:

  • Add gfx12 WMMA instruction selection/mapping for iu4 16x16x32 (K=32) and gate i4 GEMM types to RDNA4.
  • Extend rocmlir-gen dtype handling to accept -t i4 and default i4 output to i32.
  • Add tuning-key + quick-tuning entry for gfx1201_gemm_i4 and a Lit test covering gfx12 lowering and gfx11 rejection.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
README.md Documents -t i4 in rocmlir-gen dtype examples.
mlir/tools/rocmlir-gen/rocmlir-gen.cpp Adds CLI parsing for i4 and defaults i4 output dtype to i32.
mlir/test/Dialect/Rock/wmma_gemm_i4.mlir New Lit test for gfx1201 lowering to WMMA iu4 and gfx1100 rejection.
mlir/lib/Dialect/Rock/Tuning/ParamLookupTable.cpp Adds i4 (bitwidth==4) dtype string normalization for tuning keys.
mlir/lib/Dialect/Rock/IR/WmmaInsnGroup.cpp Adds gfx12 iu4 WMMA op mapping and selects K=32 for i4.
mlir/lib/Dialect/Rock/IR/RockDialect.cpp Verifier allows i4 on RDNA4 WMMA GEMM.
mlir/include/mlir/Dialect/Rock/Tuning/QuickTuningPerfconfigs.inc Adds gfx1201_gemm_i4 quick-tuning perfconfig entry.
mlir/include/mlir/Dialect/Rock/IR/RockOps.td Allows I<4> in GemmInputTypes (affects GEMM and rock.conv forward).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mlir/lib/Dialect/Rock/IR/RockDialect.cpp
Comment thread mlir/tools/rocmlir-gen/rocmlir-gen.cpp
Comment thread mlir/include/mlir/Dialect/Rock/IR/RockOps.td
…gnostic

- verifyConvOp rejects i4 (i4 WMMA is GEMM-only) instead of fatal-erroring in
  tuning-parameter selection, since rock.conv shares GemmInputTypes.
- verifyGemmTypes emits an RDNA4-specific error message that includes int4.
- test/Dialect/Rock/conv_i4_unsupported.mlir: conv i4 rejection check.

Addresses Copilot review on ROCm#2417.

Assisted-by: Claude (Anthropic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants