Skip to content

feat: support serializing closures in CompactedRegion.save#13891

Open
Kha wants to merge 2 commits into
leanprover:masterfrom
Kha:push-rmunlpwkyzmr
Open

feat: support serializing closures in CompactedRegion.save#13891
Kha wants to merge 2 commits into
leanprover:masterfrom
Kha:push-rmunlpwkyzmr

Conversation

@Kha
Copy link
Copy Markdown
Member

@Kha Kha commented May 29, 2026

This PR adds opt-in support for serializing closures (functions with captured values) to .olean files via CompactedRegion.save (allowClosures := true), so a saved function can be loaded back and called, including from a separate process. Regular module data is unaffected and continues to reject closures.

With allowClosures := true the writer emits a new v3 .olean layout, [header][data_size][data][closure offsets][lib relocation table], whose sections are all length-prefixed so the reader parses forward. The relocation table records (base_addr, id) for only the loaded libraries that contain a compacted closure's function pointer; on load each is matched by id against the currently-loaded libraries to compute a per-library address delta that relocates the closures' m_fun pointers under ASLR. A library that is no longer loaded is a hard error rather than a silent mis-relocation.

v3 files are mapped copy-on-write so function pointers are patched in place without modifying the file. The closure-offset list points the loader directly at each m_fun field, avoiding a scan of the compacted region, and a zero closure count lets closure-less v3 files skip the relocation table and get_loaded_libs() entirely. The full pointer-fixup walk still runs only when a dependency region fails to load at its saved base address.

@Kha Kha added the release-ci Enable all CI checks for a PR, like is done for releases label May 29, 2026
@Kha
Copy link
Copy Markdown
Member Author

Kha commented May 29, 2026

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented May 29, 2026

Benchmark results for 91f59d5 against 6de1d62 are in. There are significant results. @Kha

  • build//instructions: -348.1M (-0.00%)

Large changes (2🟥)

  • 🟥 size/all/.cpp//lines: +316.0 (+1.09%)
  • 🟥 size/compile/.out//bytes: +225MiB (+10.81%)

Small changes (1🟥)

  • 🟥 mvcgen/sym/MatchIota/50/vcgen//wall-clock: +10ms (+7.58%)

@Kha Kha force-pushed the push-rmunlpwkyzmr branch from 91f59d5 to 3b65add Compare May 29, 2026 14:27
This PR adds opt-in support for serializing closures (functions with captured values) to `.olean` files via `CompactedRegion.save (allowClosures := true)`, so a saved function can be loaded back and called, including from a separate process. Regular module data is unaffected and continues to reject closures.

With `allowClosures := true` the writer emits a new `v3` `.olean` layout, `[header][data_size][data][closure offsets][lib relocation table]`, whose sections are all length-prefixed so the reader parses forward. The relocation table records `(base_addr, id)` for only the loaded libraries that contain a compacted closure's function pointer; on load each is matched by `id` against the currently-loaded libraries to compute a per-library address delta that relocates the closures' `m_fun` pointers under ASLR. A library that is no longer loaded is a hard error rather than a silent mis-relocation.

`v3` files are mapped copy-on-write so function pointers are patched in place without modifying the file. The closure-offset list points the loader directly at each `m_fun` field, avoiding a scan of the compacted region, and a zero closure count lets closure-less `v3` files skip the relocation table and `get_loaded_libs()` entirely. The full pointer-fixup walk still runs only when a dependency region fails to load at its saved base address.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@Kha Kha added the changelog-library Library label May 29, 2026
@Kha Kha force-pushed the push-rmunlpwkyzmr branch from 3b65add to 438cd4e Compare May 29, 2026 16:11
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label May 29, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 6de1d62f83565e01774358e8dce4a7aa3eebb482 --onto c47a0c7cf035381a2bcdd4cdf2442782eb4a5214. You can force Mathlib CI using the force-mathlib-ci label. (2026-05-29 16:27:34)

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 6de1d62f83565e01774358e8dce4a7aa3eebb482 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force reference manual CI using the force-manual-ci label. (2026-05-29 16:27:36)

@Kha Kha force-pushed the push-rmunlpwkyzmr branch from 438cd4e to 7ad8d96 Compare May 29, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-library Library release-ci Enable all CI checks for a PR, like is done for releases toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants