On inlining in D libraries

Johannes Pfau nospam at example.com
Mon Sep 9 10:42:03 PDT 2013


On Monday, 9 September 2013 at 14:58:56 UTC, Dmitry Olshansky 
wrote:
> 09-Sep-2013 18:39, Joseph Rushton Wakeling пишет:
>> On 09/09/13 16:34, Dmitry Olshansky wrote:
>>> On the bright side of things std.regex is real fast on LDC 
>>> *when
>>> hacked* to
>>> inline the critical bits :)
>>
>> Do you mean when manually inlined, or when the design is 
>> tweaked to
>> facilitate inlining?
>
> When I put extra () to indicate that said functions are 
> templates.
> Then compiler gets its grip on them and finally inlines.
> Otherwise it generates calls and links in object code from 
> libphobos.
>
> Which is the whole reason for the topic - is THAT is the way to 
> go?
> Shouldn't compiler look into source for inlinable stuff (when 
> source is available)?
>

I only know about GDC and GDC doesn't implement cross-module 
inlining right now. If the modules are compiled in a single run 
it might work but if the modules are compiled separately then 
only LTO (not tested with GDC though!) can help.

AFAIK the problem is this: There's no high-level way to tell the 
backend "hey, I have the source code for this function. if you 
consider inlining call me back and I'll compile it for you". The 
only hack which could work is _always_ compiling _all_ functions 
from all modules. But compile times will explode.

Another issue is that whether a function will be inlined depends 
on details like the number of compiled instructions. Those 
details are only available once the function is compiled, the 
source code is not enough.

Maybe a reasonable compromise could be made with some help from 
the frontend. The frontent could give us some hints ("Likely 
inlineable"). Then we could compile all "likely inlineable" 
functions and let the backend decide if it really wants to inline 
those.

(Another options is inlining in the frontend. DMD does that right 
now but IIRC it causes problems with the GCC backend and is 
disabled in GDC). Iain can probably give a better answer here.

(Note: there's a low-level way to do this: LTO actually adds 
intermediate code to the object files. If the linker wants to 
inline a function, it calls the compiler to compile that 
intermediate code: 
http://gcc.gnu.org/onlinedocs/gccint/LTO-Overview.html . In the 
end working LTO is probably the best solution.)


More information about the Digitalmars-d mailing list