<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 03/15/2017 01:35 PM, Kaylor, Andrew
      via llvm-dev wrote:<br>
    </div>
    <blockquote
cite="mid:0983E6C011D2DC4188F8761B533492DE576FA822@ORSMSX115.amr.corp.intel.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <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:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.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]-->
      <div class="WordSection1">
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">It’s
            true, I am working on this.  I have committed the initial
            patch to add constrained intrinsics for the basic FP
            operations.  This has the desired effect of preventing
            optimizations that would violate strict FP semantics with
            regard to rounding mode and exception status, but it also
            prevents many safe optimizations.  Later this year I’ll be
            going through the code base and trying to teach various
            optimizations to handle the constrained intrinsics safely.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Nothing
            has been added to clang yet to generate these intrinsics,
            though I have some rough code to do so that’s just waiting
            for an implementation of the much harder task of figuring
            out when such restrictions are needed.  </span></p>
      </div>
    </blockquote>
    <br>
    Can you elaborate on this? What do you mean by figuring out where
    the restrictions are needed?<br>
    <br>
     -Hal<br>
    <br>
    <blockquote
cite="mid:0983E6C011D2DC4188F8761B533492DE576FA822@ORSMSX115.amr.corp.intel.com"
      type="cite">
      <div class="WordSection1">
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">If
            anyone did have a front end that generated these intrinsics,
            everything is in place to get them through to code
            generation (though they currently become at least
            theoretically unrestricted again after ISel).  I have an
            experimental pass that converts all basic FP operations to
            the constrained versions and I’ve been able to successfully
            compile and run real-world FP-using applications with that
            pass in place.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">I’m
            currently working on a patch to add constrained versions of
            the standard library FP intrinsics (sin, cos, pow, etc.).<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">If
            anyone is interested in getting pieces of the
            work-in-progress I’ve mentioned above to test drive, let me
            know.  I’d be happy to share.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">-Andy<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <p class="MsoNormal"><a moz-do-not-send="true"
            name="_MailEndCompose"><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></a></p>
        <p class="MsoNormal"><a moz-do-not-send="true"
            name="_____replyseparator"></a><b><span
              style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span
