[PATCH] D136080: [flang] Add -ffp-contract option processing

Slava Zakharin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 20 11:26:02 PDT 2022


vzakhari added inline comments.


================
Comment at: flang/include/flang/Frontend/LangOptions.h:29
+
+    // Enable the floating point pragma
+    FPM_On,
----------------
tblah wrote:
> vzakhari wrote:
> > tblah wrote:
> > > awarzynski wrote:
> > > > What are these pragmas? Perhaps you can add a test that would include them?
> > > I copied this comment from clang. I believe the pragma is 
> > > ```
> > > #pragma clang fp contract(fast)
> > > ```
> > > 
> > > See https://clang.llvm.org/docs/LanguageExtensions.html#extensions-to-specify-floating-point-flags
> > > 
> > > This patch only adds support for argument processing, so I can't test for the pragmas.
> > I do not think we should blindly copy this from clang.  I believe `-ffp-contract=on`  is there to do the contraction complying to the language standard - but Fortran standard says nothing about contraction.  I am also not aware about a Fortran compiler that supports directives related to contraction, so `fast-honor-pragmas` does not seem to be applicable as well.  Basically, we end up with just `off` and `fast`.
> > 
> > Now, it may be reasonable to support the same `-ffp-contract=` arguments so that users can apply the same options sets for C/C++ and Fortran compilations.  If we want to do this, we need to map `on` and `fast-honor-pragmas` to something, e.g. `fast`.  A driver warning (not an error) may be useful to make the option's behavior clear when `on` and `fast-honor-pragmas` are passed.
> From the clang help text:
> ```
> Form fused FP ops (e.g. FMAs):
> fast (fuses across statements disregarding pragmas)
> | on (only fuses in the same statement unless dictated by pragmas)
> | off (never fuses)
> Default is 'on'
> ```
> 
> So if we removed `on` and set the default to `off` we would no longer fuse within the same statement by default.
> 
> Classic-flang seems to support `on`, `off` and `fast`: https://github.com/flang-compiler/flang/blob/master/test/llvm_ir_correct/fma.f90
Not talking about defaults just yet, I think Flang cannot currently support the `on` mode as documented.

I do not have the latest classic flang readily availalbe, but I am curious what it will generate for this example:
```
function fn(x,y,z)
  real :: x,y,z
  fn = x * y
  fn = fn + z
end function
```

With a very old classic flang I get just `fast` math flags on the multiple and add instructions, which is obviously not what `on` is supposed to do:
```
$ flang -target aarch64-linux-gnu -O1 -c -S -emit-llvm -ffp-contract=on fma.f90
$ cat fma.ll
  %4 = fmul fast float %3, %1, !dbg !10
  %5 = bitcast i64* %z to float*, !dbg !11
  %6 = load float, float* %5, align 4, !dbg !11
  %7 = fadd fast float %6, %4, !dbg !11
```

Maybe the latest classic flang does support it properly, e.g. it only contracts operations from the same statement.  But I do not see a way to support this in Flang right now, so documenting the `on` mode as it is in clang seems confusing.

We can still support `on` in the Flang option, but I think we need to issue a warning saying that it defaults to something else, e.g. to `fast`.  If mapping `on` to `fast` is not appropriate to some users, then they will have to explicitly specify `-ffp-contract=off` for Fortran compilations in their build system.

I am also curious what `fuses in the same statement` means for Fortran.  For example:
```
  x1 = DOT_PRODUCT(x2, x3)+x4*x5+x6
```

If Fortran processor decides to implement `DOT_PRODUCT` as inline multiply+add loop, does `-ffp-contract=on` apply to them or it only applies to `x4*x5+x6`?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136080/new/

https://reviews.llvm.org/D136080



More information about the cfe-commits mailing list