Skip to content

Commit fcd86d9

Browse files
committed
wip
1 parent 9f45d1a commit fcd86d9

File tree

5 files changed

+182
-12
lines changed

5 files changed

+182
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ stack.yaml.lock
4949
*.chi
5050
*.chs.h
5151
*.prof
52+
*.hp
53+
*.ps
5254
.liquid/
5355

5456
# Agda

nix/shell.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let
1313
fourmolu = "0.17.0.0";
1414
hlint = "3.8";
1515
stylish-haskell = "latest";
16-
# hp2ps = "latest";
17-
# hp2pretty = "latest";
16+
hp2ps = "latest";
17+
hp2pretty = "latest";
1818
};
1919

2020
# Pre-commit hooks for the repo. Injects into shell via shellHook.

plutus-tx-plugin/plutus-tx-plugin.cabal

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ library
5454
import: lang, ghc-version-support, os-support
5555
hs-source-dirs: src
5656
exposed-modules:
57+
PlutusTx.Compiler.Builtins
5758
PlutusTx.Compiler.Error
59+
PlutusTx.Compiler.Types
5860
PlutusTx.Options
5961
PlutusTx.Plugin
6062

6163
other-modules:
6264
PlutusTx.Compiler.Binders
63-
PlutusTx.Compiler.Builtins
6465
PlutusTx.Compiler.Expr
6566
PlutusTx.Compiler.Kind
6667
PlutusTx.Compiler.Laziness
6768
PlutusTx.Compiler.Names
6869
PlutusTx.Compiler.Trace
6970
PlutusTx.Compiler.Type
70-
PlutusTx.Compiler.Types
7171
PlutusTx.Compiler.Utils
7272
PlutusTx.PIRTypes
7373
PlutusTx.PLCTypes
@@ -124,10 +124,6 @@ test-suite plutus-tx-plugin-tests
124124
AssocMap.Properties3
125125
AssocMap.Semantics
126126
AssocMap.Spec
127-
-- Blueprint.Tests
128-
-- Blueprint.Tests.Lib
129-
-- Blueprint.Tests.Lib.AsData.Blueprint
130-
-- Blueprint.Tests.Lib.AsData.Decls
131127
Budget.Spec
132128
Budget.WithGHCOptimisations
133129
Budget.WithoutGHCOptimisations
@@ -136,7 +132,6 @@ test-suite plutus-tx-plugin-tests
136132
ByteStringLiterals.Spec
137133
CallTrace.Lib
138134
CallTrace.OtherModule
139-
-- CallTrace.Spec
140135
DataList.Budget.Spec
141136
Inline.Spec
142137
IntegerLiterals.NoStrict.NegativeLiterals.Spec
@@ -177,10 +172,15 @@ test-suite plutus-tx-plugin-tests
177172
ShortCircuit.WithoutGHCOptimisations
178173
StdLib.Spec
179174
Strictness.Spec
180-
-- TH.Spec
181-
-- TH.TestTH
182175
Unicode.Spec
183176

177+
-- Blueprint.Tests
178+
-- Blueprint.Tests.Lib
179+
-- Blueprint.Tests.Lib.AsData.Blueprint
180+
-- Blueprint.Tests.Lib.AsData.Decls
181+
-- CallTrace.Spec
182+
-- TH.Spec
183+
-- TH.TestTH
184184
build-depends:
185185
, base >=4.9 && <5
186186
, base16-bytestring
@@ -241,3 +241,31 @@ test-suite size
241241
ghc-options:
242242
-fno-strictness -fno-unbox-strict-fields
243243
-fno-unbox-small-strict-fields -fno-full-laziness
244+
245+
test-suite plutus-tx-plugin-profile-test
246+
import: lang, ghc-version-support, os-support
247+
type: exitcode-stdio-1.0
248+
main-is: ProfileTest.hs
249+
hs-source-dirs: test/Plugin/Profiling
250+
build-depends:
251+
, base >=4.9 && <5
252+
, containers
253+
, data-default
254+
, ghc
255+
, ghc-paths
256+
, lens
257+
, mtl
258+
, plutus-core ^>=1.55
259+
, plutus-core:plutus-ir
260+
, plutus-tx ^>=1.55
261+
, plutus-tx-plugin ^>=1.55
262+
, template-haskell
263+
, text
264+
265+
default-extensions: Strict
266+
ghc-options: -threaded -rtsopts -with-rtsopts=-N
267+
268+
-- See Note [-fno-full-laziness in Plutus Tx]
269+
ghc-options:
270+
-fno-strictness -fno-unbox-strict-fields
271+
-fno-unbox-small-strict-fields -fno-full-laziness

