<div dir="ltr">Jonas, I assume you are talking about Sample-Based PGO. Yes, the problem you mentioned exists -- and your proposed solution seems reasonable. +dehao for comments.<div><br></div><div>David</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 2, 2016 at 1:40 AM, Jonas Wagner via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr" class="m_5182031958386667586gmail_msg">Hello,<div class="m_5182031958386667586gmail_msg"><br class="m_5182031958386667586gmail_msg"></div><div class="m_5182031958386667586gmail_msg">I'm working on an application that would benefit from knowing the weight of a basic block, as in "fraction of the program's execution time spent in this block".</div><div class="m_5182031958386667586gmail_msg"><br class="m_5182031958386667586gmail_msg"></div><div class="m_5182031958386667586gmail_msg">Currently, I'm computing this using the block's frequency from BlockFrequencyInfo, relative to the function's entry block frequency, and scaled by the function's entry count. This is also the computation that's done by <a href="https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/BlockFrequencyInfoImpl.cpp#L540" class="m_5182031958386667586gmail_msg" target="_blank">getBlockProfileCount</a> in lib/Analysis/<wbr>BlockFrequencyInfoImpl.cpp.</div><div class="m_5182031958386667586gmail_msg"><br class="m_5182031958386667586gmail_msg"></div><div class="m_5182031958386667586gmail_msg">The problem is that this method can be extremely imprecise, because many functions have an entry count of zero. The entry count is computed from the number of profile samples in the entry block. Depending on the function's CFG, this count can be arbitrarily low even though the function is frequently called or hot.</div><div class="m_5182031958386667586gmail_msg"><br class="m_5182031958386667586gmail_msg"></div><div class="m_5182031958386667586gmail_msg">Here's an idea to address this. I'd like to collect a bit of feedback from the community before trying it out.</div><div class="m_5182031958386667586gmail_msg"><br class="m_5182031958386667586gmail_msg"></div><div class="m_5182031958386667586gmail_msg">1) Instead of relying on a function's <i>entry count</i>, store the <i>total number of samples</i> in a function. This number is readily available from the profile loader.</div><div class="m_5182031958386667586gmail_msg">2) Compute a block's weight as `function_samples * block_weight / sum_of_block_weights_in_<wbr>function`</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">Why do I like this?</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">- Total samples in a function gives a good impression of the importance of a function, better than the entry count.</div><div class="m_5182031958386667586gmail_msg">- This scheme "preserves mass" in that all samples of a function are taken into account. The samples in a BB are compared to samples in the entire function, rather than a few (arbitrarily) selected samples from the entry block.</div><div class="m_5182031958386667586gmail_msg">- The computation avoids imprecision from multiplying by small numbers.</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">Disadvantages?</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">- BlockFrequencyInfo needs to keep track of the total frequency in a function.</div><div class="m_5182031958386667586gmail_msg">- BlockFrequencyInfo would probably scale the frequencies w.r.t. that total, rather then the maximum frequency. This loses a few bits of precision</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">Note that the entry count would not be lost in this scheme; one could easily compute it as `function_samples * entry_weight / sum_of_block_weights_in_<wbr>function`.</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">I believe using an entire function as unit of reference is a good compromise between precision and modularity. Precision is high because there's a sufficient number of samples available in a function. Modularity is preserved because the computation does not need to take other functions into account (in fact, BlockFrequencyInfo already processes one function at at time).</div><div class="m_5182031958386667586gmail_msg"><br></div><div class="m_5182031958386667586gmail_msg">What do people think about this?</div><span class="HOEnZb"><font color="#888888"><div class="m_5182031958386667586gmail_msg">- Jonas</div></font></span></div></div>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>