Open banach-space opened 2 years ago
@jeanPerier is there an option to generate the math dialect (https://mlir.llvm.org/docs/Dialects/MathOps/) operations for intrinsics? Or is the preferred approach to always generate llvm intrinsics? Will there be missing intrinsics in llvm?
What is the best approach going forward?
LLVM has a notion of precise and fast intrinsics. And finer grain control that mimics the intention of relaxed. Clang exposes these as:
-fapprox-func Allow certain math function calls to be replaced with an approximately equivalent calculation
-ffast-math Allow aggressive, lossy floating-point optimizations
-fmath-errno Require math functions to indicate errors by setting errno
-freciprocal-math Allow division operations to be reassociated
-menable-unsafe-fp-math Allow unsafe floating-point math optimizations which may decrease precision
Also, the codegen affected by precise/fast/relaxed goes beyond the selection of the math library. The front end needs to be involved because, afaik, e.g. there's no llvm intrinsics for acosh. And, e.g. the front end adds the 'fast' attribute to IR.
@jeanPerier is there an option to generate the math dialect (https://mlir.llvm.org/docs/Dialects/MathOps/) operations for intrinsics?
No, when we started lowering the intrinsics, it was not clear if MLIR real types would support all the types needed for Fortran, so I added a way to generated LLVM intrinsics instead. It would make sense to have the option to generate MLIR math dialect operations since other MLIR dialects/transforms may be able to take advantage of them, although I have not dug in detail in the matter.
There has been some progress in clang in this area. There is now a kind of umbrella flag, ffp-model=<strict|precise|fast>
. See the following links for more details.
https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffp-model
https://github.com/llvm/llvm-project/blob/main/clang/test/CodeGen/ffp-model.c
https://www.youtube.com/watch?v=GPB_o_wXPcY
Thank you all for all the pointers!
I've just watched the presentation by Andy Kaylor from LLVM Dev 2021 (thanks for the link @kiranchandramohan !) and I think that it is very relevant to our discussion here.
As Kiran pointed out, Clang implements -ffp-model=<value>
and I think that Flang should adopt that too. This way it will remain consistent with Clang. We may need to consider renaming relaxed
as strict
in Flang. Alternatively, we could add relaxed
to the mix, but for that it would be good to ask on cfe-dev
. What do you think?
I'm also more convinced now that we need a separate option for switching between pgmath
and LLVM provided Math intrinsic (i.e. "runtime library to use" and "FP mode" are orthogonal). Would that make sense to you?
Btw, what are the semantics of relaxed
in "pgmath"? Could strict
be a sensible alias for relaxed
? We would define the meaning of strict
in Flang independently of Clang.
From the definition of --math-runtime from
bbc
, I get the impression that it's geared forpgmath
:Perhaps there should be 2 options:
pgmath
orllvm
or something else?),pgmath
runtimes? (it would only work whenpgmath
runtime is used)Alternatively,
--math-runtime
could be renamed as--pgmath-runtime
that would make LLVM Flang switch from using LLVM for generating Math routines topgmath
. So, by default, LLVM Flang would use LLVM for Math routines.Currently
--math-runtime
is only available inbbc
. We are yet to decide whether to replicate it inflang-new
or try to re-define it. I think that it would be good to make sure thatflang-new
remains consistent with Clang and GFortran in this area.On a related note, Clang has
--rtlib
for selecting a runtime (see the docs). Is--math-runtime
meant to be similar/related/orthogonal/complementary?Btw, this came up in a discussion for https://github.com/flang-compiler/f18-llvm-project/issues/1373.