style="font-size:11.0pt;font-family:"Calibri",sans-serif">
            Sanjay Patel [<a class="moz-txt-link-freetext" href="mailto:spatel@rotateright.com">mailto:spatel@rotateright.com</a>]
            <br>
            <b>Sent:</b> Wednesday, March 15, 2017 8:16 AM<br>
            <b>To:</b> Samuel Antão <a class="moz-txt-link-rfc2396E" href="mailto:samuelfantao@gmail.com"><samuelfantao@gmail.com></a>;
            Kaylor, Andrew <a class="moz-txt-link-rfc2396E" href="mailto:andrew.kaylor@intel.com"><andrew.kaylor@intel.com></a><br>
            <b>Cc:</b> llvm-dev <a class="moz-txt-link-rfc2396E" href="mailto:llvm-dev@lists.llvm.org"><llvm-dev@lists.llvm.org></a>;
            <a class="moz-txt-link-abbreviated" href="mailto:acjacob@us.ibm.com">acjacob@us.ibm.com</a><br>
            <b>Subject:</b> Re: [llvm-dev] Speculative execution of FP
            divide Instructions - Call-Graph Simplify<o:p></o:p></span></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <div>
          <div>
            <div>
              <p class="MsoNormal"><span style="font-size:9.5pt">> -
                  is there any target for which fp division does not
                  have side effects?
                  <o:p></o:p></span></p>
            </div>
            <p class="MsoNormal" style="margin-bottom:12.0pt"><br>
              Yes - all of them. This goes back to the fact that
              clang/llvm do not support changing the FPENV:<br>
              <a moz-do-not-send="true"
                href="https://bugs.llvm.org/show_bug.cgi?id=8100"
                target="_blank">https://bugs.llvm.org/show_bug.cgi?id=8100</a><o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal" style="margin-bottom:12.0pt">There has
              been some effort to change that recently, so maybe this is
              (partly) fixed? (cc'ing Andy Kaylor)<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal">These reports may also provide info /
              answers / work-arounds:<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><a moz-do-not-send="true"
                href="https://bugs.llvm.org/show_bug.cgi?id=6050"
                target="_blank">https://bugs.llvm.org/show_bug.cgi?id=6050</a><br>
              <a moz-do-not-send="true"
                href="https://bugs.llvm.org/show_bug.cgi?id=24343">https://bugs.llvm.org/show_bug.cgi?id=24343</a><o:p></o:p></p>
          </div>
          <div>
            <div>
              <p class="MsoNormal"><a moz-do-not-send="true"
                  href="https://bugs.llvm.org/show_bug.cgi?id=24818"
                  target="_blank">https://bugs.llvm.org/show_bug.cgi?id=24818</a><o:p></o:p></p>
              <div>
                <p class="MsoNormal" style="margin-bottom:12.0pt"><o:p> </o:p></p>
                <div>
                  <p class="MsoNormal">On Wed, Mar 15, 2017 at 3:41 AM,
                    Samuel Antão via llvm-dev <<a
                      moz-do-not-send="true"
                      href="mailto:llvm-dev@lists.llvm.org"
                      target="_blank">llvm-dev@lists.llvm.org</a>>
                    wrote:<o:p></o:p></p>
                  <blockquote style="border:none;border-left:solid
                    #CCCCCC 1.0pt;padding:0in 0in 0in
                    6.0pt;margin-left:4.8pt;margin-right:0in">
                    <div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:9.5pt">Hi all,</span><o:p></o:p></p>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt"><o:p> </o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">I came across an
                              issue caused by the Call-Graph Simplify
                              Pass. Here is a a small repro:<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt"><o:p> </o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">```<o:p></o:p></span></p>
                        </div>
                        <div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">define double
                                @foo(double %a1, double %a2, double %a3)
                                #0 {<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">entry:<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_mul = fmul
                                double %a1, %a2<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_cmp = fcmp
                                ogt double %a3, %a_mul<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  br i1 %a_cmp,
                                label %a.then, label %a.end<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt"><o:p> </o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">a.then:        
                                                                 <o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_div = fdiv
                                double %a_mul, %a3<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  br label
                                %a.end<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt"><o:p> </o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">a.end:<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_factor =
                                phi double [ %a_div, %a.then ], [
                                1.000000e+00, %entry ]<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  ret double
                                %a_factor<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">}<o:p></o:p></span></p>
                          </div>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">```<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">Here, the
                              conditional is guarding a possible
                              division by zero. However if I run
                              CGSimplify on this I get:<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">```<o:p></o:p></span></p>
                        </div>
                        <div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">define double
                                @foo(double %a1, double %a2, double %a3)
                                local_unnamed_addr #0 {<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">entry:<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_mul = fmul
                                double %a1, %a2<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_cmp = fcmp
                                olt double %a_mul, %a3<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_div = fdiv
                                double %a_mul, %a3<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  %a_factor =
                                select i1 %a_cmp, double %a_div, double
                                1.000000e+00<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">  ret double
                                %a_factor<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">}<o:p></o:p></span></p>
                          </div>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">``` <o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">This will cause a
                              FP arithmetic exception, and the
                              application will get a SIGFPE signal. The
                              code that drives the change in the
                              optimizer relies on
                              `llvm::isSafeToSpeculativelyExecute` to
                              decide whether the division should be
                              performed speculatively. Right now, this
                              function returns true. One could do
                              something similar to integer divisions and
                              add a case there (this solved the issue
                              for me):<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">```<o:p></o:p></span></p>
                        </div>
                        <div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">diff --git
                                a/lib/Analysis/ValueTracking.cpp
                                b/lib/Analysis/ValueTracking.cpp<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">index
                                1761dac..c61fefd 100644<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">---
                                a/lib/Analysis/ValueTracking.cpp<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+++
                                b/lib/Analysis/ValueTracking.cpp<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">@@ -3352,6
                                +3352,21 @@ bool
                                llvm::isSafeToSpeculativelyExecute(const
                                Value *V,<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">     // The
                                numerator *might* be MinSignedValue.<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">     return
                                false;<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">   }<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+  case
                                Instruction::FDiv:<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+  case
                                Instruction::FRem:{<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    const Value
                                *Denominator = Inst->getOperand(1);<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    // x / y is
                                undefined if y == 0<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    // The
                                denominator is not a constant, so there
                                is nothing we can do to prove<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    // it is
                                non-zero.<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    if (auto
                                *VV =
                                dyn_cast<ConstantVector>(Denominator))<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    
                                 Denominator = VV->getSplatValue();<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    if
                                (!isa<ConstantFP>(Denominator))<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+      return
                                false;<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    // The
                                denominator is a zero constant - we
                                can't speculate here.<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    if
                                (m_AnyZero().match(Denominator))<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+      return
                                false;<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+    return
                                true;<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">+  }<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">   case
                                Instruction::Load: {<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">     const
                                LoadInst *LI =
                                cast<LoadInst>(Inst);<o:p></o:p></span></p>
                          </div>
                          <div>
                            <p class="MsoNormal"><span
                                style="font-size:9.5pt">     if
                                (!LI->isUnordered() ||<o:p></o:p></span></p>
                          </div>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">```<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">I did my tests
                              using a powerpc64le target, but I couldn't
                              find any target specific login involved in
                              this transform. In any case, I wanted to
                              drop the questions: <o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt"><o:p> </o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">- is there any
                              target that would benefit from speculative
                              fp divisions? <o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">- is there any
                              target for which fp division does not have
                              side effects? <o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt"><o:p> </o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">If not, I can go
                              ahead and post the patch above for review.<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt"><o:p> </o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">Many thanks!<o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:9.5pt">Sam<o:p></o:p></span></p>
                        </div>
                      </div>
                    </div>
                    <p class="MsoNormal" style="margin-bottom:12.0pt"><br>
                      _______________________________________________<br>
                      LLVM Developers mailing list<br>
                      <a moz-do-not-send="true"
                        href="mailto:llvm-dev@lists.llvm.org"
                        target="_blank">llvm-dev@lists.llvm.org</a><br>
                      <a moz-do-not-send="true"
                        href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev"
                        target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></p>
                  </blockquote>
                </div>
                <p class="MsoNormal"><o:p> </o:p></p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
  </body>
</html>