Skip to content

Remove the register writes no path reads - #216

Draft
hhugo wants to merge 1 commit into
ocaml-community:masterfrom
hhugo:dead-registers
Draft

hhugo wants to merge 1 commit into
ocaml-community:masterfrom
hhugo:dead-registers

Conversation

@hhugo

@hhugo hhugo commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

A backward liveness pass over the DFA that removes the register writes no path reads, run at the end of compile_ir.

The pass

remove_dead ~roots takes as roots the cells the bindings read. A register is live on entering a state if some path from it reads it before writing it: as the source of a transition Copy whose destination is live, or as the source of a final operation writing a root. Liveness is a fixpoint over that equation; then

  1. transition writes to dead registers are dropped,
  2. final operations writing a cell no binding names are dropped,
  3. the cells left are renumbered densely and the bindings remapped.

About a hundred self-contained lines. The liveness it computes is the input #210 needs, so that draft can adopt it when it is rebased.

Where dead writes come from

Rules that a higher-priority rule beats on some or all of their matches: the tag was written on a transition shared with the winning rule's path, and is never read there. Anything that later prunes the automaton also produces them: shortest match (#180), or unused boundary tags if the rest of #193 is revisited.

Verified effect

Each test pattern was generated with and without the pass before being pinned.

  • Partially shadowed capture: Plus 'a', Plus 'b' beats (Plus 'a' as x), (Plus 'b' | Plus 'c') on every 'b' tail. The write of x's end on the 'b' transition goes, the one on 'c' stays, and cells drop from 3 to 2. A runtime test checks x on both tails and the cell count.
  • Fully shadowed rule: no write survives. Its canonical cell stays, since the unreachable action still names it (init_mem 1). Dropping it too would need the PPX to skip the bindings of rules that never accept; left as is for now.

No pre-existing expect block changes: master's automata have no dead writes outside these shapes.

🤖 Generated with Claude Code

A backward liveness pass over the DFA, run at the end of compile_ir. A
register is live at a state if some path from it reads it before writing
it: as the source of a Copy whose destination is live, or of a final
operation writing a cell an action reads. Writes to dead registers are
dropped, as are final operations writing a cell no binding names, and
the cells left are renumbered densely, the bindings following.

Dead writes come from rules that a higher-priority rule beats on some
or all of their matches: the tag was written on a transition shared
with the winning rule's path, but never read there. They also come from
anything that later prunes the automaton, such as shortest match or
unused boundary tags.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant