Thread overview
Detailed inline behavior of dmd?
Aug 06, 2013
SteveGuo
Aug 06, 2013
Peter Alexander
Aug 06, 2013
SteveGuo
Aug 06, 2013
SteveGuo
Aug 10, 2013
Iain Buclaw
Aug 06, 2013
David
Aug 06, 2013
SteveGuo
Aug 06, 2013
bearophile
Aug 06, 2013
qznc
August 06, 2013
D supports inline functions, But I want to know the details.

1. Is inline the default behavior? Or I have to add a compiler switch -inline?
2. The depth of inline.
3. If I add the switch -inline when compiling, it will inline everything no matter how complex the function is?
August 06, 2013
1. You have to add -inline

2. Don't know. I'm guessing it depends on the function complexity, among other things.

3. No, it cannot possibly inline everything due to recursive functions, and indirect function calls. It would also be a terrible idea due to code bloat.
August 06, 2013
On Tuesday, 6 August 2013 at 11:07:54 UTC, Peter Alexander wrote:
> 1. You have to add -inline
>
> 2. Don't know. I'm guessing it depends on the function complexity, among other things.
>
> 3. No, it cannot possibly inline everything due to recursive functions, and indirect function calls. It would also be a terrible idea due to code bloat.

Thanks for replying:)
Ok, then is there a -forceinline option like other compiler?

August 06, 2013
> Thanks for replying:)
> Ok, then is there a -forceinline option like other compiler?

Or maybe I should ask are there levels for the switch -inline?
August 06, 2013
Am 06.08.2013 13:01, schrieb SteveGuo:
> D supports inline functions, But I want to know the details.
> 
> 1. Is inline the default behavior? Or I have to add a compiler switch
> -inline?
> 2. The depth of inline.
> 3. If I add the switch -inline when compiling, it will inline everything
> no matter how complex the function is?

My experience with -inline.
Without: runs fine
With: segfaults

Handle DMD with care, if you want performance, use LDC or GDC, both do a better job without introducing segfaults
August 06, 2013
> My experience with -inline.
> Without: runs fine
> With: segfaults
>
> Handle DMD with care, if you want performance, use LDC or GDC, both do a
> better job without introducing segfaults


Thanks for replying:) I will try LDC or GDC.
August 06, 2013
On Tuesday, 6 August 2013 at 11:01:35 UTC, SteveGuo wrote:
> D supports inline functions, But I want to know the details.
>
> 2. The depth of inline.
> 3. If I add the switch -inline when compiling, it will inline everything no matter how complex the function is?

As far as I know, no compiler specifies anything for this. Inlining is a difficult optimization, because it is hard to quantify if a specific call is worth inlining. Usually, there are handwavy heuristics and various magic numbers. Nobody should depend on exact behavior here. For specific cases, you can always inspect the assembly.

Inlining everything is not possible in general. Consider recursive functions aka circles in the call graph. Dynamic binding and function pointers lead to calls of unknown functions.
August 06, 2013
David:

> My experience with -inline.
> Without: runs fine
> With: segfaults

Recently the dmd inlining situation has improved, now the small lambdas often used with std.algorithms are inlined correctly, they have even removed one workaround in the all() function. I think the dmd patch was from Kenji.

Bye,
bearophile
August 10, 2013
On 6 August 2013 12:18, SteveGuo <steveguo@outlook.com> wrote:
> On Tuesday, 6 August 2013 at 11:07:54 UTC, Peter Alexander wrote:
>>
>> 1. You have to add -inline
>>
>> 2. Don't know. I'm guessing it depends on the function complexity, among other things.
>>
>> 3. No, it cannot possibly inline everything due to recursive functions, and indirect function calls. It would also be a terrible idea due to code bloat.
>
>
> Thanks for replying:)
> Ok, then is there a -forceinline option like other compiler?
>

I wonder what other compiler you are referring to that has a -forceinline switch... seems odd to have one.


-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';