Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC(/Clang): Merging functions with identical instructions (COMDAT folding)

Just curious, do the GCC or Clang toolsets implement the equivalent of MSVC's identical COMDAT folding (ICF) currently? If not, are there any plans to? I can't seem to find any recent authoritative links on the subject other than old GCC mailing list messages.

If not, does this imply that template instantiations over distinct types are always distinct functions in the resulting binary (in situations where they are not completely inlined), even when they are binary-compatible, or are there other mechanisms in-place to handle this at some other level?

Also, has anyone found ICF making a big difference in minimizing the size of a resulting executable in practice? I don't have any large MSVC projects handy to test it out. (I'm guessing it only really helps if you happened to instantiate templates over many different vtable-layout compatible types.)

Finally, is it C++11 standards-compliant for two functions pointers to different functions to compare equal at runtime? This link seems to imply that it isn't, but it's for C99. EDIT: found previous question on this topic

like image 693
Stephen Lin Avatar asked Mar 01 '13 23:03

Stephen Lin


1 Answers

Neither GCC nor Clang is a linker, and ICF needs to be done by the linker, or at least with cooperation with the linker. Edit: They don't do ICF, so yes, distinct instantiations produce distinct code. The GNU gold linker supports ICF with the --icf option, which needs the GCC option -ffunction-sections to be used.

Distinct functions must have distinct addresses ... I can't remember if ICF is disabled for any function that has its address taken, but if not it should be possible to put a load of no-op instructions before the combined function and make each distinct instantiation start on a different instruction, so they have different addresses. Edit: gold's --icf=safe option only enables ICF for functions that can be proven not to have their address taken, so code that relies on distinct addresses will still work.

ICF is a neat optimization, but not essential. With a bit of effort you can hoist out non-dependent code to a non-template, or a template with fewer parameters, to reduce the quantity of duplicated code in the executable. There's more information on this in the slides for a Diet Templates talk I did a couple of years ago.

like image 84
Jonathan Wakely Avatar answered Nov 04 '22 03:11

Jonathan Wakely