Skip to content

feat: add lake profile command#12545

Draft
kim-em wants to merge 4 commits intomasterfrom
lake-profile
Draft

feat: add lake profile command#12545
kim-em wants to merge 4 commits intomasterfrom
lake-profile

Conversation

@kim-em
Copy link
Collaborator

@kim-em kim-em commented Feb 18, 2026

This PR adds a lake profile command that records a CPU profile of a Lean executable using
samply, symbolicates addresses, demangles Lean compiler
names, and serves the result to Firefox Profiler.

This replaces the Python scripts in script/profiler/ with a single Lake command.

Depends on #12539.

Walkthrough

From the lean4 repo:

$ cd tests/bench/mergeSort
$ lake profile mergeSort -- 10
Building mergeSort
Recording profile (rate=1000 Hz)...
Starting symbolication server...
Symbolicating and demangling...
Demangled 335 names, wrote /tmp/lake-profile-XXXXXX/profile-demangled.json.gz

Serving profile on port 3757...
If working over SSH, forward the port:
  ssh -L 3757:127.0.0.1:3757 <host>

Open in Firefox Profiler:
  https://profiler.firefox.com/from-url/http%3A%2F%2F127.0.0.1%3A3757%2Fprofile.json.gz

Press Ctrl+C to stop.

Open the Firefox Profiler URL in your browser. Function names like
List.MergeSort.Internal.mergeSortTR₂ appear instead of their mangled forms.

Options:

  • --rate N — sampling rate in Hz (default 1000)
  • --output FILE — save to a specific path
  • --raw — skip symbolication and demangling

🤖 Prepared with Claude Code

kim-em and others added 2 commits February 18, 2026 00:32
This PR replaces three independent name demangling implementations (Lean,
C++, Python) with a single source of truth in Lean. The new
`Lean.Compiler.NameDemangling` module handles the full pipeline: prefix
parsing, postprocessing (suffix flags, private names, hygienic stripping,
specialization contexts), backtrace line parsing, and C exports. The C++
runtime backtrace handler now calls `@[export]`ed Lean functions instead
of its own reimplementation. The Python profiler demangler is replaced
with a thin subprocess wrapper around a Lean CLI tool.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…n link dependency

The `@[export lean_demangle_bt_line_cstr]` function is defined in
`Lean.Compiler.NameDemangling` (in `libLean`) but called from `libleanrt`
(`print_backtrace`). On Linux these are in separate linker groups, so
downstream executables fail to link with undefined reference.

Use a weak symbol with a no-op stub so `leanrt` doesn't require `libLean`
at link time. When the Lean demangler is linked in (as it always is for
Lean executables), the real implementation overrides the stub.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@kim-em kim-em added the changelog-lake Lake label Feb 18, 2026
@kim-em kim-em force-pushed the lake-profile branch 4 times, most recently from f9ee312 to bddfb42 Compare February 18, 2026 05:28
@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 Feb 18, 2026
@mathlib-lean-pr-testing
Copy link

mathlib-lean-pr-testing bot commented Feb 18, 2026

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase e9cc84b7c99f64220c952dee026ac0f06a031fc4 --onto 91bd6e19a7b7aca769f9720e1127c768df0cd2a7. You can force Mathlib CI using the force-mathlib-ci label. (2026-02-18 06:15:49)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase c948d24b6ddbb41c199c13edc9ebc04104a667bb --onto 333ab1c6f0ce11f33551d6a736054cb72812cad9. You can force Mathlib CI using the force-mathlib-ci label. (2026-03-07 01:05:08)

@leanprover-bot
Copy link
Collaborator

leanprover-bot commented Feb 18, 2026

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase e9cc84b7c99f64220c952dee026ac0f06a031fc4 --onto 5c7a508e21c5ebc710b0dfe65648d5beba4e34e0. You can force reference manual CI using the force-manual-ci label. (2026-02-18 06:15:50)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase c948d24b6ddbb41c199c13edc9ebc04104a667bb --onto a165292462a973c20d3cc8c8b23a3ac2334a2a4a. You can force reference manual CI using the force-manual-ci label. (2026-03-07 01:05:10)

@kim-em kim-em force-pushed the lake-profile branch 2 times, most recently from 1e615a4 to 859a35a Compare February 18, 2026 08:01
This PR adds a `lake profile` command that builds an executable target,
records a CPU profile with samply, symbolicates addresses via samply's
API, demangles Lean compiler names using `Lean.Name.Demangle.demangleSymbol`,
and writes a Firefox Profiler JSON file.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
# Conflicts:
#	script/profiler/lean_demangle.py
#	src/Lean/Compiler/NameDemangling.lean
#	src/runtime/object.cpp
#	tests/elab/demangling.lean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-lake Lake 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.

2 participants