agentleFS
buddy-compilerbuddy-compiler/buddy-mlir/.agents/skills/riscv-rvv-optimization/SKILL.md

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.

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
  1. Very specific:

    If it is far above the floor, disassemble the hot function and take the instruction histogram (references/codegen-pitfalls.md, "Reading the …

    line 19
  2. Very specific:

    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.

    line 39
  3. Distinctive:

    Prefer fewer, larger loads when load cost is per instruction.

    line 41

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

  1. Estimate the kernel's floor (bytes / bandwidth, or critical-path instructions x latency, using the target's measured costs in the hardware-targets skill).
  2. Microbenchmark the kernel alone (see performance-optimization methodology) and compare.
  3. 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 redundant vsetvli.
  4. 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's BUDDY_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 / .vx form, 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).

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.