<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi all,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">A question came up in a code review (<a href="https://reviews.llvm.org/D72820">https://reviews.llvm.org/D72820</a>) about whether or not to allow fast-math flags to be applied to constrained floating point intrinsics (<a href="http://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics">http://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics</a>).
 This has come up several times before, but I don’t think we’ve ever made a decision about it.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">By default, the optimizer assumes that floating point operations have no side effects and use the default rounding mode (round to nearest, ties to even). The constrained intrinsics are meant to prevent the optimizer from making these assumptions
 when the user wants to access the floating point environment -- to change the rounding mode, to check floating point status bits, or to unmask floating point exceptions. The intrinsics have an argument that either specify a rounding mode that may be assumed
 or specify that the rounding mode is unknown (this argument is omitted if it doesn’t apply to the operation) and an argument to specify whether the user wants precise exception semantics to be preserved, wants to prevent syntactically spurious exceptions from
 being raised, or doesn’t care about floating point exceptions.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Because the constrained mode can be localized to a sub-region within a function, we also need to support the case where a constrained intrinsic is used but the default behavior (default rounding mode, exceptions ignored) is used. For this
 reason, I think our IR definition must allow fast math flags to be applied to constrained intrinsics. That makes this primarily a question about what combinations should be permitted by front ends and how constructs like pragmas should affect the various states.
 For example, I might have source code like this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-=-=-=-=-=-=-=-=<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">double doSomethingVague(double X, double Y, double Z) {<o:p></o:p></p>
<p class="MsoNormal">  // Some operation that doesn’t need to be precise.<o:p></o:p></p>
<p class="MsoNormal">  if (X/Y > SomeThreshold)<o:p></o:p></p>
<p class="MsoNormal">    return doSomethingPrecise(X, Y);<o:p></o:p></p>
<p class="MsoNormal">  else<o:p></o:p></p>
<p class="MsoNormal">    return Z;<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">#pragma STDC FENV_ACCESS ON<o:p></o:p></p>
<p class="MsoNormal">double doSomethingPrecise(double X, double Y) {<o:p></o:p></p>
<p class="MsoNormal">  int SaveRM = fegetround();<o:p></o:p></p>
<p class="MsoNormal">  fesetround(FE_DOWNWARD);<o:p></o:p></p>
<p class="MsoNormal">  feclearexcept(FE_ALL_EXCEPT);<o:p></o:p></p>
<p class="MsoNormal">  double Temp = X * Y + Z;<o:p></o:p></p>
<p class="MsoNormal">  if (fetestexcept(FE_ALL_EXCEPT))<o:p></o:p></p>
<p class="MsoNormal">    std::cerr << “Something happened.\n”;<o:p></o:p></p>
<p class="MsoNormal">  fesetround(SaveRM);<o:p></o:p></p>
<p class="MsoNormal">  return Temp;<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-=-=-=-=-=-=-=-=<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Now suppose I compile that with “-O2 -ffp-contract=fast”. We will need to generate constrained intrinsics for the X*Y+Z expression in doSomethingPrecise. The question is, should clang (a) generate a constrained version of the llvm.fmuladd
 instrinsic, (b) generate separate constrained.fmul and constrained.fadd instrinsics with the contract fast math flag set, or (c) generate separate constrained.fmul and constrained.fadd instrinsics with no fast math flag? I would argue for (b), because I think
 it’s reasonable for a user who cares about precision and FP exceptions to still want FMA, which theoretically is more precise. I think not (a) because clang doesn’t usually generate the fmuladd intrinsic with -ffp-contract=fast. On the other hand, if the code
 also contained an FP_CONTRACT pragma around doSomethingPrecise() I think clang should do (a).<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Supporting the FP_CONTRACT case is the point of the D72820 patch.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But let’s make this more interesting. Suppose I compile with “-O2 -fp-model=strict -ffp-contract=fast -fno-honor-nans -fno-honor-infinities -fno-signed-zeros” instead and the code does not contain the FENV_ACCESS pragma (I’ll get back to
 that). Should the nnan, ninf, and nsz fast math flags be applied to the constrained intrinsics? I lean toward “yes”. The way I see it, these command line options are a way for the user to tell the compiler that their data will not contain NaNs or infinities
 and that their algorithms do not depend on the sign of zero. These flags enable us to make some optimizations that will not affect rounding or exception semantics as long as the data is as the user claimed. This will be particularly useful for the strict exception
 semantics because there are cases where we have to execute additional instructions just to preserve the exception semantics in the case where one of the operands is a NaN. If the user knows that will never happen, we can produce better code.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Now back to the reason I wanted to consider that without the pragma. Consider my code above with the pragma again, now imagine I compile it with “-O2 -fp-model=fast”. In this case, the pragma almost certainly intends to remove some fast
 math flags. For instance, I don’t think it makes sense to say you care about rounding mode but want to allow reassociation (because reassociation has a much bigger potential to change results than rounding mode). The flags discussed above could make sense,
 but we have no clear way to know what the user intended, so I think in this case we must clear all the fast math flags. This kind of conflicts with what I said about the contract flag above. I obviously have mixed feelings about that. In the presence of a
 pragma, we should probably block the contract flag too, but I don’t like having to do that for that specific case.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">There’s my opinion. I’d like to hear what others think.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal">Andy<o:p></o:p></p>
</div>
</body>
</html>