[RFC] Add support for options -fp-model= and -fp-speculation= : specify floating point behavior

Intel would like to contribute a patch to implement support for these Intel- and Microsoft -fp options. This message is to describe the options and request feedback from the community.
-fp-model=[precise|strict|fast|except[-]] and
-fp-speculation=[fast|strict|safe]

This contribution would dovetail with the llvm patch "Teach the IRBuilder about constrained fadd and friends" which is under review here, ⚙ D53157 Teach the IRBuilder about constrained fadd and friends. I have a patch ready to review that works with D53157. The motivation for providing these is that having a single option to control most basic FP options is better and easier to understand for users.

The option settings -fp-model=[precise|strict|fast|except] are supported by both ICC and CL. The fp-speculation option is supported only by ICC. The CL and ICC -fp-model option is documented on these pages:
    /fp (Specify floating-point behavior) | Microsoft Docs
    fp-model, fp

Currently, clang's default behavior corresponds to -fp-model=precise. Clang/llvm support for -fp-model=[strict|except] is being developed in the D53157 patch, and there is current llvm support for the fast settings by using the fast math flags llvm::FastMathFlags. Note: the clang-cl wrapper to support Microsoft options has simplified support for these options by mapping /fp-model=except to ftrapping-math, fp-mdel=fast to ffast-math, fp-model=precise and fp-model=strict to fno-fast-math (see clang/Driver/CLCompatOptions.td).

According to the online options documentation, you can combine except[-] with [precise|strict|fast], but combining both fast and except is not a valid setting (the Driver will emit an error).
    precise - Disables optimizations that are not value-safe on floating-point data, although FP contraction is enabled.
    strict - Enables precise and except, disables contractions (FMA), and enables pragma stdc fenv_access.
    fast - Equivalent to -ffast-math
    except/except- - Determines whether strict floating-point exception semantics are honored.

The ICC option -fp-speculation is described here,
    fp-speculation, Qfp-speculation
These are the meanings of the fp-speculation settings:
    fast - Tells the compiler to speculate on floating-point operations. This is equivalent to "fpexcept.ignore" in the constrained intrinsics review D53157.
    strict - Tells the compiler to disable speculation on floating-point operations. This is equivalent to "fpexcept.strict" in the constrained intrinsics review D53157.
    safe - Tells the compiler to disable speculation if there is a possibility that the speculation may cause a floating-point exception. This is equivalent to "fpexcept.maytrap" in the constrained intrinsics review D53157.

+1 from Cray.

If we are talking about “having a single option to control most basic FP options” in general, I’d also like to see a finer granularity for -fp-model=fast, like Intel’s own fast=[1|2]. Perhaps they just become their own options? I don’t have a full list of proposed optimizations (and/or other existing flags) that would be tied for each of the finer grained levels, but at a high level, for example:

  • precise (as described)
  • strict (as described)
  • optimize (something between precise and fast)
  • fast (as described)
  • aggressive (even more aggressive FP optimization than fast)
  • except/except- (as described)

This would maintain reasonable command line compatibility but also allow for easier tuning (and debugging) of codes that are numerically sensitive at a global user level.

Thanks Scott. I’m fixing my patches a little and I’m going to upload it for review and hopefully will get some more information from the community in that format. I agree it would be cool to have more sophisticated levels for optimizations, but I don’t have a patch that implements those, so I’m planning to submit the patch with option levels as described.