Title: feat(optimizer): distribute an accessor or application into if branches by Unisay · Pull Request #334 · purescript-lua/purescript-lua · GitHub
Open Graph Title: feat(optimizer): distribute an accessor or application into if branches by Unisay · Pull Request #334 · purescript-lua/purescript-lua
X Title: feat(optimizer): distribute an accessor or application into if branches by Unisay · Pull Request #334 · purescript-lua/purescript-lua
Description: Closes #243. Adds pushEliminatorIntoIfBranches to the IR optimizer's rewrite chain: an eliminator applied to a conditional scrutinee — a record field read (ObjectProp), an array read or length (ArrayIndex/ArrayLength), a constructor tag or field read (ReflectCtor/DataArgumentByIndex), or a saturated call (AppN) — distributes into both arms, (if p then a else b).f ==> if p then a.f else b.f and (if p then f else g) x ==> if p then f x else g x. Without the push the conditional sits in expression position, which codegen wraps in an immediately-invoked function (IIFE) allocated and called per evaluation, and the eliminator never reaches the arms where the constructor, projection, and beta folds fire. The new golden Golden.DistributeIntoIf shows all the shapes. A projection over a conditional of known records used to compile to an IIFE plus a field read off its result: local Golden_DistributeIntoIf_Test_pickName = function(b) return ((function() if b then return { name = "big" } else return { name = "small" } end end)()).name end and now distributes, folds each arm's read, and lands in statement position: local Golden_DistributeIntoIf_Test_pickName = function(b) if b then return "big" else return "small" end end A call over a conditional of lambdas was already handled late, at the Lua level, by foldCallThroughScopeCall (the backstop that pushes an applied scope call into its return sites) — but that runs after all IR folds, so it left un-reduced beta redexes allocating a closure per call: local Golden_DistributeIntoIf_Test_applyPicked = function(b) return function(n) if b then return (function(v) return v + 1 end)(n) else return (function(v0) return v0 * 2 end)(n) end end end Distributed at the IR level instead, the pushed call meets betaReduce in the arms: local Golden_DistributeIntoIf_Test_applyPicked = function(b) return function(n) if b then return n + 1 else return n * 2 end end end The arms themselves are never duplicated — each receives one copy of the eliminator — so no gate on the arms is needed. The only syntactically duplicated operands are an application's arguments, admitted by isInlinableValue (references, scalar literals, cheap projection chains: pure and bounded to re-emit, and binder-free, so the copies cannot break the unique-binders invariant). Only the arm that runs evaluates its argument copy, and evaluation order — condition, then arm, then arguments — is the same in both forms. A call whose argument does real work is declined, pinned by the golden's applyExpensive (its argument is a call, weigh(n)), which keeps its IIFE verbatim. The unrestricted transformation (arbitrary consumer contexts) needs the join points of #234 to avoid duplicating the consumer; this rewrite is deliberately the duplication-free subset. The rule subsumes the fold-gated tag-read distribution that reduceKnownConstructor carried since #180 (fire only when every branch folds to a tag string): a tag read now distributes over any conditional and folds in whichever arms turn out to be constructors, so the reflectFoldsThrough guard is deleted. The Eq-against-literal push of #203 keeps its own rule and fold gate unchanged. Golden fallout, all with eval outputs unchanged: NativeLoopsGuard and TailRecM2Shadow move only in IR — their effect-run applications (if b then act else pure unit)(run) now distribute at the IR level, producing exactly the Lua the late backstop already produced. UncurryEffect genuinely improves: distributing the effect-run application turns each arm into a direct tail call, and loopification (which requires the self call in tail position) now converts countdown into a while true loop: local Golden_UncurryEffect_Test_countdown_S_w = function(n) while true do local _ = (function() local _ = Effect_Console_log("tick")() return Effect_Console_log(Data_Show_showIntImpl(n))() end)() if n >= 1 and n ~= 1 then n = n - 1 else return Effect_Console_log("done")() end end end where before the recursion hid behind the conditional-callee shape and stayed a stack-consuming self call: local Golden_UncurryEffect_Test_countdown local Golden_UncurryEffect_Test_countdown_S_w = function(n) local _ = (function() local _ = Effect_Console_log("tick")() return Effect_Console_log(Data_Show_showIntImpl(n))() end)() if n >= 1 and n ~= 1 then return Golden_UncurryEffect_Test_countdown(n - 1)() else return Effect_Console_log("done")() end end Verification: ten focused unit specs (written and confirmed red first) cover each eliminator kind, the fold cascades, and the declined non-trivial argument both through the pipeline and as a direct guard pin; the new golden carries a hand-written eval oracle checked against real execution; the full suite is green, the IR Optimizer group was seed-stressed 12 runs without a failure or hang, and bench/ci counters are unchanged against the committed oracles.
Open Graph Description: Closes #243. Adds pushEliminatorIntoIfBranches to the IR optimizer's rewrite chain: an eliminator applied to a conditional scrutinee — a record field read (ObjectProp), an array read or length ...
X Description: Closes #243. Adds pushEliminatorIntoIfBranches to the IR optimizer's rewrite chain: an eliminator applied to a conditional scrutinee — a record field read (ObjectProp), an array read or len...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/334
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:057300ef-2af7-6021-ff51-e3669b3b8e0c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | EC8C:10DACD:9233A0:C439CB:6B00E484 |
| html-safe-nonce | 31c4b1c69415b4353674b8e00bedb16af346dce4450ae2429f4dc5e9473ab6bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzhDOjEwREFDRDo5MjMzQTA6QzQzOUNCOjZCMDBFNDg0IiwidmlzaXRvcl9pZCI6IjYxNTU5MzM5MDA4MTczNjAwMDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 744259840f4cc407c157d5c57980c4752db093d88a07d7a62849e9be895b466a |
| hovercard-subject-tag | pull_request:4150654895 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/purescript-lua/purescript-lua/pull/334/files |
| twitter:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| og:image:alt | Closes #243. Adds pushEliminatorIntoIfBranches to the IR optimizer's rewrite chain: an eliminator applied to a conditional scrutinee — a record field read (ObjectProp), an array read or length ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width