rvalues -> ref (yup... again!)

Rubn where at is.this
Tue Mar 27 23:59:09 UTC 2018


On Tuesday, 27 March 2018 at 23:35:44 UTC, kinke wrote:
> On Tuesday, 27 March 2018 at 21:52:25 UTC, Rubn wrote:
>> It happens with LDC too, not sure how it would be able to know 
>> to do any kind of optimization like that unless it was able to 
>> inline every single function called into one function and be 
>> able to do optimize it from there. I don't imagine that'll be 
>> likely though.
>
> It does it in your code sample with `-O`, there's no call to 
> bar and the foo() by-value arg is memcpy'd to the global.
>
> If you compile everything with LTO, your code and all 3rd-party 
> libs as well as druntime/Phobos, LLVM is able to optimize the 
> whole program as if it were inside a single gigantic 'object' 
> file in LLVM bitcode IR, and is thus indeed theoretically able 
> to inline *all* functions.

A bit off topic now but anyways:

Well that example I posted didn't do anything, so it would 
optimize it out quite easily. The entire function was excluded 
essentially. Just adding a few writeln it isn't able to remove 
the function entirely anymore and can't optimize it out. Idk if 
you want to try some different options but flto didn't do 
anything for it.

https://godbolt.org/g/bLdpnm

import std.stdio : writeln;

struct Foo {
     ubyte[1024] data;

     this(int a)
     {
         data[0] = cast(ubyte)a;
     }
}

void foo(T)(auto ref T t) {
     import std.functional: forward;
     writeln(gfoo.data[0]);
     bar(forward!t);
     writeln(gfoo.data[0]);
}

__gshared Foo gfoo;

void bar(T)(auto ref T t) {
     import std.algorithm.mutation : move;
     writeln(gfoo.data[0]);
     move(t, gfoo);
}

void main() {
     foo(Foo(10));
}


More information about the Digitalmars-d mailing list