plutus-tx-plugin/src/PlutusTx/Plugin.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{-# OPTIONS_GHC -O0 #-}
1212
{-# OPTIONS_GHC -fno-full-laziness -fno-cse #-}
1313

14-
module PlutusTx.Plugin (plugin, plc) where
14+
module PlutusTx.Plugin (plugin, plc, runCompiler) where
1515

1616
import PlutusPrelude
1717
import PlutusTx.AsData.Internal qualified
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
{-# LANGUAGE TemplateHaskell #-}
4+
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
5+
6+
-- | Standalone executable for profiling the plugin compilation functions.
7+
--
8+
-- This test uses the exposed 'runCompiler' from Plugin.hs to compile a simple
9+
-- Core expression. This allows profiling the plugin code at runtime, including
10+
-- the SCC annotations in Plugin.hs.
11+
--
12+
-- To run with profiling:
13+
-- cabal build plutus-tx-plugin-profile-test --enable-profiling
14+
-- cabal run plutus-tx-plugin-profile-test --enable-profiling -- +RTS -p -hc
15+
module Main where
16+
17+
import Data.Default
18+
import Data.Foldable (fold)
19+
import PlutusCore qualified as PLC
20+
import PlutusCore.Compiler qualified as PLC
21+
import PlutusCore.Pretty as PLC
22+
import PlutusCore.Quote
23+
import PlutusCore.Quote (runQuoteT)
24+
import PlutusCore.Version qualified as PLC
25+
import PlutusIR qualified as PIR
26+
import PlutusIR.Compiler qualified as PIR
27+
import PlutusIR.Compiler.Definitions qualified as PIR
28+
import PlutusIR.Compiler.Types qualified as PIR
29+
import PlutusIR.Transform.RewriteRules
30+
import PlutusIR.Transform.RewriteRules.RemoveTrace (rewriteRuleRemoveTrace)
31+
import PlutusTx.Compiler.Builtins
32+
import PlutusTx.Compiler.Error
33+
import PlutusTx.Compiler.Types
34+
import PlutusTx.Options (PluginOptions (..), defaultPluginOptions)
35+
import PlutusTx.Plugin (runCompiler)
36+
import UntypedPlutusCore qualified as UPLC
37+
38+
import GHC qualified as GHC
39+
import GHC.Core.FamInstEnv qualified as GHC
40+
import GHC.Core.Opt.OccurAnal qualified as GHC
41+
import GHC.Driver.Make qualified as GHC
42+
import GHC.Driver.Session qualified as GHC
43+
import GHC.Paths as GHC
44+
import GHC.Plugins qualified as GHC
45+
import GHC.Types.Literal qualified as GHC
46+
47+
import Control.Lens
48+
import Control.Monad.Except
49+
import Control.Monad.Reader
50+
import Control.Monad.State
51+
import Control.Monad.Writer
52+
53+
import Data.Map qualified as Map
54+
import Data.Set qualified as Set
55+
import Language.Haskell.TH.Syntax qualified as TH
56+
57+
-- | Create a simple Core expression for testing (a literal integer)
58+
createSimpleCoreExpr :: GHC.DynFlags -> GHC.CoreExpr
59+
createSimpleCoreExpr flags =
60+
let lit = GHC.Lit (GHC.LitNumber GHC.LitNumInt 42)
61+
in lit
62+
63+
-- | Set up a minimal CompileContext for testing
64+
setupCompileContext
65+
:: GHC.DynFlags
66+
-> GHC.FamInstEnvs
67+
-> NameInfo
68+
-> CompileContext PLC.DefaultUni PLC.DefaultFun
69+
setupCompileContext flags famEnvs nameInfo =
70+
let opts = defaultPluginOptions
71+
coverage = CoverageOpts mempty
72+
in CompileContext
73+
{ ccOpts =
74+
CompileOptions
75+
{ coProfile = _posProfile opts
76+
, coCoverage = coverage
77+
, coDatatypeStyle =
78+
if _posPlcTargetVersion opts < PLC.plcVersion110
79+
then PIR.ScottEncoding
80+
else PIR._dcoStyle $ _posDatatypes opts
81+
, coRemoveTrace = _posRemoveTrace opts
82+
, coInlineFix = _posInlineFix opts
83+
}
84+
, ccFlags = flags
85+
, ccFamInstEnvs = famEnvs
86+
, ccNameInfo = nameInfo
87+
, ccScope = initialScope
88+
, ccBlackholed = mempty
89+
, ccCurDef = Nothing
90+
, ccModBreaks = Nothing
91+
, ccBuiltinsInfo = def
92+
, ccBuiltinCostModel = def
93+
, ccDebugTraceOn = _posDumpCompilationTrace opts
94+
, ccRewriteRules = makeRewriteRules opts
95+
, ccSafeToInline = False
96+
}
97+
where
98+
makeRewriteRules :: PluginOptions -> RewriteRules PLC.DefaultUni PLC.DefaultFun
99+
makeRewriteRules options =
100+
fold
101+
[ mwhen (_posRemoveTrace options) rewriteRuleRemoveTrace
102+
, defaultUniRewriteRules
103+
]
104+
mwhen :: Monoid m => Bool -> m -> m
105+
mwhen b m = if b then m else mempty
106+
107+
-- | Create empty NameInfo (simplified - in real usage would need proper lookups)
108+
createEmptyNameInfo :: NameInfo
109+
createEmptyNameInfo = Map.empty
110+
111+
main :: IO ()
112+
main = do
113+
putStrLn "Setting up for plugin profiling test..."
114+
115+
-- Use GHC's API to get DynFlags
116+
GHC.defaultErrorHandler GHC.defaultFatalMessager GHC.defaultFlushOut $ do
117+
-- Initialize GHC session to get DynFlags
118+
GHC.runGhc (Just GHC.libdir) $ do
119+
-- Get DynFlags
120+
flags <- GHC.getSessionDynFlags
121+
122+
-- Create a simple Core expression (literal integer)
123+
let expr = createSimpleCoreExpr flags
124+
125+
-- Set up minimal context
126+
let famEnvs = (GHC.emptyFamInstEnv, GHC.emptyFamInstEnv)
127+
nameInfo = createEmptyNameInfo
128+
ctx = setupCompileContext flags famEnvs nameInfo
129+
opts = defaultPluginOptions
130+
st = CompileState 0 mempty
131+
moduleNameStr = "ProfileTest"
132+
-- Apply occurrence analysis like the plugin does
133+
expr' = GHC.occurAnalyseExpr expr
134+
135+
-- Call runCompiler - this is where the SCC annotations are!
136+
_ <-
137+
runExceptT . runWriterT . runQuoteT . flip runReaderT ctx . flip evalStateT st $
138+
runCompiler moduleNameStr opts expr'
139+
140+
pure ()

0 commit comments

Comments
 (0)