Repository navigation
Description
(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 anint8 × int8 → int32matrix multiply that runs on the SpaceMiT IME (vmadot) matrix instructions, by data-tiling the matmul intolinalg.mmt4dand binding it to a hand-written C microkernel that issuesvmadotvia 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/cpuinfoISA string containsv, zve32x/f, zve64x/f/d, zvfh, zvfhmin, zfh— but no IME/xsmtvdottoken (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 fromvl·SEW.LMULmust be ≤ 1. - Integer family:
vmadot(s×s),vmadotu(u×u),vmadotsu(s×u),vmadotus(u×s), plus sliding-windowvmadot{1,2,3,n}variants (for conv). Accumulator is i32, written to an even-aligned 2-register group{vd, vd+1};vd(andvs1for slide ops) must be even. - On X60 (VLEN=256, SEW=8): one
vmadot= a4×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 a4×8int8 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 a4×8packed tile = 32 B = one register. - C (ACC):
(M0,N0)int32 in the{vd,vd+1}group.
- A (LHS):
- This matches IREE's
mmt4doperand layout natively:mmt4dLHS 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 wayvmadotwants. (TheGemm_packBin 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 fixed4×8 · 8×4 → 4×4int8 GEMM; zero the{v28,v29}group withvxor.vv, sete8,m1to select the 4×4×8 unit,vle8.vA and packed-B,vmadot v28,v0,v1, re-view ase32,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 incpu_features; and - the runtime —
iree_cpu_lookup_data_by_key/ the ukernel predicates incommon_riscv_64.h.
Tile-shape selection and lowering:
- Compiler picks the data-tile
(M0,N0,K0)inenumerateMatmulTileRiscv64(compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp:997), gated onhasFeature(config,"+v")and element type. (Today: only f32/f16; no i8 branch.) - That
TileMxNxKbecomesinnerTileSizesviagetEncodingInfoForMatmul(compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils/Utils.cpp:490) — arch-agnostic, no edit needed. - The contraction lowers to
linalg.mmt4d, whichCPULowerToUKernels.cpp:166rewrites intoiree_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 inmmt4d_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.
(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)
- Path: classic
mmt4d+ C ukernel (notinner_tiled). ✅ Settled — it is the only path wired to the ukernels and to RISC-V data-tiling. - Scope v1:
s8s8s32only (vmadot, signed×signed). FP/vfmadotand the sliding-window (conv) variants are out of scope for v1. - 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),K0a multiple of 8. Final shape is a tuning decision (Phase 6) — whatever you pick, the compilerenumeratebranch and thetiles.inl/ukernel must stay in agreement (§1.3). - Feature name: use
xsmtvdotas thecpu_feature_bits.inlllvm_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.insnthe internal name is a free choice. - 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 inruntime/src/iree/builtins/ukernel/arch/arm_64/mmt4d_arm_64_fp16fml.c:10. Keep a raw.insnvariant 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(commitc4484e842274, 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=...xsmtvdotandsmt.vmadot, so the standalone/bitcode ukernel build (which uses bundled clang and unconditionally enables all RISC-V build flags viacommon_riscv_64.h:13) can emit named mnemonics. XSMTVDotis assembler-only (no intrinsics, no codegen/auto-vectorization). All real users (SpaceMiT demo, llama.cpp, remlabxstime.S) use inline asm. The instructions live in the RISC-V CUSTOM_1 opcode space; raw.insn/.wordencodings 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
vmadotvia a raw.insn/.wordencoding (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=111000forvmadot,func3selects signedness, evenvd/vs1) and validate by assemble→disassemble — the sources even disagree on the opcode (remlab cites0x7b, but standard custom-1 is0x2b), 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 thesmt.vmadotmnemonic (§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)
s8s8s32mmt4d 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. ✅ S8S8S32ukernel tests for every other arch (e.g.mmt4d_test.c:572-573arm dotprod/i8mm,:601avx512vnni) — the pattern for a RISC-V"ime"line. ✅- e2e data-tiled int8 matmul test already exists:
tests/e2e/matmul/CMakeLists.txte2e_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:
- the
llvm_nameinruntime/src/iree/schemas/cpu_feature_bits.inl(generates thecpu_databit), - the compiler's
hasFeature(config, "+xsmtvdot")check inenumerateMatmulTileRiscv64, and - the ukernel build
-march(so the assembler acceptssmt.vmadot) + the matchingiree_uk_cpu_riscv_64_xsmtvdotpredicate.
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.cwith SpaceMiT's toolchain; confirm it matches the scalar reference. Capture the exact-marchstring and toolchain version that assemblevmadot. - Confirm IREE's bundled clang assembles
smt.vmadotwith-march=rv64gcv_xsmtvdot(compile a tiny.cto bitcode/asm). Decide named-mnemonic vs.insn. - Pin the exact
.insnencoding forvmadot(s8s8s32) from the spec; verify by assembling both forms andobjdump-diffing. - Confirm on hardware:
int8vmadotis 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 inmmt4dpanel layout (LHS[K1,M0,K0], RHS[K1,N0,K0]), output[M0,N0]i32, accumulate flag. Loop overK1, issuevmadotper(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
xsmtvdotfeature gate. Register a new RISC-V CPU feature the build, the runtime tile-selector, and the compiler can all key off. The shared feature tableruntime/src/iree/schemas/cpu_feature_bits.inlis the single source of truth — thecpu_databit and the name↔bit lookups fall out of one entry there; the per-arch build flag + runtime predicate inarch/riscv_64/follow the existingzvfhpattern. Keep thexsmtvdotstring 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 issuesvmadotvia inline asm. The f32_vandzvfhkernels 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 inarch/arm_64/mmt4d_arm_64_fp16fml.c). - C3 — Register and dispatch the tile so the runtime selector picks it for
s8s8s32on 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 thecpu_databit, so no host-side detection is required. (The optionalmarchidauto-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 setsALLOW_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 exercisesvmadot.
Milestone D — Make the compiler emit the IME tile
- D1
compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp— add ani8×i8→i32branch toenumerateMatmulTileRiscv64(:997), gated onhasFeature(config, "+v") && hasFeature(config, "+xsmtvdot"), returning the IMETileMxNxK{M0,N0,K0}(+ M-truncations). Do not reuse theN0 = vlen/8, K0 = 1formula — return the fixed IME shape from Milestone B. - D2 (Optional/conventional) add a
hasXsmtvdotFeaturehelper incompiler/src/iree/compiler/Codegen/LLVMCPU/Utils.{h,cpp}mirroringhasVFeature(: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'senumeratebranch matches the registered ukernel tile (C2/C3). - D5 Add a compiler lit test for the new
enumerateMatmulTileRiscv64branch (encoding/materialization on a+v,+xsmtvdotRISC-V target).
Milestone E — End-to-end correctness
- E1 Compile a small data-tiled
i8×i8→i32matmul 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 confirmiree_codegen.ukernel.mmt4dwith 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,+xsmtvdotvariant to thee2e_matmul_cpu_dt_i8_i32test (tests/e2e/matmul/CMakeLists.txt, mirror inBUILD.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_benchmarkfor 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 theenumeratebranch andtiles.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/udmafirst. - 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_memcpyand uses plainmemcpy;aimm_memcpyis 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/tcmdriver 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_memcpyvs CPUmemcpy; 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,3as 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.ymlbut pointingQEMU_BINat a vendor QEMU fork that decodes the matrix extension (e.g. Xuantiex-matrix=on, if its encoding matches SpaceMiT IME — open question), with a force-IME test variant (temporarily dropALLOW_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
.insnencoding forvmadots8s8s32 — derive from spec, validate on hardware. - Cluster-0-only IME: running on Cluster 1 traps; confirm affinity handling (Milestone F4).
7. References
- SpaceMiT IME spec (authoritative): https://github.com/spacemit-com/riscv-ime-extension-spec —
src/program-model.adoc(MAC-unit table,Copies, LMUL≤1),src/instruction-func.adoc,example/vmadot-gemm-demo.c. - LLVM
XSMTVDot: https://llvm.org/docs/RISCVUsage.html ; PR [RISCV] Add SpacemiT XSMTVDot (SpacemiT Vector Dot Product) extension. llvm/llvm-project#151706 (assembler-only, merged 2025-08-18, ships in LLVM 22). - Independent IME analysis (XSTIME, CUSTOM_1 opcode,
.insnmacros, int16-buggy note): https://www.remlab.net/op/riscv-xstime.shtml - llama.cpp SpaceMiT IME kernel (reference inline-asm):
ggml/.../ime1_kernels.cpp,GGML_CPU_RISCV64_SPACEMIT. - IREE template PR (RVV int8
s8s8s32mmt4d + data-tiling): [LLVMCPU][RISCV] Add RVV int8 vector-contract custom kernels for widening i8*i8->i32 matmul #23734 - TCM lib + demo: https://gitee.com/bianbu-linux/tcm ; addresses: https://forum.spacemit.com/t/topic/693
- K1 datasheet: https://docs.banana-pi.org/en/BPI-F3/SpacemiT_K1_datasheet
- IREE microkernels overview (no-deps constraint): https://iree.dev/community/blog/2024-01-22-exploring-cpu-microkernels-on-a-matmul-example/
- Data-tiling walkthrough on AMDGPU: https://iree.dev/community/blog/2025-08-25-data-tiling-walkthrough
- mmt4d in IREE: https://iree.dev/community/blog/2021-10-13-matrix-multiplication-with-mmt4d/
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
Activity
noumanamir-10xe commentedon Jun 5, 2026
Just to add to the above that even if
vmadotlands 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.
coden-s commentedon Jun 9, 2026
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 toolchainspacemit-toolchain-linux-glibc-x86_64-v1.1.2.tar.xzand it uses-march=rv64imafdcv_zicsr_zifencei_zaamo_zalrsc_zve32f_zve32x_zve64d_zve64f_zve64x_zvl128b_zvl32b_zvl64b.akuhlken commentedon Jun 9, 2026
Please also assign me to this issue
egebeysel commentedon Jun 23, 2026
Any progress here, or anything I could help with? :)
coden-s commentedon Jun 23, 2026
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.
For milestone C, would you be able to give us some clarity on what the K value in
iree_uk_mmt4d_params_tactually 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?noumanamir-10xe commentedon Jun 24, 2026
Kpassed in kernel isK1.https://github.com/iree-org/iree/blob/main/compiler/src/iree/compiler/Codegen/Common/CPU/CPULowerToUKernels.cpp#L245
egebeysel commentedon Jun 24, 2026
Thanks for the update!
Each instruction computes a
4x4xi32result, so onVLEN=256it 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:
LMUL=4) on the N, giving them 28 registers being filled by accumulators, and 4 by RHS (LHS are in scalar registers due tovfmacc.vfinstruction).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.
coden-s commentedon Jun 24, 2026
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:
coden-s commentedon Jun 30, 2026
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-18that comes with the risc-v toolchain ( installed with./build_tools/riscv/riscv_bootstrap.sh) does not support SpacemiT IME. We needclang-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.Benchmarking results so far (from
mmt4d_benchmark):TMahlatini commentedon Jun 30, 2026
@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.
egebeysel commentedon Jul 1, 2026
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.
egebeysel commentedon Jul 1, 2026
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-compilebinary when you compile a VMFB. So for standalone ukernel testing, you're using a different path than theiree-compileway. No action needed there from your side.You could move on to Milestone D and progress towards the
iree-compileway :)5 remaining items
[LLVMCPU][RISCV] Enable SpaceMiT XSMTVDot (vmadot) s8s8s32 mmt4d ukernel
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enable SpaceMiT XSMTVDot (vmadot) s8s8s32 mmt4d ukernel
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[LLVMCPU][RISCV] Enable SpaceMiT XSMTVDot (vmadot) s8s8s32 mmt4d ukernel
[LLVMCPU][RISCV] Enhance RISC-V hwprobe for SpaceMiT X60 support
[RVV] Enable SpaceMiT vendor IME (#24706)