fix(optimizer): canonicalize Effect/ST heads through top-level aliases (#297) - #298
Merged
Merged
Conversation
When one module discards over both ST and Effect, purs CSE floats the shared partial application in two stages (discard = discard discardUnit; discard1 = discard bindST), a shape neither canonicalization tier matched: chains stayed nested past magic-do, and once call-site inlining exposed the canonical pair, tier 2 manufactured a reference to a foreign accessor DCE had already removed — a nil call at runtime. Tier 2 now resolves head positions through the module's top-level bindings before matching the table, and declines a rewrite whose produced reference the module can no longer resolve. The compiler additionally refuses to emit code for an optimized module with dangling imported references (lintDanglingImports); the golden harness applies the same closedness check to every golden module. Fixes #297
Unisay
force-pushed
the
issue-297/canonicalize-through-toplevel-aliases
branch
from
July 24, 2026 17:35
d9bd03b to
74c4fd6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #297.
What was broken
A module that instantiates
discardwith two differentBinddictionaries — Effect or ST plus any second monad; the repro uses ST + Effect, and Effect + Maybe produces the same shape — miscompiled: purs CSE floats the shared partial application in two stages, a shape neither canonicalization tier of #182 matched, so the Effect/ST chains stayed nested past magic-do, and once post-magic-do call-site inlining exposed the canonical pair, tier 2 manufactured a reference to a foreign accessor binding DCE had already removed. The generated Lua read a never-assigned module-table field and crashed:The fix
Tier 2 resolves head positions through top-level aliases (
canonicalizeEffectAppInModule). A reference denotes a row's name if it is that name, or if the top-level binding it names has a right-hand side that denotes it (visited-bounded). This is closed under any CSE split of the structurally bounded spines (bind/pure: two nodes,discard: three), not just the observed two-stage float. Because tier 2 runs in the pre-magic-do fixpoints, the chains flatten again — the repro'sstCountnow compiles to a flat thunk:Tier 2 declines a rewrite whose produced reference would dangle: the rule now consults the module's top-level bindings (
CanonEnv, built peroptimizeModulerun from the Standalone bindings plus the foreigns) and leaves the dictionary application in place when the canonical accessor is gone — sound, since the application it leaves is executable.The compiler refuses to emit code for a non-closed module (
lintDanglingImports, newResultDanglingImportsfailure): any future resurrect-after-DCE fails the build instead of shipping Lua that nil-calls at runtime. The check runs unconditionally incompileModulesand on every golden module in the harness. It is deliberately not a pass-boundary invariant: unit fixtures legitimately reference external names, so closedness is only meaningful for linked modules.Tests
Golden.MixedDiscardFloat.Test— the minimal reproduction (ST discard + Effect discard in one module); its hand-written eval oracle crashed at baseline and passes now, and the goldens pin the flattened output.Optimizer.Spec: the canonicalization block now runs against aCanonEnv(the rule's new contract), plus two new cases — resolution through the two-stage float, and declining when the canonical target has no binding.Linter.Spec:lintDanglingImportscoverage (binding/foreign resolution,Primexemption, per-site reporting).cabal test all: 1055 examples, 0 failures; no existing golden moved.