flang-compiler / f18-llvm-project

Fork of llvm/llvm-project for f18. In sync with f18-mlir and f18.
http://llvm.org
28 stars 16 forks source link

Should `--math-runtime` be generalised/specialised? #1377

Open banach-space opened 2 years ago

banach-space commented 2 years ago

From the definition of --math-runtime from bbc, I get the impression that it's geared for pgmath:

  --math-runtime=<value>.   - Select math runtime version:
    =fast     -   use pgmath fast runtime
    =relaxed   -   use pgmath relaxed runtime
    =precise   -   use pgmath precise runtime
    =llvm   -   only use LLVM intrinsics (may be incomplete)

Perhaps there should be 2 options:

Alternatively, --math-runtime could be renamed as --pgmath-runtime that would make LLVM Flang switch from using LLVM for generating Math routines to pgmath. So, by default, LLVM Flang would use LLVM for Math routines.

Currently --math-runtime is only available in bbc. We are yet to decide whether to replicate it in flang-new or try to re-define it. I think that it would be good to make sure that flang-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.

kiranchandramohan commented 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?

  1. Upstream pgmath to the llvm repo. And generate pgmath symbols as the default.
  2. Generate math dialect operations and then convert them to pgmath or llvm intrinsics as a separate pass. Extend the math dialect for this purpose.
  3. Generate calls to llvm intrinsics. Add missing intrinsics to llvm. If not possible add to the flang runtime?
  4. Also should we change the default to llvm intrinsics when we upstream until pgmath is in upstream llvm-project?
sscalpone commented 2 years ago

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 commented 2 years ago

@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.

kiranchandramohan commented 2 years ago

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

banach-space commented 2 years ago

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 strictin 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.