perf: fold record-literal projections in the IR optimizer - #157
Merged
Conversation
The new reduceObjectProp rule folds ObjectProp over LiteralObject and ObjectUpdate inside the optimize+dce fixpoint, where PropName keys are static and duplicate labels are ruled out by the type checker, so the fold needs no ambiguity guards. The folded value feeds beta reduction, inlining and DCE: dictionary records that only existed to be projected drop out of the generated Lua together with the wrapper IIFEs around their members (net -205 lines across the Lua goldens; eval oracles unchanged). The Lua-AST rule reduceTableDefinitionAccessor stays: its live trigger is a projection out of a foreign module, whose table constructor only materializes during lowering and is thus invisible to the IR rule. Closes #153
Contributor
There was a problem hiding this comment.
Pull request overview
Moves record-literal projection folding earlier into the IR optimizer so record projections like { foo: 1, bar: 2 }.foo reduce before Lua lowering, enabling additional simplification during the optimize+DCE fixpoint and shrinking generated Lua output.
Changes:
- Add
reduceObjectPropto the IR optimizer rule chain to foldObjectPropoverLiteralObjectand throughObjectUpdate. - Add unit + end-to-end tests exercising the new rule (including the inliner-created redex case).
- Update Lua optimizer Haddocks and refresh affected IR/Lua golden outputs; add a changelog fragment for #153.
Reviewed changes
Copilot reviewed 45 out of 54 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| lib/Language/PureScript/Backend/IR/Optimizer.hs | Adds reduceObjectProp and wires it into optimizedExpressionM’s rewrite chain. |
| lib/Language/PureScript/Backend/Lua/Optimizer.hs | Clarifies Haddock: Lua-side projection folding remains for constructors that only appear during lowering. |
| test/Language/PureScript/Backend/IR/Optimizer/Spec.hs | Adds targeted tests for projection folding and a fixpoint/inliner integration test via optimizedUberModule. |
| changelog.d/20260704_210000_unisay_ir_object_prop_fold.md | Documents the IR-level optimization and why the Lua-level rule remains. |
| test/ps/output/Golden.TailRecM2Shadow.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.TailRecM2Shadow.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.StringCodePoints.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.StringCodePoints.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.ProfunctorDictLens.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.ProfunctorDictLens.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.MaybeChainModule.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.MaybeChain.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.LongWriterBind.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongWriterBind.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.LongStateBind.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongReaderBind.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongReaderBind.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.LongDoBlock.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongDoBlock.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.LongCallbackChain.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongCallbackChain.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.LongBindFlipped.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongBindFlipped.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.LongApplyChain.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.LongApplyChain.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.Issue37.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.GenericEqTwoTypes.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.GenericEqTwoTypes.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.FloatIn.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.FloatIn.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.Fibonacci.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.Fibonacci.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.DerivedFunctor.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.DerivedFunctor.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.CharLiterals.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.CharLiterals.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.BugListGenericEq.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.BugListGenericEq.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.Bug1.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.ArrayPatternMatch.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.ArrayPatternMatch.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
| test/ps/output/Golden.ArrayOfUnits.Test/golden.lua | Updated golden Lua output reflecting earlier IR folding and downstream simplifications. |
| test/ps/output/Golden.ArrayOfUnits.Test/golden.ir | Updated golden IR output reflecting record-projection folding. |
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.
Closes #153.
Adds
reduceObjectPropto the IR rule chain:{ foo: 1, bar: 2 }.foofolds to1, and a projection through a record update takes the patched value, or reaches into the updated record when the field is not patched. At this altitude the fold needs none of the guards the Lua-AST version carries.PropNamekeys are static labels with no computed-key form, and the type checker rejects duplicate labels, so a plain lookup is exact. A miss is only possible on ill-typed input, where the rule declines.The payoff of running inside the optimize+dce fixpoint is visible in the goldens. The folded member feeds beta reduction and inlining, so shapes like
(function(dictBind) return M.Control_Bind_bind(dictBind) end)(M.Effect_bindEffect)collapse toM.Control_Bind_bind(M.Effect_bindEffect), and dictionary records that only existed to be projected (Data_Either_functorEither,Data_Either_applyEitherin the LongEitherBind golden, for example) drop out of the output entirely. The Lua goldens shrink by 205 lines net. The IR golden line counts grow, but that is per-site inlining of single members where the previous pipeline duplicated the whole dictionary table per site and folded it later on the Lua AST.One deviation from the issue: it proposed deleting
reduceTableDefinitionAccessoras superseded, but the goldens show a live trigger the IR rule cannot see. A projection out of a foreign module (ObjectProp (ForeignImport ...), e.g.sub = ({ intSub = ... }).intSubfromData.Ring) only materializes a table constructor during lowering, since the member value is raw Lua source. Deleting the rule regressed those sites, so it stays, with its haddock updated to state the division of labour.Testing: six new unit tests for the rule (literal, absent field, one-pass composition with sibling rules, patched and unpatched update projections) plus an end-to-end test through
optimizedUberModulecovering the inliner-created redex. The full suite is green over two consecutive runs after accepting the structural goldens, the IR Optimizer spec was stressed over ten random seeds, and everyeval/golden.txtoracle still matches byte for byte (they were never regenerated).