Skip to content

ENH: Add SimpleITK_DEAD_STRIP option for distribution builds - #2784

Merged
blowekamp merged 2 commits into
SimpleITK:mainfrom
hjmjohnson:enh/dead-strip-option
Nov 6, 2026
Merged

blowekamp merged 2 commits into
SimpleITK:mainfrom
hjmjohnson:enh/dead-strip-option

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Adds an advanced SimpleITK_DEAD_STRIP option (default OFF) that compiles with -ffunction-sections -fdata-sections, links with --gc-sections, and adds --icf=safe when the linker supports it (gold or lld). Nothing in the packaging or Docker scripts changes; the option is enabled in one Linux BatchBuild job so CI exercises it. Follow-up to the IPO study on #2772: this recovers more wheel size than full LTO at baseline link time and memory.

Measurements (manylinux_2_28 image, GCC 14, gold 1.16, cp312, Elastix on)

Python wrapper module _SimpleITK.cpython-312-x86_64-linux-gnu.so, stripped:

baseline DEAD_STRIP full LTO (#2772)
.text 180.8 MB 154.0 MB 144.4 MB
.gcc_except_table 16.8 MB 7.1 MB 6.0 MB
module file 300.4 MB 257.8 MB 252.0 MB
wheel 61.2 MB 51.7 MB 52.1 MB
wrapper link wall 19 s 16 s 891 s
wrapper link peak RSS 6.0 GB 7.5 GB 11.0 GB (22 GB container-wide)
Python ctest (312) pass pass pass

gold --icf=all folds more but segfaults on the first C++ exception crossing the wrapper; --icf=safe only folds functions whose address is never taken and is what the option uses.

Runtime profile before and after (same two wheels, one core, single ITK thread)
case baseline ms DEAD_STRIP ms ratio
reg_2d_euler_mattes_3lvl 54.0 54.8 1.01
reg_3d_euler_meansquares_2lvl 549.4 557.7 1.02
reg_3d_bspline_corr_lbfgsb 2047.4 2144.3 1.05
reg_2d_demons_100it 320.9 322.7 1.01
filt_gaussian_vol 355.0 357.7 1.01
filt_discrete_gaussian_vol 216.6 213.6 0.99
filt_curvature_flow_vol_5it 1373.3 1382.6 1.01
filt_median_r1_vol 448.3 453.7 1.01
filt_gradient_magnitude_vol 125.8 127.5 1.01
filt_binary_threshold_vol 10.1 10.3 1.02
filt_otsu_vol 105.0 104.5 0.99
filt_connected_relabel_vol 41.3 41.7 1.01
filt_resample_affine_vol 71.2 72.2 1.01
filt_n4_2d 515.8 521.0 1.01
filt_canny_2d 3.9 4.0 1.03
dispatch_small_add_x1000 74.3 74.8 1.01
getarray_vol_x20 128.0 128.1 1.00

Minimum of three repeats per case per round over five rounds, arms run in rotating order, on an otherwise idle Intel Xeon w7-3545 (load average about 1), taskset -c 46, ProcessObject_SetGlobalDefaultNumberOfThreads(1). Registration cases use BrainProtonDensitySliceBorder20.png/Shifted13x17y.png and RA-Float.nrrd from the test data.

Sixteen of seventeen cases are within 3% of baseline, inside the round-to-round spread of the baseline itself. The BSpline/LBFGSB registration is 4% to 5% slower in all five rounds; an earlier --gc-sections-only build without --icf=safe shows the same 5% on that case, so the cost comes from the section-per-function layout rather than from identical code folding. I have not profiled it further (no perf on this host).

Implementation notes
  • CMake/sitkDeadStrip.cmake is included from the SuperBuild (before ITK is added, so ITK and the wrappers get the same flags), the top-level CMakeLists.txt, and sitkProjectLanguageCommon.cmake. It is a no-op on MSVC and Apple (ld64 dead-strips by default; MSVC has /Gy + /OPT:REF).
  • sitkUseGoldLinker.cmake: selecting gold through CMAKE_LINKER_TYPE after project() left CMake passing bfd's --dependency-file to gold, which rejects it; CMAKE_LINK_DEPENDS_USE_LINKER is now turned off there.
  • gold is deprecated since binutils 2.44; lld accepts the same --icf=safe, which the check_linker_flag probe picks up.
  • The CI job serializes links through a CMAKE_JOB_POOLS pool of one. Each stripped wrapper link peaks near 7.5 GB; the first run let the three wrapper links overlap and the 16 GB runner was shut down mid-build.

🤖 Generated with Claude Code

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.

🟡 Changes recommended

Dead stripping can break BFD links by adding the unsupported --icf=safe flag, and SuperBuild fails to propagate the parent's gold-linker opt-out to its SimpleITK child.

2 open issues
What changed in this PR

Adds opt-in dead stripping to reduce SimpleITK distribution sizes without full link-time optimization, integrating it across dependency builds and language wrappers.

Changes:

  • Adds the advanced, default-OFF SimpleITK_DEAD_STRIP option for section garbage collection and optional safe identical-code folding.
  • Integrates the option into build entry points and adjusts gold linker dependency handling.
  • Enables one Linux BatchBuild job with serialized links and documents the option; packaging scripts remain unchanged.
File Description
SuperBuild/​SuperBuild.cmake Applies dead-strip settings before dependency builds.
docs/​source/​building.rst Documents the option and its intended effects.
CMakeLists.txt Loads dead-strip settings before ITK integration.
CMake/​sitkUseGoldLinker.cmake Disables linker dependency-file handling when switching to gold.
CMake/​sitkProjectLanguageCommon.cmake Enables dead stripping in standalone wrapper projects.
CMake/​sitkDeadStrip.cmake Defines the option and compiler/linker flags.
.github/​workflows/​BatchBuild.yml Exercises the option with serialized links in Linux CI.

Review effort: Balanced

Comment thread CMake/sitkDeadStrip.cmake Resolved
Comment thread SuperBuild/SuperBuild.cmake Resolved
@hjmjohnson
hjmjohnson force-pushed the enh/dead-strip-option branch 3 times, most recently from 234dd4a to 1534132 Compare November 5, 2026 20:24
@hjmjohnson

Copy link
Copy Markdown
Contributor Author

Force-pushed 234dd4a71c to 1534132969: content only, no rebase. It corrects the option's documentation and hardens two linker details; the CI commit is unchanged.

What changed
  • docs/source/building.rst: the text said the option is enabled by the Linux wheel packaging script and does not affect link memory. Neither is true: only the BatchBuild job enables it, and each stripped link uses more memory (6.0 GB to 7.5 GB in the measurements above). The paragraph now also says that enabling the option selects gold when gold is available and no other linker was chosen.
  • CMake/sitkDeadStrip.cmake: the --icf=safe probe result is cached per CMAKE_LINKER_TYPE, so changing the linker in an existing build tree probes again instead of reusing the old answer.
  • CMake/sitkUseGoldLinker.cmake: CMAKE_LINK_DEPENDS_USE_LINKER is also turned off on the CMake < 3.29 path, which selects gold after project() in the same way.

The edited module was configure-tested on macOS with a scratch project (probe runs once per linker type; flags unchanged). The --icf=safe branches themselves can only run on Linux, so the BatchBuild job is their first executor.

Compile with -ffunction-sections -fdata-sections and link with
--gc-sections, plus --icf=safe when gold (or another linker that
accepts it) is used. The option is off by default and is a no-op with
MSVC and on macOS, where ld64 dead-strips by default.

Measured on the manylinux_2_28 x86_64 image (GCC 14.2, gold 1.16,
Elastix on, cp312 wheel): the stripped _SimpleITK module shrinks from
300.4 MB to 257.7 MB and the wheel from 61.2 MB to 51.7 MB, which is
smaller than a full LTO build (252.0 MB module, 52.1 MB wheel) with no
increase in link time and 25% more link memory: the wrapper link takes
16 s and 7.5 GB versus 19 s and 6.0 GB without the option, and 891 s
and 11 GB (22 GB container-wide) with LTO. The Python test suite
passes. gold --icf=all would shrink .text further but crashes on the
first C++ exception, so only the safe mode is used.

When the gold linker is selected after project() has probed the
default linker, CMake would pass --dependency-file to gold, which
rejects it; linker-driven dependency files are disabled in that case.

Assisted-by: Claude Code — measurements across six manylinux build variants
The ubuntu-24.04 SuperBuild job with Python, Lua, R, and Elastix
wrapping exercises the option on every language wrapper link. The
links are serialized through a job pool: each stripped wrapper link
peaks near 7.5 GB, and three of them overlapping exhaust the 16 GB
runner.
@hjmjohnson
hjmjohnson force-pushed the enh/dead-strip-option branch from 1534132 to 38ca696 Compare November 6, 2026 07:09
@hjmjohnson

Copy link
Copy Markdown
Contributor Author

Force-pushed 1534132969 to 38ca696a9e: wording only, no rebase and no change to CMake logic or CI. The commit message said the option comes "at no cost"; each stripped link uses about 25% more memory (6.0 GB to 7.5 GB), and the message now says so.

What changed
  • ENH commit message: "at no cost" became "with no increase in link time and 25% more link memory".
  • docs/source/building.rst: the sentence on linker selection is more exact: gold is selected when available, unless USE_GOLD_LINKER is off or CMAKE_LINKER_TYPE names another linker.
  • CMake/sitkDeadStrip.cmake: one comment reworded.

The previous BatchBuild run on 1534132969 finished with one red job, Windows ARM, which the option cannot affect (it is a no-op with MSVC). This push starts a fresh run that covers that job again.

@blowekamp

Copy link
Copy Markdown
Member

Thank you.

@blowekamp
blowekamp merged commit c61bdf8 into SimpleITK:main Nov 6, 2026
32 of 33 checks passed
@hjmjohnson
hjmjohnson deleted the enh/dead-strip-option branch November 6, 2026 21:30
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 19, 2026
Port of the SimpleITK_DEAD_STRIP option from SimpleITK
(SimpleITK/SimpleITK#2784, which followed the
link-time optimization study in
SimpleITK/SimpleITK#2772). On Linux it compiles
with -ffunction-sections -fdata-sections and links with gold
--gc-sections --icf=safe. Off by default; other platforms are unchanged.

A/B on ITK 957816d, manylinux_2_28 (GCC 14.2, gold 1.15), cp311 abi3:

  wheel                 off      on
  itk_core            89.5 MB  87.0 MB
  itk_filtering       89.3 MB  86.4 MB
  itk_filtering_extra 60.3 MB  57.8 MB
  itk_numerics        64.7 MB  62.9 MB

.text across the 96 wrapper modules fell 4.4%, against 15% in
SimpleITK: _ITKCommonPython exports 33,625 symbols from its statically
linked ITK libraries, and the linker must keep them all.
ITK already links its wrapper modules with -flto, so link cost barely
moved: the three largest modules, linked alone, took 1-4% longer and
used the same peak memory (9.05 GB for _ITKImageIntensityPython).

Seventeen Python cases gave bit-identical output in both arms;
BSpline/LBFGSB registration ran 5-7% slower, as it did in SimpleITK,
and the rest stayed within run-to-run noise.

ld64 -dead_strip changed the macOS arm64 wheels by under 0.1% and is
not applied there.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 19, 2026
The wrapper modules link ITK statically, and ITK's static libraries keep
default visibility on Linux, so _ITKCommonPython exported 33,625
symbols. The linker had to keep every one of them, which limited
--gc-sections to a 4% saving. SimpleITK avoids this with a version
script on its Python module
(SimpleITK/SimpleITK#2784 and the visibility
settings in SimpleITK's SuperBuild/External_ITK.cmake); this applies
the same script to every ITK Python module. The modules share ITK's
global state through the _ITKCommonPython capsule, not through
exported symbols, and Python loads them RTLD_LOCAL.

A/B on ITK 957816d, manylinux_2_28, cp311 abi3 (MB):

  wheel                off  --gc-sections  + this
  itk_core            89.5     87.0        70.7
  itk_filtering       89.3     86.4        67.8
  itk_filtering_extra 60.3     57.8        39.3
  itk_numerics        64.7     62.9        52.3
  itk_io              36.9     34.9        17.6
  itk_registration    37.3     36.0        27.6
  itk_segmentation    19.7     18.9        14.3

Each module now exports 84 symbols. Seventeen Python cases gave
bit-identical output; the BSpline/LBFGSB slowdown seen with
--gc-sections alone is gone (2% faster than off), and the rest stayed
within run-to-run noise. Thread defaults, the global warning flag,
object factories, observers, down-casts, exceptions, and a pipeline
through four modules all behave across modules as before.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 20, 2026
Port of the SimpleITK_DEAD_STRIP option from SimpleITK
(SimpleITK/SimpleITK#2784, which followed the
link-time optimization study in
SimpleITK/SimpleITK#2772). On Linux it compiles
with -ffunction-sections -fdata-sections and links with gold
--gc-sections --icf=safe. Off by default; other platforms are unchanged.

A/B on ITK 957816d, manylinux_2_28 (GCC 14.2, gold 1.15), cp311 abi3:

  wheel                 off      on
  itk_core            89.5 MB  87.0 MB
  itk_filtering       89.3 MB  86.4 MB
  itk_filtering_extra 60.3 MB  57.8 MB
  itk_numerics        64.7 MB  62.9 MB

.text across the 96 wrapper modules fell 4.4%, against 15% in
SimpleITK: _ITKCommonPython exports 33,625 symbols from its statically
linked ITK libraries, and the linker must keep them all.
ITK already links its wrapper modules with -flto, so link cost barely
moved: the three largest modules, linked alone, took 1-4% longer and
used the same peak memory (9.05 GB for _ITKImageIntensityPython).

Seventeen Python cases gave bit-identical output in both arms;
BSpline/LBFGSB registration ran 5-7% slower, as it did in SimpleITK,
and the rest stayed within run-to-run noise.

ld64 -dead_strip changed the macOS arm64 wheels by under 0.1% and is
not applied there.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 20, 2026
The wrapper modules link ITK statically, and ITK's static libraries keep
default visibility on Linux, so _ITKCommonPython exported 33,625
symbols. The linker had to keep every one of them, which limited
--gc-sections to a 4% saving. SimpleITK avoids this with a version
script on its Python module
(SimpleITK/SimpleITK#2784 and the visibility
settings in SimpleITK's SuperBuild/External_ITK.cmake); this applies
the same script to every ITK Python module. The modules share ITK's
global state through the _ITKCommonPython capsule, not through
exported symbols, and Python loads them RTLD_LOCAL.

A/B on ITK 957816d, manylinux_2_28, cp311 abi3 (MB):

  wheel                off  --gc-sections  + this
  itk_core            89.5     87.0        70.7
  itk_filtering       89.3     86.4        67.8
  itk_filtering_extra 60.3     57.8        39.3
  itk_numerics        64.7     62.9        52.3
  itk_io              36.9     34.9        17.6
  itk_registration    37.3     36.0        27.6
  itk_segmentation    19.7     18.9        14.3

Each module now exports 84 symbols. Seventeen Python cases gave
bit-identical output; the BSpline/LBFGSB slowdown seen with
--gc-sections alone is gone (2% faster than off), and the rest stayed
within run-to-run noise. Thread defaults, the global warning flag,
object factories, observers, down-casts, exceptions, and a pipeline
through four modules all behave across modules as before.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 20, 2026
Port of the SimpleITK_DEAD_STRIP option from SimpleITK
(SimpleITK/SimpleITK#2784, which followed the
link-time optimization study in
SimpleITK/SimpleITK#2772). On Linux it compiles
with -ffunction-sections -fdata-sections and links with gold
--gc-sections --icf=safe. Off by default; other platforms are unchanged.

A/B on ITK 957816d, manylinux_2_28 (GCC 14.2, gold 1.15), cp311 abi3:

  wheel                 off      on
  itk_core            89.5 MB  87.0 MB
  itk_filtering       89.3 MB  86.4 MB
  itk_filtering_extra 60.3 MB  57.8 MB
  itk_numerics        64.7 MB  62.9 MB

.text across the 96 wrapper modules fell 4.4%, against 15% in
SimpleITK: _ITKCommonPython exports 33,625 symbols from its statically
linked ITK libraries, and the linker must keep them all.
ITK already links its wrapper modules with -flto, so link cost barely
moved: the three largest modules, linked alone, took 1-4% longer and
used the same peak memory (9.05 GB for _ITKImageIntensityPython).

Seventeen Python cases gave bit-identical output in both arms;
BSpline/LBFGSB registration ran 5-7% slower, as it did in SimpleITK,
and the rest stayed within run-to-run noise.

ld64 -dead_strip changed the macOS arm64 wheels by under 0.1% and is
not applied there.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 20, 2026
The wrapper modules link ITK statically, and ITK's static libraries keep
default visibility on Linux, so _ITKCommonPython exported 33,625
symbols. The linker had to keep every one of them, which limited
--gc-sections to a 4% saving. SimpleITK avoids this with a version
script on its Python module
(SimpleITK/SimpleITK#2784 and the visibility
settings in SimpleITK's SuperBuild/External_ITK.cmake); this applies
the same script to every ITK Python module. The modules share ITK's
global state through the _ITKCommonPython capsule, not through
exported symbols, and Python loads them RTLD_LOCAL.

A/B on ITK 957816d, manylinux_2_28, cp311 abi3 (MB):

  wheel                off  --gc-sections  + this
  itk_core            89.5     87.0        70.7
  itk_filtering       89.3     86.4        67.8
  itk_filtering_extra 60.3     57.8        39.3
  itk_numerics        64.7     62.9        52.3
  itk_io              36.9     34.9        17.6
  itk_registration    37.3     36.0        27.6
  itk_segmentation    19.7     18.9        14.3

Each module now exports 84 symbols. Seventeen Python cases gave
bit-identical output; the BSpline/LBFGSB slowdown seen with
--gc-sections alone is gone (2% faster than off), and the rest stayed
within run-to-run noise. Thread defaults, the global warning flag,
object factories, observers, down-casts, exceptions, and a pipeline
through four modules all behave across modules as before.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 20, 2026
Port of the SimpleITK_DEAD_STRIP option from SimpleITK
(SimpleITK/SimpleITK#2784, which followed the
link-time optimization study in
SimpleITK/SimpleITK#2772). On Linux it compiles
with -ffunction-sections -fdata-sections and links with gold
--gc-sections --icf=safe. Off by default; other platforms are unchanged.

A/B on ITK 957816d, manylinux_2_28 (GCC 14.2, gold 1.15), cp311 abi3:

  wheel                 off      on
  itk_core            89.5 MB  87.0 MB
  itk_filtering       89.3 MB  86.4 MB
  itk_filtering_extra 60.3 MB  57.8 MB
  itk_numerics        64.7 MB  62.9 MB

.text across the 96 wrapper modules fell 4.4%, against 15% in
SimpleITK: _ITKCommonPython exports 33,625 symbols from its statically
linked ITK libraries, and the linker must keep them all.
ITK already links its wrapper modules with -flto, so link cost barely
moved: the three largest modules, linked alone, took 1-4% longer and
used the same peak memory (9.05 GB for _ITKImageIntensityPython).

Seventeen Python cases gave bit-identical output in both arms;
BSpline/LBFGSB registration ran 5-7% slower, as it did in SimpleITK,
and the rest stayed within run-to-run noise.

ld64 -dead_strip changed the macOS arm64 wheels by under 0.1% and is
not applied there.
hjmjohnson added a commit to BRAINSia/ITKPythonPackage that referenced this pull request Nov 20, 2026
The wrapper modules link ITK statically, and ITK's static libraries keep
default visibility on Linux, so _ITKCommonPython exported 33,625
symbols. The linker had to keep every one of them, which limited
--gc-sections to a 4% saving. SimpleITK avoids this with a version
script on its Python module
(SimpleITK/SimpleITK#2784 and the visibility
settings in SimpleITK's SuperBuild/External_ITK.cmake); this applies
the same script to every ITK Python module. The modules share ITK's
global state through the _ITKCommonPython capsule, not through
exported symbols, and Python loads them RTLD_LOCAL.

A/B on ITK 957816d, manylinux_2_28, cp311 abi3 (MB):

  wheel                off  --gc-sections  + this
  itk_core            89.5     87.0        70.7
  itk_filtering       89.3     86.4        67.8
  itk_filtering_extra 60.3     57.8        39.3
  itk_numerics        64.7     62.9        52.3
  itk_io              36.9     34.9        17.6
  itk_registration    37.3     36.0        27.6
  itk_segmentation    19.7     18.9        14.3

Each module now exports 84 symbols. Seventeen Python cases gave
bit-identical output; the BSpline/LBFGSB slowdown seen with
--gc-sections alone is gone (2% faster than off), and the rest stayed
within run-to-run noise. Thread defaults, the global warning flag,
object factories, observers, down-casts, exceptions, and a pipeline
through four modules all behave across modules as before.
blowekamp added a commit that referenced this pull request Nov 20, 2026
Follow-up to #2784, which added the option but left packaging/Docker
scripts untouched. Verified by building the cp311 abi3 wheel with the
manylinux_2_28_x86_64 release image (Elastix on, matching the nightly
pipeline config): wheel shrinks 61.81 MB -> 52.17 MB (-15.6%),
_SimpleITK.abi3.so shrinks 302.93 MB -> 260.17 MB (-14.1%), and all
400 ctest cases (including the Elastix examples) pass.
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.

3 participants