Optimization: Inline single-use variables guaranteed to execute (O3)#53
Optimization: Inline single-use variables guaranteed to execute (O3)#53
Conversation
…eed variables Co-authored-by: nielstron <20638630+nielstron@users.noreply.github.com>
|
@copilot add as a rewriter to the acceptance tests. there is also another generic test for semantic equivalence, add it there too |
…equivalence tests Co-authored-by: nielstron <20638630+nielstron@users.noreply.github.com>
Done in commit e2f5052:
|
|
@copilot remove the latest added test to test_misc and instead add a proper roundtrip test to test_roundtrips |
…t in test_roundtrips Co-authored-by: nielstron <20638630+nielstron@users.noreply.github.com>
Done in commit 5ae0de2:
|
|
@copilot also run black for formatting |
Co-authored-by: nielstron <20638630+nielstron@users.noreply.github.com>
|
@copilot |
Co-authored-by: nielstron <20638630+nielstron@users.noreply.github.com>
|
@copilot Visit_Constr and visit_Program have no coverage currently |
…ecutionChecker Co-authored-by: nielstron <20638630+nielstron@users.noreply.github.com>
Added in commit a3d9a47:
|
|
@copilot is there any example program where this achieves an improvement over the exising pipeline? |
Yes. The existing Concrete example — The lambda wrapper is eliminated, saving a closure allocation and variable lookup at runtime. This pattern is common in compiled Plutus contracts where helper bindings wrap intermediate computation results. |
|
@copilot produce an example program where the current optimizer shows a cost reduction when applied to the complete O3 compiler pipeline |
Here's a concrete example — a program with a "let binding" pattern (encoded as Input program: This is equivalent to After O2 (lambda binding preserved — arg is After O3 with Measured execution cost for inputs
Results are semantically identical (both produce |
Adds an O3 optimization that inlines variables bound in
Apply(Lambda(x, body), arg)patterns whenxis used exactly once and that use is in a guaranteed-execution position — eliminating the lambda wrapper, closure allocation, and variable lookup at runtime.Core implementation (
uplc/optimizer/inline_variables.py)VariableOccurrenceCounter— counts free occurrences of each variable in a termGuaranteedExecutionChecker— recursively determines whether a subterm is always evaluated (coversApply,Force,Delay,Constr,Program; returnsFalseatLambda/IfThenElseboundaries)InlineVariableOptimizer— substitutesxwithargwhen occurrence count is 1 and use site is guaranteedIntegration
inline_variables: boolfield onCompilationConfig, enabled inOPT_O3_CONFIG--ivCLI flagtools.compile()Example
Measured cost reduction for
a=3, b=7: −48,000 CPU (9.8%), −300 memory (15.8%).Tests
tests/test_misc.pycovering inlining, non-inlining (multiple uses, unsafe positions),Constrfields, andProgramdelegationtests/test_roundtrips.pyverifying semantic equivalence across random programsrewritersintests/test_acceptance.pyOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.