__builtin_fmaf compiles to a call to libm fmaf; a * b + c gets an inline fmadd.s (1.9x on a matrix-vector kernel)

Aperta
#521 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
compilers

Direzione di ricerca

Start with the Vortex LLVM lowering of __builtin_fmaf and llvm.fma.f32, then reproduce with the supplied RISC-V target and kernel flags. Inspect the FMA-related cases in tests/regression/flash_attn and compare flash_attn_tcu_wg. Done means the intended fused semantics are confirmed and the lowering produces the expected inline fmadd.s without changing results.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What

__builtin_fmaf becomes a tail call to libm's fmaf, and libm's fmaf is itself just fmadd.s plus ret. The plain expression a * b + c is contracted to an inline fmadd.s.

This is with the Vortex LLVM (clang 20.1.8, vortexgpgpu/llvm f44a6bb) and the kernel flags the regression tests use: --target=riscv32-unknown-elf -march=rv32imaf -mabi=ilp32f -O3 -Xclang -target-feature -Xclang +xvortex ....

float f_builtin(float a, float b, float c) { return __builtin_fmaf(a, b, c); }
float f_contract(float a, float b, float c) { return a * b + c; }
<_Z9f_builtinfff>:
   auipc  t1, 0x0        R_RISCV_CALL_PLT fmaf
   jr     t1
<_Z10f_contractfff>:
   fmadd.s fa0, fa0, fa1, fa2
   ret

Cost

Inside a loop, each fused multiply-add becomes a call, a return and register moves. The loop's FMA results also funnel through fa0.

Measured on a bf16 matrix-vector kernel (one core, 32 threads × 8 warps, SimX, a 3072 × 576 product) after replacing __builtin_fmaf with an inline-asm fmadd.s:

  • cycles: 1,691,965 → 880,889 (1.92×);
  • instructions: about 702k → 425k per product;
  • results: identical.

In the tree

  • tests/regression/flash_attn with #520 has 27 calls to fmaf, from the software exp's polynomial. We can switch #520's expf_fast to a * b + c if you'd like.
  • flash_attn_tcu_wg has none.

Question

Is the libcall intended?

Upstream RISC-V LLVM lowers llvm.fma.f32 to fmadd.s when the F extension is present. If Vortex's fmadd.s is an IEEE fused multiply-add, doing the same here would speed up every kernel that calls fmaf or __builtin_fmaf. If it isn't exactly fused, that would explain the libcall, but then a * b + c contracting to it is the case to check.

🤖 Generated with Claude Code

Lingua principale
Verilog
Stelle
2.4k
Fork
563
Merge medio
1g 7h
PR unite (30g)
71

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vortexgpgpu/vortex

Tutte le issue di vortexgpgpu/vortex

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.