Thread overview
Inline optimization
Feb 26
Dennis
February 26
Given a function such as a computation that would benefit from running at full speed, is it possible to optimize only that function rather than the full program?

That is, compile with debug but optimize specific code:

pragma(optimize, "O3")
auto fastAdd(A,B)
February 26
On Tuesday, 26 February 2019 at 02:43:36 UTC, Michelle Long wrote:
> Given a function such as a computation that would benefit from running at full speed, is it possible to optimize only that function rather than the full program?
>
> That is, compile with debug but optimize specific code:
>
> pragma(optimize, "O3")
> auto fastAdd(A,B)

GCC has such a pragma, but as far as I know D compilers don't. The closest thing I found was the optimization strategy attribute in LDC:
https://wiki.dlang.org/LDC-specific_language_changes#.40.28ldc.attributes.optStrategy.28.22strategy.22.29.29

But it seems you can only disable, not enable optimizations with that.

What you can do in any case is put the performance critical code in a separate compilation unit (i.e. its own file) and compile that with optimizations and link it with the rest of the program compiled without optimizations. With a makefile that shouldn't be hard, though for expressing that in Dub I'm afraid you have to put it in its own sub package with an optimization flag in the 'dflags' attribute.
February 26
On Tuesday, 26 February 2019 at 02:43:36 UTC, Michelle Long wrote:
> Given a function such as a computation that would benefit from running at full speed, is it possible to optimize only that function rather than the full program?
>
> That is, compile with debug but optimize specific code:
>
> pragma(optimize, "O3")
> auto fastAdd(A,B)

Closest I know of for LDC is @fastmath

https://github.com/ldc-developers/druntime/blob/54cb25c7ba2f73ddf83873579d3d9860149f20cc/src/ldc/attributes.d#L158
February 27
On Tuesday, 26 February 2019 at 14:30:58 UTC, Dennis wrote:
> On Tuesday, 26 February 2019 at 02:43:36 UTC, Michelle Long wrote:
>> Given a function such as a computation that would benefit from running at full speed, is it possible to optimize only that function rather than the full program?
>>
>> That is, compile with debug but optimize specific code:
>>
>> pragma(optimize, "O3")
>> auto fastAdd(A,B)
>
> GCC has such a pragma, but as far as I know D compilers don't. The closest thing I found was the optimization strategy attribute in LDC:
> https://wiki.dlang.org/LDC-specific_language_changes#.40.28ldc.attributes.optStrategy.28.22strategy.22.29.29
>
> But it seems you can only disable, not enable optimizations with that.
>
> What you can do in any case is put the performance critical code in a separate compilation unit (i.e. its own file) and compile that with optimizations and link it with the rest of the program compiled without optimizations. With a makefile that shouldn't be hard, though for expressing that in Dub I'm afraid you have to put it in its own sub package with an optimization flag in the 'dflags' attribute.

That's a lot of trouble...

Surely it would be very simple for the compiler to do this... it has the flag inside and so when it is parsing a function it either runs an optimizer on it or not... given the command line options. But it could make this dynamic and modify it based on pragma.... seems like it would be much simpler to modify the compiler to do this?
February 26
On Tuesday, February 26, 2019 7:07:40 PM MST Michelle Long via Digitalmars-d wrote:
> On Tuesday, 26 February 2019 at 14:30:58 UTC, Dennis wrote:
> > On Tuesday, 26 February 2019 at 02:43:36 UTC, Michelle Long
> >
> > wrote:
> >> Given a function such as a computation that would benefit from running at full speed, is it possible to optimize only that function rather than the full program?
> >>
> >> That is, compile with debug but optimize specific code:
> >>
> >> pragma(optimize, "O3")
> >> auto fastAdd(A,B)
> >
> > GCC has such a pragma, but as far as I know D compilers don't.
> > The closest thing I found was the optimization strategy
> > attribute in LDC:
> > https://wiki.dlang.org/LDC-specific_language_changes#.40.28ldc.attribute
> > s.optStrategy.28.22strategy.22.29.29
> >
> > But it seems you can only disable, not enable optimizations with that.
> >
> > What you can do in any case is put the performance critical code in a separate compilation unit (i.e. its own file) and compile that with optimizations and link it with the rest of the program compiled without optimizations. With a makefile that shouldn't be hard, though for expressing that in Dub I'm afraid you have to put it in its own sub package with an optimization flag in the 'dflags' attribute.
>
> That's a lot of trouble...
>
> Surely it would be very simple for the compiler to do this... it has the flag inside and so when it is parsing a function it either runs an optimizer on it or not... given the command line options. But it could make this dynamic and modify it based on pragma.... seems like it would be much simpler to modify the compiler to do this?

As far as dmd goes, Walter seems to generally be against fine-grained control of optimizations like this. It took a lot of arguing from folks to even get pragma(inline, true), and it still doesn't do what most folks want, since folks wanted to be able to force inlining, and it gives an error when it can't inline so that you know that the function isn't being inlined, but it doesn't actually force anything. The ldc or gdc devs may be more open to fine-grained control of optimizations via pragmas and the like, but nothing like that is likely to ever be standard.

- Jonathna M Davis