riscv-rvv-optimization
Skill773 starsChanged 34 days agoApache-2.0View on GitHub
Write and tune RISC-V Vector (RVV 1.0) code generated by Buddy-MLIR (vector dialect, scf loops, MLIR Python-built kernels lowered through LLVM) - LMUL and register pressure, spills, reductions, broadcasts and permutes, load sizes, scheduling for in-order cores, VLEN options. Use when an RVV kernel is slower than its estimate or when writing a new one.
In this repository6 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- hardware-targetsSkill
- llm-inference-optimizationSkill
- performance-optimizationSkill
- upstream-prSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 19
If it is far above the floor, disassemble the hot function and take the instruction histogram (references/codegen-pitfalls.md, "Reading the …
- Very specific: line 39
Do not broadcast a vector element with a permute: extract to a scalar (cheap) and use the .vf / .vx form, or read it from memory.
- Distinctive: line 41
Prefer fewer, larger loads when load cost is per instruction.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
RVV optimization
The source (MLIR vector ops) does not tell what runs: LLVM decides register allocation, how extracts and broadcasts lower, and the schedule. Check the generated code, not only the source.
Workflow
- Estimate the kernel's floor (bytes / bandwidth, or critical-path
instructions x latency, using the target's measured costs in the
hardware-targetsskill). - Microbenchmark the kernel alone (see
performance-optimizationmethodology) and compare. - If it is far above the floor, disassemble the hot function and take the
instruction histogram (
references/codegen-pitfalls.md, "Reading the code"). Look for spills, permutes, slides, scalar moves and redundantvsetvli. - Change the structure of the source (loop instead of unroll, fewer live vectors, a different data path for a scalar) and confirm the histogram changed before timing.
Rules of thumb
- Vector length: fix the VLEN when the target is known
(
+zvl<N>b,-riscv-v-vector-bits-max=<N>; Buddy'sBUDDY_RISCV_VLEN). Kernels stay correct for any VLEN. - 32 vector registers: at LMUL 4 only 8 groups. Count the vectors live across a loop iteration (accumulators, loop-invariant operands, loaded rows) before unrolling; past 8 groups LLVM spills whole groups.
- Unrolling helps an in-order core only while everything stays in registers; a small loop with scalar bookkeeping is often faster.
- Reductions (
vfredusum,vfredmax) are long-latency; give the core independent work between them. - Do not broadcast a vector element with a permute: extract to a scalar
(cheap) and use the
.vf/.vxform, or read it from memory. - Prefer fewer, larger loads when load cost is per instruction.
- For in-order cores, schedule with the core's model (
-mcpu=...) and try-misched-prera-direction=topdown.
references/codegen-pitfalls.md has the patterns observed in Buddy's
kernels and their fixes. Target costs: hardware-targets (e.g.
spacemit-k3-instruction-costs.md).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

