<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""><div><blockquote type="cite" 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="">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-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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,<o:p class=""></o:p></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=""><o:p class=""> </o:p></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="">https://reviews.llvm.org/D26708</a><o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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'):<o:p class=""></o:p></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=""><o:p class=""> </o:p></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="">http://llvm.org/docs/LangRef.html#fast-math-flags</a><o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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:<o:p class=""></o:p></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=""><o:p class=""> </o:p></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);<o:p class=""></o:p></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) {<o:p class=""></o:p></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;<o:p class=""></o:p></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;<o:p class=""></o:p></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);<o:p class=""></o:p></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=""> }<o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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:<o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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.<o:p class=""></o:p></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=""><o:p class=""> </o:p></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="">https://llvm.org/bugs/show_bug.cgi?id=27372#c2</a><o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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:<o:p class=""></o:p></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=""><o:p class=""> </o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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=""><o:p class=""> </o:p></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'<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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 type="cite" class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: 'Courier New';" class=""><o:p class=""></o:p></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=""><o:p class=""> </o:p></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="">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 type="cite" class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: 'Courier New';" class=""><o:p class=""></o:p></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<o:p class=""></o:p></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="">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>However I don’t need anything need to be changed here to address the use-case you want to fix.</div><div><br class=""></div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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 type="cite" class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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<o:p class=""></o:p></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<o:p class=""></o:p></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<o:p class=""></o:p></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.<o:p class=""></o:p></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=""><o:p class=""> </o:p></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></body></html>