Skip to content

Commit bf0fd76

Browse files
committed
Address remaining review comments
1 parent 135e695 commit bf0fd76

19 files changed

Lines changed: 341 additions & 196 deletions

File tree

plutus-core/plutus-core.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ library
114114
PlutusCore.Check.Value
115115
PlutusCore.Compiler
116116
PlutusCore.Compiler.Erase
117+
PlutusCore.Compiler.Opts
117118
PlutusCore.Compiler.Types
118119
PlutusCore.Core
119120
PlutusCore.Core.Plated
@@ -750,6 +751,7 @@ library plutus-core-testlib
750751
, dependent-map >=0.4.0.0
751752
, filepath
752753
, free
754+
, hashable
753755
, hedgehog >=1.0
754756
, hedgehog-quickcheck
755757
, lazy-search

plutus-core/plutus-core/src/PlutusCore/Compiler.hs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
module PlutusCore.Compiler
2-
( module UntypedPlutusCore.Optimize.Opts
2+
( module Opts
33
, compileTerm
44
, compileProgram
55
, compileProgramWithTrace
66
) where
77

88
import PlutusCore.Compiler.Erase
9+
import PlutusCore.Compiler.Opts as Opts
910
import PlutusCore.Compiler.Types
1011
import PlutusCore.Core
1112
import PlutusCore.Name.Unique
1213
import PlutusCore.Rename
13-
import UntypedPlutusCore.Analysis.Builtins
1414
import UntypedPlutusCore.Core.Type qualified as UPLC
1515
import UntypedPlutusCore.Optimize qualified as UPLC
16-
import UntypedPlutusCore.Optimize.Opts
1716

1817
import Control.Lens (view)
19-
import Control.Monad.Reader (MonadReader, ask)
18+
import Control.Monad.Reader (MonadReader)
2019

2120
-- | Compile a PLC term to UPLC, and optimize it.
2221
compileTerm
2322
:: ( Compiling m uni fun name a
24-
, MonadReader (OptimizeOpts name uni fun a) m
23+
, MonadReader (CompilationOpts name fun a) m
2524
)
2625
=> Term tyname name uni fun a
2726
-> m (UPLC.Term name uni fun a)
2827
compileTerm t = do
29-
builtinSemanticsVariant <- view (ooBuiltinsInfo . biSemanticsVariant)
28+
optimizeOpts <- view coOptimizeOpts
29+
builtinSemanticsVariant <- view coBuiltinSemanticsVariant
3030
let erased = eraseTerm t
3131
renamed <- rename erased
32-
optimizeOpts <- ask
3332
UPLC.optimizeTerm optimizeOpts builtinSemanticsVariant renamed
3433

3534
-- | Compile a PLC program to UPLC, and optimize it.
3635
compileProgram
3736
:: ( Compiling m uni fun name a
38-
, MonadReader (OptimizeOpts name uni fun a) m
37+
, MonadReader (CompilationOpts name fun a) m
3938
)
4039
=> Program tyname name uni fun a
4140
-> m (UPLC.Program name uni fun a)
@@ -45,15 +44,15 @@ compileProgram (Program a v t) = UPLC.Program a v <$> compileTerm t
4544
the compilation trace in the result. -}
4645
compileProgramWithTrace
4746
:: ( Compiling m uni fun name a
48-
, MonadReader (OptimizeOpts name uni fun a) m
47+
, MonadReader (CompilationOpts name fun a) m
4948
)
5049
=> Program tyname name uni fun a
5150
-> m (UPLC.Program name uni fun a, UPLC.OptimizerTrace name uni fun a)
5251
compileProgramWithTrace (Program a v t) = do
53-
builtinSemanticsVariant <- view (ooBuiltinsInfo . biSemanticsVariant)
52+
optimizeOpts <- view coOptimizeOpts
53+
builtinSemanticsVariant <- view coBuiltinSemanticsVariant
5454
let erased = eraseTerm t
5555
renamedProgram <- UPLC.Program a v <$> rename erased
56-
optimizeOpts <- ask
5756
UPLC.optimizeProgramWithTrace
5857
optimizeOpts
5958
builtinSemanticsVariant
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
3+
module PlutusCore.Compiler.Opts
4+
( CompilationOpts (..)
5+
, coOptimizeOpts
6+
, coBuiltinSemanticsVariant
7+
, defaultCompilationOpts
8+
) where
9+
10+
import Control.Lens (makeLenses)
11+
import Data.Default.Class (Default (def))
12+
import PlutusCore.Builtin.Meaning (BuiltinSemanticsVariant)
13+
import UntypedPlutusCore.Optimize.Opts (OptimizeOpts, defaultOptimizeOpts)
14+
15+
data CompilationOpts name fun a = CompilationOpts
16+
{ _coOptimizeOpts :: OptimizeOpts name a
17+
, _coBuiltinSemanticsVariant :: BuiltinSemanticsVariant fun
18+
}
19+
20+
$(makeLenses ''CompilationOpts)
21+
22+
defaultCompilationOpts :: Default (BuiltinSemanticsVariant fun) => CompilationOpts name fun a
23+
defaultCompilationOpts =
24+
CompilationOpts
25+
{ _coOptimizeOpts = defaultOptimizeOpts
26+
, _coBuiltinSemanticsVariant = def
27+
}

