[Issue 8991] adding a __ctfe branch with return to a function breaks NRVO

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 14 06:43:06 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8991



--- Comment #4 from Dmitry Olshansky <dmitry.olsh at gmail.com> 2012-11-14 06:42:57 PST ---
(In reply to comment #3)
>I have no idea why this evil hacky code exists in Phobos, I cannot see how it
>can possibly be correct. If it bypasses postblit, surely that's wrong.
>If it is faster to use memcpy(), that's a compiler bug.

The whole point of move is to avoid extra postblit and turn l-value into an
r-value.
The way to do it seems to be (and very simillar in swap) is to blit T.init into
source and whatever it contained before return as result. 
The source will eventually get destroyed with T.init. 

Thus the postblit is not required and no double destroy of the same object
happens.

While I thought this could work to paint things as r-value:
T move(ref T x ){ return x; }

it will still do a postblit as ref-ed param stays intact elsewhere.

> The workaround is easy:
> 
> +    T result = void;
>   if (__ctfe)
>     {
>     *cast(int*)0 = 0; //to demonstrate that no CTFE is attempted
> -        T result = source;    
> +        result = source;    
>         return result;   //must have broke NRVO
>     }   
> 

That was what I did in the first place. Problem is  - it doesn't work for
structs with immutable fields as after:
    T result = void; 
this line:
    result = source;
does't compile. 
I wouldn't have noticed this if Phobos unittests haven't failed
while memcpy hacked through any such inconveniences.

In any case I've found a workaround that seems to pass through:
https://github.com/D-Programming-Language/phobos/pull/936

> The problem is, that NRVO is run during the semantic pass, rather than
> afterwards.
> Personally I think that inlining should happen after the semantic3 pass (it
> would make CTFE *much* simpler), and I think NRVO could happen then as well.
> Otherwise I'm not sure that this is worth doing anything about.

Okay let's either close it or turn into an enhancement request for doing
inlining and NRVO after completion of the semantic pass.

> It is still
> true that if (__ctfe ) never affects backend code generation, it's just odd
> that DMD is doing NRVO in the front-end.

Okay that makes it clearer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list