François Fayard
2015-May-04 09:31 UTC
[LLVMdev] Memory Allocation Optimized away with new by not with ::operator new
Hi, I’ve made my own version of std::vector which is called il::Vector. Due to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html>, 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). 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. Is it expected or a bug from LLVM? François Fayard Founder & Consultant - Inside Loop Tel: +33 (0)6 01 44 06 93 <tel:+33%206%2001%2044%2006%2093> Web: www.insideloop.io <http://www.insideloop.io/> Twitter: @insideloop_io ==== #include <iostream> #include <vector> std::vector<double> f_val(std::size_t i, std::size_t n) { auto v = std::vector<double>(n); for (std::size_t k = 0; k < v.size(); ++k) { v[k] = static_cast<double>(i); } return v; } int main (int argc, char const *argv[]) { const auto n = std::size_t{10}; const auto nb_loops = std::size_t{300000000}; auto v = std::vector<double>( n, 0.0 ); for (std::size_t i = 0; i < nb_loops; ++i) { auto w = f_val(i, n); for (std::size_t k = 0; k < v.size(); ++k) { v[k] += w[k]; } } std::cout << v[0] << " " << v[n - 1] << std::endl; return 0; } -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150504/59afcdf8/attachment.html>
David Blaikie
2015-May-06 05:21 UTC
[LLVMdev] Memory Allocation Optimized away with new by not with ::operator new
Didn't we just have this discussion on cfe-dev? (or is there something different I've missed?) On Mon, May 4, 2015 at 2:31 AM, François Fayard <fayard at insideloop.io> wrote:> Hi, > > I’ve made my own version of std::vector which is called il::Vector. Due to > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html, 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). > > 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. > > Is it expected or a bug from LLVM? > > François Fayard > Founder & Consultant - Inside Loop > Tel: +33 (0)6 01 44 06 93 <+33%206%2001%2044%2006%2093> > Web: www.insideloop.io > Twitter: @insideloop_io > > ====> > #include <iostream> > #include <vector> > > > std::vector<double> f_val(std::size_t i, std::size_t n) { > auto v = std::vector<double>(n); > for (std::size_t k = 0; k < v.size(); ++k) { > v[k] = static_cast<double>(i); > } > return v; > } > > int main (int argc, char const *argv[]) > { > const auto n = std::size_t{10}; > const auto nb_loops = std::size_t{300000000}; > > auto v = std::vector<double>( n, 0.0 ); > for (std::size_t i = 0; i < nb_loops; ++i) { > auto w = f_val(i, n); > for (std::size_t k = 0; k < v.size(); ++k) { > v[k] += w[k]; > } > } > std::cout << v[0] << " " << v[n - 1] << std::endl; > > return 0; > } > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150505/0da7935c/attachment.html>
François Fayard
2015-May-06 07:41 UTC
[LLVMdev] Memory Allocation Optimized away with new by not with ::operator new
I’ve missed it. I did a quick search and the only thread I found is back from 2013. Could you please provide me with a link to the thread? François Fayard Founder & Consultant - Inside Loop Tel: +33 (0)6 01 44 06 93 <tel:+33%206%2001%2044%2006%2093> Web: www.insideloop.io <http://www.insideloop.io/> Twitter: @insideloop_io> On 06 May 2015, at 07:21, David Blaikie <dblaikie at gmail.com> wrote: > > Didn't we just have this discussion on cfe-dev? (or is there something different I've missed?)-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150506/90532b35/attachment.html>