<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 1, 2015 at 8:34 AM, François Fayard <span dir="ltr"><<a href="mailto:fayard.francois@icloud.com" target="_blank">fayard.francois@icloud.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi,<div><br></div><div>Even though this question does not only concern LLVM, it seems that only compilers guru can answer it. So I am giving a try here, hoping for the best.</div><div><br></div><div>In a recent talk by Chandler Carruth, “Performance with algorithms, efficiency with data structures” ( <a href="https://www.youtube.com/watch?v=fHNmRkzxHWs" target="_blank">https://www.youtube.com/watch?v=fHNmRkzxHWs</a> ), Chandler says that one should never return by reference for efficiency. Although I totally agree with him in most cases because pointers make it difficult for a compiler to optimise, I still don’t always have an efficient solution with value semantics. Here is the case that I am thinking of :</div><div><br></div><div>====</div><div>std::vector<double> f(std::size_t i);</div><div><br></div><div>auto v = std::vector<double>( n );</div><div>for (std::size_t i = 0; i < 1000; ++i) {</div><div> auto w = f(i);</div><div> for (std::size_t k = 0; k < v.size(); ++k) {</div><div> v[k] += w[k];</div><div> }</div><div>}</div><div>
====</div><div><br></div><div>which would be way slower than</div><div><br></div><div>====</div><div><div>void f(std::size_t i, std::vector<double>& w);</div><div><br></div><div>auto v = std::vector<double>( n );</div><div>auto w = std::vector<double>( n );</div><div>for (std::size_t i = 0; i < 1000; ++i) {</div><div> f(i, w);</div><div> for (std::size_t k = 0; k < v.size(); ++k) {</div><div> v[k] += w[k];</div><div> }</div><div>}</div></div><div>====</div><div><br></div><div>because there is no memory allocation in the i-loop, inside the f-call. In the Q&A where a guy seems to give him such an example (at 1:06:46), he says that smart compilers such as LLVM can deduplicate memory allocation. It does not seem to me to be applicable to this kind of algorithm. Does anyone have a concrete example where a compiler deduplicates memory allocation?</div></div></blockquote><div><br>Probably the easiest way to give the compiler an opportunity here would be to have 'f's definition available for inlining - a compiler might be able to inline that, then, depending on the particular allocation behavior of 'f', the allocation /may/ be reused (I don't think we do anything like reusing yet - though we can, to some degree, inline a bunch of std::vector's ops and then just remove the whole std::vector thing in favor of a stack allocation, for example) </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>Thanks,</div><div>François</div></div><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div></div>