Skip to content

ENH: Add SimpleITK_INTERPROCEDURAL_OPTIMIZATION option - #2766

Closed
blowekamp wants to merge 2 commits into
SimpleITK:mainfrom
blowekamp:enh/interprocedural-optimization
Closed

blowekamp wants to merge 2 commits into
SimpleITK:mainfrom
blowekamp:enh/interprocedural-optimization

Conversation

@blowekamp

@blowekamp blowekamp commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Add a SimpleITK_INTERPROCEDURAL_OPTIMIZATION CMake option (default OFF) that enables LTO for Release, RelWithDebInfo and MinSizeRel builds. It applies to ITK in both the FetchContent and SuperBuild paths.

The second commit enables the option in a single CI entry (mac-arm64-elastix-python) so it is exercised in CI. That entry runs on a self-hosted runner, so links are not serialized.

Design
  • check_ipo_supported() (C and CXX) is run when the option is ON; configuration fails if IPO is not supported.
  • CMAKE_INTERPROCEDURAL_OPTIMIZATION_<CONFIG> is set before ITK is added so FetchContent ITK inherits it.
  • The SuperBuild includes the same module and forwards the IPO variables to ITK, Elastix and SimpleITK.
  • The standalone language wrapping projects (e.g. Python, Java, CSharp built against an existing SimpleITK build) include the same module, so they honor the option too.
Local measurements (AppleClang 21, arm64, static, Release, no Elastix)
No LTO LTO
Build wall time 368 s 934 s
Static libs 1.0 GB 2.3 GB
Example executables (sum) 1462 MB 1306 MB (-11%)
HelloWorld 39.3 MB 35.1 MB

Runtime: about 22% faster affine MeanSquares registration and 7-8% faster small-image filter calls. Large-image filters were unchanged.

Testing

Configure-only checks of the FetchContent and SuperBuild paths: -flto=thin is present for ITK and SimpleITK objects when ON, absent when OFF, and the IPO variables reach the ITK and SimpleITK external projects.

CI experience: enabling IPO in all Build.yml entries made hosted and Linux jobs take 2-3+ hours longer (e.g. linux-arm with the gold linker 12 min -> 3h15; macos-26 1h25 -> 3h34; mac-arm64-elastix 22 min -> 1h19), and some jobs were killed or timed out. MSVC (v142 and v143 ARM64) hits an internal compiler error (C1001) linking ITK's itkTestDriver with link-time code generation, and a GCC full-IPO link of the Python/Java wrappers with Elastix needs about 19 GB of RAM, which exceeds the 16 GB runners. Hence only one CI entry uses IPO here. The experiment of enabling IPO in the release packaging workflow is in #2772.

AI assistance

Developed with GitHub Copilot assistance; the changes and measurements were reviewed by the author.

@blowekamp
blowekamp force-pushed the enh/interprocedural-optimization branch from e4b2010 to 27d129b Compare October 22, 2026 18:13
Add an option, default OFF, to enable interprocedural optimization (LTO)
for Release, RelWithDebInfo and MinSizeRel builds. Support is verified
with check_ipo_supported. The CMAKE_INTERPROCEDURAL_OPTIMIZATION_<CONFIG>
variables are set before ITK is added so FetchContent builds of ITK use
them, and they are forwarded to the ITK, Elastix and SimpleITK projects
in the SuperBuild. Standalone language wrapping projects include the same
module so they link against an IPO build of SimpleITK with IPO enabled.

Co-authored-by: Copilot <[email protected]>
@blowekamp
blowekamp force-pushed the enh/interprocedural-optimization branch from 27d129b to c567f28 Compare October 27, 2026 13:24
@blowekamp
blowekamp requested review from dave3d and a balanced review from Copilot October 27, 2026 14:10
@blowekamp
blowekamp marked this pull request as ready for review October 27, 2026 14:10

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

Disabling IPO after an enabled SuperBuild leaves cached IPO settings active in existing child builds.

1 open issue
What changed in this PR

Adds opt-in interprocedural optimization (LTO) to SimpleITK’s build system, including dependency builds and standalone language wrappers.

Changes:

  • Checks compiler IPO support and enables it for optimized build configurations.
  • Propagates IPO settings through FetchContent and SuperBuild.
  • Exercises IPO in one macOS ARM64 CI entry with serialized linking and documents usage.
File Description
SuperBuild/​SuperBuild.cmake Loads the IPO module and forwards settings to child projects.
docs/​source/​building.rst Documents the option, build costs, and standalone wrapper usage.
CMakeLists.txt Initializes IPO before adding ITK.
CMake/​sitkProjectLanguageCommon.cmake Adds IPO support to standalone language wrappers.
CMake/​sitkInterproceduralOptimization.cmake Defines the option, checks support, and enables optimized configurations.
.github/​workflows/​Build.yml Enables IPO and serialized linking in one CI entry.
.github/​workflows/​additional_dictionary.txt Adds “interprocedural” to the spelling dictionary.

Review effort: Balanced

@blowekamp
blowekamp force-pushed the enh/interprocedural-optimization branch 2 times, most recently from 1128dc4 to c567f28 Compare October 27, 2026 14:51
Only one entry uses IPO to limit CI time, since IPO builds took 2-3
extra hours on hosted and Linux runners. This entry runs on a
self-hosted runner, so links are not serialized.

Co-authored-by: Copilot <[email protected]>
@blowekamp
blowekamp force-pushed the enh/interprocedural-optimization branch from c567f28 to 4cbca79 Compare October 27, 2026 19:41
@blowekamp

Copy link
Copy Markdown
Member Author

The IPO linking is not working out of the box with the self hosted runner with generous resources. This option can be directly enable through the CMAKE_INTERPROCEDURAL_OPTIMIZATION_{config} variables. This does not need a special SimpleITK option.

@blowekamp blowekamp closed this Oct 28, 2026
blowekamp added a commit that referenced this pull request Nov 4, 2026
Add CMAKE_INTERPROCEDURAL_OPTIMIZATION and the per-configuration
CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE/RELWITHDEBINFO/MINSIZEREL
variables to ep_common_list so that IPO/LTO settings passed to the
top-level SuperBuild configure are forwarded to the ITK, Elastix,
SimpleITKExamples, Lua, and SimpleITK sub-builds. Previously these
cache variables were not propagated at all, so e.g.
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE=ON set on the SuperBuild
configure silently had no effect on the sub-projects.

Debug is intentionally excluded, since IPO is not meaningful for
debug builds.

This is a narrower follow-up to the discussion in #2766, which
proposed a full SimpleITK_INTERPROCEDURAL_OPTIMIZATION option but was
closed due to CI cost; this change only wires up propagation of the
variables CMake itself already recognizes, without adding any new
option or CI behavior.
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