<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 10pt; color: #000000'><br><hr id="zwchr"><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"Mehdi Amini via llvm-dev" <llvm-dev@lists.llvm.org><br><b>To: </b>"Warren Ristow" <warren.ristow@sony.com><br><b>Cc: </b>llvm-dev@lists.llvm.org<br><b>Sent: </b>Tuesday, November 15, 2016 11:10:48 PM<br><b>Subject: </b>Re: [llvm-dev] RFC: Consider changing the semantics of 'fast' flag implying all fast-math-flags<br><br>
Hi,<div class=""><br class=""><div><blockquote class=""><div class="">On Nov 15, 2016, at 5:15 PM, Ristow, Warren via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">Hi all,</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">This is about<span class="Apple-converted-space"> </span><a href="https://reviews.llvm.org/D26708" style="color: rgb(149, 79, 114); text-decoration: underline;" class="" target="_blank">https://reviews.llvm.org/D26708</a></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">Currently when the command-line switch '-ffast-math' is specified, the</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">IR-level fast-math-flag 'fast' gets attached to appropriate FP math</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">instructions.  That flag acts as an "umbrella" to implicitly turn on all the</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">other fast-math-flags ('nnan', 'ninf', 'nsz' and 'arcp'):</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""><a href="http://llvm.org/docs/LangRef.html#fast-math-flags" style="color: rgb(149, 79, 114); text-decoration: underline;" class="" target="_blank">http://llvm.org/docs/LangRef.html#fast-math-flags</a></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">This approach has the shortcoming that when there is a desire to disable one</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">of these fast-math-flags, if the 'fast' flag remains, it implicitly</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">re-enables the one being disabled.  For example, compiling this test-case:</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    extern void use(float x, float y);</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    void test(float a, float b, float c) {</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">      float q1 = a / c;</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">      float q2 = b / c;</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">      use(q1, q2);</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    }</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">at '-O2 -ffast-math' does a reciprocal-transformation, so only one division</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">is done (as desired with fast-math).  Compiling it with:</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">  -O2 -ffast-math -fno-reciprocal-math</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">should disable the reciprocal transformations (the flag 'arcp'), but leave</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">all the other fast-math transformations enabled.  The current implementation</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">doesn't do that, since the 'fast' IR-level flag still gets set.</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">Motivation of this discussion:<span class="Apple-converted-space"> </span><a href="https://llvm.org/bugs/show_bug.cgi?id=27372#c2" style="color: rgb(149, 79, 114); text-decoration: underline;" class="" target="_blank">https://llvm.org/bugs/show_bug.cgi?id=27372#c2</a></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">As an aside, when '-ffast-math' is specified on the command-line, the</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">following six switches are all passed to cc1:</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    -menable-no-infs</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    -menable-no-nans</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    -fno-signed-zeros</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    -freciprocal-math</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    -fno-trapping-math</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">    -ffp-contract=fast</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">and '-ffast-math' itself is also passed cc1 (the act of passing '-ffast-math'</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">to cc1 results in the macro '__FAST_MATH__' being defined).  When (for</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">example) '-fno-reciprocal-math' is passed in addition to '-ffast-math', then</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">'-freciprocal-math' is no longer passed to cc1 (and the other five listed</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">above still are passed, along with '-ffast-math' still being passed).  It</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">seems like the intention was that these individual switches were to enable</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">the individual floating-point transformations (and so the lack of any of</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">those switches would suppress the relevant transformations), but the</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">'-ffast-math' "umbrella" is over-riding the attempted suppression.</span></div></div></div></blockquote><div><br class=""></div><div>Sure, this looks like a bug, disable an individual fast-math flag on the command line should be possible and override a prior -ffast-math (usually the last one on the command line “wins”/override).</div><div><br class=""></div><div>The Cfe-dev mailing list would be more appropriate to discuss the behavior of clang command line flags though.</div><div><br class=""></div><br class=""><blockquote class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">The change proposed at<span class="Apple-converted-space"> </span><a href="https://reviews.llvm.org/D26708" style="color: rgb(149, 79, 114); text-decoration: underline;" class="" target="_blank">https://reviews.llvm.org/D26708</a><span class="Apple-converted-space"> </span>deals with this issue</span></div></div></div></blockquote><div><br class=""></div><div>This patch seems to modify on LLVM, it does not deal at all with the issue you describe above. </div><div>I don’t see why the issue with the clang command line flags need to be dealt with at the LLVM level.</div><div><br class=""></div><br class=""><blockquote class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">just for the reciprocal-transformation case, but it changes the semantics of</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">the 'fast' IR-level flag so that it no longer implies all the others. </span></div></div></div></blockquote><div><br class=""></div><div>The starting point for any change is: <a href="http://llvm.org/docs/LangRef.html#fast-math-flags" class="" target="_blank">http://llvm.org/docs/LangRef.html#fast-math-flags</a></div><div>You would need to write a new definition for what “fast” would mean.</div><div><br class=""></div><div id="DWT3626">However I don’t need anything need to be changed here to address the use-case you want to fix.</div></div></div></blockquote>I suspect that we want to start by getting rid of 'fast' on the IR level and replacing it with individual flags for the various optimization classes - Do we have only allowing reassociation and libm optimizations? Then we can readjust the Clang flags in a straightforward way.<br><br> -Hal<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><div class=""><div><div></div><div><br class=""></div><div><br class=""></div><blockquote class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> With</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">that proposed approach, rather than an "umbrella" flag such as 'fast' being</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">checked in the back-end (along with an individual flag like 'arcp'), just</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">checking the individual flag ('arcp') would be done. </span></div></div></div></blockquote><div><br class=""></div><div>There is already no need to check the “fast” *and* arcp flag, if a transformation is about reciprocal, then you only need to check arcp (fast implies arcp, checking for fast would be redundant).</div><div><br class=""></div><div>Be careful also that the fast-math flags are mainly an IR level definition, the backend only inherited these per instruction flag very recently. It has been entirely converted to use these, and it still uses a global flag in some places.</div><div>The line you’re touching in your patch for instance is about this legacy:</div><div><br class=""></div><div>  if (!UnsafeMath && !Flags->hasAllowReciprocal())</div><div><br class=""></div><div>The first flag is the global “fast-math” mode on the backend, which is not as fine grain as the per-instruction model.</div><div>The second flag is the “per instruction” flag, which is the model we aim at.</div><div><br class=""></div><div>We should get rid of the “global” UnsafeMath in the backend, but that does not require any change to the IR or the individual fast-math flags.</div><div><br class=""></div><div><br class=""></div><blockquote class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> Any fast-math-related</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">transformation that doesn't have an individual flag (e.g., re-association</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">currently doesn't), should eventually have an individual flag defined for</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">it, and then that individual flag should be checked.</span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class=""> </span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri,sans-serif;" class=""><span style="font-family: 'Courier New';" class="">What do people think?</span></div></div></div></blockquote><div><br class=""></div></div><div class="">I think these are valuable problems to solve, but you should tackle them piece by piece: </div><div class=""><br class=""></div><div class="">1) the clang part of overriding the individual FMF and emitting the right IR is the first thing to fix.</div><div class="">2) the backend is still using the global UnsafeFPMath and it should be killed.</div><div class=""><br class=""></div><div class="">Hope this makes sense.</div><div class=""><br class=""></div><div class="">— </div><div class="">Mehdi</div><div class=""><br class=""></div></div><br>_______________________________________________<br>LLVM Developers mailing list<br>llvm-dev@lists.llvm.org<br>http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br></blockquote><br><br><br>-- <br><div><span name="x"></span>Hal Finkel<br>Lead, Compiler Technology and Programming Languages<br>Leadership Computing Facility<br>Argonne National Laboratory<span name="x"></span><br></div></div></body></html>