René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:236b668c-7b99-5a4c-5c63-16a6b260d382
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9B10:EA06E:905EB2:BB86AE:6B00DB73
html-safe-noncebc4b064a9626566fbc44dc36faea31a422437be97473f031af666959fa6004d2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjEwOkVBMDZFOjkwNUVCMjpCQjg2QUU6NkIwMERCNzMiLCJ2aXNpdG9yX2lkIjoiMTMxMzY1MzMxMzA2MDc4OTEwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac1fc044fab6ad3c6351a5b1166e98ab10e743bda3d6ecbf6743dc4aeaedaa92bb
hovercard-subject-tagissue:4980350292
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/303/issue_layout
twitter:imagehttps://opengraph.githubassets.com/61b5a37ffdf297452bdfe4e7990d1e9c35562403988094cd0577156e61ee5b32/purescript-lua/purescript-lua/issues/303
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/61b5a37ffdf297452bdfe4e7990d1e9c35562403988094cd0577156e61ee5b32/purescript-lua/purescript-lua/issues/303
og:image:altProblem 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameUnisay
hostnamegithub.com
expected-hostnamegithub.com
None043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf
turbo-cache-controlno-preview
go-importgithub.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git
octolytics-dimension-user_id131581790
octolytics-dimension-user_loginpurescript-lua
octolytics-dimension-repository_id587200487
octolytics-dimension-repository_nwopurescript-lua/purescript-lua
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id587200487
octolytics-dimension-repository_network_root_nwopurescript-lua/purescript-lua
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5425dc24e8bd8d6fca9641a12afc984825a43875
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/issues/303#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2F303
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2F303
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=purescript-lua%2Fpurescript-lua
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/303
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/303
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/303
purescript-luahttps://github.com/purescript-lua
purescript-luahttps://github.com/purescript-lua/purescript-lua
Codehttps://github.com/purescript-lua/purescript-lua
Issues48 (48)https://github.com/purescript-lua/purescript-lua/issues
Pull requestshttps://github.com/purescript-lua/purescript-lua/pulls
Discussionshttps://github.com/purescript-lua/purescript-lua/discussions
Actionshttps://github.com/purescript-lua/purescript-lua/actions
Projectshttps://github.com/purescript-lua/purescript-lua/projects
Security and qualityhttps://github.com/purescript-lua/purescript-lua/security
Insightshttps://github.com/purescript-lua/purescript-lua/insights
https://github.com/purescript-lua/purescript-lua
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/303
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/303
Golden harness optimizes runnable modules AsModule but codegens AsApplication, leaking dead bindingshttps://github.com/purescript-lua/purescript-lua/issues/303#top
area: irIR / optimizer / DCE / inlinerhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20ir%22
choreMaintenance / infrastructurehttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22chore%22
https://github.com/Unisay
Unisayhttps://github.com/Unisay
on Jul 26, 2026https://github.com/purescript-lua/purescript-lua/issues/303#issue-4980350292
#235https://github.com/purescript-lua/purescript-lua/issues/235
Unisayhttps://github.com/Unisay
area: irIR / optimizer / DCE / inlinerhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20ir%22
choreMaintenance / infrastructurehttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22chore%22
on Jul 26, 2026https://github.com/purescript-lua/purescript-lua/issues/303#event-28497365852
Sign up for freehttps://github.com/signup?return_to=https://github.com/purescript-lua/purescript-lua/issues/303
Sign in to commenthttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/303
area: irIR / optimizer / DCE / inlinerhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20ir%22
choreMaintenance / infrastructurehttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22chore%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.