<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="">Hi,<div class=""><br class=""></div><div class="">I’ve made my own version of std::vector which is called il::Vector. Due to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html" class="">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html</a>, LLVM can optimise away memory allocation. Therefore, the following code optimise away all memory allocation for w resulting in a single allocation during the whole program (for v).</div><div class=""><br class=""></div><div class="">When using my own vector implementation, I realised that the allocation were not optimized away because I was using ::operator new. When I’ve switched back to new, the optimisation came back.</div><div class=""><br class=""></div><div class="">Is it expected or a bug from LLVM?</div><div class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><br class=""></div><div class="">François</div><div class=""><br class=""></div></div></div></div></div></div></div></div></div><div class="">=====</div><div class=""><br class=""></div><div class=""><div class="">#include <iostream></div><div class="">#include <vector></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">std::vector<double> f_val(std::size_t i, std::size_t n) {</div><div class=""> auto v = std::vector<double>(n);</div><div class=""> for (std::size_t k = 0; k < v.size(); ++k) {</div><div class=""> v[k] = static_cast<double>(i);</div><div class=""> }</div><div class=""> return v;</div><div class="">}</div><div class=""><br class=""></div><div class="">int main (int argc, char const *argv[])</div><div class="">{</div><div class=""> const auto n = std::size_t{10};</div><div class=""> const auto nb_loops = std::size_t{300000000};</div><div class=""><br class=""></div><div class=""> auto v = std::vector<double>( n, 0.0 );</div><div class=""> for (std::size_t i = 0; i < nb_loops; ++i) {</div><div class=""> auto w = f_val(i, n);</div><div class=""> for (std::size_t k = 0; k < v.size(); ++k) {</div><div class=""> v[k] += w[k];</div><div class=""> }</div><div class=""> }</div><div class=""> std::cout << v[0] << " " << v[n - 1] << std::endl;</div><div class=""><br class=""></div><div class=""> return 0;</div><div class="">}</div></div>
<br class=""></body></html>