Skip to content

UPLC Inliner: effect safety #7022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2025
Merged

Conversation

Unisay
Copy link
Contributor

@Unisay Unisay commented Apr 8, 2025

Added test coverage before changing this functionality for #1492

@Unisay Unisay added the No Changelog Required Add this to skip the Changelog Check label Apr 8, 2025
@Unisay Unisay requested a review from zliu41 April 8, 2025 16:31
@Unisay Unisay self-assigned this Apr 8, 2025
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from f219e45 to 12ee913 Compare April 10, 2025 14:04
@Unisay Unisay changed the title spec: isFirstVarBeforeEffects Inline: effect safety Apr 11, 2025
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from 12ee913 to a6a86a2 Compare April 11, 2025 12:14
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from a6a86a2 to df45158 Compare April 11, 2025 14:09
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from df45158 to 720affe Compare April 11, 2025 14:19
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from 720affe to ae5c7ec Compare April 11, 2025 14:20
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from ae5c7ec to 2be75be Compare April 11, 2025 15:05
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from 9ae0492 to ebf009b Compare April 11, 2025 15:17
@Unisay
Copy link
Contributor Author

Unisay commented Apr 11, 2025

I have disabled logs preservation in Marlowe validators and witnessed Ex Budget improvement:

    +--------------------++-------------+--------------+--------+
    |                    ||   CPU units | Memory units |   Size |
    +====================++=============+==============+========+
    | Baseline (average) || 540 460 151 |    1 784 424 |  7 945 |
    |   Actual (average) || 533 611 472 |    1 742 839 |  7 831 |
    |              Delta ||      -1.27% |       -2.33% | -1.43% |
    +--------------------++-------------+--------------+--------+

@Unisay Unisay requested a review from zliu41 April 14, 2025 11:14
@Unisay Unisay requested a review from zliu41 April 14, 2025 16:29
Copy link
Member

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

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

The title of the PR should mention UPLC, as you are only updating the UPLC inliner, not the PIR inliner.

-> String
-> (Name, Name)
uniqueNames2 n1 n2 =
(name n1 0, name n2 1)
Copy link
Member

Choose a reason for hiding this comment

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

Second, this is a strange way of getting unique names. In most if not every other API (e.g., template haskell API, GHC API), getting uinque names is a monadic action. This way the names are truly unique. In your method, calling uniqueNames2 "a" "b" the second time returns the same names, which are no longer unique.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is by design: reducing a uniqueness scope allows to get rid of the monadic context.

calling uniqueNames2 "a" "b" the second time returns the same names

We never call freshName "x" more than once in tests anyway, so this is not a problem in practice.
OTOH the monadic contexts that requires to declare unique names before they are used is a (readability) problem that this approach solves.

`app` m
`app` xs
`app` i
`app` b
Copy link
Member

Choose a reason for hiding this comment

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

Also: the heavy use of infix functions like `app` looks visually unappealing.

Copy link
Contributor Author

@Unisay Unisay Apr 15, 2025

Choose a reason for hiding this comment

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

I guess its a matter of taste 🤷🏼‍♂️
I was thinking of having a variation of $ operator for infix applications, something like:

  term
    $. m
    $. xs
    $. i
    $. b

but in the end app is not so bad and doesn't require a creation/memoization of an extra operator.


{- | @(\a -> f (a 0 1) (a 2)) (\x y -> g x y)@
inlineImpure5 :: Term Name PLC.DefaultUni PLC.DefaultFun ()
inlineImpure5 =
Copy link
Member

Choose a reason for hiding this comment

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

Have you verified that before your change, the inliner won't inline this? I think that should be the case but it's worth verifying to be certain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I did.

inlineImpure5:           FAIL
  Test output was different from 'untyped-plutus-core/test/Transform/inlineImpure5.uplc.golden'. 
  It was:
  \a -> (\b -> addInteger (addInteger 1 a) b) (addInteger 3 a)
  Use -p '/simplify/&&/inlineImpure5/' to rerun this test only.

@zliu41
Copy link
Member

zliu41 commented Apr 15, 2025

Also this should have a changelog entry. Please do not default to using the "no changelog required" tag. If you are in doubt about whether a changelog entry is needed, err on the side of adding one.

@Unisay Unisay removed the No Changelog Required Add this to skip the Changelog Check label Apr 15, 2025
@Unisay Unisay changed the title Inline: effect safety UPLC Inliner: effect safety Apr 15, 2025
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from b1258d2 to 5120ba0 Compare April 23, 2025 11:58
@Unisay Unisay requested a review from zliu41 April 23, 2025 11:59
@Unisay Unisay force-pushed the yura/1492-test-isFirstVarBeforeEffects branch from 5120ba0 to 926e3a9 Compare April 23, 2025 12:08
Copy link
Member

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

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

👍

It occurred to me that the PIR equivalent of isVarEventuallyEvaluated will be useful for another optimization: if a non-strict var is eventually evaluated, then we can turn it into a strict var.

@Unisay Unisay merged commit 6b31493 into master Apr 23, 2025
7 checks passed
@Unisay Unisay deleted the yura/1492-test-isFirstVarBeforeEffects branch April 23, 2025 14:10
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