Skip to content

fix: stop truncating kernel counts and indices to 32 bits - #16279

Merged
nomeata merged 8 commits into
masterfrom
joachim/issue12746
Nov 5, 2026
Merged

nomeata merged 8 commits into
masterfrom
joachim/issue12746

Conversation

@nomeata

@nomeata nomeata commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

This PR fixes Expr.hasLooseBVar, Expr.liftLooseBVars, Expr.lowerLooseBVars, task priorities and the kernel's handling of the number of parameters of an inductive declaration, which silently truncated values of 2^32 and above to 32 bits. For example, the kernel accepted an inductive declaration with numParams := 2^32 and recorded numParams = 0, and a task with priority 2^32 - 1 ran synchronously in the caller rather than on a dedicated thread.

The kernel's counts and indices are now size_t, so that any value that fits in memory is represented exactly. The hot bound variable traversals still compute at 32 bits, as their start index is first checked against the 20-bit loose bound variable range. To keep it that way, the kernel is now compiled with -Werror=conversion under Clang, which rejects implicit narrowing and signedness changes; lean.h and gmp.h are exempt. Truncations of 64-bit hashes are intended and are now explicit casts that compute the same values.

This changes neither the .olean format nor any extern "C" signature: these values cross into Lean as boxed Nats. Only C++ signatures internal to the toolchain change, and their headers are not installed. Expr.liftLooseBVars with a shift of 2^32 or more on an expression with loose bound variables now reaches the existing "too many bound variables" panic, like any shift above 2^20.

Closes #12746. This also covers its closed duplicates #13602 and #13618.

🤖 Generated with Claude Code

nomeata and others added 2 commits November 5, 2026 11:01
This PR fixes `Expr.hasLooseBVar`, `Expr.liftLooseBVars`, `Expr.lowerLooseBVars` and the kernel's handling of the number of parameters of an inductive declaration, which silently truncated values of `2^32` and above to 32 bits. For example, the kernel accepted an inductive declaration with `numParams := 2^32` and recorded `numParams = 0`.

The kernel's counts and indices are now `size_t`, so that any value that fits in memory is represented exactly. To keep it that way, the kernel is now compiled with `-Werror=shorten-64-to-32` under Clang. Truncations of 64-bit hashes are intended and are now explicit casts that compute the same values; `lean.h` is exempt from the flag.

This changes neither the `.olean` format nor any `extern "C"` signature: these values cross into Lean as boxed `Nat`s. Only C++ signatures internal to the toolchain change, and their headers are not installed.

Closes #12746

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This PR compiles the kernel with `-Werror=conversion` instead of `-Werror=shorten-64-to-32`, which also rejects implicit narrowing to 8 and 16 bits and signedness changes. `gmp.h` is exempt alongside `lean.h`, as it is not kernel code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nomeata nomeata added the release-ci Enable all CI checks for a PR, like is done for releases label Nov 5, 2026
@nomeata

nomeata commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Nov 5, 2026 •

Copy link
Copy Markdown

Benchmark results for 54c06e5 against c029d91 are in. There are significant results. @nomeata

  • 🟥 build//instructions: +3.9G (+0.04%)

Large changes (2🟥)

  • 🟥 elab/bitvec_zero_counts//instructions: +82.9M (+0.46%)
  • 🟥 elab/trailingZeros//instructions: +63.8M (+0.31%)

Medium changes (4🟥)

  • 🟥 elab/big_struct_dep//instructions: +105.5M (+1.17%)
  • 🟥 elab/grind_field_kernel_cancel//instructions: +266.9M (+0.80%)
  • 🟥 elab/whnfMatcherImplicitTransparencyCaching//instructions: +24.6M (+0.39%)
  • 🟥 misc/leanchecker --fresh Init//instructions: +1.8G (+0.74%)

