www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Sub-optimal code generated for certain calls to functions with auto

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
As a follow-up to recent discussions at

https://github.com/dlang/dmd/pull/11000

I wonder if the compiler generates sub-optimal code for functions 
with `auto ref` parameters when passed l-values to parameters 
whose type fit in registers.

For instance,

T square_nt(T)(immutable scope T x)
{
     return x*x;
}

T square_t(T)(const auto ref scope T x)
{
     return x*x;
}

 safe pure nothrow  nogc unittest
{
     long x;
     auto y = square_nt(x); // passed via register
     auto z = square_t(x); // passed via indirection
}

According to my interpretation of

https://d.godbolt.org/z/5G4Eqn

it does.
Aug 10 2020
next sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 10 August 2020 at 11:24:36 UTC, Per Nordlöw wrote:
 T square_t(T)(const auto ref scope T x)
 {
     return x*x;
 }
Same difference in generated code for `square_nt` and `square_t` for T square_nt(T)(immutable scope T x) { return x*x; } T square_t(T)(immutable auto ref scope T x) { return x*x; } safe pure nothrow nogc unittest { immutable long x; auto y = square_nt(x); auto z = square_t(x); }
Aug 10 2020
prev sibling parent reply kinke <kinke gmx.net> writes:
On Monday, 10 August 2020 at 11:24:36 UTC, Per Nordlöw wrote:
 As a follow-up to recent discussions at

 https://github.com/dlang/dmd/pull/11000

 I wonder if the compiler generates sub-optimal code for 
 functions with `auto ref` parameters when passed l-values to 
 parameters whose type fit in registers.
It generates code according to the specification of `auto ref` (passing by ref for all lvalues and by value for all rvalues, the type doesn't matter at all), which isn't the ideal solution for passing input parameters in the most efficient way, hence that PR.
Aug 10 2020
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 10 August 2020 at 15:40:19 UTC, kinke wrote:
 It generates code according to the specification of `auto ref` 
 (passing by ref for all lvalues and by value for all rvalues, 
 the type doesn't matter at all), which isn't the ideal solution 
 for passing input parameters in the most efficient way, hence 
 that PR.
Thanks
Aug 10 2020