<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 26, 2015, at 11:19 AM, Chandler Carruth <<a href="mailto:chandlerc@google.com" class="">chandlerc@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 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;" class=""><div class="gmail_extra"><div class="gmail_quote">On Mon, Jan 26, 2015 at 10:59 AM, Pete Cooper<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:peter_cooper@apple.com" target="_blank" class="">peter_cooper@apple.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><br class=""><div class=""><div class=""><div class="h5"><blockquote type="cite" class=""><div class="">On Jan 26, 2015, at 10:30 AM, Chandler Carruth <<a href="mailto:chandlerc@google.com" target="_blank" class="">chandlerc@google.com</a>> wrote:</div><br class=""><div class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br class="">On Mon, Jan 26, 2015 at 10:29 AM, Pete Cooper<span class=""> </span><span dir="ltr" class=""><<a href="mailto:peter_cooper@apple.com" target="_blank" class="">peter_cooper@apple.com</a>></span><span class=""> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 26, 2015, at 10:24 AM, Chandler Carruth <<a href="mailto:chandlerc@google.com" target="_blank" class="">chandlerc@google.com</a>> wrote:</div><br class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sun, Jan 25, 2015 at 9:37 PM, Pete Cooper<span class=""> </span><span dir="ltr" class=""><<a href="mailto:peter_cooper@apple.com" target="_blank" class="">peter_cooper@apple.com</a>></span><span class=""> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="overflow: hidden;" class="">Hi hfinkel,<br class=""><br class="">This teaches the constant folder that an llvm.expect intrinsic with a constant as the first operand will return that constant.<br class=""><br class="">This reduces the size of an LTO'd llc by over 1% due to branches which can be removed once the constant is propagated.</div></blockquote></div><br class=""></div><div class="gmail_extra">I really don't understand this.</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">The design llvm.expect was specifically for LowerExpect to remove *all* of these intrinsics from the IR? Why isn't that working?</div></div></div></blockquote></div></div>Looks like LowerExpect is only added in populateFunctionPassManager, which is only called by clang, opt, and bug point.  There’s no call to this in llvm-lto.</div><div class=""><br class=""></div><div class="">I still think its worthwhile to constant fold expect as soon as possible as this patch does, but it looks like we also need that pass in the LTO pipeline.</div></blockquote></div><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; float: none; display: inline !important;" class="">I really don't think it is. We should be running LowerExpect really early in every single pass pipeline because, as you saw, there are tons of passes we would have to teach about it otherwise. Not constant folding is, if anything, a nice way to flush these issues out.</span></div></blockquote></div></div>I see what you mean.  Personally I find that a bit gross that we have an intrinsic for this at all then.</div></div></blockquote><div class=""><br class=""></div><div class="">I kind-of do too... But at the time it went in, it was "hard" to connect these dots.</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><br class=""></div><div class="">If anyone is curious, these are the passes 'opt -O2' runs up to LowerExpect.  So the SimplifyCFG here for example is possibly missing the opportunity to do some useful work because of the expects.</div></div></blockquote><div class=""><br class=""></div><div class="">Fortunately, we run simplify-cfg again at the start of the module pass manager.</div><div class=""><br class=""></div><div class="">The reason we do things in this order:</div><div class=""> <br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Pass Arguments:  -datalayout -notti -basictti -x86tti -no-aa -tbaa -scoped-noalias -assumption-cache-tracker -targetlibinfo -basicaa -verify -simplifycfg -domtree -sroa -early-cse -lower-expect</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Data Layout</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">No target information</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Target independent code generator's TTI</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">X86 Target Transform Info</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">No Alias Analysis (always returns 'may' alias)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Type-Based Alias Analysis</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Scoped NoAlias Alias Analysis</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Assumption Cache Tracker</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Target Library Information</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Basic Alias Analysis (stateless AA impl)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span class="Apple-converted-space"> </span>FunctionPass Manager</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">   <span class="Apple-converted-space"> </span>Module Verifier</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">   <span class="Apple-converted-space"> </span>Simplify the CFG</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">   <span class="Apple-converted-space"> </span>Dominator Tree Construction</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">   <span class="Apple-converted-space"> </span>SROA</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">   <span class="Apple-converted-space"> </span>Early CSE</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">   <span class="Apple-converted-space"> </span>Lower 'expect' Intrinsics</div></div></div></blockquote><div class=""><br class=""></div><div class="">Is because 1) SROA isn't heavily impacted by expect intrinsics, and 2) we want simplify-cfg to have "normalized" the CFG enough to more easily attach the expect intrinsic to the eventual branch or switch.</div><div class=""><br class=""></div><div class="">However, I suspect (without evidence sadly) that we could get all of the benifit from just running simplify-cfg + lower-expect, and then running the rest afterward.</div><div class=""><br class=""></div><div class="">I also suspect that with minimal effort (again, without evidence) we could just generate weights on branches rather than using the expect intrinsic.... :: shrug ::</div></div></div></div></div></blockquote>That would be nice.  I expect (ha!) that we’d do this in IRGen.  I might look in to this, although i’m not too familiar with clang.</div><div><br class=""></div><div>If you’re interested, i’ve worked out why I see this benefit at all in this patch.  I LTO my Debug LLVM.  This results in -O0 on the bitcode produced by LLVM, but then the linker LTO’s with optimizations enabled.  And because LTO doesn’t have LowerExpect in its pipeline, I see many cases of 'call i64 @llvm.expect.i64(i64 1, i64 1)’ in my IR.</div><div><br class=""></div><div>So, getting the front-end to generate metadata would certainly fix this.  As would calling LowerExpect in libLTO or in populateFunctionPassManager even when on -O0.  Or we could just say that a Debug LTO build should live with this issue, although I’d prefer one of the other options to this one.</div><div><br class=""></div><div>Pete</div><br class=""></body></html>