Tracking down a 25% regression on LLVM RISC‑V

What happened
It has been reported that a recent change in LLVM altered floating‑point cast folding and unintentionally removed a narrowing optimization on RISC‑V, producing roughly a 24% performance regression on a benchmark running on the SiFive P550. The symptom was stark: LLVM-compiled code emitted fdiv.d (double precision) where GCC and an earlier LLVM build produced fdiv.s (single precision). On the P550 that matters — fdiv.d costs about 33 cycles vs 19 for fdiv.s — and those extra cycles added up in the hot loop.
The sleuthing
Kavin Gnanapandithan dug in with llvm-mca and assembly diffs. At first the two builds looked almost identical; then the extra fdiv.d showed up in the new build’s timing profile even though it sometimes didn’t appear in static slices of the loop. Weird, right? He traced the behavior back through the IR and codegen: LLVM’s isKnownExactCastIntToFP was folding an fpext(sitofp x -> float) to double into a direct uitofp x -> double, which allegedly broke a downstream narrowing path in visitFPTrunc that had relied on that fpext to reduce double-to-float sequences.
The fix
The fix was surgical and elegant. Kavin extended getMinimumFPType with range analysis so the compiler can recognize that fptrunc(uitofp x double) to float can be simplified to uitofp x to float, restoring the narrowing optimization. It has been reported that this patch eliminates the performance gap for this benchmark, bringing LLVM back in line with GCC for the case in question.
Why it matters
Optimization regressions like this are small in code but large in impact. One folded cast, one missed narrowing, and suddenly you’ve lost a quarter of your performance in a hotspot. Want to avoid future surprises? Tests that catch microarchitectural latency effects — and careful IR-level reasoning about cast transformations — are worth their weight in gold. This episode is a tidy reminder: compiler tweaks are subtle, CPU pipelines are fickle, and the devil lives in the lowers.
Sources: kaving.me, Hacker News
Comments