Emscripten wasm64 support and ~1 MB browser Worker stack limits

Aperta
#16,564 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Ambito
backend, web-dev

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)
  1. src/CMakeLists.txt: add an opt-in CMake flag (off by default) for -sMEMORY64=1 (and -mtail-call if desired), keeping the default wasm32 build unchanged.
  2. src/runtime/thread.cpp: #if defined(LEAN_EMSCRIPTEN) || UINTPTR_MAX == UINT32_MAX sets LEAN_DEFAULT_THREAD_STACK_SIZE to 8*1024*1024 even under wasm64 (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.
  3. src/runtime/io.cpp & src/util/shell.cpp: with -sPROXY_TO_PTHREAD=1 and -sEXIT_RUNTIME=0 (needed in browser workers because EXIT_RUNTIME=1 runs static destructors from the main thread and hits Atomics.wait / pthread lock traps), lean_io_exit calling exit(code) tears down the runtime. Throwing an exit exception caught in run_shell_main + lean_mark_mt(shell_opts.raw()) before run_with_thread_stack avoids this.
  4. IR interpreter lookup for @[extern] in static builds: in statically linked Emscripten builds without dlsym (using a generated bsearch table of .c exports), @[extern] C++ functions like IO.getRandomBytes (lean_io_get_random_bytes) don't emit l_*___boxed wrappers in generated .c, so lookup_symbol_in_cur_exe returns NULL when 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 MB and 8 MB); bare WAT JSPI: 17,258 (V8 --wasm-stack-switching-stack-size default is 984 kB).
  • whnfCore (WHNF.lean compiled with lean -c + emcc -c -O2 -mtail-call + wasm2wat): 550 functions and 173 return_call total, but 0 return_call in the hot loop (lean_whnf 1,024 lines / 0, whnfEasyCases 8,026 lines / 0, reduceRec___redArg 5,458 lines / 0, reduceQuotRec___redArg 1,989 lines / 0) because whnfImp/reduceRec consume recursive results before returning. In Chrome this traps around N ≈ 1200.
  • replace_fn.cpp: N = 800 trapped in Lean_Expr_instantiateLevelParams -> lean::replace_fn::apply. On host (g++, ulimit -s 1024), recursive replace_fn segfaults at N = 10,000; our iterative std::vector<frame> version (computing is_likely_unshared before pushing) passes at N = 1,000,000.
  • Simp/Main.lean (simpLoop): after fixing replace_fn, N = 800 still trapped (84 simpLoop / simpLoop_visitPreContinue pairs visible in a 200-frame trace) due to non-tail mkEqTrans + 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
  1. Would small PRs for the CMake MEMORY64 flag, thread.cpp 64-bit Emscripten stack default, and/or EXIT_RUNTIME=0 lean_io_exit handling be accepted?
  2. 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.