[LLVMdev] Deduplication of memory allocation

Daniel Berlin dberlin at dberlin.org
Fri May 1 09:27:43 PDT 2015


On Fri, May 1, 2015 at 9:24 AM, François Fayard
<fayard.francois at icloud.com> wrote:
>
>> On 01 May 2015, at 18:11, Daniel Berlin <dberlin at dberlin.org> wrote:
>>
>> There will be no allocation in the first example. it will transform
>> the first into the equivalent of the second.

>
> The following code is must faster with reference semantics than with value semantics. The “value” code is not transformed into the “reference” code by >llvm.

This is a question of "what does your current version of clang and llvm do.
Not a question of what can be done.
It *can*, it may not do it right now, but it in fact, can.


> Just compile this code, and the difference in timings shows.


>
> #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>(k + i);
>     }
>     return v;
> }
>
> void f_ref(std::size_t i, std::vector<double>& v) {
>     for (std::size_t k = 0; k < v.size(); ++k) {
>         v[k] = static_cast<double>(k + i);
>     }
> }
>
> int main (int argc, char const *argv[])
> {
>     const auto n = std::size_t{10};
>
>     {
>         auto v = std::vector<double>( n, 0.0 );
>         for (std::size_t i = 0; i < 100000000; ++i) {
>             auto w = f_val(i, n);
>             for (std::size_t k = 0; k < v.size(); ++k) {
>                 v[k] += w[k];
>             }
>         }
>         std::cout << v[n - 1] << std::endl;
>     }
>
>     {
>         auto v = std::vector<double>( n, 0.0 );
>         auto w = std::vector<double>( n );
>         for (std::size_t i = 0; i < 100000000; ++i) {
>             f_ref(i, w);
>             for (std::size_t k = 0; k < v.size(); ++k) {
>                 v[k] += w[k];
>             }
>         }
>         std::cout << v[n - 1] << std::endl;
>     }
>
>     return 0;
> }
>




More information about the llvm-dev mailing list