This is an archive of the discontinued LLVM Phabricator instance.

[RFC] Alternate implementation of D53157 IRBuilder for Constrained FP using enumeration vs MDNode and add support for fp-model and fp-speculation language options
AbandonedPublic

Authored by mibintc on May 31 2019, 6:55 AM.

Details

Summary

This is part of RFC to support language options -fp-model and -fp-speculation.

This makes a small modification to D53157 to pass some arguments using an enumeration type instead of MDNode.

In addition I created the FPState.h file which declares 2 enumeration types corresponding to the option settings. I want to put them into llvm so it will be available not only to clang but to other language front ends that might want to offer floating point control using option settings. The language options are combined using certain semantics into IRBuilder settings. There's a member function in FPState.h that does this.

There's a clang patch that goes along with this patch, see https://reviews.llvm.org/D62731

Here's the RFC pasted from email to llvm-dev and clang-dev:
Intel would like to contribute a patch to implement support for these Intel- and Microsoft -fp options:
-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, https://reviews.llvm.org/D53157/new/. 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:

https://docs.microsoft.com/en-us/cpp/build/reference/fp-specify-floating-point-behavior?view=vs-2019
https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-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,

https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-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.

Diff Detail

Repository
rL LLVM

Event Timeline

mibintc created this revision.May 31 2019, 6:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2019, 6:55 AM
Herald added a subscriber: mgorny. · View Herald Transcript
mibintc edited the summary of this revision. (Show Details)May 31 2019, 7:02 AM
mibintc added a reviewer: chandlerc.

New files are missing header blurbs; the precise explanation of what is what is missing (should be in doc/, not patch description)

include/llvm/IR/FPState.h
9–31

All this needs comments, and possibly better names.
FPM_Off,etc is very non-self-explanatory.

kpn added a comment.May 31 2019, 7:56 AM

Incorporating feedback from D53157 is probably a good idea. Looks like that hasn't been done yet here completely.

kpn added inline comments.May 31 2019, 8:14 AM
include/llvm/IR/FPState.h
9–31

Shouldn't there be an "accept default" value? That way CreateConstrainedFAdd()/CreateConstrainedBinOp() can accept enumerations and avoid having front ends having to deal with MDNode*.

mibintc marked 3 inline comments as done.May 31 2019, 8:53 AM
mibintc added inline comments.
include/llvm/IR/FPState.h
9–31

By "off" I just meant, not specified, and therefore the default setting would prevail. If you prefer "default" instead of "off" that's fine by me.

include/llvm/IR/IRBuilder.h
247

In order to match the code that @kpn wrote, I should set DefaultConstrainedExcept = strict instead of just leaving it off.

261

to match @kpn patch, I should set DefaultConstrainedRounding to dynamic instead of leaving it off

pengfei added a subscriber: pengfei.Jun 2 2019, 5:35 PM
mibintc abandoned this revision.Jun 3 2019, 11:13 AM

I'm abandoning this patch in deference to @kpn 's patch under review D53157