tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Core: share match arm bodies through lifted continuations (issue #295)

A match on a (String, List<String>) tuple — the HTTP-router shape —
compiled to Core that grew super-linearly with the arms: 9 arms took
52 s and 1.6 GB to `louise run` and printed 400 MB of Core; `louise
test` in a package was OOM-killed. Two compounding defects in the
matrix compiler (lower/pattern.rs):

- simplify projected tuple fields into fresh variables per row, so no
  two rows ever tested the same occurrence and the "does this row
  test the first column?" check answered no for every row but the
  first: every row was copied into every branch of every switch — a
  factorial-size decision tree (even a 4-arm match re-tested the
  scrutinee's fields on every failure edge). Tuple fields are now
  projected once per frame (a (VarId, index) memo), so a column split
  partitions the rows and the tree is linear in the patterns.
- Every leaf re-lowered its arm's body inline (and a guard recompiled
  the remaining rows on its failure edge), multiplying body code
  across leaves. Each arm body and guard is now lowered once into a
  synthetic top-level function (f$matchN, reusing the lambda-lifting
  machinery: the row's bound variables are parameters after the outer
  names lowering the body captures; capabilities and dictionaries are
  ordinary variables), and every leaf emits a direct CallFn to it.
  No new IR nodes; memoised by the body's ExprId and the row's bound
  names, so or-pattern alternatives of one arm share a continuation.

Two kinds of bodies stay inline as before: a body holding a `return`
that leaves the function (ADR 0037) — lifted, its exit would leave the
continuation — and a body that names the function being lowered when
the match is in that function's own body, so self-recursion stays
visible to the simplifier's never-inline-recursive rule, to TRMC, and
to fusion (hiding map's self-call in a continuation made map inlinable
and fusion, which recognises std/list.map calls, stopped firing).

The redundant expand_or in compile is removed: match_ already expands
or-patterns, and expansion is exhaustive.

Measurements (9-arm repro, this host, release build):
- louise run: 52.2 s / 1.59 GB -> 0.11 s / 44 MB, same output
- louise core: 46.2 s / 1.58 GB, 399.5 MB printed -> 0.11 s / 44 MB,
  476 KB printed (the router's own functions: 195 lines, 165 nodes)

Phase 2 of the design (shared row-set continuations) was not needed:
the tree is linear after occurrence sharing and body lifting.

tests/run/match_router.lse pins the repro on both backends, optimised
and not; lowering's decision_trees gains the router shape, and a
size-regression test bounds the 9-arm repro at 5000 Core nodes (165
today). Snapshot drift in lowering/simplify/specialise/trmc/perceus
reviewed by hand: same optimised bodies modulo variable numbering,
single-leaf continuations move-inlined back, lambdas under lifted arms
renamed (sum$lambda1 -> sum$match2$lambda1); TRMC still rewrites
every std list function, fusion and the run tests are unchanged.
Perceus's sized-reuse snapshot loses flip's reuse token only in the
unoptimised shape the snapshots pin; with the optimiser on, the
simplifier move-inlines the single-site continuation and the reuse
fires as before (checked by hand).

ADR 0058 records the decision.

olivier · il y a 1 mois · 0a1c9aed96ebed004128aecd22a962c0c54a6ad3

Parents 3e3cb38

23 fichiers modifiés, 943 ajouts, 366 suppressions

Mcrates/louise_core/src/lower/derive.rs+1−0
Mcrates/louise_core/src/lower/mod.rs+31−1
Mcrates/louise_core/src/lower/pattern.rs+220−7
Mcrates/louise_core/tests/lowering.rs+70−0
Mcrates/louise_core/tests/snapshots/lowering__core_of_a_small_program.snap+24−8
Mcrates/louise_core/tests/snapshots/lowering__decision_trees.snap+269−208
Mcrates/louise_core/tests/snapshots/simplify__case_of_case_folds_a_switch_on_a_switchs_value.snap+10−2
Mcrates/louise_core/tests/snapshots/simplify__case_of_case_substitutes_the_scrutinee.snap+21−10
Mcrates/louise_core/tests/snapshots/simplify__case_of_known_constructor.snap+8−0
Mcrates/louise_core/tests/snapshots/simplify__dead_lets_and_copies.snap+8−0
Mcrates/louise_core/tests/snapshots/simplify__inlines_an_exit_body_only_at_a_tail_site.snap+8−4
Mcrates/louise_core/tests/snapshots/specialise__a_closure_unknown_here_is_left_alone.snap+2−2
Mcrates/louise_core/tests/snapshots/specialise__a_copy_is_shared_per_lambda.snap+28−28
Mcrates/louise_core/tests/snapshots/specialise__specialises_map_on_a_known_lambda.snap+6−6
Mcrates/louise_core/tests/snapshots/trmc__a_function_of_ours_and_two_cells_per_call.snap+1−1
Mcrates/louise_core/tests/snapshots/trmc__std_list_functions.snap+20−19
Mcrates/louise_perceus/tests/snapshots/programs__borrowing_and_reuse_of_a_small_program.snap+1−1
Mcrates/louise_perceus/tests/snapshots/programs__rc_of_a_small_program.snap+17−25
Mcrates/louise_perceus/tests/snapshots/programs__rc_of_early_returns.snap+38−36
Mcrates/louise_perceus/tests/snapshots/programs__sized_reuse_tuples_and_calls.snap+3−8
Adocs/adr/0058-match-continuations.md+113−0