Skip to content

Commit df551ec

Browse files
Refactor topentity triple to 'TopEntityT'
1 parent 20a11a7 commit df551ec

File tree

9 files changed

+61
-65
lines changed

9 files changed

+61
-65
lines changed

benchmark/benchmark-normalization.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
55

66
import Clash.Annotations.BitRepresentation.Internal (CustomReprs)
7-
import Clash.Annotations.TopEntity
87
import Clash.Core.TyCon
98
import Clash.Core.Var
109
import Clash.Driver
1110
import Clash.Driver.Types
1211
import Clash.GHC.Evaluator
12+
import Clash.Netlist.Types (TopEntityT)
1313
import Clash.Primitives.Types
1414

1515
import Criterion.Main
@@ -52,7 +52,7 @@ setupEnv
5252
:: [FilePath]
5353
-> FilePath
5454
-> IO ((BindingMap, TyConMap, IntMap TyConName
55-
,[(Id, Maybe TopEntity, Maybe Id)]
55+
,[TopEntityT]
5656
,CompiledPrimMap, CustomReprs, [Id], Id
5757
)
5858
,Supply.Supply

benchmark/common/BenchmarkCommon.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module BenchmarkCommon where
77

88
import Clash.Annotations.BitRepresentation.Internal (CustomReprs, buildCustomReprs)
9-
import Clash.Annotations.TopEntity
109
import Clash.Backend
1110
import Clash.Backend.VHDL
1211
import Clash.Core.TyCon
@@ -18,7 +17,7 @@ import Clash.GHC.Evaluator
1817
import Clash.GHC.GenerateBindings
1918
import Clash.GHC.NetlistTypes
2019
import Clash.Netlist.BlackBox.Types (HdlSyn(Other))
21-
import Clash.Netlist.Types (HWMap, FilteredHWType)
20+
import Clash.Netlist.Types (HWMap, FilteredHWType, TopEntityT, topId)
2221
import Clash.Primitives.Types
2322

2423
import Util (OverridingBool(..))
@@ -58,7 +57,7 @@ runInputStage
5857
-> IO (BindingMap
5958
,TyConMap
6059
,IntMap TyConName
61-
,[(Id, Maybe TopEntity, Maybe Id)]
60+
,[TopEntityT]
6261
,CompiledPrimMap
6362
,CustomReprs
6463
,[Id]
@@ -67,16 +66,15 @@ runInputStage
6766
runInputStage idirs src = do
6867
pds <- primDirs backend
6968
(bindingsMap,tcm,tupTcm,topEntities,primMap,reprs) <- generateBindings Auto pds idirs [] (hdlKind backend) src Nothing
70-
let topEntityNames = map (\(x,_,_) -> x) topEntities
71-
((topEntity,_,_):_) = topEntities
72-
tm = topEntity
69+
let topEntityNames = map topId topEntities
70+
tm = head topEntityNames
7371
return (bindingsMap,tcm,tupTcm,topEntities, primMap, buildCustomReprs reprs, topEntityNames,tm)
7472

7573
runNormalisationStage
7674
:: [FilePath]
7775
-> String
7876
-> IO (BindingMap
79-
,[(Id, Maybe TopEntity, Maybe Id)]
77+
,[TopEntityT]
8078
,CompiledPrimMap
8179
,TyConMap
8280
,CustomReprs

benchmark/profiling/run/profile-netlist-run.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Clash.Annotations.TopEntity
21
import Clash.Annotations.BitRepresentation.Internal (CustomReprs)
32
import Clash.Backend
43
import Clash.Core.Name
@@ -61,7 +60,7 @@ benchFile idirs src = do
6160
setupEnv
6261
:: FilePath
6362
-> IO (BindingMap
64-
,[(Id, Maybe TopEntity, Maybe Id)]
63+
,[TopEntityT]
6564
,CompiledPrimMap'
6665
,TyConMap
6766
,CustomReprs

clash-ghc/src-ghc/Clash/GHC/GenerateBindings.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import qualified Var as GHC
4141
import qualified SrcLoc as GHC
4242

4343
import Clash.Annotations.BitRepresentation.Internal (DataRepr')
44-
import Clash.Annotations.TopEntity (TopEntity)
4544
import Clash.Annotations.Primitive (HDL, extractPrim)
4645

4746
import Clash.Core.Subst (extendGblSubstList, mkSubst, substTm)
@@ -60,6 +59,7 @@ import Clash.GHC.GHC2Core
6059
makeAllTyCons, qualifiedNameString, emptyGHC2CoreState)
6160
import Clash.GHC.LoadModules (ghcLibDir, loadModules)
6261
import Clash.Netlist.BlackBox.Util (usedArguments)
62+
import Clash.Netlist.Types (TopEntityT(..))
6363
import Clash.Primitives.Types
6464
(Primitive (..), CompiledPrimMap)
6565
import Clash.Primitives.Util (generatePrimMap)
@@ -85,10 +85,7 @@ generateBindings
8585
-> IO ( BindingMap
8686
, TyConMap
8787
, IntMap TyConName
88-
, [( Id
89-
, Maybe TopEntity -- (maybe) TopEntity annotation
90-
, Maybe Id -- (maybe) associated testbench
91-
)]
88+
, [TopEntityT]
9289
, CompiledPrimMap -- The primitives found in '.' and 'primDir'
9390
, [DataRepr']
9491
)
@@ -124,11 +121,13 @@ generateBindings useColor primDirs importDirs dbs hdl modName dflagsM = do
124121
(\m -> fst (RWS.evalRWS m GHC.noSrcSpan tcMap')) $ mapM (\(topEnt,annM,benchM) -> do
125122
topEnt' <- coreToName GHC.varName GHC.varUnique qualifiedNameString topEnt
126123
benchM' <- traverse coreToId benchM
127-
return (topEnt',annM,benchM')) topEntities
128-
topEntities'' = map (\(topEnt,annM,benchM) -> case lookupUniqMap topEnt allBindings of
129-
Just (v,_,_,_) -> (v,annM,benchM)
130-
Nothing -> error "This shouldn't happen"
131-
) topEntities'
124+
return (topEnt', annM, benchM')) topEntities
125+
topEntities'' =
126+
map (\(topEnt, annM, benchM) ->
127+
case lookupUniqMap topEnt allBindings of
128+
Just (v,_,_,_) -> TopEntityT v annM benchM
129+
Nothing -> error "This shouldn't happen"
130+
) topEntities'
132131
-- Parsing / compiling primitives:
133132
prepTime <- startTime `deepseq` primMapC `seq` Clock.getCurrentTime
134133
let prepStartDiff = reportTimeDiff prepTime startTime

clash-lib/src/Clash/Annotations/TopEntity/Extra.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
module Clash.Annotations.TopEntity.Extra where
1010

1111
import Clash.Annotations.TopEntity (TopEntity, PortName)
12+
import Clash.Netlist.Types (TopEntityT)
1213
import Language.Haskell.TH.Syntax
1314
(ModName, Name, NameFlavour, NameSpace, PkgName, OccName)
1415
import Data.Binary (Binary)
1516
import Data.Hashable (Hashable)
1617
import Control.DeepSeq (NFData)
1718

19+
instance Binary TopEntityT
1820
instance Binary TopEntity
1921
instance Binary PortName
2022

@@ -25,6 +27,7 @@ instance Binary ModName
2527
instance Binary NameSpace
2628
instance Binary PkgName
2729

30+
instance Hashable TopEntityT
2831
instance Hashable TopEntity
2932
instance Hashable PortName
3033

@@ -35,6 +38,7 @@ instance Hashable NameSpace
3538
instance Hashable PkgName
3639
instance Hashable OccName
3740

41+
instance NFData TopEntityT
3842
instance NFData TopEntity
3943
instance NFData PortName
4044

clash-lib/src/Clash/Driver.hs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ import Clash.Netlist.Util (genComponentName, genTopCompo
7777
import Clash.Netlist.BlackBox.Parser (runParse)
7878
import Clash.Netlist.BlackBox.Types (BlackBoxTemplate, BlackBoxFunction)
7979
import Clash.Netlist.Types
80-
(BlackBox (..), Component (..), Identifier, FilteredHWType, HWMap, SomeBackend (..))
80+
(BlackBox (..), Component (..), Identifier, FilteredHWType, HWMap,
81+
SomeBackend (..), TopEntityT(..))
8182
import Clash.Normalize (checkNonRecursive, cleanupGraph,
8283
normalize, runNormalization)
8384
import Clash.Normalize.Util (callGraph)
@@ -109,13 +110,8 @@ generateHDL
109110
-- ^ Hardcoded 'Type' -> 'HWType' translator
110111
-> (PrimStep, PrimUnwind)
111112
-- ^ Hardcoded evaluator (delta-reduction)
112-
-> [( Id
113-
, Maybe TopEntity
114-
, Maybe Id
115-
)]
116-
-- ^ topEntity bndr
117-
-- + (maybe) TopEntity annotation
118-
-- + (maybe) testBench bndr
113+
-> [TopEntityT]
114+
-- ^ Topentities and associated testbench
119115
-> ClashOpts
120116
-- ^ Debug information level for the normalization process
121117
-> (Clock.UTCTime,Clock.UTCTime)
@@ -128,7 +124,7 @@ generateHDL reprs bindingsMap hdlState primMap tcm tupTcm typeTrans eval
128124
reportTimeDiff prevTime startTime
129125

130126
-- Process the next TopEntity
131-
go prevTime seen ((topEntity,annM,benchM):topEntities') = do
127+
go prevTime seen (TopEntityT topEntity annM benchM:topEntities') = do
132128
let topEntityS = Data.Text.unpack (nameOcc (varName topEntity))
133129
putStrLn $ "Clash: Compiling " ++ topEntityS
134130

@@ -259,7 +255,7 @@ generateHDL reprs bindingsMap hdlState primMap tcm tupTcm typeTrans eval
259255
. snd
260256
. Supply.freshId
261257
<$> Supply.newSupply
262-
let topEntityNames = map (\(x,_,_) -> x) topEntities
258+
let topEntityNames = map topId topEntities
263259

264260
(topTime,manifest',seen') <- if useCacheTop
265261
then do
@@ -705,32 +701,23 @@ normalizeEntity reprs bindingsMap primMap tcm tupTcm typeTrans eval topEntities
705701
-- | topologically sort the top entities
706702
sortTop
707703
:: BindingMap
708-
-> [( Id
709-
, Maybe TopEntity
710-
, Maybe Id
711-
)]
712-
-- ^ topEntity bndr
713-
-- + (maybe) TopEntity annotation
714-
-- + (maybe) testBench bndr
715-
-> [( Id
716-
, Maybe TopEntity
717-
, Maybe Id
718-
)]
719-
-- ^ topEntity bndr
720-
-- + (maybe) TopEntity annotation
721-
-- + (maybe) testBench bndr
704+
-> [TopEntityT]
705+
-> [TopEntityT]
722706
sortTop bindingsMap topEntities =
723707
let (nodes,edges) = unzip (map go topEntities)
724708
in case reverseTopSort nodes (concat edges) of
725709
Left msg -> error msg
726710
Right tops -> tops
727711
where
728-
go t@(topE,_,tbM) =
712+
go t@(TopEntityT topE _ tbM) =
729713
let topRefs = goRefs topE topE
730714
tbRefs = maybe [] (goRefs topE) tbM
731715
in ((varUniq topE,t)
732-
,map ((\(v,_,_) -> (varUniq topE, varUniq v))) (tbRefs ++ topRefs))
716+
,map ((\top -> (varUniq topE, varUniq (topId top)))) (tbRefs ++ topRefs))
733717

734718
goRefs top i =
735719
let cg = callGraph bindingsMap i
736-
in filter (\(v,_,_) -> v /= top && v /= i && v `elemVarEnv` cg) topEntities
720+
in
721+
filter
722+
(\t -> topId t /= top && topId t /= i && topId t `elemVarEnv` cg)
723+
topEntities

clash-lib/src/Clash/Netlist.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ genNetlist
8585
-- ^ Custom bit representations for certain types
8686
-> BindingMap
8787
-- ^ Global binders
88-
-> [(Id,Maybe TopEntity,Maybe Id)]
88+
-> [TopEntityT]
8989
-- ^ All the TopEntities
9090
-> CompiledPrimMap
9191
-- ^ Primitive definitions
@@ -114,17 +114,15 @@ genNetlist
114114
-- ^ Name of the @topEntity@
115115
-> IO ([([Bool],SrcSpan,HashMap Identifier Word,Component)],HashMap Identifier Word)
116116
genNetlist isTb opts reprs globals tops primMap tcm typeTrans iw mkId extId ite be seen env prefixM topEntity = do
117-
(_,s) <- runNetlistMonad isTb opts reprs globals (mkTopEntityMap tops)
117+
(_,s) <- runNetlistMonad isTb opts reprs globals topEntityMap
118118
primMap tcm typeTrans iw mkId extId ite be seen env prefixM $
119119
genComponent topEntity
120120
return ( eltsVarEnv $ _components s
121121
, _seenComps s
122122
)
123123
where
124-
mkTopEntityMap
125-
:: [(Id,Maybe TopEntity,Maybe Id)]
126-
-> VarEnv (Type,Maybe TopEntity)
127-
mkTopEntityMap = mkVarEnv . map (\(a,b,_) -> (a,(varType a,b)))
124+
topEntityMap :: VarEnv TopEntityT
125+
topEntityMap = mkVarEnv (zip (map topId tops) tops)
128126

129127
-- | Run a NetlistMonad action in a given environment
130128
runNetlistMonad
@@ -136,7 +134,7 @@ runNetlistMonad
136134
-- ^ Custom bit representations for certain types
137135
-> BindingMap
138136
-- ^ Global binders
139-
-> VarEnv (Type, Maybe TopEntity)
137+
-> VarEnv TopEntityT
140138
-- ^ TopEntity annotations
141139
-> CompiledPrimMap
142140
-- ^ Primitive Definitions
@@ -219,7 +217,7 @@ genComponentT
219217
genComponentT compName componentExpr = do
220218
varCount .= 0
221219
componentName1 <- (`lookupVarEnv'` compName) <$> Lens.use componentNames
222-
topEntMM <- fmap snd . lookupVarEnv compName <$> Lens.use topEntityAnns
220+
topEntMM <- fmap topAnnotation . lookupVarEnv compName <$> Lens.use topEntityAnns
223221
prefixM <- Lens.use componentPrefix
224222
let componentName2 = case (prefixM,join topEntMM) of
225223
((Just p,_),Just ann) -> p `StrictText.append` StrictText.pack ('_':t_name ann)
@@ -233,8 +231,8 @@ genComponentT compName componentExpr = do
233231
-- HACK: Determine resulttype of this function by looking at its definition
234232
-- in topEntityAnns, instead of looking at its last binder (which obscure
235233
-- any attributes [see: Clash.Annotations.SynthesisAttributes]).
236-
topEntityTypeM <- lookupVarEnv compName <$> Lens.use topEntityAnns
237-
let topEntityTypeM' = snd . splitCoreFunForallTy tcm . fst <$> topEntityTypeM
234+
topEntityTypeM <- lookupVarEnv compName <$> Lens.use topEntityAnns
235+
let topEntityTypeM' = snd . splitCoreFunForallTy tcm . varType . topId <$> topEntityTypeM
238236

239237
seenIds .= HashMapS.empty
240238
(wereVoids,compInps,argWrappers,compOutps,resUnwrappers,binders,resultM) <-
@@ -564,13 +562,15 @@ mkFunApp dstId fun args tickDecls = do
564562
topAnns <- Lens.use topEntityAnns
565563
tcm <- Lens.use tcCache
566564
case lookupVarEnv fun topAnns of
567-
Just (ty,annM)
568-
| let (fArgTys0,fResTy) = splitFunTys tcm ty
565+
Just topEntity
566+
| let ty = varType (topId topEntity)
567+
, let (fArgTys0,fResTy) = splitFunTys tcm ty
569568
-- Take into account that clocks and stuff are split off from any product
570569
-- types containing them
571570
, let fArgTys1 = splitShouldSplit tcm fArgTys0
572571
, length fArgTys1 == length args
573572
-> do
573+
let annM = topAnnotation topEntity
574574
argHWTys <- mapM (unsafeCoreTypeToHWTypeM' $(curLoc)) fArgTys1
575575
(argExprs, concat -> argDecls) <- unzip <$>
576576
mapM (\(e,t) -> mkExpr False Concurrent (NetlistId dstId t) e)

clash-lib/src/Clash/Netlist/Types.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ import Clash.Util (HasCallStack, makeLenses)
6868
import Clash.Annotations.BitRepresentation.Internal
6969
(CustomReprs, DataRepr', ConstrRepr')
7070

71+
-- | Structure describing a top entity: it's id, its port annotations, and
72+
-- associated testbench.
73+
data TopEntityT = TopEntityT
74+
{ topId :: Id
75+
-- ^ Id of top entity
76+
, topAnnotation :: Maybe TopEntity
77+
-- ^ (Maybe) a topentity annotation
78+
, associatedTestbench :: Maybe Id
79+
-- ^ (Maybe) a test bench associated with the topentity
80+
} deriving (Generic)
81+
7182
-- | Monad that caches generated components (StateT) and remembers hidden inputs
7283
-- of components that are being generated (WriterT)
7384
newtype NetlistMonad a =
@@ -115,7 +126,7 @@ data NetlistState
115126
-- filter duplicate warning invocations for dubious blackbox instantiations,
116127
-- see GitHub pull request #286.
117128
, _componentNames :: VarEnv Identifier
118-
, _topEntityAnns :: VarEnv (Type, Maybe TopEntity)
129+
, _topEntityAnns :: VarEnv TopEntityT
119130
, _hdlDir :: FilePath
120131
, _curBBlvl :: Int
121132
-- ^ The current scoping level assigned to black box contexts

testsuite/src/Test/Tasty/Clash/NetlistTest.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ runToNetlistStage target f src = do
9292
(bm, tcm, tupTcm, tes, pm, rs)
9393
<- generateBindings Auto pds (opt_importPaths opts) [] (hdlKind backend) src Nothing
9494

95-
let teNames = fmap fstTriple tes
96-
te = fstTriple (P.head tes)
95+
let teNames = fmap topId tes
96+
te = topId (P.head tes)
9797
reprs = buildCustomReprs rs
9898

9999
supplyN <- Supply.newSupply
@@ -106,8 +106,6 @@ runToNetlistStage target f src = do
106106
backend = mkBackend target
107107
opts = f mkClashOpts
108108

109-
fstTriple (x, _, _) = x
110-
111109
netlistFrom (bm, tcm, tes, pm, rs, te) =
112110
genNetlist False opts rs bm tes pm tcm typeTrans
113111
iw mkId1 extId ite (SomeBackend hdlSt) seen hdlDir prefixM te

0 commit comments

Comments
 (0)