<div dir="ltr"><div>Beyond implementing my own TargetTransformInfo is there a way for me to alter the cost here? Just to see whether that'd be enough to prevent 
FoldTwoEntryPHINode from doing the wrong thing.</div><div><br></div><div>Also I think Hiroshi is probably right - even if the cost of the very unlikely branch is low, I've specifically told the optimizer that the code will nearly never be executed, so it seems wrong that it'd hoist even a genuinely low cost call.</div><div><br></div><div>So perhaps 
FoldTwoEntryPHINode needs fixed and the cost model needs altered. Gulp!</div><div><br></div><div>Cheers,</div><div>-Neil.<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 22, 2020 at 5:30 PM Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com">lebedev.ri@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I would think that it is, again, a cost model issue.</div><div><a href="https://godbolt.org/z/cTKnfK" target="_blank">https://godbolt.org/z/cTKnfK</a> (latency)<br></div><div><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(0,128,128)">Cost Model: Found an estimated cost of 3 for instruction:</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, "">   </span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(205,49,49)">%3</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, ""> </span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(205,49,49)">=</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, ""> </span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(0,0,255)">call</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, ""> </span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(0,128,128)">double</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, ""> </span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(0,128,128)">@llvm.cos.f64</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, "">(</span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(0,128,128)">double</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, ""> </span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(205,49,49)">%</span><span style="background-color:rgb(255,255,254);font-family:"Consolas, ";color:rgb(9,134,88)">2</span><span style="background-color:rgb(255,255,254);color:rgb(0,0,0);font-family:"Consolas, "">)</span><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:"Consolas, ""><div><span style="color:rgb(0,128,128)">Cost Model: Found an estimated cost of 3 for instruction:</span>   <span style="color:rgb(205,49,49)">%4</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">call</span> <span style="color:rgb(0,128,128)">double</span> <span style="color:rgb(0,128,128)">@llvm.pow.f64</span>(<span style="color:rgb(0,128,128)">double</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,134,88)">2</span>, <span style="color:rgb(0,128,128)">double</span> <span style="color:rgb(9,134,88)">4.242000e+01</span>)<br></div><div><br></div><div>Is that actually correct? I'd expect it to be somewhat larger..</div><div><br></div><div>Roman.</div><div><br></div><div><span style="color:rgb(0,128,128)"></span></div></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 22, 2020 at 7:17 PM Hiroshi Yamauchi via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 22, 2020 at 2:52 AM Neil Henning via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hey all - me again,</div><div><br></div><div>So I'm looking at llvm.expect specifically for branch hints. In the following example LLVM will hoist the pow/cos calls into the entry block even though I've used the llvm.expect intrinsic to make it clear that one of the calls is unlikely to occur.</div><div><br></div><div>target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"<br>target triple = "x86_64-pc-windows-msvc-coff"<br><br>define dllexport double @foo(i32 %val) {<br>entry:<br>  %0 = icmp slt i32 %val, 42<br>  %1 = call i1 @llvm.expect.i1(i1 %0, i1 false)<br>  %2 = sitofp i32 %val to double<br>  br i1 %1, label %true, label %false<br><br>true:<br>  %3 = call double @llvm.cos.f64(double %2)<br>  br label %merge<br><br>false:<br>  %4 = call double @llvm.pow.f64(double %2, double 4.242000e+01)<br>  br label %merge<br><br>merge:<br>  %var.1.0 = phi double [ %4, %false ], [ %3, %true ]<br>  ret double %var.1.0<br>}<br><br>declare i1 @llvm.expect.i1(i1, i1)<br>declare double @llvm.cos.f64(double)<br>declare double @llvm.pow.f64(double, double)</div><div><br></div><div>This seems counter intuitive to me - I've told LLVM that one of the calls will probably not happen, and I expected it to preserve the call in the unlikely branch so we don't pay the cost for something unlikely to be used.</div><div><br></div><div>I also injected a pass locally that adds, for branches whose condition is llvm.expect, the branch weight metadata - but LLVM will still always fold the branch away ensuring that the expensive call is always called.<br></div><div><br></div><div>The part of SimplifyCFG that does this is FoldTwoEntryPHINode from what I can tell.</div><div><br></div><div>So is there anything I can do here without modifying LLVM? Have I missed something?</div><div><br></div><div>Otherwise I guess I'd have to change 
FoldTwoEntryPHINode to not do this in the presence of branch weights / expect?</div></div></blockquote><div><br></div><div style="font-family:arial,helvetica,sans-serif">Passing -two-entry-phi-node-folding-threshold=1 seems to prevent this folding, but that may not be what you need.</div><div style="font-family:arial,helvetica,sans-serif"><br></div><div style="font-family:arial,helvetica,sans-serif">As it doesn't look like FoldTwoEntryPHINode checks for branch hints, it may make sense to change FoldTwoEntryPHINode.</div><div style="font-family:arial,helvetica,sans-serif"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>Thanks for any help,</div><div><br></div><div>Cheers,</div><div>-Neil.<br></div><div><br>-- <br><div dir="ltr"><div dir="ltr"><table style="border-collapse:collapse;border-spacing:0px;color:rgb(90,90,91);font-size:13px;margin:0px 0px 20px;padding:0px" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px 0px 20px;vertical-align:top" align="left"><table style="border-collapse:collapse;border-spacing:0px;margin:0px;padding:0px" cellspacing="0" cellpadding="0" border="0" align="left"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:1.12em;line-height:1.5em;padding:0px;vertical-align:top;width:64px"><img style="border: medium none; border-radius: 0px; display: block; font-size: 13px; height: auto; line-height: 100%; margin: 0px; max-width: 100%; outline-style: none; outline-width: medium; padding: 20px 0px 0px; width: 100%;" alt="" src="https://unity3d.com/profiles/unity3d/themes/unity/images/ui/other/unity-logo-dark-email.png" width="64" height="auto"></td></tr></tbody></table></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;font-weight:600;line-height:15px;margin:0px;padding:0px">Neil Henning</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;line-height:15px;margin:0px;padding:0px 0px 10px">Senior Software Engineer Compiler</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:12px;line-height:15px;margin:0px;padding:0px"><a href="http://unity.com" target="_blank">unity.com</a></div></td></tr></tbody></table></div></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><table style="border-collapse:collapse;border-spacing:0px;color:rgb(90,90,91);font-size:13px;margin:0px 0px 20px;padding:0px" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px 0px 20px;vertical-align:top" align="left"><table style="border-collapse:collapse;border-spacing:0px;margin:0px;padding:0px" cellspacing="0" cellpadding="0" border="0" align="left"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:1.12em;line-height:1.5em;padding:0px;vertical-align:top;width:64px"><img style="border: medium none; border-radius: 0px; display: block; font-size: 13px; height: auto; line-height: 100%; margin: 0px; max-width: 100%; outline-style: none; outline-width: medium; padding: 20px 0px 0px; width: 100%;" alt="" src="https://unity3d.com/profiles/unity3d/themes/unity/images/ui/other/unity-logo-dark-email.png" width="64" height="auto"></td></tr></tbody></table></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;font-weight:600;line-height:15px;margin:0px;padding:0px">Neil Henning</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;line-height:15px;margin:0px;padding:0px 0px 10px">Senior Software Engineer Compiler</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:12px;line-height:15px;margin:0px;padding:0px"><a href="http://unity.com" target="_blank">unity.com</a></div></td></tr></tbody></table></div></div></div>