Skip to content

perf: fold record-literal projections in the IR optimizer - #157

Merged
Unisay merged 1 commit into
mainfrom
issue-153/object-prop-fold
Jul 5, 2026
Merged

perf: fold record-literal projections in the IR optimizer#157
Unisay merged 1 commit into
mainfrom
issue-153/object-prop-fold

Conversation

@Unisay

@Unisay Unisay commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Closes #153.

Adds reduceObjectProp to the IR rule chain: { foo: 1, bar: 2 }.foo folds to 1, 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. PropName keys 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 to M.Control_Bind_bind(M.Effect_bindEffect), and dictionary records that only existed to be projected (Data_Either_functorEither, Data_Either_applyEither in 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 reduceTableDefinitionAccessor as 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 = ... }).intSub from Data.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 optimizedUberModule covering 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 every eval/golden.txt oracle still matches byte for byte (they were never regenerated).

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
@Unisay
Unisay requested a review from Copilot July 4, 2026 19:35
@Unisay Unisay self-assigned this Jul 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reduceObjectProp to the IR optimizer rule chain to fold ObjectProp over LiteralObject and through ObjectUpdate.
  • 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.

@Unisay
Unisay marked this pull request as ready for review July 4, 2026 20:04
@Unisay
Unisay merged commit a2fdf06 into main Jul 5, 2026
3 checks passed
@Unisay
Unisay deleted the issue-153/object-prop-fold branch July 5, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fold record-literal projection in the IR, not on the Lua AST

2 participants