Skip to content

Enable function-level linking in the CMake build (smaller downstream footprint)#1485

Closed
bmehta001 wants to merge 1 commit into
microsoft:mainfrom
bmehta001:bhamehta/vcpkg-function-level-linking
Closed

Enable function-level linking in the CMake build (smaller downstream footprint)#1485
bmehta001 wants to merge 1 commit into
microsoft:mainfrom
bmehta001:bhamehta/vcpkg-function-level-linking

Conversation

@bmehta001

Copy link
Copy Markdown
Contributor

Problem

The compiler-flags block in CMakeLists.txt (lines ~139–207) that sets -ffunction-sections / -fdata-sections / --gc-sections is wrapped entirely in if(NOT MATSDK_USE_VCPKG_DEPS), and its MSVC branch only sets warning flags — never /Gy.

So the CMake/vcpkg build — the one packaged for downstream consumers (e.g. Foundry Local) — compiles every TU without function-level COMDATs. Referencing a single symbol then drags the whole .obj into the consumer's image, because the linker can only include/exclude entire object files, not individual functions. Measured impact downstream: linking 1DS adds ~3.86 MB to a consumer DLL even when only a slice of the API is used.

The MSBuild Release projects already do the right thing:

Solutions/win32-lib/win32-lib.vcxproj:396  <FunctionLevelLinking>true</FunctionLevelLinking>   (/Gy)
Solutions/win32-dll/win32-dll.vcxproj:298  <EnableCOMDATFolding>true</EnableCOMDATFolding>      (/OPT:ICF)
Solutions/win32-dll/win32-dll.vcxproj:299  <OptimizeReferences>true</OptimizeReferences>        (/OPT:REF)

The CMake path just never got the equivalent.

Change

Add a small block, applied in both vendored and vcpkg modes, that splits functions/data into separate COMDATs/sections:

Toolchain Flags
MSVC /Gy /Gw
GCC / Clang -ffunction-sections -fdata-sections
AppleClang -ffunction-sections (mach-o dead-strips per symbol)

This lets a consumer's linker dead-strip unreferenced SDK code (/OPT:REF + /OPT:ICF, --gc-sections, -dead_strip). No source or ABI change — purely a code-generation/linkage hint.

Validation

  • Confirmed /Gy /Gw reach the SDK target's compile line under MATSDK_USE_VCPKG_DEPS=ON with the MSVC + Ninja generator.
  • Cross-platform CI (Windows/Linux/macOS/iOS/Android + vcpkg) exercises the full build.

Consumer note (not in this repo)

MSVC defaults /OPT:REF and /OPT:ICF off when /DEBUG is set (e.g. RelWithDebInfo), so the consuming link must enable them explicitly to realize the savings. This PR only enables the compile-side prerequisite.

A follow-up could also add /OPT:REF,ICF / --gc-sections to the SDK's own shared-library link for dynamic vcpkg consumers.

…d-strip

The compiler-flags block in CMakeLists.txt (the one that sets
-ffunction-sections/-fdata-sections/--gc-sections) is wrapped entirely in
`if(NOT MATSDK_USE_VCPKG_DEPS)`, and its MSVC branch only sets warning flags --
never /Gy. As a result the CMake/vcpkg build (the one packaged for downstream
consumers such as Foundry Local) compiles every translation unit without
function-level COMDATs. Referencing a single symbol then pulls the entire .obj
into the consumer's image, because the linker can only include/exclude whole
object files, not individual functions.

Add a small block, applied in BOTH vendored and vcpkg modes, that splits
functions/data into separate COMDATs/sections:
  * MSVC:            /Gy /Gw
  * GCC / Clang:     -ffunction-sections -fdata-sections
  * AppleClang:      -ffunction-sections (mach-o dead-strips per symbol)

This lets a consumer's linker dead-strip unreferenced SDK code (MSVC
/OPT:REF + /OPT:ICF, GNU/Clang --gc-sections, Apple ld -dead_strip) and brings
the CMake build in line with the MSBuild Release projects, which already enable
FunctionLevelLinking + OptimizeReferences + EnableCOMDATFolding
(Solutions/win32-lib/win32-lib.vcxproj, Solutions/win32-dll/win32-dll.vcxproj).

No source or ABI change; purely a code-generation/linkage hint. Verified that
/Gy /Gw reach the SDK target's compile line under MATSDK_USE_VCPKG_DEPS=ON with
the MSVC + Ninja generator.

Note for consumers: MSVC defaults /OPT:REF and /OPT:ICF OFF when /DEBUG is set
(e.g. RelWithDebInfo), so the consuming link must enable them explicitly to
realize the savings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bmehta001 bmehta001 requested a review from a team as a code owner June 15, 2026 17:22
@bmehta001 bmehta001 requested a review from Copilot June 15, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Enables function-/data-level sectioning in the CMake build so downstream consumers can dead-strip unused SDK code (especially in the vcpkg/MSVC configuration), reducing linked binary footprint without changing source or ABI.

Changes:

  • Adds a new global “dead-strip enablement” block that applies in both vendored and vcpkg modes.
  • Enables /Gy /Gw for MSVC and -ffunction-sections / -fdata-sections for GCC/Clang (with an AppleClang-specific choice).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CMakeLists.txt
Comment on lines +209 to +214
# --- Dead-strip enablement (applies in BOTH vendored and vcpkg modes) ---------
# Split functions/data into separate COMDATs/sections so a consumer's linker can
# drop unreferenced SDK code: MSVC /OPT:REF + /OPT:ICF, GNU/Clang --gc-sections,
# Apple ld -dead_strip. The vendored-deps flag block above sets these only when
# NOT MATSDK_USE_VCPKG_DEPS, and never sets /Gy at all on MSVC -- so the vcpkg
# build (the one packaged for downstream consumers) and every MSVC build link
Comment thread CMakeLists.txt
Comment on lines +220 to +223
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
# -fdata-sections historically conflicted with bitcode on AppleClang, and the
# mach-o linker already dead-strips at symbol granularity with -dead_strip.
add_compile_options(-ffunction-sections)
@bmehta001

Copy link
Copy Markdown
Contributor Author

Folding this into the existing vcpkg PR #1475 instead of a standalone PR.

@bmehta001 bmehta001 closed this Jun 15, 2026
@bmehta001 bmehta001 deleted the bhamehta/vcpkg-function-level-linking branch June 15, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants