Skip to content

Globally enable linker dead-code elimination on Unix#128232

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/enable-linker-dead-code-elimination
Draft

Globally enable linker dead-code elimination on Unix#128232
Copilot wants to merge 3 commits into
mainfrom
copilot/enable-linker-dead-code-elimination

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 14, 2026

Follow-up to #128228 per this review comment: flip --gc-sections / -dead_strip globally in configurecompiler.cmake instead of opting in per binary, mirroring the existing global /OPT:REF on Windows.

eng/native/configurecompiler.cmake:

  • Compile: add -fdata-sections alongside the existing -ffunction-sections (Unix/WASI) so unreferenced data, not just functions, can be stripped.
  • Link, Apple: -Wl,-dead_strip for Checked / Release / RelWithDebInfo.
  • Link, other Unix (ELF, incl. Linux, BSDs, SunOS, Haiku, wasm-ld): -Wl,--gc-sections for the same three configurations.
  • Debug: intentionally untouched — preserves full symbols, incremental linking, and the existing PAL -Wl,--no-gc-sections debug+sanitizers workaround.

Goes broader than #128228 (which scoped this to apphost / dotnet / nethost). Targets that need specific symbols retained can still opt out per-symbol via KEEP / __attribute__((used)) / export lists; -fvisibility=hidden plus existing export lists already cover the common cases.

Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com>
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/runtime-infrastructure
See info in area-owners.md if you want to be subscribed.

Comment thread eng/native/configurecompiler.cmake
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 15, 2026 01:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the native build configuration (eng/native/configurecompiler.cmake) to enable link-time dead-code elimination (section GC) by default on non-Windows Unix builds, and ensures compilation places both functions and data into separate sections to make that elimination effective.

Changes:

  • Add global non-Debug linker flags to strip unused sections (-Wl,-dead_strip on Apple, -Wl,--gc-sections elsewhere).
  • Add -fdata-sections alongside the existing -ffunction-sections for Unix/WASI compilation.
  • Update nearby comments to describe the function/data sectioning intent.
Show a summary per file
File Description
eng/native/configurecompiler.cmake Enables global link-time section GC on Unix non-Debug builds and adds -fdata-sections to support stripping unused data.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 3

Comment thread eng/native/configurecompiler.cmake Outdated
Comment on lines +324 to +333
# Strip unreferenced sections at link time, paired with -ffunction-sections/-fdata-sections
# (set in the Unix/WASI compile-options block below). Gated to non-Debug builds so Debug
# keeps full symbols and incremental linking. Windows uses /OPT:REF, configured above.
if(CLR_CMAKE_HOST_UNIX)
if(CLR_CMAKE_HOST_APPLE)
add_linker_flag($<$<CONFIG:CHECKED,RELEASE,RELWITHDEBINFO>:-Wl,-dead_strip>)
else()
add_linker_flag($<$<CONFIG:CHECKED,RELEASE,RELWITHDEBINFO>:-Wl,--gc-sections>)
endif()
endif()
Comment thread eng/native/configurecompiler.cmake Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 15, 2026 15:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment on lines +327 to +333
if(CLR_CMAKE_HOST_UNIX)
if(CLR_CMAKE_HOST_APPLE)
add_linker_flag(-Wl,-dead_strip CHECKED RELEASE RELWITHDEBINFO)
else()
add_linker_flag(-Wl,--gc-sections CHECKED RELEASE RELWITHDEBINFO)
endif()
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status
Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants