Skip to content

fix: ensure lean_initialize is called when Lean is only privately imported - #14505

Merged
Kha merged 2 commits into
masterfrom
lean-initialize-module-test
Jul 26, 2026
Merged

Kha merged 2 commits into
masterfrom
lean-initialize-module-test

Conversation

@Kha

@Kha Kha commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

This PR fixes a compiler issue where private imports of the Lean library could lead to segfaults by ensuring the necessary call to lean_initialize happens in each module's initializer when necessary. As a follow-up clean up, the call to lean_initialize_runtime_module is made implicit as well, meaning users of Lean as an FFI library do not need to call these functions themselves anymore.

@Kha
Kha requested review from hargoniX and leodemoura as code owners July 22, 2026 15:34
@Kha

Kha commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

!bench

1 similar comment
@Kha

Kha commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

!bench

@Kha Kha added the changelog-compiler Compiler, runtime, and FFI label Jul 22, 2026
@leanprover-radar

leanprover-radar commented Jul 22, 2026 •

Copy link
Copy Markdown

Benchmark results for 16f8546 against 3259610 are in. (These commits have already been benchmarked in a previous command.) No significant results found. @Kha

  • ✅ build//instructions: -673.2M (-0.01%)

Small changes (2✅, 1🟥)

  • 🟥 build/module/Lean.Compiler.LCNF.EmitC//instructions: +196.0M (+0.95%)
  • ✅ misc/leanchecker --fresh Init//task-clock: -1s (-4.62%)
  • ✅ misc/leanchecker --fresh Init//wall-clock: -1s (-4.58%)

1 similar comment
@leanprover-radar

leanprover-radar commented Jul 22, 2026 •

Copy link
Copy Markdown

Benchmark results for 16f8546 against 3259610 are in. (These commits have already been benchmarked in a previous command.) No significant results found. @Kha

  • ✅ build//instructions: -673.2M (-0.01%)

Small changes (2✅, 1🟥)

  • 🟥 build/module/Lean.Compiler.LCNF.EmitC//instructions: +196.0M (+0.95%)
  • ✅ misc/leanchecker --fresh Init//task-clock: -1s (-4.62%)
  • ✅ misc/leanchecker --fresh Init//wall-clock: -1s (-4.58%)

@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 Jul 22, 2026
@leanprover-bot

leanprover-bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-07-21 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-07-22 16:32:49)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-07-22 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-07-23 13:08:30)

@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Jul 22, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Jul 22, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ✅ Mathlib branch lean-pr-testing-14505 has successfully built against this PR. (2026-07-22 17:32:23) View Log
  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-07-22 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. You can force Mathlib CI using the force-mathlib-ci label. (2026-07-23 13:08:29)

@Kha Kha added the downstream Request a downstream-lean4 adaptation PR. label Jul 23, 2026
@Kha

Kha commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

!bench mathlib

@leanprover-radar

leanprover-radar commented Jul 23, 2026 •

Copy link
Copy Markdown

Benchmark results for leanprover-community/mathlib4-nightly-testing@8b920c2 against leanprover-community/mathlib4-nightly-testing@eaeb601 are in. No significant results found. @Kha

  • 🟥 build//instructions: +4.7G (+0.00%)

No significant changes detected.

@downstream-lean4

downstream-lean4 Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

The adaptation PR for this PR is leanprover/downstream-lean4#9.

Comment thread src/Lean/Compiler/LCNF/EmitC.lean Outdated
… imported

When a `module` privately imports (parts of) `Lean` and that module is used by an executable whose root module does not visibly import `Lean`, the emitted `main` called only `lean_initialize_runtime_module`, so the C++ components of the Lean package (kernel, library, ...) were never initialized and the executable crashed at startup.

Localize the initialization decision into each module: the generated initializer of every module whose environment shows runtime use of the `Lean` package now calls `lean_initialize` itself (except for modules of the core packages, which `lean_initialize` initializes), and the emitted `main` always calls just `lean_initialize_runtime_module`. Make `lean_initialize` and `lean_initialize_runtime_module` idempotent so repeated calls from several module initializers are no-ops. The LLVM emitter keeps the previous `main`-level check as it has no module system support.

Also add a regression test building and running such an executable.

Co-Authored-By: Claude


feat: initialize the Lean runtime in every generated module initializer

Every generated module initializer now calls `lean_initialize_runtime_module` (or, for modules using the `Lean` package, the full `lean_initialize`) before anything else, so external users of a Lean library no longer have to remember calling it before the library's initializer. Both functions are idempotent, so the emitted `main` and existing embedders that already call them are unaffected.

Co-Authored-By: Claude


doc: drop manual runtime initialization from the reverse FFI example

Module initializers now initialize the Lean runtime themselves, so the example no longer needs to call `lean_initialize_runtime_module` first; a comment notes the call is still required on older toolchains. This also makes the example test verify the new self-initialization behavior.

Co-Authored-By: Claude
@Kha
Kha force-pushed the lean-initialize-module-test branch from 09e325e to 7983e94 Compare July 26, 2026 13:50
@Kha
Kha enabled auto-merge July 26, 2026 13:50
…d `main`

The module initializer that `main` calls initializes the runtime itself, so `main` no longer needs to call `lean_initialize_runtime_module` beforehand.

Co-Authored-By: Claude
@Kha
Kha force-pushed the lean-initialize-module-test branch from 7983e94 to 63a979f Compare July 26, 2026 13:51
@Kha
Kha added this pull request to the merge queue Jul 26, 2026
Merged via the queue into master with commit 0bfc3ac Jul 26, 2026
19 checks passed
robsimmons pushed a commit that referenced this pull request Jul 29, 2026
… imported (#14505)

This PR fixes a compiler issue where private imports of the `Lean`
library could lead to segfaults by ensuring the necessary call to
`lean_initialize` happens in each module's initializer when necessary.
As a follow-up clean up, the call to `lean_initialize_runtime_module` is
made implicit as well, meaning users of Lean as an FFI library do not
need to call these functions themselves anymore.
@Kha
Kha deleted the lean-initialize-module-test branch August 9, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-compiler Compiler, runtime, and FFI downstream Request a downstream-lean4 adaptation PR. mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN 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.

4 participants