How does inlining work for ref parameters?

Bill Baxter dnewsgroup at billbaxter.com
Sat May 17 14:12:09 PDT 2008


Janice Caron wrote:
> I get how inlining works for non-ref parameters. If I do
> 
>     int foo(int x, int y)
>     {
>         return x + y;
>     }
> 
>     int a = ...;
>     int b = ...;
>     int c = foo(a,b);
> 
> Then this can turn into
> 
>     int a = ...;
>     int b = ...;
>         int x = a;
>         int y = b;
>         int t = a + b;
>     int c = t;
> 
> which can subsequently be optimised to
> 
>     int a = ...;
>     int b = ...;
>     int c = a + b;
> 
> But I don't get how it all hangs together with reference arguments.
> Here's an example:
> 
>     int bar(ref int* p, ref int* q)
>     {
>         return *p++ + *q++;
>     }
> 
>     int* ap = ...;
>     int* bp = ...;
>     int c = bar(ap,bp);
> 
> My complete lack of understanding of how inlining works suggests to me
> that this would translate into
> 
>     int* ap = ...;
>     int* bp = ...;
>         int** p = ≈
>         int** q = &bp;
>         int t = *(*p)++ + *(*q)++;
>     int c = t;
> 
> which doesn't really optimise, except for the elimination of t. 

IANACG, but a reference parameter is pretty much like passing an alias 
to the original value, so I'm not sure the optimizer would need to make 
those intermediate p,q arguments.  It can just go straight to the 
version below where ap and bp are substituted in directly.

But either way, inlining does eliminate a function call.  I believe 
that's mostly the point of inlining.  That's orthogonal to the other 
sorts of optimization tricks you can do after the code is inlined.

> And yet, we would /hope/ to end up with is:
> 
>     int* ap = ...;
>     int* bp = ...;
>     int c = *ap++ + *bp++;

 > [...]

> So - can someone tell me - if a function takes lots of parameters
> declared "ref", and that function is inlined, is the "ref" part
> (passing the address and then dereferencing when needed) completely
> eliminated, or not? Does anyone have a definitive answer?

Sadly, I believe what I have heard mentioned here on the NG is that DMD 
does *not* currently inline *any* functions that have ref args.  Which 
is a serious problem, since performance is one reason you would switch 
to ref args in the first place.  (To avoid passing large structs by value.)

This is heresay, though.  Can anyone confirm?  It really would be nice 
to get a "performance tips" page up somewhere describing what sorts of 
things DMD can and cannot inline.

--bb



More information about the Digitalmars-d mailing list