Repository navigation
ENH: Add SimpleITK_DEAD_STRIP option for distribution builds - #2784
Conversation
fe4d7e9 to
07dab48
Compare
There was a problem hiding this comment.
🟡 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_STRIPoption 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
234dd4a to
1534132
Compare
|
Force-pushed What changed
The edited module was configure-tested on macOS with a scratch project (probe runs once per linker type; flags unchanged). The |
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.
1534132 to
38ca696
Compare
|
Force-pushed What changed
The previous BatchBuild run on |
|
Thank you. |
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.
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.
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.
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.
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.
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.
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.
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.
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.


Adds an advanced
SimpleITK_DEAD_STRIPoption (default OFF) that compiles with-ffunction-sections -fdata-sections, links with--gc-sections, and adds--icf=safewhen 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:.text.gcc_except_tablegold
--icf=allfolds more but segfaults on the first C++ exception crossing the wrapper;--icf=safeonly 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)
reg_2d_euler_mattes_3lvlreg_3d_euler_meansquares_2lvlreg_3d_bspline_corr_lbfgsbreg_2d_demons_100itfilt_gaussian_volfilt_discrete_gaussian_volfilt_curvature_flow_vol_5itfilt_median_r1_volfilt_gradient_magnitude_volfilt_binary_threshold_volfilt_otsu_volfilt_connected_relabel_volfilt_resample_affine_volfilt_n4_2dfilt_canny_2ddispatch_small_add_x1000getarray_vol_x20Minimum 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 useBrainProtonDensitySliceBorder20.png/Shifted13x17y.pngandRA-Float.nrrdfrom 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=safeshows 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 (noperfon this host).Implementation notes
CMake/sitkDeadStrip.cmakeis included from the SuperBuild (before ITK is added, so ITK and the wrappers get the same flags), the top-levelCMakeLists.txt, andsitkProjectLanguageCommon.cmake. It is a no-op on MSVC and Apple (ld64 dead-strips by default; MSVC has/Gy+/OPT:REF).sitkUseGoldLinker.cmake: selecting gold throughCMAKE_LINKER_TYPEafterproject()left CMake passing bfd's--dependency-fileto gold, which rejects it;CMAKE_LINK_DEPENDS_USE_LINKERis now turned off there.--icf=safe, which thecheck_linker_flagprobe picks up.CMAKE_JOB_POOLSpool 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