plutus-core/plutus-ir/test/PlutusIR/Transform/StrictLetRec/Tests/Lib.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ compileTplcProgramOrFail
9292
compileTplcProgramOrFail plcProgram =
9393
handlePirErrorByFailing @SrcSpan =<< do
9494
TPLC.compileProgram plcProgram
95-
& flip runReaderT TPLC.defaultOptimizeOpts
95+
& flip runReaderT TPLC.defaultCompilationOpts
9696
& runQuoteT
9797
& runExceptT
9898

plutus-core/testlib/PlutusCore/Test.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import PlutusPrelude
6363

6464
import PlutusCore qualified as TPLC
6565
import PlutusCore.Annotation
66+
import PlutusCore.Builtin
6667
import PlutusCore.Check.Scoping
6768
import PlutusCore.Compiler qualified as TPLC
6869
import PlutusCore.DeBruijn
@@ -84,6 +85,7 @@ import Control.Monad.Except
8485
import Control.Monad.Reader
8586
import Control.Monad.State
8687
import Data.Either.Extras
88+
import Data.Hashable
8789
import Data.Kind qualified as GHC
8890
import Data.Text (Text)
8991
import Hedgehog
@@ -186,15 +188,19 @@ instance ToUPlc (UPLC.Program TPLC.Name uni fun ()) uni fun where
186188
toUPlc = pure
187189

188190
instance
189-
ToUPlc
190-
(TPLC.Program TPLC.TyName UPLC.Name UPLC.DefaultUni UPLC.DefaultFun ())
191-
UPLC.DefaultUni
192-
UPLC.DefaultFun
191+
( TPLC.Typecheckable uni fun
192+
, CaseBuiltin uni
193+
, Hashable fun
194+
, TPLC.GEq uni
195+
, TPLC.Closed uni
196+
, TPLC.Everywhere uni Eq
197+
)
198+
=> ToUPlc (TPLC.Program TPLC.TyName UPLC.Name uni fun ()) uni fun
193199
where
194200
toUPlc =
195201
pure
196202
. TPLC.runQuote
197-
. flip runReaderT UPLC.defaultOptimizeOpts
203+
. flip runReaderT TPLC.defaultCompilationOpts
198204
. TPLC.compileProgram
199205

200206
instance ToUPlc (UPLC.Program UPLC.NamedDeBruijn uni fun ()) uni fun where

plutus-core/testlib/PlutusIR/Test.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import PlutusIR.Transform.RewriteRules
4444
import PlutusIR.TypeCheck
4545
import System.FilePath (joinPath, splitExtension, (</>))
4646

47+
import Data.Hashable
4748
import Data.Text qualified as T
4849
import Data.Text.IO qualified as T
4950

@@ -70,16 +71,23 @@ instance
7071
toTPlc = asIfThrown . fmap void . compileWithOpts id
7172

