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

オープン
#521 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
cpp
領域
compilers

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Verilog
スター
2.4k
フォーク
563
平均マージ
1日 7時間
マージ済み PR(30日)
71

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

vortexgpgpu/vortex のほかの issue

vortexgpgpu/vortex の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。