Title: Golden harness optimizes runnable modules AsModule but codegens AsApplication, leaking dead bindings · Issue #303 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Golden harness optimizes runnable modules AsModule but codegens AsApplication, leaking dead bindings · Issue #303 · purescript-lua/purescript-lua
X Title: Golden harness optimizes runnable modules AsModule but codegens AsApplication, leaking dead bindings · Issue #303 · purescript-lua/purescript-lua
Description: Problem The golden test harness compiles a runnable module (one with an eval/golden.txt) with a different link mode for its optimization phase than for its code-generation phase, and no dead-code-elimination (DCE) pass bridges the two. A...
Open Graph Description: Problem The golden test harness compiles a runnable module (one with an eval/golden.txt) with a different link mode for its optimization phase than for its code-generation phase, and no dead-code-e...
X Description: Problem The golden test harness compiles a runnable module (one with an eval/golden.txt) with a different link mode for its optimization phase than for its code-generation phase, and no dead-code-e...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/303
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Golden harness optimizes runnable modules AsModule but codegens AsApplication, leaking dead bindings","articleBody":"## Problem\n\nThe golden test harness compiles a *runnable* module (one with an `eval/golden.txt`) with a different link mode for its optimization phase than for its code-generation phase, and no dead-code-elimination (DCE) pass bridges the two. As a result, the committed `golden.lua` of a runnable module can contain dead top-level bindings that the real compiler (`Backend.compileModules`) would eliminate. The golden therefore misrepresents the compiler's actual output for such modules. This is a test-harness fidelity gap, not a compiler correctness bug: the production output is correct.\n\nWhy it matters:\n\n- **Runnable golden `.lua` files can carry dead code that the real compiler never emits**, so they misrepresent production output for exactly the modules that also run end-to-end.\n- **A DCE regression could be masked for application-linked modules**: because the harness always optimizes `AsModule`, a binding that should be pruned from an application but is not would still show up in the golden either way, so the golden could not distinguish the two.\n\nThis is currently latent in the committed corpus: the accompanying change padded `DirectiveAccessor` with subtraction (which no rewrite coalesces) specifically so its over-budget premise — and thus the whole test — survives constant-chain reassociation. The gap is structural and will resurface whenever an optimizer improvement folds away a binding's last use site in a runnable golden.\n\n## Background\n\n\"Linking\" here means `Language.PureScript.Backend.IR.Linker.makeUberModule`, which flattens all reachable modules into one `UberModule`. It has two modes: `LinkAsModule`, where every top-level binding is an export (an external consumer may call any of them, so all are roots for DCE), and `LinkAsApplication moduleName entry`, where only the named entry point (e.g. `main`) is a root and everything it does not transitively reach is dead. DCE runs inside the IR optimizer (`optimizedUberModule`), pruning bindings that are not reachable from the uber-module's export set.\n\nProduction compilation, in `Backend.compileModules` (`lib/Language/PureScript/Backend.hs`), links once in the *final* mode and optimizes that:\n\n```haskell\nlet linkedModule = Linker.makeUberModule (linkerMode appOrModule) irModules\n...\nuberModule \u003c- ... optimizedUberModule dataDecls liftedModule ...\nchunk \u003c- Lua.fromUberModule foreignDir needsRuntimeLazy appOrModule uberModule\n```\n\nWhen building an application, `linkerMode appOrModule` is `LinkAsApplication`, so DCE runs with `main` as the sole root and removes anything `main` no longer reaches.\n\nThe golden harness (`test/Language/PureScript/Backend/Lua/Golden/Spec.hs`) does not do this. `compileCorefn` always links `LinkAsModule` and runs the optimizer there:\n\n```haskell\nlet uberModule = Linker.makeUberModule (LinkAsModule uberModuleName) modules\n...\noptimized \u003c- ... optimizedUberModuleChecked dataDecls liftedModule ...\n```\n\nso DCE keys off the *module* export set, where every top-level binding is a root and nothing is pruned. Only later, at code generation, does the harness pick the real mode — and only for the `.lua` golden of a runnable module:\n\n```haskell\nappOrModule \u003c-\n doesFileExist evalGolden \u003c&> \\case\n True -> AsApplication moduleName (PS.Ident \"main\")\n False -> AsModule moduleName\ncfn \u003c- compileCorefn ... -- always AsModule-optimized\ncompileIr appOrModule lua51Limits cfn -- re-linked AsApplication only now\n```\n\n`compileIr` re-links `AsApplication` for code generation but does not re-run the optimizer, so the entry-point-driven DCE that production performs never happens. Any binding that became dead during optimization — because the optimizer folded away its last remaining use site — survives into the generated Lua.\n\n## Approach\n\nMake the harness's `.lua` golden path optimize in the same mode it generates code for, matching `Backend.compileModules`. Either link `compileCorefn` in the real `appOrModule` mode for the `.lua` golden (keeping the always-`AsModule` link only for the `.ir` golden, which deliberately shows full module shape), or route the `.lua` golden through the production `compileModules` pipeline instead of the harness's re-implemented `compileCorefn` + `compileIr`. The latter also closes the standing \"the golden harness re-implements the IR pipeline\" fragility called out in `CLAUDE.md`.\n\n## Prerequisites / Relations\n\nIndependent — no issue must land first, and this blocks no other issue. Surfaced by the constant-chain reassociation work (#235, merged): that change had to pad `DirectiveAccessor` with subtraction to keep the leak from becoming a committed golden. Related to the standing \"the golden harness re-implements the IR pipeline\" note in `CLAUDE.md` (Testing Strategy) — the second fix direction in **Approach** would retire that re-implementation.\n\n## Reproduction\n\nThe `Golden/DirectiveAccessor` module has an `eval/golden.txt`, so the harness generates its `.lua` golden `AsApplication` with entry `main`. Its `mkOps` builds a four-method dictionary padded to sit over the inline-size budget; `main`'s last statement is `(mkOps 2).mul 3 4`. Pad `mkOps`'s methods with *added* constant chains (`… + 10 + 20 + … + 60`):\n\n```purescript\nmkOps n =\n { add: \\a b -> a + b + n + 10 + 20 + 30 + 40 + 50 + 60\n , sub: \\a b -> a - b + n + 10 + 20 + 30 + 40 + 50 + 60\n , mul: \\a b -> a * b + n + 10 + 20 + 30 + 40 + 50 + 60\n , divide: \\a b -> a * 2 + b * 3 + n + 10 + 20 + 30 + 40\n }\n```\n\nWith constant-chain reassociation in the optimizer (folding `+ 10 + 20 + … + 60` to `+ 210`), each method shrinks below the inline-size budget, so `mkOps` gets pasted at the undirected `(mkOps 2).mul` site, the projection resolves, and the whole expression constant-folds to `224`. That was `mkOps`'s only surviving reference, so `mkOps` is now dead.\n\nHarness `actual.lua` (link-as-module optimize, then link-as-application codegen):\n\n```lua\nM.Golden_DirectiveAccessor_Test_mkOps = function(n)\n return {\n add = function(a) return function(b) return a + b + n + 210 end end,\n sub = function(a0) return function(b0) return a0 - b0 + n + 210 end end,\n mul = function(a1) return function(b1) return a1 * b1 + n + 210 end end,\n divide = function(a2)\n return function(b2) return a2 * 2 + b2 * 3 + n + 100 end\n end\n }\nend\nreturn (function()\n local _S_cse357 = Golden_DirectiveAccessor_Test_ops.mul\n local _ = Effect_Console_log(Data_Show_showIntImpl(_S_cse357(6)(7)))()\n local _ = Effect_Console_log(Data_Show_showIntImpl(_S_cse357(2)(3)))()\n local _ = Effect_Console_log(Data_Show_showIntImpl(252))()\n return Effect_Console_log(Data_Show_showIntImpl(224))()\nend)()\n```\n\n`mkOps` is emitted in full, yet nothing references it.\n\nRunning the *production* compiler on the same CoreFn as an application removes it:\n\n```\n$ pslua --ps-output ./output --foreign-path ./foreign \\\n --entry Golden.DirectiveAccessor.Test.main --lua-output-file /tmp/app.lua\n$ grep -c mkOps /tmp/app.lua\n0\n```\n\nThe `main` body of `/tmp/app.lua` is identical to the harness output above (same `252` / `224` folds); only the dead `mkOps` binding differs — present in the golden, absent in real output.\n\n## Verification / Measurement\n\nThe fix is confirmed when, for a runnable golden module whose optimizer folds away a binding's last use site, the harness-emitted `.lua` golden matches the production compiler's output — i.e. contains no top-level binding unreachable from the entry point. Concretely, reuse the reproduction: with `DirectiveAccessor` padded by addition (so `mkOps` becomes dead after reassociation), the regenerated `golden.lua` must contain zero `mkOps` references, matching `grep -c mkOps` on `pslua --entry Golden.DirectiveAccessor.Test.main` output (`0`). A cheap standing guard: assert that every runnable golden's `.lua` has no top-level binding absent from the corresponding production `--entry` build. The `.ir` golden (deliberately `AsModule`) and all eval oracles stay unchanged.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-26T11:37:20Z","url":"https://github.com/purescript-lua/purescript-lua/issues/303"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:236b668c-7b99-5a4c-5c63-16a6b260d382 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B10:EA06E:905EB2:BB86AE:6B00DB73 |
| html-safe-nonce | bc4b064a9626566fbc44dc36faea31a422437be97473f031af666959fa6004d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjEwOkVBMDZFOjkwNUVCMjpCQjg2QUU6NkIwMERCNzMiLCJ2aXNpdG9yX2lkIjoiMTMxMzY1MzMxMzA2MDc4OTEwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1fc044fab6ad3c6351a5b1166e98ab10e743bda3d6ecbf6743dc4aeaedaa92bb |
| hovercard-subject-tag | issue:4980350292 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/303/issue_layout |
| twitter:image | https://opengraph.githubassets.com/61b5a37ffdf297452bdfe4e7990d1e9c35562403988094cd0577156e61ee5b32/purescript-lua/purescript-lua/issues/303 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/61b5a37ffdf297452bdfe4e7990d1e9c35562403988094cd0577156e61ee5b32/purescript-lua/purescript-lua/issues/303 |
| og:image:alt | Problem The golden test harness compiles a runnable module (one with an eval/golden.txt) with a different link mode for its optimization phase than for its code-generation phase, and no dead-code-e... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| go-import | github.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 587200487 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 587200487 |
| octolytics-dimension-repository_network_root_nwo | purescript-lua/purescript-lua |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width