Small changes (1✅, 64🟥)

  • 🟥 elab/bv_stress_structures_2//instructions: +52.3M (+0.40%)
  • 🟥 elab/cbv_divisors//instructions: +112.5M (+0.38%)
  • 🟥 elab/cbv_leroy//instructions: +185.3M (+0.54%)
  • 🟥 elab/cbv_merge_sort//instructions: +54.2M (+0.35%)
  • 🟥 elab/cbv_system_f//instructions: +169.2M (+0.26%)
  • 🟥 elab/grind_bitvec2//instructions: +169.2M (+0.15%)
  • 🟥 elab/grind_ring_5//instructions: +2.5M (+0.18%)
  • 🟥 elab/mut_rec_wf//instructions: +37.2M (+0.24%)
  • 🟥 elab/omega_stress//instructions: +6.9M (+0.28%)
  • 🟥 elab/reduceMatch//instructions: +31.5M (+0.37%)
  • 🟥 elab/string_simp_ne//instructions: +18.7M (+0.49%)
  • 🟥 misc/import Init.Data.BitVec.Lemmas//instructions: +246.2M (+0.30%)
  • ✅ vcgen/AddSubCancel/200/vcgen//wall-clock: -4ms (-12.12%)
  • 🟥 vcgen/ReaderState/600/kernel//wall-clock: +12ms (+17.28%)
  • and 51 hidden

This PR removes the kernel slowdown introduced by widening the start index of `instantiate`, `lift_loose_bvars`, `lower_loose_bvars` and `has_loose_bvar`, by narrowing the index again once it is known to be below the 20-bit loose bound variable range. The filter that runs on every visited expression then uses the same 32-bit arithmetic as before, and indices of `2^32` and above are still handled correctly by the early return.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 Nov 5, 2026
@nomeata

nomeata commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Nov 5, 2026 •

Copy link
Copy Markdown

Benchmark results for bd44d6c against c029d91 are in. No significant results found. @nomeata

  • ✅ build//instructions: -204.5M (-0.00%)

Small changes (4✅, 1🟥)

  • ✅ elab/bitvec_zero_counts//instructions: -10.9M (-0.06%)
  • ✅ elab/trailingZeros//instructions: -11.0M (-0.05%)
  • ✅ vcgen/AddSubCancel/200/vcgen//wall-clock: -3ms (-11.48%)
  • and 2 hidden

This PR adds `unsigned long long` overloads of the `nat` constructor and comparison operators, as that is what `size_t` is on Windows, so that `nat(n)` and `nat == n` do the right thing for a `size_t` on every platform. This replaces the separate `nat::of_size_t`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nomeata
nomeata marked this pull request as ready for review November 5, 2026 16:21
@nomeata
nomeata requested a review from leodemoura as a code owner November 5, 2026 16:21
This PR replaces the `--system-header-prefix` flags on the kernel target with `#pragma clang diagnostic` around `lean.h` and `gmp.h`. Those flags treated the headers as system headers, which turned off all of their warnings in kernel code, while the pragmas turn off only `-Wconversion`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nomeata nomeata added the changelog-language Language features and metaprograms label Nov 5, 2026
This PR fixes `Task` and `BaseIO` task functions truncating their priority to 32 bits. A priority of `2^32 - 1` ran the task synchronously in the caller, and other priorities of `2^32` and above could end up in the shared thread pool instead of on the dedicated thread that their documentation promises.

This also fixes a cast to `int16_t` in `lean_uint16_of_nat` to be `uint16_t`, which does not change its result.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nomeata
nomeata enabled auto-merge November 5, 2026 17:29
nomeata and others added 2 commits November 5, 2026 17:30
This PR ends the `-Wconversion` exemption in `lean.h` before its existing `-Wunused-parameter` pragma. Popping the diagnostic state at the end of the file also undid that pragma, which has so far applied to everything that includes `lean.h`, and failed the `-Werror` build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nomeata
nomeata added this pull request to the merge queue Nov 5, 2026
Merged via the queue into master with commit de275a3 Nov 5, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-language Language features and metaprograms 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.

proj_idx truncation: size_t → unsigned

2 participants