Skip to content
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

Attempt to rewrite unnecessary constructor applications in the plugin #5649

Merged
merged 6 commits into from
Nov 23, 2023

Conversation

zliu41
Copy link
Member

@zliu41 zliu41 commented Nov 21, 2023

This should make the "Unsupported feature: Cannot case on a value on type" error happen much less often, if not eliminating it entirely. Tested on 38b6cd9.

@zliu41 zliu41 requested review from michaelpj and a team November 21, 2023 23:16
@@ -823,22 +829,91 @@ compileExpr e = traceCompilation 2 ("Compiling expr:" GHC.<+> GHC.ppr e) $ do
body' <- compileExpr body
pure $ PIR.mkLet annMayInline PIR.Rec binds body'

GHC.Case scrutinee b t alts ->
compileCase (const . GHC.isDeadOcc . GHC.occInfo . GHC.idInfo) True binfo scrutinee b t alts
Copy link
Contributor

Choose a reason for hiding this comment

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

was rewriteOpaque supposed to become a plugin option?

Copy link
Member Author

Choose a reason for hiding this comment

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

No because I don't see a reason why it should be turned off.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I couldn't see what else it was for, but I see you're passing it as false the second time.

, let dataConName = GHC.dataConName dataCon
, let dataConModule = GHC.moduleNameString . GHC.moduleName <$> GHC.nameModule_maybe dataConName
, let dataConBase = GHC.occNameString (GHC.nameOccName dataConName)
, (dataConModule, dataConBase) `elem` opaqueTypes -> do
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a case where we really should be using NameInfo. We're looking for exact types here, so there's no reason why we shouldn't be able to identify the exact Name.

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT, this transformation is totally safe regardless of what data constructor we use it on. If we see this pattern with a non-opaque data constructor, that's still pointless reboxing.

So... maybe we should just drop the whole "is this an opaque data constructor business". The downside is that then I guess this would fire more frequently.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Will try that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done; it actually led to some small performance wins!

plutus-tx-plugin/src/PlutusTx/Compiler/Expr.hs Outdated Show resolved Hide resolved
plutus-tx-plugin/src/PlutusTx/Compiler/Expr.hs Outdated Show resolved Hide resolved
isDead' b = not . any (== b) . universeBi
-- If some binders are still alive, we have to give up (rather than trying to rewrite
-- opaque constructor applications again, which will loop), hence `False`.
compileCase isDead' False binfo scrutinee binder t [GHC.Alt con bs (transform f body)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't love the recursion here, since it makes me think about whether or not we can re-enter the same branch. I'm sure it's fine, but I'd be tempted to structure it as a two-phase thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Please elaborate how a two-phase approach looks like.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant, have a version that clearly does do the rewriting and then one that clearly doesn't. But I think you achieve that with the boolean flag, I just hadn't seen that you pass it as False here.

@zliu41 zliu41 changed the title Attempt to rewrite opaque constructor applications in the plugin Attempt to rewrite unnecessary constructor applications in the plugin Nov 22, 2023
@zliu41
Copy link
Member Author

zliu41 commented Nov 22, 2023

CI failure is unrelated.

@michaelpj michaelpj enabled auto-merge (squash) November 23, 2023 10:28
@michaelpj
Copy link
Contributor

CI failures are spurious

@michaelpj michaelpj disabled auto-merge November 23, 2023 10:30
@michaelpj michaelpj merged commit 98c5167 into master Nov 23, 2023
4 of 50 checks passed
@michaelpj michaelpj deleted the zliu41/opaque branch November 23, 2023 10:30
v0d1ch pushed a commit to v0d1ch/plutus that referenced this pull request Dec 6, 2024
…IntersectMBO#5649)

* Attempt to rewrite opaque constructor applications in the plugin

* Update plutus-tx-plugin/src/PlutusTx/Compiler/Expr.hs

Co-authored-by: Michael Peyton Jones <[email protected]>

* Update plutus-tx-plugin/src/PlutusTx/Compiler/Expr.hs

Co-authored-by: Michael Peyton Jones <[email protected]>

* Rewrite all unnecessary constructor applications

* cabal files

---------

Co-authored-by: Michael Peyton Jones <[email protected]>
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