Skip to content

FDIV/FSQRT as TFU micro-ops - #587

Open
jel221 wants to merge 2 commits into
fix/tfu-timingfrom
feature/fdiv-tfu
Open

jel221 wants to merge 2 commits into
fix/tfu-timingfrom
feature/fdiv-tfu

Conversation

@jel221

@jel221 jel221 commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Human comment:
Commercial GPUs usually don't provide fdiv and sqrt units, they are usually approximations. But just to not break the RISC-V ISA, it is instead implemented as uops. I guess it slightly breaks the ISA in that it is not IEEE compliant, but most GPUs don't provide full IEEE compliant floating point division operators because the hardware for it tends to be too expensive as you'll se e below. So it's up to you, or we can discuss later this week

Moves FP division and sqrt into the TFU as approximations (not IEEE compliant nevessarily). There are two reasons for this. The first reason is just pure area. Beacon doesn't show you a ton of reduction, but the look-up table (not the FPGA resource, the literal look up table for transcendentals) is in BRAM in a real Vivado build, and the amount of flip flop reduction is -30%, which comes out to around 40% reduction on ASIC builds.

The next reason is that it removes the tag and the 2 ops in flight limitation from the FPU. So you can remove this by carrying the tag like any other unit like ALU in the FPU pipeline registers. However, the 17 stages of FDIV and FSQRT makes the area blow up immensely if you do this. So, we make it a pipeline. Because of this, it actually gives you a speedup because FMA is fully pipelined and has better throughput. This is the next PR.


Summary

FDIV and FSQRT (.S and .D) no longer have their own hardware. A new uop generator, VX_uop_fdiv, expands each one into TFU reciprocal / reciprocal-square-root micro-ops plus FPU micro-ops (FMUL, FSUB, FSGNJX, F2F). Every micro-op writes only rd, so any register aliasing works:

instruction micro-ops
FDIV, rd != rs1 rd = rcp(rs2); rd = rs1 * rd
FDIV, rd == rs1 != rs2 rd = rcp(rd); rd = rd * rs2; rd = rcp(rd)
FDIV, rd == rs1 == rs2 rd = abs(rd); rd = lg2(rd); rd = rd - rd; rd = ex2(rd) (gives NaN for 0, inf, NaN)
FSQRT rd = rsqrt(rs1); rd = rcp(rd)
.D each TFU step runs on the single-precision conversion: cvt.s.d, TFU op, cvt.d.s

What goes away:

  • The divide and square-root units of all four FPU backends (DPI, STD, DSP, FPNEW): VX_fdivsqrt_unit, VX_fsqrt_unit, VX_fsqrt_unit_rtl, their unit tests and the vendor IP scripts. VX_fdiv_unit stays, because the RTU uses it.
  • The knobs VX_CFG_FDIV_LATENCY, VX_CFG_FSQRT_LATENCY, VX_CFG_FDIV_PE_RATIO and VX_CFG_FSQRT_PE_RATIO, including their entries in the GPU profiles.

What changes:

  • VX_CFG_EXT_TFU_ENABLE now follows VX_CFG_EXT_F_ENABLE, so every F build has a TFU.
  • VX_CFG_NUM_TFU_LANES = max(1, FPU lanes / 8): one TFU lane per 8 FMA lanes. At 16 threads that is 2 lanes, where master's TFU builds had 1.
  • SimX runs the same micro-op sequences, so the two models stay in step.

This PR is stacked on #584 (TFU timing fix). Without that fix, the TFU's 21–24-level stages would become the deepest path of every core. The base branch is fix/tfu-timing; retarget to master once #584 merges.

Accuracy

The results are approximations, not correctly rounded. Measured by the new fdiv_tfu test, for normal inputs with normal results:

operation max error
FDIV 2 ulp
FDIV with rd == rs1 3 ulp
FSQRT 2 ulp
  • The rd == rs1 case can only run reciprocal, multiply, reciprocal, because every micro-op writes only rd and the first source is gone after the first step. Getting it to 2 ulp would need a hidden scratch register.
  • Flags: FDIV raises no divide-by-zero (DZ) or inexact (NX) flag, because the TFU reciprocal raises none. 0/0 raises invalid (NV) in the multiply (0 × inf).
  • For comparison: OpenCL's default bounds are 2.5 ulp for divide and 3 ulp for sqrt. CUDA's fast-math divide is about 2 ulp.

Area

FPGA Beacon (U55C):

