debug=PRINTF;
debug(PRINTF) import core.stdc.stdio;
int foo()()
{
return 0;
}
int bar()()
{
return 0;
}
void main()
{
auto f1 = &foo!();
auto f2 = &bar!();
debug(PRINTF) printf("%p %p\n", f1, f2);
assert(f1 !is f2);
}
----
Because identical COMDAT folding (/OPT:ICF) is enabled by default this will assert. When compiled with '-g' we pass '/DEBUG' to the linker which also disables ICF so the assertion passes.
This could lead to difficult to track down bug when function pointer comparison is used, e.g. as key to an AA.
http://msdn.microsoft.com/en-us//library/bxwfs976(v=vs.110).aspx#alert_note
Comment #1 by bugzilla — 2013-03-01T14:12:22Z
I have mixed feelings about whether this is a bug or not. First off, nothing in the dmd spec requires that identical function bodies must have distinct addresses. Second, this is an important optimization to reduce template bloat.
Comment #2 by code — 2013-03-04T04:53:03Z
(In reply to comment #1)
> I have mixed feelings about whether this is a bug or not.
Me too and as this optimization becomes probably even more important to fold TypeInfos and precise GC metadata I will close this for now.
Related C++ article "Can Two Functions Have the Same Address?"
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=561
Comment #3 by bugzilla — 2013-03-05T21:52:05Z
(In reply to comment #2)
> Related C++ article "Can Two Functions Have the Same Address?"
> http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=561
This quote pretty much sells me:
--
Additionally, Google's compiler team have experimented with Identical Code Folding (ICF) and reported that "[d]etailed experiments on the x86 platform show that ICF can reduce the text size [the program section in which functions' code is stored, DK] of some Google binaries, whose average text size is 50 MB, by up to 7%."
--
We should settle the issue by updating the D spec to explicitly allow functions to have the same address.
Comment #4 by code — 2014-11-06T14:09:13Z
Also see bug 9655.
Comment #5 by r.sagitario — 2014-11-07T13:34:12Z
See also issue 10664.
It is the reason why we currently have -L/OPT:NOICF in sc.ini.