Skip to content

[RVV] Enable SpaceMiT vendor IME (vmadot) #24576

Closed
1 of 1 issue completed
Closed

Description

@egebeysel

(this issue is guided by me, but generated by Claude Code)

[RISC-V][CPU] Target the SpaceMiT K1 "AI capability" (IME matrix extension) via data-tiled mmt4d + a C microkernel

Audience: contributors new to IREE with basic LLVM/MLIR and RISC-V knowledge who have access to a Banana Pi BPI-F3 / Milk-V Jupiter (SpaceMiT K1 / X60).
Goal: make IREE emit and execute an int8 × int8 → int32 matrix multiply that runs on the SpaceMiT IME (vmadot) matrix instructions, by data-tiling the matmul into linalg.mmt4d and binding it to a hand-written C microkernel that issues vmadot via inline assembly.

This issue is intentionally detailed: it records what was verified, why each decision was made, and exactly which files to touch, so the work can be split across several people. File references point at the current main checkout.


0. TL;DR — what we're building and the key facts that shape it

The K1's advertised "2.0 TOPS AI" is not a separate NPU. It is the IME (Integrated Matrix Extension) — a set of custom RISC-V matrix-multiply instructions (vmadot family) executed by the X60 CPU cores themselves, reusing the RVV (vector) register file. ⇒ We target it through IREE's LLVM-CPU backend, not a new HAL driver / plugins/target accelerator. (The newer K3 SoC's headline ~60 TOPS is likewise in-core — a scaled-up matrix extension, "IME2", on its A100 cores (1024-bit VLEN, more dtypes, new vpack/vnspack mnemonics), not a separate NPU. Out of scope here, but the same mmt4d + ukernel lane — this K1 work is the stepping to it.)

Consequences that drive the whole plan:

Fact (verified) Consequence for the plan
IME = in-core CPU instructions reusing RVV regs Use the data-tiled mmt4d + C-microkernel CPU path. No HAL driver.
One vmadot computes a fixed MAC tile; on X60 (VLEN=256, int8) that is (M,N,K) = (4,4,8) The IME data-tile inner shape is (M0,N0,K0) built from multiples of (4,4,8), not the existing RVV N0 = vlen/8, K0 = 1 shape.
int8 is the reliable IME path; int16 reportedly buggy on X60; FP vfmadot is not in mainline XSMTVDot Scope v1 to s8s8s32.
XSMTVDot is in mainline LLVM 22 (assembler-only, mnemonics smt.vmadot, integer-only); no intrinsics/builtins The kernel must use inline asm. IREE's bundled LLVM is new enough (see §3), so named mnemonics work in the bitcode build; raw .insn is the portable fallback.
No OS-advertised IME feature flag Gate at compile time via a chosen target feature; pick +xsmtvdot and keep it consistent (see §3.3), exactly like the existing zvfh/zvfhmin ukernels.
The classic mmt4d path drives the C ukernels; the newer inner_tiled/MMAIntrinsic CPU path is x86-only and lowers in-compiler (no ukernel) Integrate via the mmt4d path, as intended. Do not use inner_tiled.

This is essentially "PR #23734 (RVV int8 s8s8s32 mmt4d kernel + data-tiling), but with the kernel body issuing vmadot inline asm, gated on a new +xsmtvdot feature, with the IME (4,4,8)-derived tile shape." PR #23734 is the closest template; note it is not in this checkout (no mmt4d_riscv_64_v_i8.c, no s8s8s32 lines in tiles.inl, no i8 branch in enumerateMatmulTileRiscv64). The present f32 _v kernel (mmt4d_riscv_64_v.c) and the zvfh/zvfhmin kernels are the live structural twins to copy.


1. Background: hardware, instruction, and where it maps in IREE

1.1 The chip

  • X60 is the RISC-V application core; the K1 SoC integrates 8 of them in two asymmetric quad-core clusters. Cluster 0 (cores 0–3) is AI-capable: it has the IME and a 512 KB TCM scratchpad. Cluster 1 (cores 4–7) has neither. ⇒ IME dispatches must run on cores 0–3 (Cluster 1 would trap on vmadot). This is a constraint for the IREE task scheduler / affinity, flagged below.
  • X60: RVV 1.0, VLEN = 256.
  • The provided /proc/cpuinfo ISA string contains v, zve32x/f, zve64x/f/d, zvfh, zvfhmin, zfh — but no IME/xsmtvdot token (see §3.3). This is the reason we gate at build time.

1.2 The instruction (from the SpaceMiT spec, cross-checked against the demo)

  • IME adds no new architectural state: it reuses the RVV vector registers and the vsetvli/SEW config path, and selects a matrix MAC unit from vl·SEW. LMUL must be ≤ 1.
  • Integer family: vmadot (s×s), vmadotu (u×u), vmadotsu (s×u), vmadotus (u×s), plus sliding-window vmadot{1,2,3,n} variants (for conv). Accumulator is i32, written to an even-aligned 2-register group {vd, vd+1}; vd (and vs1 for slide ops) must be even.
  • On X60 (VLEN=256, SEW=8): one vmadot = a 4×4×8 (M×N×K) int8→int32 MAC (Copies = 1).
  • Operand layout the instruction expects (this is the load-bearing detail for packing):
    • A (LHS): (M0,K0) row-major, K-contiguous → A[m*K0 + k]. On X60 a 4×8 int8 tile = 32 B = one 256-bit register.
    • B (RHS): must be supplied transposed to (N0,K0), K-contiguous per output column → packB[n*K0 + k]. On X60 a 4×8 packed tile = 32 B = one register.
    • C (ACC): (M0,N0) int32 in the {vd,vd+1} group.
  • This matches IREE's mmt4d operand layout natively: mmt4d LHS is [M1,K1,M0,K0] (K0-contiguous) and RHS is already transposed to [N1,K1,N0,K0] (K0-contiguous). So no extra in-kernel transpose is needed — the data-tiling pack step lays operands out the way vmadot wants. (The Gemm_packB in the SpaceMiT demo exists only because the demo starts from a plain (K,N) array.)
  • Reference kernel: spacemit-com/riscv-ime-extension-spec/example/vmadot-gemm-demo.c (a fixed 4×8 · 8×4 → 4×4 int8 GEMM; zero the {v28,v29} group with vxor.vv, set e8,m1 to select the 4×4×8 unit, vle8.v A and packed-B, vmadot v28,v0,v1, re-view as e32,m2, vse32.v). llama.cpp also has a SpaceMiT IME kernel (ggml/.../ime1_kernels.cpp, GGML_CPU_RISCV64_SPACEMIT=ON) — a second real-world reference.

1.3 The IREE path (verified file:line map)

The compiler→runtime contract has a single source of truth: the RISCV_64 rows of runtime/src/iree/schemas/cpu_feature_bits.inl (currently V/ZVFHMIN/ZVFH at lines 125–127). The llvm_name string there is the join point read by both:

  • the compiler — HALDispatchABI::updateProcessorDataFromTargetAttr (compiler/src/iree/compiler/Codegen/LLVMCPU/DispatchABI.cpp:896, #includes the same .inl) bakes the bit into the executable when the feature is in cpu_features; and
  • the runtime — iree_cpu_lookup_data_by_key / the ukernel predicates in common_riscv_64.h.

Tile-shape selection and lowering:

  • Compiler picks the data-tile (M0,N0,K0) in enumerateMatmulTileRiscv64 (compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp:997), gated on hasFeature(config,"+v") and element type. (Today: only f32/f16; no i8 branch.)
  • That TileMxNxK becomes innerTileSizes via getEncodingInfoForMatmul (compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils/Utils.cpp:490) — arch-agnostic, no edit needed.
  • The contraction lowers to linalg.mmt4d, which CPULowerToUKernels.cpp:166 rewrites into iree_codegen.ukernel.mmt4d → the runtime C kernel.
  • Microkernel mechanism selects the tile func in iree_uk_mmt4d_select_tile_func_arch (runtime/src/iree/builtins/ukernel/arch/riscv_64/mmt4d_riscv_64_entry_point.c:10), matching (mmt4d_type, M0, K0, cpu-feature predicate). Tiles are registered in mmt4d_riscv_64_tiles.inl. The shiniest possible one is then compiled to LLVM bytecode and inlined later into the dispatch, check https://iree.dev/community/blog/2024-01-22-exploring-cpu-microkernels-on-a-matmul-example/#overview-of-the-compilation-and-linking-flow.

⚠️ The compiler-chosen tile and the registered ukernel tile must agree, or data-tiling silently falls back to plain codegen: the (type, M0, K0) returned by enumerateMatmulTileRiscv64 must match a row in mmt4d_riscv_64_tiles.inl (and the kernel it names). Treat this as an invariant to assert in review and tests.


2. Design decisions (please confirm before coding)

  1. Path: classic mmt4d + C ukernel (not inner_tiled). ✅ Settled — it is the only path wired to the ukernels and to RISC-V data-tiling.
  2. Scope v1: s8s8s32 only (vmadot, signed×signed). FP/vfmadot and the sliding-window (conv) variants are out of scope for v1.
  3. Atomic vs. data-tile shape: the instruction tile is (4,4,8). The data-tile (M0,N0,K0) the ukernel implements should be small multiples of that to fill the 32 vector registers and amortize loop overhead (each (4,4) accumulator is a 2-vreg group, so budget ≈ 2·(M0/4)·(N0/4) acc regs + operands ≤ 32). Candidate starting shapes: (8, 8, 8) or (8, 16, 8), K0 a multiple of 8. Final shape is a tuning decision (Phase 6) — whatever you pick, the compiler enumerate branch and the tiles.inl/ukernel must stay in agreement (§1.3).
  4. Feature name: use xsmtvdot as the cpu_feature_bits.inl llvm_name (matches mainline LLVM/GCC and IREE's bundled LLVM). ⚠️ The IREE-internal name only has to be self-consistent across the .inl, hasFeature, and the build -march; pin it to whatever the codegen toolchain you compile the ukernel bitcode with actually accepts. With raw .insn the internal name is a free choice.
  5. Inline-asm form: start with named mnemonics (smt.vmadot) in the bitcode build (bundled LLVM supports it — §3), following the #ifdef __clang__ statement-expression precedent in runtime/src/iree/builtins/ukernel/arch/arm_64/mmt4d_arm_64_fp16fml.c:10. Keep a raw .insn variant documented as the portability fallback for the system-toolchain build.

3. Pre-work / facts already established (so you don't re-discover them)

3.1 Toolchain — RESOLVED

  • IREE's bundled LLVM submodule is sm-iree-integrates/llvm-20260602 (commit c4484e842274, 2026-06-02) — after the XSMTVDot merge (LLVM PR #151706, 2025-08-18) and the LLVM 22 branch (2026-01-13). ⇒ IREE's own clang recognizes -march=...xsmtvdot and smt.vmadot, so the standalone/bitcode ukernel build (which uses bundled clang and unconditionally enables all RISC-V build flags via common_riscv_64.h:13) can emit named mnemonics.
  • XSMTVDot is assembler-only (no intrinsics, no codegen/auto-vectorization). All real users (SpaceMiT demo, llama.cpp, remlab xstime.S) use inline asm. The instructions live in the RISC-V CUSTOM_1 opcode space; raw .insn/.word encodings assemble on any toolchain regardless of -march (good for the system-toolchain build whose binutils may predate IME support).
  • Fallback path only: if you emit vmadot via a raw .insn/.word encoding (the portability route for old system binutils that don't know the mnemonic), you must pin the exact bit-fields from the spec (src/images/wavedrom/ma-format.adoc: Custom opcode, func7=111000 for vmadot, func3 selects signedness, even vd/vs1) and validate by assemble→disassemble — the sources even disagree on the opcode (remlab cites 0x7b, but standard custom-1 is 0x2b), so a hand-written encoding must be round-tripped through a known-good assembler before relying on it. Not needed for the primary path: IREE's bundled LLVM knows the smt.vmadot mnemonic (§3.1), so the bitcode build just uses named mnemonics and the assembler computes the encoding.

3.2 What already exists in-tree (reuse, don't recreate)

  • s8s8s32 mmt4d type + flag mapping: mmt4d_internal.h:45, :69. ✅
  • int8 scalar reference for tests: mmt4d_test.c:155 (iree_mmt4d_reference_innerloop_s8s8s32), already wired at :308. ✅
  • S8S8S32 ukernel tests for every other arch (e.g. mmt4d_test.c:572-573 arm dotprod/i8mm, :601 avx512vnni) — the pattern for a RISC-V "ime" line. ✅
  • e2e data-tiled int8 matmul test already exists: tests/e2e/matmul/CMakeLists.txt e2e_matmul_cpu_dt_i8_i32. ✅

3.3 Runtime detection — gate at compile time on a chosen feature flag

To the best of my knowledge, there is no OS-advertised "has IME" flag to query, so gate the IME path at compile time, exactly like the existing zvfh/zvfhmin ukernels: pick the feature string +xsmtvdot and use that same string consistently in the three places that must agree:

  1. the llvm_name in runtime/src/iree/schemas/cpu_feature_bits.inl (generates the cpu_data bit),
  2. the compiler's hasFeature(config, "+xsmtvdot") check in enumerateMatmulTileRiscv64, and
  3. the ukernel build -march (so the assembler accepts smt.vmadot) + the matching iree_uk_cpu_riscv_64_xsmtvdot predicate.

The user selects it via --iree-llvmcpu-target-cpu-features=+v,+xsmtvdot; the compiler embeds the bit into the executable and the runtime predicate reads it — no OS probe involved.


4. Implementation plan (milestones & checkboxed tasks)

Milestone A — Hardware & toolchain bring-up (do this first; de-risks everything)

  • On the BPI-F3 / Jupiter, confirm IME works at all: build & run the upstream vmadot-gemm-demo.c with SpaceMiT's toolchain; confirm it matches the scalar reference. Capture the exact -march string and toolchain version that assemble vmadot.
  • Confirm IREE's bundled clang assembles smt.vmadot with -march=rv64gcv_xsmtvdot (compile a tiny .c to bitcode/asm). Decide named-mnemonic vs .insn.
  • Pin the exact .insn encoding for vmadot (s8s8s32) from the spec; verify by assembling both forms and objdump-diffing.
  • Confirm on hardware: int8 vmadot is bit-exact vs the scalar reference; note int16/FP behavior (so we can justify scoping to int8).

Milestone B — Standalone IME microkernel (pure C, outside IREE)

  • Write a self-contained mmt4d-shaped IME microkernel: inputs in mmt4d panel layout (LHS [K1,M0,K0], RHS [K1,N0,K0]), output [M0,N0] i32, accumulate flag. Loop over K1, issue vmadot per (4,4,8) sub-tile, accumulate in even-aligned {vd,vd+1} groups, store once.
  • Validate against a scalar reference for the chosen (M0,N0,K0) (e.g. (8,8,8)), accumulate and non-accumulate modes, on hardware.
  • Decide the (M0,N0,K0) macro-tile by quick register-pressure + microbench sweep (keep ≤ 32 vregs). This becomes the canonical shape for Milestones C/D.

Milestone C — Wire the IME ukernel into the IREE runtime

This is the same shape of change as PR #23734's runtime half and the existing zvfh/zvfhmin kernels — read those first; they are the templates. The items below are outcomes, not recipes: explore the existing arch/riscv_64/ structure and implement to fit it.

  • C1 — Add the xsmtvdot feature gate. Register a new RISC-V CPU feature the build, the runtime tile-selector, and the compiler can all key off. The shared feature table runtime/src/iree/schemas/cpu_feature_bits.inl is the single source of truth — the cpu_data bit and the name↔bit lookups fall out of one entry there; the per-arch build flag + runtime predicate in arch/riscv_64/ follow the existing zvfh pattern. Keep the xsmtvdot string consistent (§3.3).
  • C2 — Implement the kernel (a new arch/riscv_64/mmt4d_riscv_64_*.c): port the Milestone-B kernel into an mmt4d tile function that issues vmadot via inline asm. The f32 _v and zvfh kernels are the structural templates. Two things they don't exercise, so watch for them: K0 > 1, and the freestanding / no-libc constraint (there's an inline-asm precedent worth copying in arch/arm_64/mmt4d_arm_64_fp16fml.c).
  • C3 — Register and dispatch the tile so the runtime selector picks it for s8s8s32 on an IME-capable CPU, gated on the new feature (the tile registry .inl + the entry-point dispatch).
  • C4 — Build wiring: compile the new source in both the standalone/bitcode path (the one data-tiling actually uses) and the system-toolchain path, with the IME -march, keeping the Bazel and CMake definitions in sync.
  • C5 — cpu.c: nothing needed for v1. Compile-time gating means the compiler supplies the cpu_data bit, so no host-side detection is required. (The optional marchid auto-dispatch is a later enhancement — §3.3 / runtime-detection-notes.md.)
  • C6 — Test it: add an int8 IME case to the ukernel test + benchmark. ⚠️ Gotcha: the harness sets ALLOW_GENERIC_FALLBACK, so a green run does not prove the IME tile executed — only a forced-IME run on hardware or a vendor QEMU (Milestone E) actually exercises vmadot.

Milestone D — Make the compiler emit the IME tile

  • D1 compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp — add an i8×i8→i32 branch to enumerateMatmulTileRiscv64 (:997), gated on hasFeature(config, "+v") && hasFeature(config, "+xsmtvdot"), returning the IME TileMxNxK{M0,N0,K0} (+ M-truncations). Do not reuse the N0 = vlen/8, K0 = 1 formula — return the fixed IME shape from Milestone B.
  • D2 (Optional/conventional) add a hasXsmtvdotFeature helper in compiler/src/iree/compiler/Codegen/LLVMCPU/Utils.{h,cpp} mirroring hasVFeature (:69).
  • D3 Confirm getDefaultEnabledUkernels (compiler/src/iree/compiler/Codegen/Utils/Utils.cpp:204) — ukernels are off-by-default on RISC-V, so e2e/manual runs must pass --iree-llvmcpu-enable-ukernels=mmt4d. Decide whether to make IME targets default-enable mmt4d.
  • D4 Verify the (M0,N0,K0) invariant (§1.3) holds: the (type, M0, K0) from D1's enumerate branch matches the registered ukernel tile (C2/C3).
  • D5 Add a compiler lit test for the new enumerateMatmulTileRiscv64 branch (encoding/materialization on a +v,+xsmtvdot RISC-V target).

Milestone E — End-to-end correctness

  • E1 Compile a small data-tiled i8×i8→i32 matmul for the K1 target (--iree-opt-data-tiling --iree-llvmcpu-enable-ukernels=mmt4d --iree-llvmcpu-target-cpu-features=+v,+zvl256b,+xsmtvdot + the riscv triple). Inspect the dispatch IR to confirm iree_codegen.ukernel.mmt4d with the IME tile is selected (not a fallback).
  • E2 Run it on real BPI-F3/Jupiter hardware (pinned to cores 0–3) and check numerical correctness vs a reference.
  • E3 (Optional, I'll deal with this later) Add a riscv_64:ime:+v,+zvl256b,+xsmtvdot variant to the e2e_matmul_cpu_dt_i8_i32 test (tests/e2e/matmul/CMakeLists.txt, mirror in BUILD.bazel, regenerate). Note that on stock CI QEMU this only exercises the plumbing in fallback mode (see §1, E5).

Milestone F — Benchmarking & tuning (after correctness)

  • F1 Microbenchmark mmt4d_benchmark for the IME tile across shapes on hardware; compare against the RVV-only path (PR [LLVMCPU][RISCV] Add RVV int8 vector-contract custom kernels for widening i8*i8->i32 matmul #23734 reported ~4× RVV i8 vs codegen — IME could beat that; SpaceMiT cites larger speedups).
  • F2 Sweep the (M0,N0,K0) macro-tile and the K-loop pipelining/prefetch; lock in the best shape (and keep the enumerate branch and tiles.inl/ukernel in agreement).
  • F3 Run a few end-to-end int8-quantized models, report throughput/latency, profile, and identify where IME isn't being selected or is under-utilized (e.g. K not a multiple of 8, narrow dims forcing padding).
  • F4 Apply the Cluster-0 affinity workaround for all IME runs/benchmarks (see Optional 3): IME instructions only exist on cores 0–3, so any IME dispatch landing on Cluster 1 traps. Pin the task system to Cluster 0 (--task_topology_cpu_ids=0,1,2,3) for correctness; track the proper heterogeneous-topology fix as Optional 3.

5. Optional / stretch follow-ups

Optional 1 — TCM double-buffering (only after the core path works)

The K1 Cluster-0 TCM is a 512 KB / cluster (4 × 128 KB) on-chip SRAM, accessed via the Bianbu /dev/tcm char driver (mmap + ioctls TCM_VA_TO_PA/TCM_REQUEST_MEM/TCM_RELEASE_MEM) plus a /dev/udma DMA driver; vendor lib + demo at gitee.com/bianbu-linux/tcm. Caveats that make this a research task, not a slam-dunk:

  • Requires the Bianbu kernel drivers + DT node present (likely absent on stock kernels) — ls /dev/tcm /dev/udma first.
  • Contended with ONNX Runtime (the vendor AI stack already uses TCM) — must go through the request/release arbitration ioctls.
  • Async DRAM↔TCM DMA is doubtful (forum says "no DMA"; the vendor demo disables udma_memcpy and uses plain memcpy; aimm_memcpy is DRAM→TCM only) — this undercuts classic double-buffering and is the single biggest risk; measure on hardware.
  • Process must be pinned to cores 0–3.
  • A microkernel must have no external library deps (freestanding), so the /dev/tcm driver interaction cannot live inside the ukernel — it would need to be a modular addition to the LLVM-CPU runtime (RISC-V) / HAL allocator path, gated so other RISC-V targets are unaffected.
  • Discovery: confirm drivers/DT on-device; microbench TCM-resident vs L2-resident GEMM and udma_memcpy vs CPU memcpy; only then design where double-buffering lives.

Optional 2 — Heterogeneous CPU topology: keep IME work off Cluster 1 (correctness hazard + interim workaround)

IME instructions exist only on Cluster 0 (cores 0–3). Today, LLVM-CPU codegen and the local-task work distribution do not specialize by CPU topology — there is no per-core capability check in the dispatch path — so if an IME dispatch is scheduled onto a Cluster-1 core (4–7) it will fault with an illegal instruction. This becomes a live correctness hazard the moment the IME kernel is selected and run multi-threaded.

Interim workaround (no code changes needed): pin the task system to Cluster-0 cores by passing --task_topology_cpu_ids=0,1,2,3 to iree-run-module / iree-benchmark-module (the flag is an IREE_FLAG_LIST in runtime/src/iree/task/api.c:88; it creates worker threads bound to exactly those CPU IDs). This guarantees every worker runs on an IME-capable core. The trade-off is that you are limited to the 4 Cluster-0 cores, so multi-threaded throughput is capped at half the chip until the topology is handled properly.

  • Document --task_topology_cpu_ids=0,1,2,3 as the required flag for any IME run/benchmark, and bake it into any helper/benchmark scripts.
  • (Deeper, optional design investigation) Add first-class support for this asymmetric topology so all 8 cores can be used without faulting — i.e. ensure IME dispatches are only ever scheduled on Cluster-0 cores while non-IME work is free to use all cores. This likely touches the task topology/affinity layer (runtime/src/iree/task/) and how a dispatch's required CPU features are matched to eligible worker cores. Treat it as a design discussion with trade-offs to weigh; out of scope for the core IME enablement.

Optional 3 — IME execution under emulation (better CI)

  • Stand up an opt-in RISC-V CI job mirroring pkgci_test_riscv64.yml but pointing QEMU_BIN at a vendor QEMU fork that decodes the matrix extension (e.g. Xuantie x-matrix=on, if its encoding matches SpaceMiT IME — open question), with a force-IME test variant (temporarily drop ALLOW_GENERIC_FALLBACK). Document the QEMU-fork + toolchain pairing required.

6. Risks & open questions (carry these in the issue)

  • Bundled-toolchain assembly: confirm IREE's bundled clang truly assembles smt.vmadot (highly likely given the 2026-06 LLVM) — else fall back to .insn. (Owner: Milestone A.)
  • Exact .insn encoding for vmadot s8s8s32 — derive from spec, validate on hardware.
  • Cluster-0-only IME: running on Cluster 1 traps; confirm affinity handling (Milestone F4).

7. References

Key in-tree files

  • Compiler tile selection: compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp:997 (enumerateMatmulTileRiscv64)
  • mmt4d→ukernel: compiler/src/iree/compiler/Codegen/Common/CPU/CPULowerToUKernels.cpp:166
  • runtime dispatch: runtime/src/iree/builtins/ukernel/arch/riscv_64/mmt4d_riscv_64_entry_point.c:10, mmt4d_riscv_64_tiles.inl, common_riscv_64.h
  • f32/zvfh kernel templates: runtime/src/iree/builtins/ukernel/arch/riscv_64/mmt4d_riscv_64_v.c, mmt4d_riscv_64_zvfh.c
  • inline-asm precedent: runtime/src/iree/builtins/ukernel/arch/arm_64/mmt4d_arm_64_fp16fml.c:10

(Les relevant ones for reference)

  • compiler→runtime feature bit: compiler/src/iree/compiler/Codegen/LLVMCPU/DispatchABI.cpp:896
  • feature/CPU resolution: compiler/plugins/target/LLVMCPU/ResolveCPUAndCPUFeatures.cpp:92
  • single source of truth: runtime/src/iree/schemas/cpu_feature_bits.inl:125
  • runtime detection: runtime/src/iree/base/internal/cpu.c:419
  • tests: runtime/src/iree/builtins/ukernel/tools/mmt4d_test.c:155,606; tests/e2e/matmul/CMakeLists.txt; CI .github/workflows/pkgci_test_riscv64.yml

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. noumanamir-10xe commented on Jun 5, 2026

    @noumanamir-10xe
    Contributor

    Just to add to the above that even if vmadot lands on cluster-1 cores, the Binabu Linux kernel traps the illegal instruction and moves the thread over to cluster-0 AI cores. The process keeps running because the kernel handles the switch.

    We have the vmadot mmt4d enabled in a downstream fork. So happy to help review this change whenever this lands in upstream.

  2. coden-s commented on Jun 9, 2026

    @coden-s

    IME does work on the BPI board. Using the toolchain and demo from SpacemiT's IME extension spec, I was able to run the vmadot-gemm-demo.c. I used the toolchain spacemit-toolchain-linux-glibc-x86_64-v1.1.2.tar.xz and it uses -march=rv64imafdcv_zicsr_zifencei_zaamo_zalrsc_zve32f_zve32x_zve64d_zve64f_zve64x_zvl128b_zvl32b_zvl64b.

  3. akuhlken commented on Jun 9, 2026

    @akuhlken

    Please also assign me to this issue

  4. egebeysel commented on Jun 23, 2026

    @egebeysel
    ContributorAuthor

    Any progress here, or anything I could help with? :)

  5. coden-s commented on Jun 23, 2026

    @coden-s

    We've finished milestone B. There's no intrinsics so we had to implement the ukernel with inline assembly which took longer than anticipated. We looked at different tile sizes and we're likely going to use (M0, N0, K0) of (8, 16, 8). This is based off of the benchmarking we've done, but it would be helpful to know if there's another value we should be looking at.

    kernel                 (M0xN0)  depth          μs/call         GOP/s
    s8s8s32_4x8x8_ime      4x8      K1=1024        12.71 us/call     20.632 GOP/s
    s8s8s32_8x4x8_ime      8x4      K1=1024        14.11 us/call     18.574 GOP/s
    s8s8s32_8x8x8_ime      8x8      K1=1024        20.71 us/call     25.320 GOP/s
    s8s8s32_8x16x8_ime     8x16     K1=1024        28.37 us/call     36.957 GOP/s
    s8s8s32_16x8x8_ime     16x8     K1=1024        28.73 us/call     36.503 GOP/s
    

    For milestone C, would you be able to give us some clarity on what the K value in iree_uk_mmt4d_params_t actually represents? The existing kernels use a K0 value of 1 and loop over K directly. Since our kernel uses a K0 value of 8, should we still be looping over K or instead over K / K0? Is K the size of the entire K dimension (K1 * K0) or is it always equivalent to K1?

  6. egebeysel commented on Jun 24, 2026

    @egebeysel
    ContributorAuthor

    Thanks for the update!

    we're likely going to use (M0, N0, K0) of (8, 16, 8). This is based off of the benchmarking we've done, but it would be helpful to know if there's another value we should be looking at.

    Each instruction computes a 4x4xi32 result, so on VLEN=256 it takes 2 registers to store them. You have unrolling factors 2 x 4 for those dims, giving you 16 registers filled with your accumulators. That is the register budget filled by the AArch64 or x86_64 kernels, as well as all-powers-of-two, so that sounds good to me.

    Though, the current FP32 and int8 kernels for RVV use a "full register budget" scheme instead:

    • they unroll 7 times on the M dimension, and 4 times (via LMUL=4) on the N, giving them 28 registers being filled by accumulators, and 4 by RHS (LHS are in scalar registers due to vfmacc.vf instruction).
    • that seems to be more performant on a standalone matmul, but in an end-to-end model, we try and fuse the pack/unpack operations with their producers and consumers, and 7 is not really a canonical tile size :) So it might introduce more overhead overall, I have not tested this.

    That being said, maybe you might also want to give (M0, N0, K0) = (12, 16, 8) a try (3 x 4 unrolling factors, utilizes 24 registers for accumulators, 3 for LHS and 4 for RHS) on matmuls-only a try**. Do you have any insights here @NoumanAmir657 ?

    (**) If that wouldn't be too much effort. Otherwise, you could continue with the next item on your list, what you have sounds good to me.

  7. coden-s commented on Jun 24, 2026

    @coden-s

    I've just added some additional kernels to test, including 12x16x8 and 16x12x8. Based on the numbers, it was worth looking into. Thanks for the suggestion! We'll move forward with (M0, N0, K0) = (12, 16, 8).

    Here's the results with all test kernels:

    kernel                 (M0xN0)  depth          μs/call           GOP/s
    s8s8s32_4x8x8_ime      4x8      K1=1024        12.71 us/call     20.623 GOP/s
    s8s8s32_8x4x8_ime      8x4      K1=1024        13.86 us/call     18.910 GOP/s
    s8s8s32_8x8x8_ime      8x8      K1=1024        20.26 us/call     25.881 GOP/s
    s8s8s32_8x16x8_ime     8x16     K1=1024        28.19 us/call     37.200 GOP/s
    s8s8s32_16x8x8_ime     16x8     K1=1024        27.88 us/call     37.605 GOP/s
    s8s8s32_12x12x8_ime    12x12    K1=1024        36.22 us/call     32.569 GOP/s
    s8s8s32_12x16x8_ime    12x16    K1=1024        32.84 us/call     47.899 GOP/s
    s8s8s32_16x12x8_ime    16x12    K1=1024        32.24 us/call     48.789 GOP/s
    
  8. coden-s commented on Jun 30, 2026

    @coden-s

    Milestone C is done :)

    In C2, it mentions the freestanding / no-libc constraint. Looking at the inline asm in the test and the related issue, it seems like maybe it's not relevant to this issue as it's addressing an error with how Clang handles AArch64 instructions. Correct me if I'm wrong and we'll look into it :)

    We were hitting the generic fallback that was mentioned in C6, which we only realized when trying to figure out why the benchmarks were so slow. From what we found, clang-18 that comes with the risc-v toolchain ( installed with ./build_tools/riscv/riscv_bootstrap.sh ) does not support SpacemiT IME. We need clang-23, which is included in IREE's bundled clang. To get it working, we replaced the bin/clang file with a bash script (see below) so it uses clang-23 and targets riscv64.

    #!/bin/bash
    exec /path/to/build-host/llvm-project/bin/clang-23 \
      --target=riscv64-unknown-linux-gnu \
      --sysroot=/path/to/riscv/toolchain/clang/linux/RISCV/sysroot \
      --gcc-toolchain=/path/to/riscv/toolchain/clang/linux/RISCV \
      "$@"
    

    Benchmarking results so far (from mmt4d_benchmark):

    -----------------------------------------------------------------------------------------------------------
    Benchmark                                                 Time             CPU   Iterations UserCounters...
    -----------------------------------------------------------------------------------------------------------
    BM_mmt4d_s8s8s32_tile_12x16x8_xsmtvdot/real_time       9.29 us         9.28 us        65535 items_per_second=84.6889G/s
    
  9. TMahlatini commented on Jun 30, 2026

    @TMahlatini
    Contributor

    @egebeysel Please also assign me to this issue, I have been working on it with the rest of the team. Just forgot to comment earlier.

  10. egebeysel commented on Jul 1, 2026

    @egebeysel
    ContributorAuthor

    In C2, it mentions the freestanding / no-libc constraint. Looking at the inline asm in the test and the related issue, it seems like maybe it's not relevant to this issue as it's addressing an error with how Clang handles AArch64 instructions. Correct me if I'm wrong and we'll look into it :)

    Generally speaking, it should be able to run anywhere. Take a look at this here: https://iree.dev/community/blog/2024-01-22-exploring-cpu-microkernels-on-a-matmul-example/#part-2-microkernels-compilation-part-of-the-iree-build , though it shouldn't be relevant for you if you also followed the schema on the other existing ukernels.

  11. egebeysel commented on Jul 1, 2026

    @egebeysel
    ContributorAuthor

    We were hitting the generic fallback that was mentioned in C6, which we only realized when trying to figure out why the benchmarks were so slow. From what we found, clang-18 that comes with the risc-v toolchain ( installed with ./build_tools/riscv/riscv_bootstrap.sh ) does not support SpacemiT IME. We need clang-23, which is included in IREE's bundled clang. To get it working, we replaced the bin/clang file with a bash script (see below) so it uses clang-23 and targets riscv64.

    As per this, I'd have to check this one again. But the RISC-V toolchain downloaded with that script just conformed to what we were testing in CI with QEMU at the moment. Furthermore, it was used there to build the runtime, not the compiler itself.

    The microkernels are built at compiler build time with the IREE-bundled clang-23, and then linked in by the iree-compile binary when you compile a VMFB. So for standalone ukernel testing, you're using a different path than the iree-compile way. No action needed there from your side.

    You could move on to Milestone D and progress towards the iree-compile way :)

  12. 5 remaining items

  13. added 8 commits that reference this issue on Jul 15, 2026
    8406916
    47075ff
    f5894e9
    eafb0c9
    60332b4
    4354747
    d2c0e7b
    e33a309
  14. added 4 commits that reference this issue on Jul 27, 2026
    c8e1c7e
    1c4a700
    9763c3c
    b663bd6
  15. added a commit that references this issue on Jul 31, 2026
    47a54e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions