Dear LLVM developers,

I have a question on the fast-math floating-point optimizations applied by LLVM:

Judging by the documentation at https://llvm.org/docs/LangRef.html#fast-math-flags I understood that rewriting with associativity and using reciprocal computations are possible optimizations. As the folklore description of fast-math is that it "applies real-valued identities", I was wondering whether LLVM does also rewrite with distributivity.

If this is the case, could you point me to some specification when it is applied? If not, is there any particular reason against applying distributivity or whether this just has not been looked into so far?

Thank you and best regards,

Heiko

Distribution doesn’t seem to be used by many transforms at present. My vague recollection is that the fast math flags didn’t do a great job of characterizing when it would be allowed, and using it aggressively broke a lot of code in practice (code which was numerical unstable already, but depended on getting the same unstable results), so people have been gun-shy about using it. Owen might remember more of the gory details.

Arguably, it is implicitly used when FMA formation is combined with fast-math, e.g.:

float foo(float x, float y) {

return x*(y + 1);

}

Compiled with -mfma -ffast-math, this generates fma(x, y, x). Even though this transform superficially appears to use distributivity, that’s somewhat debatable because the fma computes the whole result without any intermediate rounding, so it’s pretty wishy-washy to say that it’s been used here.

– Steve

Distribution doesn’t seem to be used by many transforms at present. My vague recollection is that the fast math flags didn’t do a great job of characterizing when it would be allowed, and using it aggressively broke a lot of code in practice (code which was numerical unstable already, but depended on getting the same unstable results), so people have been gun-shy about using it. Owen might remember more of the gory details.

Arguably, it is implicitly used when FMA formation is combined with fast-math, e.g.:

float foo(float x, float y) {

return x*(y + 1);

}

Compiled with -mfma -ffast-math, this generates fma(x, y, x). Even though this transform superficially appears to use distributivity, that’s somewhat debatable because the fma computes the whole result without any intermediate rounding, so it’s pretty wishy-washy to say that it’s been used here.

It most definitely has been used here, because of inf/nan behavior.

inf*(0 + 1) == inf

inf*0 + inf == nan

(I actually fixed this bug in the past because it occurred in practice.)

Cheers,

Nicolai