Discussion Thread: DIP 1040--Copying, Moving, and Forwarding--Community Review Round 1

tsbockman thomas.bockman at gmail.com
Fri Mar 5 20:29:01 UTC 2021


On Friday, 5 March 2021 at 18:35:39 UTC, Ben Jones wrote:
> Second, it might be good to include a discussion of how structs 
> are passed at the ABI level (I assume that's in the spec, but 
> it would be helpful context to just highlight in the DIP).  My 
> understanding is that this proposal doesn't actually change the 
> function ABIs at all (for struct parameters and return values 
> the caller always passes a pointer to the appropriate structs).

Very small structs are often placed in registers when passed by 
value. This is true both arguments, and return values. For 
example, when compiled with LDC for AMD64, the function below 
does not use any stack memory at all - only registers.

struct S {
     int* ptr;
     size_t length;
}

S sliceroo(S s) {
     const bump = int(s.length >= 2);
     return S(s.ptr + bump, s.length - bump);
}

(You can check out the disassembly online with the 
https://godbolt.org/ Compiler Explorer.)

This optimization would need to be disabled for any type with a 
move constructor.


More information about the Digitalmars-d mailing list