www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - Best way to add templated "intrinsic" functions?

reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
I want to add templated functions that always should be inlined, 
basically templated functions that have special syntax added for 
it in the parser.

What is the best way to go about this? Is there a better way than 
adding the functions to object.d in the d-runtime and mark them 
as "pragma(inline,true"?

Maybe not a LDC specific question.
Jan 03 2021
next sibling parent reply Guillaume Piolat <first.last gmail.com> writes:
On Sunday, 3 January 2021 at 16:54:57 UTC, Ola Fosheim Grøstad 
wrote:
 I want to add templated functions that always should be 
 inlined, basically templated functions that have special syntax 
 added for it in the parser.

 What is the best way to go about this? Is there a better way 
 than adding the functions to object.d in the d-runtime and mark 
 them as "pragma(inline,true"?

 Maybe not a LDC specific question.
pragma(inline,true) probably. If it's worth inlining and you are using LDC, you can have high confidence it will be inlined. There is a bit too much of "forced inlined" in this world, often without any measurements.
Jan 04 2021
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Monday, 4 January 2021 at 21:52:03 UTC, Guillaume Piolat wrote:
 If it's worth inlining and you are using LDC, you can have high 
 confidence it will be inlined. There is a bit too much of 
 "forced inlined" in this world, often without any measurements.
I am inlining "a < b < c" as "(a<b) && (b<c)" so it is totally worth it. :-) I got it to work, but haven't checked the asm yet...
Jan 04 2021
prev sibling parent reply kinke <noone nowhere.com> writes:
On Sunday, 3 January 2021 at 16:54:57 UTC, Ola Fosheim Grøstad 
wrote:
 Is there a better way than adding the functions to object.d in 
 the d-runtime and mark them as "pragma(inline,true"?
That's how I would do it, and I don't think it's too bad. Adding to object.d is in line with other to-template-lowerings; one-liners etc. are probably fine to add directly, more complex functions can be imported and aliased in object.d. [I think object.d needs to be split up to a little library or package at some point...] And `pragma(inline, true)` is fine for your needs.
Jan 05 2021
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Tuesday, 5 January 2021 at 17:00:32 UTC, kinke wrote:
 On Sunday, 3 January 2021 at 16:54:57 UTC, Ola Fosheim Grøstad 
 wrote:
 Is there a better way than adding the functions to object.d in 
 the d-runtime and mark them as "pragma(inline,true"?
That's how I would do it, and I don't think it's too bad. Adding to object.d is in line with other to-template-lowerings; one-liners etc. are probably fine to add directly, more complex functions can be imported and aliased in object.d. [I think object.d needs to be split up to a little library or package at some point...]
Ok, so if they are marked as always-inline then they won't be generated for linking even though they are present in object.d? Or do I have to mark them in a special way to avoid library-code-gen?
Jan 05 2021
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Tuesday, 5 January 2021 at 19:51:54 UTC, Ola Fosheim Grøstad 
wrote:
 Ok, so if they are marked as always-inline then they won't be 
 generated for linking even though they are present in object.d? 
 Or do I have to mark them in a special way to avoid 
 library-code-gen?
Never mind, this was a stupid question. I did not mean library-code-gen as that is not possible for templated code, but code gen for the object file. I guess not, and even if, I guess ldc would prune it away during linkage. So never mind... :-)
Jan 05 2021