Repository navigation
[LLVMCPU][RISCV] Add RVV int8 vector-contract custom kernels for widening i8*i8->i32 matmul - #23734
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a RISC-V RVV-based custom kernel path for widening int8 matmul (i8×i8→i32) by generating RVV LLVM intrinsics instead of inline asm, and wires the relevant tiling/dispatch heuristics and tests.
Changes:
- Add RVV i8×i8→i32 custom kernels implemented via
llvm.riscv.vwmul+llvm.riscv.vwadd.wintrinsics. - Register RVV kernel shapes derived from VLEN (+zvl*) and add MLIR tests validating the intrinsic-based lowering.
- Extend RISC-V matmul tiling heuristics/encoding enumeration to recognize widening i8→i32 matmul patterns.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| compiler/src/iree/compiler/Codegen/LLVMCPU/VectorContractCustomKernels.cpp | Introduces RVV intrinsic kernel generation + pattern registration based on VLEN. |
| compiler/src/iree/compiler/Codegen/LLVMCPU/test/vector_contract_to_riscv_rvv_intrinsics.mlir | New test ensuring vector.contract is replaced with RVV intrinsics for widening int8 matmul shapes. |
| compiler/src/iree/compiler/Codegen/LLVMCPU/test/riscv64_rvv_vector_lowering.mlir | New tests covering RVV vector-lowering behavior and kernel enable/disable toggles. |
| compiler/src/iree/compiler/Codegen/LLVMCPU/KernelDispatch.cpp | Adds widening i8→i32 matmul recognition and updates RISC-V vector tiling heuristic. |
| compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp | Extends riscv64 matmul tile enumeration to include widening i8×i8→i32. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| MMTKernel kernel; | ||
| kernel.lhsType = MMTKernel::ScalarType::I8; | ||
| kernel.rhsType = MMTKernel::ScalarType::I8; | ||
| kernel.accType = MMTKernel::ScalarType::I32; | ||
| kernel.m0 = static_cast<int16_t>(m0); | ||
| kernel.k0 = 1; | ||
| kernel.n0 = static_cast<int16_t>(n0); | ||
| kernel.lhsRegSize = static_cast<int16_t>(m0); | ||
| kernel.rhsRegSize = static_cast<int16_t>(n0); | ||
| kernel.accRegSize = static_cast<int16_t>(n0); | ||
| kernel.lhsRegs = 1; | ||
| kernel.rhsRegs = 1; | ||
| kernel.accRegs = static_cast<int16_t>(m0); |
There was a problem hiding this comment.
MMTKernel fields (m0/n0/lhsRegSize/rhsRegSize/accRegSize/accRegs) are int8_t, but this constructor assigns values via int16_t casts. For larger RVV VLENs (e.g. +zvl2048b => n0=128, +zvl4096b => n0=256), these will truncate/overflow, breaking validate() invariants and generating invalid kernels. Please either clamp the derived n0 to the representable range (and avoid registering kernels that don't fit), or widen the MMTKernel dimension/register-size fields to a larger integer type and validate bounds explicitly.
| MMTKernel kernel; | |
| kernel.lhsType = MMTKernel::ScalarType::I8; | |
| kernel.rhsType = MMTKernel::ScalarType::I8; | |
| kernel.accType = MMTKernel::ScalarType::I32; | |
| kernel.m0 = static_cast<int16_t>(m0); | |
| kernel.k0 = 1; | |
| kernel.n0 = static_cast<int16_t>(n0); | |
| kernel.lhsRegSize = static_cast<int16_t>(m0); | |
| kernel.rhsRegSize = static_cast<int16_t>(n0); | |
| kernel.accRegSize = static_cast<int16_t>(n0); | |
| kernel.lhsRegs = 1; | |
| kernel.rhsRegs = 1; | |
| kernel.accRegs = static_cast<int16_t>(m0); | |
| // Clamp dimensions and register sizes to the range representable by the | |
| // underlying MMTKernel fields (int8_t), to avoid truncation/overflow. | |
| auto clampToInt8 = [](int64_t v) -> int16_t { | |
| if (v > 127) | |
| return static_cast<int16_t>(127); | |
| if (v < -128) | |
| return static_cast<int16_t>(-128); | |
| return static_cast<int16_t>(v); | |
| }; | |
| int16_t m0Clamped = clampToInt8(m0); | |
| int16_t n0Clamped = clampToInt8(n0); | |
| MMTKernel kernel; | |
| kernel.lhsType = MMTKernel::ScalarType::I8; | |
| kernel.rhsType = MMTKernel::ScalarType::I8; | |
| kernel.accType = MMTKernel::ScalarType::I32; | |
| kernel.m0 = m0Clamped; | |
| kernel.k0 = 1; | |
| kernel.n0 = n0Clamped; | |
| kernel.lhsRegSize = m0Clamped; | |
| kernel.rhsRegSize = n0Clamped; | |
| kernel.accRegSize = n0Clamped; | |
| kernel.lhsRegs = 1; | |
| kernel.rhsRegs = 1; | |
| kernel.accRegs = m0Clamped; |
| static size_t getRISCVVVlenFromCPUFeatures(DictionaryAttr config) { | ||
| if (hasFeature(config, "+zvl65536b")) { | ||
| return 65536; | ||
| } else if (hasFeature(config, "+zvl32768b")) { | ||
| return 32768; | ||
| } else if (hasFeature(config, "+zvl16384b")) { | ||
| return 16384; | ||
| } else if (hasFeature(config, "+zvl8192b")) { | ||
| return 8192; | ||
| } else if (hasFeature(config, "+zvl4096b")) { | ||
| return 4096; | ||
| } else if (hasFeature(config, "+zvl2048b")) { | ||
| return 2048; | ||
| } else if (hasFeature(config, "+zvl1024b")) { | ||
| return 1024; | ||
| } else if (hasFeature(config, "+zvl512b")) { | ||
| return 512; | ||
| } else if (hasFeature(config, "+zvl256b")) { | ||
| return 256; | ||
| } | ||
| return 128; | ||
| } |
There was a problem hiding this comment.
This file introduces a local getRISCVVVlenFromCPUFeatures() helper, but the same feature-parsing logic already exists in Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp. Duplicating the exact zvl* chain increases the risk of the two drifting (e.g. when new zvl sizes are added). Consider moving this helper to a shared header/utility (or reusing the existing one) so RVV VLEN derivation stays consistent across codegen components.
| static size_t getRISCVVVlenFromCPUFeatures(DictionaryAttr config) { | |
| if (hasFeature(config, "+zvl65536b")) { | |
| return 65536; | |
| } else if (hasFeature(config, "+zvl32768b")) { | |
| return 32768; | |
| } else if (hasFeature(config, "+zvl16384b")) { | |
| return 16384; | |
| } else if (hasFeature(config, "+zvl8192b")) { | |
| return 8192; | |
| } else if (hasFeature(config, "+zvl4096b")) { | |
| return 4096; | |
| } else if (hasFeature(config, "+zvl2048b")) { | |
| return 2048; | |
| } else if (hasFeature(config, "+zvl1024b")) { | |
| return 1024; | |
| } else if (hasFeature(config, "+zvl512b")) { | |
| return 512; | |
| } else if (hasFeature(config, "+zvl256b")) { | |
| return 256; | |
| } | |
| return 128; | |
| } | |
| // Defer to the shared RVV VLEN derivation helper defined in the external | |
| // CPU encoding models implementation, to keep zvl* feature parsing logic | |
| // centralized and consistent across codegen components. | |
| size_t getRISCVVVlenFromCPUFeatures(DictionaryAttr config); |
| if (isRISCV(targetConfig) && hasFeature(targetConfig, "+v")) { | ||
| int64_t vlen = static_cast<int64_t>(getRISCVVVlenFromCPUFeatures(targetConfig)); | ||
| int64_t n0 = vlen / 16; | ||
|
|
||
| // Follow the existing RVV tiling style: primary shape plus truncations. | ||
| patterns.add<MMTCustomKernelPattern>(context, MMTKernel_RVV_i8i8i32(8, n0)); | ||
| patterns.add<MMTCustomKernelPattern>(context, MMTKernel_RVV_i8i8i32(7, n0)); | ||
| patterns.add<MMTCustomKernelPattern>(context, MMTKernel_RVV_i8i8i32(4, n0)); |
There was a problem hiding this comment.
n0 is derived as vlen/16 and then used to build MMTKernel instances, but MMTKernel stores n0/rhsRegSize/accRegSize as int8_t. For VLEN >= 2048b this produces n0 >= 128, which will overflow/truncate and may register nonsensical kernels for such targets. Please add an explicit bound check here (and skip pattern registration when n0 is out of range) or widen the MMTKernel dimension fields so this computation is safe for all supported zvl* values.
| // use K = 1 and derive N from the accumulator width. | ||
| int64_t maxNumberElementsForLMUL4 = | ||
| (nativeVectorSize * 2 * kByteSizeInBits) / kAccumulatorBitWidth; | ||
|
|
||
| sizes.assign({8, maxNumberElementsForLMUL4, 1}); |
There was a problem hiding this comment.
The widening i8i8->i32 path computes maxNumberElementsForLMUL4 as (nativeVectorSize * 2 * 8) / 32, which corresponds to LMUL4 when nativeVectorSize is already based on LMUL=2 (per the comment). However, the RVV custom kernels / encoding tiling added in this PR use N0 = VLEN/16 (i32 LMUL2), not VLEN/8 (i32 LMUL4). With typical configs where native_vector_size = VLEN2/8, this heuristic will pick an N tile that is 2x larger than the available custom kernel shapes and may also cause excessive register pressure. Please align this calculation with the chosen kernel/encoding tiling (e.g. derive N from LMUL2 here, or update the kernel/encoding tiling to match LMUL4 if that’s the intent).
| // use K = 1 and derive N from the accumulator width. | |
| int64_t maxNumberElementsForLMUL4 = | |
| (nativeVectorSize * 2 * kByteSizeInBits) / kAccumulatorBitWidth; | |
| sizes.assign({8, maxNumberElementsForLMUL4, 1}); | |
| // use K = 1 and derive N from the accumulator width. Here we derive | |
| // the number of i32 accumulator elements for LMUL=2, matching the | |
| // RVV custom kernel / encoding tiling (e.g. N0 = VLEN/16). | |
| int64_t maxNumberElementsForLMUL2 = | |
| (nativeVectorSize * kByteSizeInBits) / kAccumulatorBitWidth; | |
| sizes.assign({8, maxNumberElementsForLMUL2, 1}); |
df64237 to
02c68e0
Compare
commented
Mar 11, 2026
|
Thanks for the PR. Just some context as I will be OOO for a week (March 12-22). VectorContractCustomKernels is the most ancient form of microkernels, dating back to 2021. Handling only a vector.contract op and not a whole loop, it is inherently limited in what it can do. In particular, it has to operate on the linear layout vectors, it has nowhere to hide layout changes and that tends to severely limit its ability to achieve peak performance outside of simple cases. This is mentioned here as the original motivation for whole-loop microkernels (which became the current mmt4d microkernels): It's only incidental that VectorContractCustomKernels hasn't been removed yet. But now the timing is that even mmt4d microkernels are becoming legacy as I'm trying to import more modern techniques that we developed on the GPU side, into the CPU side: https://hackmd.io/ti1_aQYbQK2rn0YUM1lFKQ So I don't really want to tell you to do mmt4d microkernels instead now, as that's what I'm trying to make legacy. So I'm OK with merging this, seeing that it's not very big. Just know that this is assuming that this is a one-off: we wouldn't want too much more of this. Regarding testing:
|
left a comment
There was a problem hiding this comment.
Approving to unblock as I'll be OOO but:
- Wait for @egebeysel 's review on RISC-V aspects
- Read my other comment.
- e2e testing for this is optional.
commented
Mar 11, 2026
|
Actually, based on what you pointed me to, I would be open to putting in the effort and taking inspiration from what you just linked. Ideally, I would like to follow your latest practices. I will have a read to what you just sent. I actually ended up here after what we discussed regarding the mmt4d ukernel and mlir ukernels, when you mentioned about moving things to the compiler and doing proper data tiling. We can still merge it if you think is good. But in any case, I will definitely do my best to move things to how you are trying to implement now things moving forward @bjacob |
commented
Mar 11, 2026
|
So the problem though is that we are in the early stages of a transition where the existing framework (mmt4d) will soon be legacy and the new framework (inner_tiled) isn't available (for CPU) just yet. That's why I'm OK if you just want to merge this. Up to you. |
commented
Mar 11, 2026
|
Okay I see, then in that case it would be lovely to merge it if possible :) Is there any way to help on the inner tile approach? Or parallelly while you design it start getting ready some RVV approach for it? |
commented
Mar 12, 2026
Retryign the bazel build to see if the failure was temporary.
Probably not at first as I need to lay the foundation. Yes in later stages I'll work with you and other stakeholders to port your existing kernels to the new framework.
Yes, I'm talking with @egebeysel and others to ensure alignment. |
02c68e0 to
c93efd3
Compare
commented
Mar 19, 2026
|
I did a test on the Banana-Pi SpacemiTX60 For running this though I need to change the following: runtime/src/iree/base/internal/cpu.c Benchmark ResultsBoth paths compiled with
Tile configuration
|
| IREE_UK_MMT4D_TILE(riscv_64, s8, s8, s32, 2, 1, _v) | ||
| IREE_UK_MMT4D_TILE(riscv_64, s8, s8, s32, 4, 1, _v) | ||
| IREE_UK_MMT4D_TILE(riscv_64, s8, s8, s32, 7, 1, _v) | ||
| IREE_UK_MMT4D_TILE(riscv_64, s8, s8, s32, 8, 1, _v) |
There was a problem hiding this comment.
Indeed, the logic in compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp does not produce the case 8 so this tile function is likely unused.
There was a problem hiding this comment.
Good catch, this was from a previous test of mine in that patch that I forgot to remove. Will do so tomorrow!
There was a problem hiding this comment.
Also I will do an implementation with sext + vwmacc and get back to you on that :)
| #include "iree/builtins/ukernel/arch/riscv_64/mmt4d_riscv_64_internal.h" | ||
|
|
||
| IREE_UK_ATTRIBUTE_ALWAYS_INLINE static inline void | ||
| iree_uk_mmt4d_tile_s8s8s32_1xXXx1_to_8xXXx1_riscv_64_v( |
There was a problem hiding this comment.
just to be clear, you choose the inner tile size M to be 7 max, but implement the ukernel for 8 as well?
| __riscv_vse32_v_i32m4(out_ptr + N0 * 4, acc4, vl); | ||
| __riscv_vse32_v_i32m4(out_ptr + N0 * 5, acc5, vl); | ||
| __riscv_vse32_v_i32m4(out_ptr + N0 * 6, acc6, vl); | ||
| } else if (M0 == 8) { |
There was a problem hiding this comment.
as previously mentioned, I guess in this case you wouldn't have enough registers and would spill (8 * 4 for acc and you're out for the prods and rhs), so if I'm not missing something, there's no need to keep this here. If this is intended and I'm missing something, please add the layout to the external models file, and a comment explaining the kernel :)
There was a problem hiding this comment.
Will change it accordingly, you are definitely right there and it was something I got leftover
| #define IREE_UK_RISCV_64_VWACC_I8(ACC, LHS, RHS, VL) \ | ||
| do { \ | ||
| vint16m2_t prod = __riscv_vwmul_vx_i16m2(RHS, LHS, VL); \ | ||
| ACC = __riscv_vwadd_wv_i32m4(ACC, prod, VL); \ | ||
| } while (0) |
There was a problem hiding this comment.
Do we have to use a two-step multiply-add here? What if we sign-extend the operands to i16 and use vwmacc i16 x i16 -> i32 instead? How would that look like in means of performance? Is there a specific reason you do it this way? I think @adeel10x also pointed this out in Discord.
There was a problem hiding this comment.
To be honest I focused on getting out the patch I had before so that I could get some feedback first an not overengineer it, but that is a very valid point that I forgot to test :)
Tomorrow I will get to it and get you some metrics back
There was a problem hiding this comment.
Yes, In my experience sign-extending the operands to i16 and using vwmacc i16 x i16 -> i32 instead of the 2-step multiply-add results in better perf. I see that you've already made the required changes :).
| IREE_UK_MMT4D_TILE_FUNC_IMPL_FOR_M0( | ||
| iree_uk_mmt4d_tile_s8s8s32_1xXXx1_to_8xXXx1_riscv_64_v, | ||
| iree_uk_mmt4d_tile_s8s8s32_8xXXx1_riscv_64_v, 8) |
| const iree_uk_query_tile_sizes_2d_params_t* params) { | ||
| #if defined(IREE_UK_BUILD_RISCV_64_V) | ||
| if (iree_uk_cpu_riscv_64_v(params->cpu_data)) { | ||
| return (iree_uk_matmul_tile_sizes_t){.M = 7, .K = 1, .N = 32}; |
There was a problem hiding this comment.
I'm not sure how important this actually is, but the N parameter passed here has the assumption that we have LMUL=4 and VLEN=256bits here, whereas the above f32 one has LMUL=4 but VLEN=128bits. Not sure if they have to be aligned or not...
There was a problem hiding this comment.
query_tile_sizes is a historical thing, from an era when we considered supporting fully dynamic tile sizes with a runtime query based on the device CPU. hasn't actually been used. So, I don't care much what you do here; I don't remember how much the test harness forces you to implement, but do stick to the bare minimum required to pass tests.
There was a problem hiding this comment.
Okay then I will just focus on keeping it to a bare minimum to get the tests passing
commented
Mar 23, 2026
|
cc @adeel10x |
commented
Mar 24, 2026
|
Will update tomorrow addressing your comments |
commented
Mar 25, 2026
|
I went through the review points:
On Banana Pi SpacemiT X60:
So the final implementation is both better inner sequence and better e2e :) Let me know if this is sufficient or I should change something |
| #define IREE_UK_RISCV_64_VWMACC_I8(ACC, LHS, RHS, VL) \ | ||
| do { \ | ||
| ACC = __riscv_vwmacc_vx_i32m4(ACC, (iree_uk_int16_t)(LHS), RHS, VL); \ | ||
| } while (0) |
There was a problem hiding this comment.
Is there a specific reason to use a macro here? Wouldn't it be a bit cleaner to use the vwmacc built-in directly in the ukernel?
There was a problem hiding this comment.
Fair enough, let me fix that :)
There was a problem hiding this comment.
Should be fixed now, sorry for the delay!
|
Can you fix the DCO error? @copparihollmann Also, do you need this patch to run this? If so, someone else has to review that. Other than that, it looks good to me, thanks for updating this and testing :) I'll approve once the DCO is fixed and according to the above patch situation 👍 Would also be nice if you could update the title and the description. If you have time to do so, you can also get some measurements with more regular/irregular shapes, e.g. pow2s from 128 to 4096 instead of just 512, and non-pow2s, with possibly wide M/N/K dims. But that's not necessary for the PR, you can defer that to later. |
commented
Apr 8, 2026
|
Hi all! Sorry I was a bit by busy last week, I will get down with it toda/tomorrow :) |
commented
Apr 8, 2026
The patch is only if you want to run it on the banana-pi because of the old linux distro
Working on this right now :) |
commented
Apr 17, 2026
|
@copparihollmann Just a quick ping here. If you could fix the DCO issue and the CI failures we can land this PR as is prior to the more extensive measurements. |
Add mmt4d runtime microkernel for RISC-V 64 with the V extension,
targeting s8*s8->s32 matmul using widening multiply-accumulate:
vwmul.vx (i8*i8->i16) + vwadd.wv (i16+i32->i32).
Tile: M0={1,2,4,7} x N0=dynamic x K0=1. Primary tile for VLEN=256
is 7x32x1, matching the f32 RVV path's register pressure budget
(7*LMUL4 + LMUL2 + LMUL1 = 31/32 vector registers).
Tested on SpacemiT X60 (VLEN=256): 4x speedup on 512x512 i8 matmul
vs codegen-only path.
Fixes iree-org#23730
Signed-off-by: Agustin N. Coppari Hollmann <[email protected]>
Add VLEN-aware tile size selection for widening integer matmul in
enumerateMatmulTileRiscv64(). Uses N0=VLEN/8 (same formula as f32)
to fully utilize the m1->m2->m4 widening chain at LMUL=4.
Primary tile: {7, VLEN/8, 1} with truncations at M0={4,2,1}.
Signed-off-by: Agustin N. Coppari Hollmann <[email protected]>
Signed-off-by: Agustin N. Coppari Hollmann <[email protected]>
Signed-off-by: Agustin N. Coppari Hollmann <[email protected]>
f096cd6 to
7bc368d
Compare
commented
Apr 17, 2026
|
Done! Sorry for the delay, our board died (forgot to give a heads-up), so I was waiting for the new one to arrive and do the tests. DCO should be fixed now @egebeysel |
commented
Jun 8, 2026
|
Merging this as is, will fix in a follow-up PR if anything pops up because of something that changed since the quite oldish CI run. |
Fixes #23730
This PR adds RVV custom vector-contract kernel support for widening i8*i8->i32 matmul on RISC-V.
It updates:
Something I would like some feedback on is what would be your preferred way to test this e2e. So far I compile it and run it on the Banana-pi SpacemiT X60.
Assisted-by: ChatGPT