GDC dont optimize calls to template functions
Iain Buclaw
ibuclaw at gdcproject.org
Mon Oct 6 16:06:17 UTC 2025
On Sunday, 5 October 2025 at 03:26:21 UTC, qxi wrote:
> as expected. Now changing 'pass' to template function
>
> ```d
> int pass()(int a) { return a; } //template function
>
> int add(int num) { return pass(num) + num;}
> ```
>
> Output:
-- snip --
> way worst output.
Tracked in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102765
DMD emits templates as weak symbols, that it chooses to inline
such functions violates ODR.
There was a proposal to fix druntime that would allow reversing
the behaviour change: https://github.com/dlang/druntime/pull/3716.
As it stands, one potential way forward would be to restrict weak
linkage to just extern(C) declarations in templates. All other
symbols then get thrown into the linkonce section.
> Now we get expected output.
> But lack of inlining is not the only problem because
>
> ```d
> pragma(inline,false)
> int pass(int a) { return a; } //normal function (no inline)
>
> int add(int num) { return pass(num) + num;}
> ```
>
> Output:
--snip--
>
> which is still better output than templated one.
These two code blocks are not equivalent. G++ and GDC produce
the same code when the regular function is annotated correctly.
https://compiler-explorer.com/z/YqoG918GK
More information about the D.gnu
mailing list