7273
instance
73-
( Pretty a
74+
( PLC.GEq uni
75+
, uni `PLC.Everywhere` Eq
76+
, PLC.Typecheckable uni fun
77+
, PLC.CaseBuiltin uni
78+
, PLC.PrettyUni uni
79+
, Pretty fun
80+
, Hashable fun
81+
, Pretty a
7482
, Typeable a
7583
, Ord a
7684
, PLC.AnnInline a
7785
, PLC.AnnCase a
86+
, Default (PLC.CostingPart uni fun)
87+
, Default (BuiltinsInfo uni fun)
88+
, Default (RewriteRules uni fun)
7889
)
79-
=> ToUPlc
80-
(PIR.Program PIR.TyName PIR.Name PLC.DefaultUni PLC.DefaultFun a)
81-
PLC.DefaultUni
82-
PLC.DefaultFun
90+
=> ToUPlc (PIR.Program PIR.TyName PIR.Name uni fun a) uni fun
8391
where
8492
toUPlc = toTPlc >=> toUPlc
8593

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Analysis/Builtins.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,5 @@ defaultUniUnserializableConstants = \case
7474
Some (ValueOf DefaultUniBLS12_381_MlResult _) -> True
7575
_ -> False
7676

77-
{- Note [Unserializable constants]
78-
See Note [Unserializable constants] in PlutusIR.Analysis.Builtins.
77+
{- See Note [Unserializable constants] in PlutusIR.Analysis.Builtins.
7978
-}

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Contexts.hs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE LambdaCase #-}
22
{-# LANGUAGE RankNTypes #-}
3-
{-# LANGUAGE TupleSections #-}
43

54
module UntypedPlutusCore.Contexts where
65

@@ -40,30 +39,6 @@ fillAppCtx term = \case
4039
AppCtxTerm ann arg ctx -> fillAppCtx (Apply ann term arg) ctx
4140
AppCtxType ann ctx -> fillAppCtx (Force ann term) ctx
4241

43-
lengthContext :: AppCtx name uni fun a -> Int
44-
lengthContext = go 0
45-
where
46-
go acc = \case
47-
AppCtxEnd -> acc
48-
AppCtxTerm _ _ ctx -> go (acc + 1) ctx
49-
AppCtxType _ ctx -> go (acc + 1) ctx
50-
51-
appendAppCtx
52-
:: AppCtx name uni fun a
53-
-> AppCtx name uni fun a
54-
-> AppCtx name uni fun a
55-
appendAppCtx ctx1 ctx2 = go ctx1
56-
where
57-
go AppCtxEnd = ctx2
58-
go (AppCtxTerm ann arg ctx') = AppCtxTerm ann arg $ go ctx'
59-
go (AppCtxType ann ctx') = AppCtxType ann $ go ctx'
60-
61-
instance Semigroup (AppCtx name uni fun a) where
62-
(<>) = appendAppCtx
63-
64-
instance Monoid (AppCtx name uni fun a) where
65-
mempty = AppCtxEnd
66-
6742
data Saturation = Oversaturated | Undersaturated | Saturated
6843

6944
-- | Do the given arguments saturate the given arity?
@@ -82,14 +57,3 @@ saturates (AppCtxType {}) (TermParam : _) = Nothing
8257
-- Arguments left - undersaturated
8358
saturates (AppCtxTerm {}) [] = Just Oversaturated
8459
saturates (AppCtxType {}) [] = Just Oversaturated
85-
86-
{- Note [Ctx splitting in a recursive pass]
87-
When writing a recursive pass that processes the whole program, you must be
88-
a bit cautious when using a Ctx split. The context split may traverse
89-
part of the program, which will _also_ be traversed by the main recursive
90-
traversal. This can lead to quadratic runtime.
91-
92-
This is usually okay for something like 'splitApplication', since it is
93-
quadratic in the longest application in the program, which is usually not
94-
significantly long.
95-
-}

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Optimize.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ module UntypedPlutusCore.Optimize
1313
, module UntypedPlutusCore.Transform.Optimizer
1414
) where
1515

16+
import PlutusCore.Builtin (CostingPart)
1617
import PlutusCore.Compiler.Types
1718
import PlutusCore.Default qualified as PLC
1819
import PlutusCore.Default.Builtins
1920
import PlutusCore.Name.Unique
21+
import UntypedPlutusCore.Analysis.Builtins (BuiltinsInfo, biSemanticsVariant)
2022
import UntypedPlutusCore.Core.Type
2123
import UntypedPlutusCore.Optimize.Opts as Opts
2224
import UntypedPlutusCore.Transform.ApplyToCase (applyToCase)
@@ -31,7 +33,9 @@ import UntypedPlutusCore.Transform.Inline (InlineHints (..), inline)
3133
import UntypedPlutusCore.Transform.LetFloatOut (letFloatOut)
3234
import UntypedPlutusCore.Transform.Optimizer
3335

36+
import Control.Lens ((&), (.~))
3437
import Control.Monad
38+
import Data.Default.Class (def)
3539
import Data.Either (isRight)
3640
import Data.List as List (foldl')
3741
import Data.Typeable
@@ -40,7 +44,7 @@ import Data.Vector.Orphans ()
4044
optimizeProgram
4145
:: forall name uni fun m a
4246
. Compiling m uni fun name a
43-
=> OptimizeOpts name uni fun a
47+
=> OptimizeOpts name a
4448
-> BuiltinSemanticsVariant fun
4549
-> Program name uni fun a
4650
-> m (Program name uni fun a)
@@ -50,7 +54,7 @@ optimizeProgram opts builtinSemanticsVariant (Program a v t) =
5054
optimizeProgramWithTrace
5155
:: forall name uni fun m a
5256
. Compiling m uni fun name a
53-
=> OptimizeOpts name uni fun a
57+
=> OptimizeOpts name a
5458
-> BuiltinSemanticsVariant fun
5559
-> Program name uni fun a
5660
-> m (Program name uni fun a, OptimizerTrace name uni fun a)
@@ -63,7 +67,7 @@ optimizeProgramWithTrace opts builtinSemanticsVariant (Program a v t) = do
6367
optimizeTerm
6468
:: forall name uni fun m a
6569
. Compiling m uni fun name a
66-
=> OptimizeOpts name uni fun a
70+
=> OptimizeOpts name a
6771
-> BuiltinSemanticsVariant fun
6872
-> Term name uni fun a
6973
-> m (Term name uni fun a)
@@ -73,7 +77,7 @@ optimizeTerm opts builtinSemanticsVariant term =
7377
termOptimizer
7478
:: forall name uni fun m a
7579
. Compiling m uni fun name a
76-
=> OptimizeOpts name uni fun a
80+
=> OptimizeOpts name a
7781
-> BuiltinSemanticsVariant fun
7882
-> Term name uni fun a
7983
-> OptimizerT name uni fun a m (Term name uni fun a)
@@ -147,10 +151,13 @@ termOptimizer opts builtinSemanticsVariant =
147151
LetFloatOutStage ->
148152
letFloatOut
149153
ConstantFoldingStage ->
150-
evaluateBuiltinsPass
151-
(_ooPreserveLogging opts)
152-
(_ooBuiltinsInfo opts)
153-
(_ooBuiltinCostModel opts)
154+
case (eqT @uni @PLC.DefaultUni, eqT @fun @DefaultFun) of
155+
(Just Refl, Just Refl) ->
156+
evaluateBuiltinsPass
157+
(_ooPreserveLogging opts)
158+
((def :: BuiltinsInfo PLC.DefaultUni DefaultFun) & biSemanticsVariant .~ builtinSemanticsVariant)
159+
(def :: CostingPart PLC.DefaultUni DefaultFun)
160+
_ -> pure
154161

155162
caseOfCase'
156163
:: Term name uni fun a

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Optimize/Opts.hs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,17 @@ module UntypedPlutusCore.Optimize.Opts
1313
, ooInlineCallsiteGrowth
1414
, ooPreserveLogging
1515
, ooCertifiedOptsOnly
16-
, ooBuiltinsInfo
17-
, ooBuiltinCostModel
1816
, defaultOptimizeOpts
1917
, CseWhichSubterms (..)
20-
, BuiltinsInfo (..)
21-
, biSemanticsVariant
2218
) where
2319

2420
import Control.Lens.TH (makeLenses)
2521
import Data.Default.Class
2622

2723
import PlutusCore.Annotation (InlineHints (..))
2824
import PlutusCore.AstSize
29-
import PlutusCore.Builtin qualified as PLC
30-
import PlutusCore.Default
3125
import PlutusCore.Pretty
3226
import Prettyprinter (viaShow)
33-
import UntypedPlutusCore.Analysis.Builtins
3427

3528
-- | Which subterms should be considered as candidates for CSE?
3629
data CseWhichSubterms = AllSubterms | ExcludeWorkFree
@@ -39,7 +32,7 @@ data CseWhichSubterms = AllSubterms | ExcludeWorkFree
3932
instance Pretty CseWhichSubterms where
4033
pretty = viaShow
4134

42-
data OptimizeOpts name uni fun a = OptimizeOpts
35+
data OptimizeOpts name a = OptimizeOpts
4336
{ _ooMaxSimplifierIterations :: Int
4437
, _ooMaxCseIterations :: Int
4538
, _ooCseWhichSubterms :: CseWhichSubterms
@@ -51,13 +44,12 @@ data OptimizeOpts name uni fun a = OptimizeOpts
5144
, _ooPreserveLogging :: Bool
5245
, _ooApplyToCase :: Bool
5346
, _ooCertifiedOptsOnly :: Bool
54-
, _ooBuiltinsInfo :: BuiltinsInfo uni fun
55-
, _ooBuiltinCostModel :: PLC.CostingPart uni fun
5647
}
48+
deriving stock (Show)
5749

5850
$(makeLenses ''OptimizeOpts)
5951

60-
defaultOptimizeOpts :: OptimizeOpts name DefaultUni DefaultFun a
52+
defaultOptimizeOpts :: OptimizeOpts name a
6153
defaultOptimizeOpts =
6254
OptimizeOpts
6355
{ _ooMaxSimplifierIterations = 12
@@ -71,6 +63,4 @@ defaultOptimizeOpts =
7163
, _ooPreserveLogging = True
7264
, _ooApplyToCase = True
7365
, _ooCertifiedOptsOnly = False
74-
, _ooBuiltinsInfo = def
75-
, _ooBuiltinCostModel = def
7666
}

0 commit comments

Comments
 (0)