[llvm-dev] AVX2 codegen - question reg. FMA generation

Fabian Giesen via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 2 09:40:12 PDT 2019


On 9/2/2019 5:49 AM, Uday Kumar Reddy B via llvm-dev wrote:
> On Mon, 2 Sep 2019 at 16:59, Roman Lebedev <lebedev.ri at gmail.com> wrote:
>>
>> It appears you need 'reassoc' on fmul/fadd:
>> https://godbolt.org/z/nuTzx2
> 
> Thanks very much, that was it. Either that or providing
> -enable-unsafe-fp-math to llc yielded FMAs. I didn't expect this since
> using FMAs here instead of mul/add appears to be safer (the reverse is
> unsafe).
> 
> ~ Uday

It goes in both directions. There are expressions that are more accurate 
when evaluated with FMAs but also cases (albeit less common) where 
replacing a mul-then-add with a FMA causes problems.

One example (due to Kahan) is that x^2 - y^2 evaluated as fma(-y,y, 
fmul(x,x)) can result in a negative value for x=y (if the original 
product rounded down) whereas fsub(fmul(x,x), fmul(y,y)) won't.

-Fabian


More information about the llvm-dev mailing list