Skip to content

Commit

Permalink
Account for SimOnly in makeTopEntity (#2897)
Browse files Browse the repository at this point in the history
Note that we still want to generate `PortName`s for zero-width
constructs, we just don't want them split out.
  • Loading branch information
martijnbastiaan authored Mar 5, 2025
1 parent 802f2c8 commit db19820
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2025-03-04T15_40_21+01_00_account_for_simonly
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FIXED: `makeTopEntity` now accounts for `SimOnly` constructs. This can prevent warnings in situtations where the `SimOnly` type would contain types `makeTopEntity` cannot handle.
4 changes: 4 additions & 0 deletions clash-prelude/src/Clash/Annotations/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import Data.Generics.Uniplate.Data (rewrite)
import Clash.Annotations.TopEntity ( PortName(..)
, TopEntity(..)
)
import Clash.Magic (SimOnly)
import Clash.NamedTypes ((:::))
import Clash.Signal ( HiddenClockResetEnable
, HiddenClock, HiddenReset, HiddenEnable
Expand Down Expand Up @@ -225,6 +226,7 @@ datatypeNameToPorts name = do
constructors <- tryReifyDatatype [] datatypeCons name

names <- case constructors of
_ | name == ''SimOnly -> return $ Complete []
[] -> return $ Complete []
[x] -> portsFromTypes (constructorFields x)
xs -> handleNamesInSum xs
Expand Down Expand Up @@ -283,6 +285,8 @@ typeTreeToPorts (ConTF name) = do
typeTreeToPorts f@(AppTF (a,a') (b,b')) = do
-- Gather types applied to a head type
case unapp (AppT a b) of
-- Skip SimOnly constructs - they're always zero bits
(ConT x : _) | x == ''SimOnly -> pure (Complete [])
-- Return the inner type for signals
(ConT x : _ : _ : []) | x == ''Clash.Signal.Signal -> b'
(ConT x : _ : _ : _ : []) | x == ''Clash.Signal.Delayed.DSignal -> b'
Expand Down
42 changes: 42 additions & 0 deletions clash-prelude/tests/Clash/Tests/TopEntityGeneration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ data HKD f = HKD
, fd2 :: OrderedCF 'True f ("fd2" ::: Bool)
}

data RecordWithSimOnlyField = RecordWithSimOnlyField
{ simOnlyField1 :: "a" ::: SimOnly (Signal System ("foo" ::: Int))
, simOnlyField3 :: "b" ::: Signal System (SimOnly ("bar" ::: Int))
, someOtherField :: "c" ::: Signal System ("wobble" ::: Int)
}


topEntity1 :: "in1" ::: Signal System Int
-> "in2" ::: Signal System Bool
Expand Down Expand Up @@ -225,6 +231,36 @@ expectedTopEntity8 =
]
(PortName "out")

topEntity9 :: (HiddenClockResetEnable System)
=> "pair" ::: SimOnly (Signal System (Pair Bool))
-> "pair" ::: Signal System (SimOnly (Pair Single))
-> "out" ::: Signal System Int
topEntity9 = undefined
makeTopEntity 'topEntity9

expectedTopEntity9 :: TopEntity
expectedTopEntity9 =
Synthesize "topEntity9"
[ PortProduct "" [PortName "clk", PortName "rst", PortName "en"]
, PortName "pair"
, PortName "pair"
]
(PortName "out")

topEntity10 :: (HiddenClockResetEnable System)
=> "record" ::: Signal System RecordWithSimOnlyField
-> "out" ::: Signal System Int
topEntity10 = undefined
makeTopEntity 'topEntity10

expectedTopEntity10 :: TopEntity
expectedTopEntity10 =
Synthesize "topEntity10"
[ PortProduct "" [PortName "clk", PortName "rst", PortName "en"]
, PortProduct "record" [PortName "a",PortName "b",PortName "c_wobble"]
]
(PortName "out")

topEntityFailure1
:: "int" ::: Signal System Int
-> "tuple" ::: ("tup1" ::: Signal System (BitVector 7), "tup2" ::: Signal System (BitVector 9))
Expand Down Expand Up @@ -300,6 +336,12 @@ tests =
, testCase "topEntity8" $
$(unTypeQ $ maybeBuildTopEntity Nothing 'topEntity8)
@?= Just expectedTopEntity8
, testCase "topEntity9" $
$(unTypeQ $ maybeBuildTopEntity Nothing 'topEntity9)
@?= Just expectedTopEntity9
, testCase "topEntity10" $
$(unTypeQ $ maybeBuildTopEntity Nothing 'topEntity10)
@?= Just expectedTopEntity10
]
, testGroup "Expected failures"
[ testCase "topEntityFailure1" $
Expand Down

0 comments on commit db19820

Please sign in to comment.