build LUT FF DSP max levels
core, 16 threads × 16 warps 127,132 → 116,403 (−8.4%) 140,277 → 103,820 (−26%) 97 → 107 15 → 15
top_vortex, 2 cores × 4 threads × 4 warps 126,581 → 126,185 (−0.3%) 142,344 → 125,254 (−12%) 50 → 60 15 → 15

Beacon counts each TFU lane's lookup tables as 4.2k LUTs. Vivado puts them in block RAM: on the tfu DUT, Beacon reports 5.7k LUTs where Vivado reports about 2k. So the Vivado LUT saving on the core is larger than Beacon's −8%:

Vivado 2024.2 (U55C, 300 MHz) LUT FF BRAM DSP Fmax
core, 16 threads × 16 warps 168,414 → 123,981 (−26%) 144,158 → 86,336 (−40%) 137 → 43 96 → 102 286.0 → 287.1 MHz
top 138,369 → 132,571 (−4.2%) 153,132 → 136,068 (−11%) 76 → 85 48 → 54 250.9 → 252.8 MHz
tfu (16 threads: now 2 lanes) 2,030 → 2,052 1,105 → 1,745 1 → 2 3 → 6 317.9 → 354.6 MHz

ASIC (ASAP7):

build Beacon cells Beacon FF Beacon Fmax Yosys area Yosys Fmax
core 743,174 → 429,305 (−42%) 93,652 → 76,744 (−18%) 1079 → 1107 MHz 71,902 → 54,700 µm² (−24%) 1044 → 1046 MHz
top 1,341,436 → 1,039,532 (−22%) 241,658 → 225,836 (−7%) 777 → 777 MHz 155,559 → 124,053 µm² (−20%) 1044 → 1040 MHz
tfu (16 threads: now 2 lanes) 14,074 → 27,431 1,816 → 2,581 1239 → 1239 MHz 2,014 → 3,488 µm² 1055 → 1236 MHz

The tfu DUT grows because its 16-thread config now gets 2 lanes. Its Yosys Fmax gain is from #584.

Performance

perf_gate (51 rtlsim cases, re-recorded):

  • Cycle geomean: +0.24%.
  • Cases that move by more than 1%: stencil3d +2.2% (16 threads) and +1.6% (4 threads), rt_raycast +1.3–1.5%, draw3d +1.1%, raycast +1.0%.
  • Instruction counts are identical in 39 of 51 cases. The other 12 use divide or sqrt, and now retire the micro-ops.

