Emscripten wasm64 support and ~1 MB browser Worker stack limits
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.)
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 19h 8m
- PR unite (30g)
- 421
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16681 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16305 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
leanprover/lean4#16299 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16257 ·
I maintainer di solito rispondono entro 1 giorno
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
leanprover/lean4#15683 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 1 giorno
-
Upgrade Django to 5.2.18Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
blacklanternsecurity/bbot#3678 ·
I maintainer di solito rispondono entro 2 giorni
-
A failed hydrate call can be dropped when the row's wait is ready tooForse già presa @turbot-agent-fleet l’ha presa oggi. Apertaagent:ready
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
turbot/steampipe-plugin-sdk#1018 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
router-for-me/CLIProxyAPI#7976 ·
I maintainer di solito rispondono entro 1 giorno