Skip to content

Globally enable linker dead-code elimination on Unix#128232

Open
Copilot wants to merge 10 commits into
mainfrom
copilot/enable-linker-dead-code-elimination
Open

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

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 14, 2026

-ffunction-sections is set globally for non-MSVC builds in eng/native/configurecompiler.cmake, but the matching -Wl,--gc-sections / -Wl,-dead_strip is never passed to the linker, so on Linux/macOS unreferenced sections are not actually stripped. Windows is unaffected because /OPT:REF is set globally. This became visible in #126367 when apphost picked up a dn-containers link dependency and grew unexpectedly.

cc @dotnet/appmodel @jkotas

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.

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 Outdated
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 thread eng/native/configurecompiler.cmake Outdated
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 thread eng/native/configurecompiler.cmake Outdated
@elinor-fung
Copy link
Copy Markdown
Member

@copilot what is the size delta on linux for runtime binaries (in particular coreclr and apphost) with this change?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 18, 2026

@copilot what is the size delta on linux for runtime binaries (in particular coreclr and apphost) with this change?

I'm unable to run a full Linux build in this environment to produce actual measurements. What I can say:

  • apphost: PR Enable linker dead code elimination for apphost, dotnet, and nethost #128228 already measured the effect of --gc-sections on apphost in isolation (76,240 → 58,640 bytes, −23% stripped). Since this PR applies the same flags globally, apphost should see roughly the same benefit.
  • coreclr (libcoreclr.so): This is the harder question and I can't provide a number without a real build. libcoreclr.so has a large, well-defined exported surface (EE exports, profiler APIs, DAC interfaces, etc.), and -fvisibility=hidden is already applied globally, so the linker's view of reachable symbols is constrained by the export lists. That said, internal dead code (e.g. unused static initializers, cold paths behind feature flags) could be stripped. I'd expect a smaller percentage reduction than apphost/dotnet, but still measurable.

If you can kick off a CI build or a local linux-x64 Release build comparing main vs. this branch with size/wc -c on the stripped binaries, that would give the definitive numbers. I'm happy to help interpret the results.

Comment thread eng/native/configurecompiler.cmake Outdated
Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com>
Comment thread eng/native/configurecompiler.cmake Outdated
Copilot AI review requested due to automatic review settings May 19, 2026 01:54
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread eng/native/configurecompiler.cmake
Comment thread eng/native/configurecompiler.cmake Outdated
Copilot AI review requested due to automatic review settings May 19, 2026 01:58
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

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 19, 2026 02:05
Comment thread eng/native/configurecompiler.cmake Outdated
Comment thread eng/native/configurecompiler.cmake Outdated
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread eng/native/configurecompiler.cmake Outdated
Comment thread eng/native/configurecompiler.cmake Outdated
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 19, 2026 13:39
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

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.

5 participants