Skip to content

cmake: disable LTO for some configure checks#1850

Merged
Dead2 merged 1 commit intozlib-ng:developfrom
thesamesam:cmake-lto
Jan 19, 2025
Merged

cmake: disable LTO for some configure checks#1850
Dead2 merged 1 commit intozlib-ng:developfrom
thesamesam:cmake-lto

Conversation

@thesamesam
Copy link
Copy Markdown
Contributor

@thesamesam thesamesam commented Jan 9, 2025

Some of zlib-ng's configure tests define a function expecting it to be compiled but don't call that function, or don't use its return value. This is risky with LTO where the whole thing may be optimised out, which has happened before:

Closes: #1841

Summary by CodeRabbit

  • Chores
    • Updated CMake configuration flags to improve compiler compatibility
    • Modified compiler flag settings for Link Time Optimization (LTO) checks
    • Adjusted compilation settings to ensure consistent function detection

Some of zlib-ng's configure tests define a function expecting it to be compiled but
don't call that function, or don't use its return value. This is risky with
LTO where the whole thing may be optimised out, which has happened before:
* zlib-ng#1616
* zlib-ng#1622
* https://gitlab.kitware.com/cmake/cmake/-/issues/26103

Closes: zlib-ng#1841
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 9, 2025

Walkthrough

The changes in the CMakeLists.txt file focus on addressing potential issues with Link Time Optimization (LTO) during CMake configuration checks. The modifications involve adding ${ZNOLTOFLAG} to CMAKE_REQUIRED_FLAGS to disable LTO when performing checks for specific built-in functions. This approach aims to prevent compiler optimizations from potentially removing or altering function detection tests that do not explicitly use the function's return value.

Changes

File Change Summary
CMakeLists.txt Modified CMAKE_REQUIRED_FLAGS to include ${ZNOLTOFLAG} during compiler feature checks

Assessment against linked issues

Objective Addressed Explanation
Prevent LTO from breaking configure checks [#1841]
Ensure compiler checks are not optimized out

The changes directly address the core concern raised in issue #1841 by introducing a mechanism to disable Link Time Optimization during specific compiler configuration checks. This prevents the compiler from potentially optimizing out test functions that are not explicitly used, which could lead to incorrect feature detection.

Finishing Touches

  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@thesamesam
Copy link
Copy Markdown
Contributor Author

I diffed the generated CMake cache files on amd64 + x86 and saw no change as expected (given this is a defensive change).

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
CMakeLists.txt (1)

304-304: Consider documenting the LTO disable pattern.

The pattern of disabling LTO for configure checks is now used in multiple places. Consider adding a comment in the CMake code explaining why LTO needs to be disabled for these checks, or create a helper function to encapsulate this pattern.

Example helper function:

function(check_compiler_feature_no_lto)
    cmake_parse_arguments(ARG "" "FEATURE_NAME;TEST_SOURCE" "" ${ARGN})
    set(CMAKE_REQUIRED_FLAGS ${ZNOLTOFLAG})
    check_c_source_compiles(
        "${ARG_TEST_SOURCE}"
        ${ARG_FEATURE_NAME}
    )
    set(CMAKE_REQUIRED_FLAGS)
endfunction()

Also applies to: 494-507, 512-525, 530-543

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 787c7f6 and 57b1b2f.

📒 Files selected for processing (1)
  • CMakeLists.txt (5 hunks)
🔇 Additional comments (3)
CMakeLists.txt (3)

304-304: LGTM! Properly disables LTO for MSVC AVX detection.

The addition of ${ZNOLTOFLAG} prevents Link Time Optimization from interfering with the AVX feature detection check.


Line range hint 494-507: LGTM! Properly disables LTO for __builtin_assume_aligned detection.

The addition of ${ZNOLTOFLAG} prevents the test function from being optimized away during configure checks. The flag is correctly scoped and cleared after the check.


Line range hint 512-525: LGTM! Properly disables LTO for __builtin_ctz and __builtin_ctzll detection.

The addition of ${ZNOLTOFLAG} prevents the test functions from being optimized away during configure checks. The flags are correctly scoped and cleared after each check.

Also applies to: 530-543

# Workaround for MSVC. By default MSVC does not define the __SSE*__ macros.
# Fix it if AVX is enabled.
set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG} ${ZNOLTOFLAG}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (if-minor): Does this actually make any difference? AFAIK there is no support for LTO in MSVC, so the variable would be empty.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered the same -- I figured I'd be consistent given someone might copy it in future, or MSVC might gain it at some point.

I googled a bit and found https://stackoverflow.com/questions/42230141/how-to-write-lto-enabled-code which implies it has it under the name "LTCG", but honestly, I'm no MSVC expert at all. Happy to drop it if you want.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 32.80%. Comparing base (787c7f6) to head (57b1b2f).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff              @@
##           develop    #1850       +/-   ##
============================================
- Coverage    82.19%   32.80%   -49.40%     
============================================
  Files          138       69       -69     
  Lines        10985     5661     -5324     
  Branches      2695     1233     -1462     
============================================
- Hits          9029     1857     -7172     
- Misses        1261     3548     +2287     
+ Partials       695      256      -439     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some CMake configure checks may be broken with LTO

3 participants