GCC(/Clang):合并具有相同指令的函数(COMDAT 折叠) [英] GCC(/Clang): Merging functions with identical instructions (COMDAT folding)

查看:28
本文介绍了GCC(/Clang):合并具有相同指令的函数(COMDAT 折叠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,GCC 或 Clang 工具集是否实现了等效于 MSVC 的 相同的 COMDAT 折叠 (ICF) 目前?如果没有,有什么计划吗?除了旧的 GCC 邮件列表消息之外,我似乎找不到任何有关该主题的最新权威链接.

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?

此外,有没有人发现 ICF 在实践中最小化生成的可执行文件的大小方面有很大的不同?我没有任何大型 MSVC 项目可以方便地对其进行测试.(我猜只有当您碰巧在许多不同的 vtable-layout 兼容类型上实例化模板时,它才会真正有帮助.)

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.)

最后,两个指向不同函数的函数指针在运行时比较相等是否符合 C++11 标准?这个链接似乎暗示它不是,但它是对于 C99.发现上一个问题这个话题

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. found previous question on this topic

推荐答案

GCC 和 Clang 都不是链接器,ICF 需要由链接器来完成,或者至少与链接器合作完成.他们不做 ICF,所以是的,不同的实例会产生不同的代码. GNU gold 链接器通过 --icf 支持 ICF选项,需要使用 GCC 选项 -ffunction-sections.

Neither GCC nor Clang is a linker, and ICF needs to be done by the linker, or at least with cooperation with the linker. 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.

不同的函数必须有不同的地址......我不记得是否为任何获取其地址的函数禁用了 ICF,但如果没有,应该可以在组合函数之前放置大量无操作指令并使每个不同的实例化从不同的指令开始,因此它们具有不同的地址.gold 的 --icf=safe 选项只为可以证明其地址未被占用的函数启用 ICF,因此依赖于不同地址的代码仍然可以工作.

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. 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 是一种巧妙的优化,但不是必需的.通过一些努力,您可以将非依赖代码提升到非模板或具有较少参数的模板,以减少可执行文件中重复代码的数量.饮食模板的幻灯片中有更多关于此的信息我做了几个多年前.

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.

这篇关于GCC(/Clang):合并具有相同指令的函数(COMDAT 折叠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