Skip to content

Add a constant folding pass to UPLC - #7764

Merged
ana-pantilie merged 12 commits into
masterfrom
ana/constant-folding-uplc-2
May 6, 2026
Merged

Add a constant folding pass to UPLC#7764
ana-pantilie merged 12 commits into
masterfrom
ana/constant-folding-uplc-2

Conversation

@ana-pantilie

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

Execution Budget Golden Diff

c8f962a (master) vs 69f54a0

output

plutus-tx-plugin/test/Budget/9.12/applicative.golden.eval

Metric Old New Δ%
CPU 197_308 32_100 -83.73%
Memory 702 300 -57.26%
Flat Size 13 9 -30.77%

plutus-tx-plugin/test/Budget/9.12/monadicDo.golden.eval

Metric Old New Δ%
CPU 197_308 32_100 -83.73%
Memory 702 300 -57.26%
Flat Size 13 9 -30.77%

plutus-tx-plugin/test/Budget/9.12/toFromData.golden.eval

Metric Old New Δ%
CPU 3_182_422 96_100 -96.98%
Memory 13_404 700 -94.78%
Flat Size 153 15 -90.20%

plutus-tx-plugin/test/Budget/9.6/applicative.golden.eval

Metric Old New Δ%
CPU 197_308 32_100 -83.73%
Memory 702 300 -57.26%
Flat Size 13 9 -30.77%

plutus-tx-plugin/test/Budget/9.6/monadicDo.golden.eval

Metric Old New Δ%
CPU 197_308 32_100 -83.73%
Memory 702 300 -57.26%
Flat Size 13 9 -30.77%

plutus-tx-plugin/test/Budget/9.6/toFromData.golden.eval

Metric Old New Δ%
CPU 3_182_422 96_100 -96.98%
Memory 13_404 700 -94.78%
Flat Size 153 15 -90.20%

This comment will get updated when changes are made.

