即使通过-fno -lto通过

发布于 2025-01-27 14:29:26 字数 1021 浏览 3 评论 0原文

我有一个CMAKE项目,其中有几个子标记,这些项目创建了使用-flto = thin构建的静态库。

该项目有许多与上述库有关的测试。使用LTO,它需要大量时间来构建测试,因此我使用-fno-lto禁用LTO进行测试。

我注意到的是,即使使用-fno-ltolld也可以在测试上执行LTO。如果我用运行链接器 - 时间跟踪我可以看到大部分时间都花在LTO上。

我的问题是:

  1. 这是期望的吗?如果是这样,我可以假设lld在其链接的对象中找到LTO信息时,可以执行LTO。
  2. 如果没有,是否有办法禁用这种行为?将-fno-lto添加到编译器似乎不起作用,lld没有明确禁用LTO的参数。
  3. 如果没有,这是一个错误吗?

更新1:

这是我在CMAKE中处理lto的方式:

# Enable Thin LTO only on non-test targets.
if(ENABLE_LTO)
  if (IS_TEST)
    target_compile_options(${TARGET} PRIVATE -fno-lto)
    # Probably pointless.
    target_link_options(${TARGET} PRIVATE -fno-lto)
  else()
    message(STATUS "ENABLE_LTO on target ${TARGET})")
    target_compile_options(${TARGET} PRIVATE -flto=thin)
    target_link_options(${TARGET} PRIVATE -flto=thin -Wl,--thinlto-cache-dir=${CMAKE_BINARY_DIR}/lto.cache)
  endif()
endif()

I have a CMake project with several subprojects that create static libraries built with -flto=thin.

The project has a lot of tests that are linked against the aforementioned libraries. With LTO it takes a lot of time to build tests, therefore I have disabled LTO for tests using -fno-lto.

What I noticed though, is that lld performs LTO on tests even with -fno-lto. If I run the linker with --time-trace I can see that the majority of the time is spent on LTO.

My questions are:

  1. Is this expected? If so I can assume that lld performs LTO whenever it finds the LTO info in the object it links.
  2. If not, is there a way to disable this behavior? Adding -fno-lto to the compiler does not seem to work, and lld does not have a param to explicitly disable LTO.
  3. If not, is this a bug?

Update 1:

This is how I handle lto in CMake:

# Enable Thin LTO only on non-test targets.
if(ENABLE_LTO)
  if (IS_TEST)
    target_compile_options(${TARGET} PRIVATE -fno-lto)
    # Probably pointless.
    target_link_options(${TARGET} PRIVATE -fno-lto)
  else()
    message(STATUS "ENABLE_LTO on target ${TARGET})")
    target_compile_options(${TARGET} PRIVATE -flto=thin)
    target_link_options(${TARGET} PRIVATE -flto=thin -Wl,--thinlto-cache-dir=${CMAKE_BINARY_DIR}/lto.cache)
  endif()
endif()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2025-02-03 14:29:26

如果将库中的库编译为 -flto ,则至少对于GCC,对象文件仅包含中间语言而不包含二进制代码。

这意味着,当您将它们链接到与 -fno-lto 的测试用例中时,没有二进制代码可链接到。链接器别无选择,只能首先将中间语言编译为每个所需功能的二进制语言,您将其视为LTO阶段。

在GCC中,有一个选项 -ffat-lto-objects 告诉GCC在对象文件中同时包括中间语言以及二进制代码。然后,它们可用于与LTO链接。缺点是,这需要更长的时间来编译并生成较大的对象文件。

您必须检查Clang是否具有相同的选项,它们通常与选项非常兼容。

If you compile the libraries with -flto then, at least for gcc, the object files will only contain the intermediate language and no binary code.

That means when you link them into your test cases compiled with -fno-lto there is no binary code to link to. The linker has no choice but to first compile the intermediate language into binary for each needed function, which you would see as an LTO phase.

In gcc there is an option -ffat-lto-objects that tells gcc to include both the intermediate language as well as binary code in the object files. They can then be used for linking with LTO or without. The drawback is that this takes a bit longer to compile and produces larger object files.

You have to check if clang has the same option, they are usually pretty compatible with options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文