-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make CommuteFnWithConst transformation a RewriteRule
- Loading branch information
Showing
19 changed files
with
111 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
plutus-core/plutus-ir/src/PlutusIR/Transform/RewriteRules/DecodeEncodeUtf8.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
|
||
{- | Commute such that constants are the first arguments. Consider: | ||
(1) equalsInteger 1 x | ||
(2) equalsInteger x 1 | ||
We have unary application, so these are two partial applications: | ||
(1) (equalsInteger 1) x | ||
(2) (equalsInteger x) 1 | ||
With (1), we can share the `equalsInteger 1` node, and it will be the same across any place where | ||
we do this. | ||
With (2), both the nodes here include x, which is a variable that will likely be different in other | ||
invocations of `equalsInteger`. So the second one is harder to share, which is worse for CSE. | ||
So commuting `equalsInteger` so that it has the constant first both a) makes various occurrences of | ||
`equalsInteger` more likely to look similar, and b) gives us a maximally-shareable node for CSE. | ||
This applies to any commutative builtin function that takes constants as arguments, although we | ||
might expect that `equalsInteger` is the one that will benefit the most. | ||
Plutonomy only commutes `EqualsInteger` in their `commEquals`. | ||
-} | ||
|
||
module PlutusIR.Transform.RewriteRules.DecodeEncodeUtf8 | ||
( decodeEncodeUtf8 | ||
) where | ||
|
||
import PlutusCore.Default | ||
import PlutusIR | ||
|
||
|
||
decodeEncodeUtf8 :: Semigroup a => Term tyname name uni DefaultFun a -> Term tyname name uni DefaultFun a | ||
decodeEncodeUtf8 = \case | ||
BA DecodeUtf8 a1 a2 (BA EncodeUtf8 a3 a4 t) -> | ||
-- place the missed annotations inside the rewritten term | ||
(<> a1 <> a2 <> a3 <> a4) <$> t | ||
t -> t | ||
|
||
pattern BA b a1 a2 t <- Apply a1 (Builtin a2 b) t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
plutus-core/plutus-ir/test/PlutusIR/Transform/CommuteFnWithConst/Tests.hs
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
plutus-core/plutus-ir/test/PlutusIR/Transform/Rewrite/Tests.hs
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
plutus-core/plutus-ir/test/PlutusIR/Transform/RewriteRules/Tests.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module PlutusIR.Transform.RewriteRules.Tests where | ||
|
||
import PlutusIR.Parser | ||
import PlutusIR.Test | ||
import PlutusIR.Analysis.Builtins | ||
import PlutusIR.Transform.RewriteRules as RewriteRules | ||
|
||
import Data.Default.Class | ||
import Control.Lens | ||
import Test.Tasty | ||
import Test.Tasty.Extras | ||
|
||
test_RewriteRules :: TestTree | ||
test_RewriteRules = runTestNestedIn ["plutus-ir/test/PlutusIR/Transform"] $ | ||
testNested "RewriteRules" $ | ||
fmap | ||
(goldenPir (RewriteRules.userRewrite builtinsInfo) pTerm) | ||
[ "decodeEncodeUtf8" | ||
, "equalsInt" -- this tests that the function works on equalInteger | ||
, "divideInt" -- this tests that the function excludes not commutative functions | ||
, "multiplyInt" -- this tests that the function works on multiplyInteger | ||
, "let" -- this tests that it works in the subterms | ||
] | ||
where | ||
builtinsInfo = def & rewriteRules .~ RewriteRules.defaultUniRewriteRules |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.