Emscripten wasm64 support and ~1 MB browser Worker stack limits
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start with src/CMakeLists.txt, src/runtime/thread.cpp, src/runtime/io.cpp, and src/util/shell.cpp; the issue also links scripts/patches/ and docs/WASM64-SPIKE.md with reproductions for the stack patches. Read the linked spike notes and inspect the listed browser Worker constraints and Lean call paths. Done means maintainers decide which Emscripten changes fit upstream and whether the iterative stack patches are in scope.
Written by the indexing model from the issue text.
Description
We have been testing a wasm64 (-sMEMORY64=1, USE_GMP=ON) Emscripten build of Lean v4.36.0-rc2 running --worker inside browser Dedicated Workers, and wanted to ask maintainers before opening small PRs—and see whether anything around the browser stack issue is even in scope upstream.
(Checked master first: LEAN_SCALAR_PTR_LITERAL, lean_kernel_diag_is_enabled, and get_io_uint32_result are already fixed on master, so we only backport those to v4.36.0-rc2.)
1. Small Emscripten diffs we'd like to PR (if welcome)
src/CMakeLists.txt: add an opt-in CMake flag (off by default) for-sMEMORY64=1(and-mtail-callif desired), keeping the defaultwasm32build unchanged.src/runtime/thread.cpp:#if defined(LEAN_EMSCRIPTEN) || UINTPTR_MAX == UINT32_MAXsetsLEAN_DEFAULT_THREAD_STACK_SIZEto8*1024*1024even underwasm64(UINTPTR_MAX == UINT64_MAX). Right now-DLEAN_DEFAULT_THREAD_STACK_SIZE=...works as an override, or we could split the 32-bit vs 64-bit Emscripten branch cleanly.src/runtime/io.cpp&src/util/shell.cpp: with-sPROXY_TO_PTHREAD=1and-sEXIT_RUNTIME=0(needed in browser workers becauseEXIT_RUNTIME=1runs static destructors from the main thread and hitsAtomics.wait/ pthread lock traps),lean_io_exitcallingexit(code)tears down the runtime. Throwing an exit exception caught inrun_shell_main+lean_mark_mt(shell_opts.raw())beforerun_with_thread_stackavoids this.- IR interpreter lookup for
@[extern]in static builds: in statically linked Emscripten builds withoutdlsym(using a generatedbsearchtable of.cexports),@[extern]C++ functions likeIO.getRandomBytes(lean_io_get_random_bytes) don't emitl_*___boxedwrappers in generated.c, solookup_symbol_in_cur_exereturnsNULLwhen interpreted code reaches them.
2. Why we're currently carrying larger stack patches (and the cost)
Chrome Dedicated Workers give Wasm ~1 MB of native/JS call stack, regardless of -sSTACK_SIZE or -sDEFAULT_PTHREAD_STACK_SIZE. Measurements in HeadlessChrome 157 (emsdk 6.0.0):
- Micro-probes (
depth-probe):- Non-tail C++ recursion: 7,974 frames (~121 B/frame).
- Mutual tail recursion with
-mtail-call(return_call): >= 4,194,304. - Heap-allocated work stack: >= 1,048,576.
- ASYNCIFY (yield every 64 frames; 1 / 8 / 32 MB buffers): 24,453–24,455; yield every 4096 frames: 8,191.
- Emscripten JSPI (
-sJSPI=1,emscripten_sleep(0)every 64 frames): 15,359 (identical with-sSTACK_SIZE=1 MBand8 MB); bare WAT JSPI: 17,258 (V8--wasm-stack-switching-stack-sizedefault is 984 kB).
whnfCore(WHNF.leancompiled withlean -c+emcc -c -O2 -mtail-call+wasm2wat): 550 functions and 173return_calltotal, but 0return_callin the hot loop (lean_whnf1,024 lines / 0,whnfEasyCases8,026 lines / 0,reduceRec___redArg5,458 lines / 0,reduceQuotRec___redArg1,989 lines / 0) becausewhnfImp/reduceRecconsume recursive results before returning. In Chrome this traps aroundN ≈ 1200.replace_fn.cpp:N = 800trapped inLean_Expr_instantiateLevelParams -> lean::replace_fn::apply. On host (g++,ulimit -s 1024), recursivereplace_fnsegfaults atN = 10,000; our iterativestd::vector<frame>version (computingis_likely_unsharedbefore pushing) passes atN = 1,000,000.Simp/Main.lean(simpLoop): after fixingreplace_fn,N = 800still trapped (84simpLoop/simpLoop_visitPreContinuepairs visible in a 200-frame trace) due to non-tailmkEqTrans+cacheResult.
We patched all three locally (lean-replace-fn-iterative.patch, lean-whnf-stack.patch, lean-simp-loop-stack.patch).
Side effect on .olean compatibility:
An unmodified v4.36.0-rc2 wasm64 build with USE_GMP=ON matches the official Linux x86_64 .olean header (flags=1, githash), so in principle it can reuse official prebuilt .oleans without rebuilding them (this works for Init/Std/Lean). However, once we patch WHNF.lean and Simp/Main.lean to work around the 1 MB Worker stack, our build diverges from upstream releases and can no longer cleanly drop in prebuilt downstream caches (for example, testing official lake exe cache get Mathlib .oleans fails with (interpreter) unknown declaration 'Lean.Option.register._at_._private.Lean.Meta.Tactic.Simp.SimpTheorems.0...spec_0', whereas self-built Mathlib slices compile and pass LSP diagnostics).
Questions
- Would small PRs for the CMake
MEMORY64flag,thread.cpp64-bit Emscripten stack default, and/orEXIT_RUNTIME=0lean_io_exithandling be accepted? - Is there any appetite upstream for iterative
replace_fn.cpp/whnfCore/simpLoop(assuming native benchmarks show no regression), or should those stay downstream?
Reproducer / patches (scripts/patches/, docs/WASM64-SPIKE.md): https://github.com/3y3y3y-huaiji/lean-init-ide/tree/spike/wasm64
Live browser testbed (wasm32 default, experimental wasm64 toggle in toolbar): https://criticalthinking.de5.net
(Note: This issue was translated and organized with an LLM; all measurements, logs, and technical facts have been verified by a human.)
- Dominant language
- Lean
- Stars
- 10.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16257 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
vllm-project/vllm-sail#398 ·
Maintainers usually reply within 1 day
-
kubernetes-ingestor: fetchCRDMapping() runs even when crossplane.enabled is false, logging a 403 ERROR every runPossibly taken @kc9ddi claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
TeraSky-OSS/backstage-plugins#228 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
nightscout/nocturne#4057 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
opendatalab/MinerU#5800 · 2 comments ·
Maintainers usually reply within 1 day