@ana-pantilie
ana-pantilie force-pushed the ana/constant-folding-uplc-2 branch from 5c12951 to 13e8d0b Compare May 4, 2026 20:15
@ana-pantilie
ana-pantilie marked this pull request as ready for review May 4, 2026 21:14
@ana-pantilie
ana-pantilie requested a review from a team May 4, 2026 21:34
Comment thread plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Contexts.hs Outdated
Comment thread plutus-core/plutus-core/src/PlutusCore/Compiler.hs Outdated
Comment thread plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Optimize.hs Outdated
Comment thread plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Contexts.hs Outdated
Comment thread plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Contexts.hs Outdated
Comment thread plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Analysis/Builtins.hs Outdated
Comment thread plutus-core/plutus-core/src/PlutusCore/Compiler.hs Outdated
Comment thread plutus-tx/src/PlutusTx/Lift.hs Outdated
, PLC.Everywhere uni Eq
, MonadError (PIR.Error uni fun (Provenance ())) m
:: forall a m
. ( Lift.Lift UPLC.DefaultUni a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see why everything in this module needs to be hardcoded to DefaultUni. If PIR constant folding doesn't require this, then neither should UPLC constant folding.

@ana-pantilie ana-pantilie May 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is Plinth ever used with anything other than DefaultUni and DefaultFun? Are there any plans to add other universes of builtins? As far as I know there aren't any, and I believe abstraction just for the sake of abstraction is a code smell.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we decide that only DefaultUni is needed, the right thing to do is to open a PR and remove the uni type parameter altogether. That doesn't seem like a good idea though, since other universes are being used for testing and benchmarking.

If we keep the uni parameter, then this monomorphization just makes it inconsistent with everything else.

@ana-pantilie ana-pantilie May 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the right thing to do is to open a PR and remove the uni type parameter altogether.

I don't think removing it from Plinth means we need to remove it from all of the other packages at the same time. Although, I do need to double check if they're used in some other Plinth code as well.

That doesn't seem like a good idea though, since other universes are being used for testing and benchmarking.

I asked Claude where we use other universes, this is the response:

Custom Builtin Function Sets

ExtensionFun — plutus-core/examples/PlutusCore/Examples/Builtins.hs
Used as Either DefaultFun ExtensionFun in example programs and tests. Includes factorial, polymorphic identity functions, cost-tracking builtins, unsafe coercions, and semantic variant testing.

NopFun — cost-model/budgeting-bench/Benchmarks/Nops.hs

No-op builtins used exclusively for cost model calibration benchmarks — measure the raw overhead of calling a builtin function with varying argument types/counts.
Custom Builtin Function Sets ExtensionFun — plutus-core/examples/PlutusCore/Examples/Builtins.hs
Used as Either DefaultFun ExtensionFun in example programs and tests. Includes factorial, polymorphic identity functions, cost-tracking builtins, unsafe coercions, and semantic variant testing.

If Claude is correct, we're keeping the whole universe machinery just for an example and for cost model calibration. I think it would be worth looking into whether the cost model calibration can be represented in a different way. It doesn't make sense to keep all of the complicated universe abstraction around just for that. The current representation would be good if UPLC was used to target other platforms (not just Cardano), but that doesn't seem grounded in reality. Also, there might be performance benefits for the CEK machine. I think this would be worth exploring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Btw, I'm reverting the change to Lift here since I tend to agree this would be better done separately, but I still think we should discuss ^.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If Claude is correct, we're keeping the whole universe machinery just for an example and for cost model calibration.

Cost model calibration sounds quite important to me?

Btw, as a general rule, avoid posting Claude answers verbatim. They are often quite verbose and of questionable quality - inaccuracy is common. You are of course free to use Claude, but make sure to quality-check, shorten and rephrase, make it suitable for human conversation before asking others to read it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cost model calibration sounds quite important to me?

Of course it's important, I'm wondering if there's some other way to represent it.

Btw, as a general rule, avoid posting Claude answers verbatim. They are often quite verbose and of questionable quality - inaccuracy is common. You are of course free to use Claude, but make sure to quality-check, shorten and rephrase, make it suitable for human conversation before asking others to read it.

I'm 100% on the same page on this, so I'm not sure where this is coming from? If I ever post/send something for review which looks like AI slop I probably forgot to check it myself so please let me know.

, (\ds -> constr 1 []) ]
args) ]) ]) ]
args) ])) No newline at end of file
(program 1.1.0 (constr 1 [(constr 0 [(constr 0 [True, 1, False])])])) No newline at end of file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks like running this pass at the UPLC level as well unlocked further optimization opportunities.

@ana-pantilie
ana-pantilie force-pushed the ana/constant-folding-uplc-2 branch from c1f9b1e to bf0fd76 Compare May 5, 2026 18:23
@ana-pantilie
ana-pantilie requested a review from zliu41 May 5, 2026 18:23
Comment thread plutus-tx/src/PlutusTx/Lift.hs Outdated
, PLC.Everywhere uni Eq
, MonadError (PIR.Error uni fun (Provenance ())) m
:: forall a m
. ( Lift.Lift UPLC.DefaultUni a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If Claude is correct, we're keeping the whole universe machinery just for an example and for cost model calibration.

Cost model calibration sounds quite important to me?

Btw, as a general rule, avoid posting Claude answers verbatim. They are often quite verbose and of questionable quality - inaccuracy is common. You are of course free to use Claude, but make sure to quality-check, shorten and rephrase, make it suitable for human conversation before asking others to read it.

@ana-pantilie
ana-pantilie enabled auto-merge (squash) May 6, 2026 12:39
@ana-pantilie
ana-pantilie merged commit 55c464c into master May 6, 2026
9 of 71 checks passed
@ana-pantilie
ana-pantilie deleted the ana/constant-folding-uplc-2 branch May 6, 2026 14:58
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.

2 participants