Microbenchmarks (rtlsim, cycles vs master, measured before the rebase onto #584, which does not change cycles):

4 threads × 4 warps 8 × 16 32 × 16
fdiv, many warps, independent 0.73–0.80 0.83–0.85 0.83–0.84
fdiv, one warp, dependent chain 1.32–1.41 1.40–1.53 1.41–1.53
fsqrt, one warp 1.41–1.52 1.59–1.78 1.57–1.77
fsqrt, many warps 1.05 1.62–1.66 1.62–1.66
  • Many-warp fdiv gets faster because master's divider was itself capped by the FPU's 2-entry tag store.
  • Sqrt throughput drops because a sqrt takes two TFU passes, and there is one TFU lane per 8 FMA lanes.

In the regression and OpenCL suites, only raycast uses divide and sqrt heavily. The OpenCL kernels cfd, srad, blackscholes and myocyte are divide- or sqrt-dense but are not perf-gated, and their cost has not been measured.

The next PR removes the FPU's 2-in-flight limit, which this PR makes affordable. With it, the app geomean goes to −1.6% / −2.2% at 8 and 32 threads.

Tests

  • Added:
    • tests/regression/fdiv_tfu: every register aliasing of FDIV and FSQRT, checked against ulp bounds, plus special values.
    • parity-fdiv-tfu: that test at 4 threads (1 TFU lane) and 16 threads (2 lanes).
  • Removed: dogfood-7, dogfood-8 and parity-raycast-fdiv4. They ran VX_CFG_FDIV_PE_RATIO / VX_CFG_FSQRT_PE_RATIO, which no longer exist.
  • Skipped: the rv32uf/rv64uf fdiv ISA test, which checks correctly rounded FDIV/FSQRT results and the DZ flag.
  • Changed: the dogfood flag test expects no DZ or NX from FDIV.

Baselines re-recorded

The second commit re-records the golden baselines this change moves. Each was recorded with its update command and is authorized by jel221:

  • ci/baselines/perf/*.json: pytest ci -m perf_gate --update-baselines. All 51 entries change, because removing the FDIV/FSQRT knobs changes the resolved config that every entry's config_hash covers.
  • ci/baselines/synthesis/beacon/core.json: ci/logic_gate.py --update-baseline (core, core_ldst, top_vortex).
  • ci/baselines/synthesis/beacon_asic/{core,tfu}.json: ci/logic_gate.py --tool beacon_asic --update-baseline (core, top, tfu).
  • ci/baselines/synthesis/yosys/{core,tfu}.json: ci/asic_gate.py --update-baseline (core, top, tfu).
  • ci/baselines/synthesis/xilinx/{core,tfu}.json: ci/fpga_gate.py --update-baseline (core, top, tfu).

gfx, gfxvm and tensor were re-run too: they contain no core, and their metrics were unchanged, so they are not re-recorded.

Impact and evidence

Impact class: R3 (detector:config-knobs, detector:test-coverage; from ci/change_impact.py)

Contracts touched: VX_config.toml knobs:

  • Changed: VX_CFG_EXT_TFU_ENABLE and VX_CFG_NUM_TFU_LANES.
  • Removed: VX_CFG_FDIV_LATENCY, VX_CFG_FSQRT_LATENCY, VX_CFG_FDIV_PE_RATIO and VX_CFG_FSQRT_PE_RATIO.
  • The GPU profiles drop the removed knobs.
evidence status detail
functional tests (simx / rtlsim / xrt) run Before the rebase: make -C tests/regression run-simx and run-rtlsim, and the rv32 ISA suites on both, all pass; every app passes at 4×4, 8×16 and 32×16. After the rebase: the 51 perf_gate workloads pass on rtlsim, as do vecadd (simx) and demo (rtlsim). The full suite was not re-run locally after the rebase; CI runs it. xrt not run.
model_parity run Before the rebase: SimX and RTL instruction counts match exactly and cycles are within 2.2% on every case checked, including heavy FMA load. parity-fdiv-tfu added.
perf_gate run re-recorded with --update-baselines; geomean +0.24%, details above
Beacon logic_gate run core, core_ldst, top_vortex: no deeper unit, max 15 levels as before
synthesis (fpga_gate / asic_gate) run core, top, tfu on Vivado 2024.2 and Yosys/OpenSTA (ASAP7)
XLEN 64 build not run the .D sequences are implemented in RTL and SimX but not run locally; CI's XLEN 64 cells are their first run

Self-check: ci/precheck.sh summary: precheck: 6 pass, 1 with findings, 0 fail, 0 not run. The finding is advisory C-STY-1 on the untouched dpi_fdiv declaration, which sits next to the deleted dpi_fsqrt line. It is left as is to match its neighbours' T* x style.

Departures from SHOULD rules:

  • FDIV/FSQRT are not IEEE 754 correctly rounded, and they raise no DZ or NX.
  • The rv32uf/rv64uf fdiv ISA test is skipped for that reason.

Design note: docs/designs/floating_point_unit.md, updated in this PR.

  • No test silenced (known_issue:, wider tolerance, smaller args, removed case) without a root-cause plan. The fdiv ISA test is skipped and the dogfood flag expectations are relaxed on purpose, because the results are approximations by design. The three removed cases tested knobs that no longer exist.
  • Golden data changed only through its update command; the commit names the command and who ran it (X-EV-1).
  • Cycle-moving RTL changes carry the matching SimX change, or the parity case that shows none moved.
  • A toolchain pin moves only after the prebuilt tag exists.

Removing the FDIV/FSQRT knobs changes the resolved config, so every perf
entry's config_hash moves; the synthesis entries move with the smaller FPU
and the 2-lane TFU at 16 threads. Re-recorded with:

  pytest ci -m perf_gate --update-baselines                 (all 51 cases)
  ci/logic_gate.py --update-baseline                        (core, core_ldst, top_vortex)
  ci/logic_gate.py --tool beacon_asic --update-baseline     (core, top, tfu)
  ci/asic_gate.py --update-baseline                         (core, top, tfu)
  ci/fpga_gate.py --update-baseline                         (core, top, tfu)

gfx, gfxvm and tensor were re-run too; they contain no FPU and their
metrics did not move, so they are not re-recorded.

Tools: TOOLCHAIN_REV v3.0.4, beacon 134d03b9e, CIRCT 90c5fd313,
Yosys 0.69, OpenSTA 3.1.0 (asap7 rvt tt), Vivado 2024.2 (xcu55c).

Re-recorded by: agent, authorized by jel221 2026-12-03.
@jel221
jel221 added this pull request to stack #589 December 4, 2026 08:28
@jel221
jel221 marked this pull request as ready for review December 4, 2026 08:28

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant