Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more quotations in TH module #124

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clash-protocols/src/Protocols/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

-- | Protocol-agnostic acknowledgement
newtype Ack = Ack Bool
deriving (Generic, C.NFDataX, Show, C.Bundle, Eq, Ord)

Check warning on line 62 in clash-protocols/src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.2.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 62 in clash-protocols/src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.2.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 62 in clash-protocols/src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.4.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 62 in clash-protocols/src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.4.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 62 in clash-protocols/src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.6.4 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 62 in clash-protocols/src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.6.4 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

-- | Acknowledge. Used in circuit-notation plugin to drive ignore components.
instance Default Ack where
Expand Down Expand Up @@ -265,6 +265,7 @@
)

drivableTupleInstances 3 maxTupleSize

instance (CE.KnownNat n, Simulate a) => Simulate (C.Vec n a) where
type SimulateFwdType (C.Vec n a) = C.Vec n (SimulateFwdType a)
type SimulateBwdType (C.Vec n a) = C.Vec n (SimulateBwdType a)
Expand Down
67 changes: 37 additions & 30 deletions clash-protocols/src/Protocols/Internal/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Protocols.Internal.TH where

import qualified Clash.Prelude as C
import Control.Monad (zipWithM)
import Control.Monad.Extra (concatMapM)
import Data.Proxy
import GHC.TypeNats
Expand Down Expand Up @@ -53,7 +54,7 @@ simulateTupleInstance n =
sigToSimFwd _ $fwdPat0 = $(tupE $ zipWith (\ty expr -> [e|sigToSimFwd (Proxy @($ty)) $expr|]) circTys fwdExpr)
sigToSimBwd _ $bwdPat0 = $(tupE $ zipWith (\ty expr -> [e|sigToSimBwd (Proxy @($ty)) $expr|]) circTys bwdExpr)

stallC $(varP $ mkName "conf") $(varP $ mkName "rem0") = $(letE (stallVecs ++ stallCircuits) stallCExpr)
stallC $(varP $ mkName "conf") $(varP $ mkName "rem0") = $stallCExpr
|]
where
-- Generate the types for the instance
Expand All @@ -73,43 +74,49 @@ simulateTupleInstance n =
bwdExpr1 = map (\i -> varE $ mkName $ "bwdStalled" <> show i) [1 .. n]

-- stallC Declaration: Split off the stall vectors from the large input vector
stallVecs = zipWith mkStallVec [1 .. n] circTys
mkStallVec i ty =
valD
mkStallPat
( normalB [e|(C.splitAtI @(SimulateChannels $ty) $(varE (mkName $ "rem" <> show (i - 1))))|]
)
[]
where
mkStallPat =
tupP
[ varP (mkName $ "stalls" <> show i)
, varP (mkName $ if i == n then "_" else "rem" <> show i)
]
[d|
$[p|
( $(varP (mkName $ "stalls" <> show i))
, $(varP (mkName $ if i == n then "_" else "rem" <> show i))
)
|] =
C.splitAtI @(SimulateChannels $ty)
$(varE $ mkName $ "rem" <> show (i - 1))
|]

-- stallC Declaration: Generate stalling circuits
stallCircuits = zipWith mkStallCircuit [1 .. n] circTys
mkStallCircuit i ty =
valD
[p|Circuit $(varP $ mkName $ "stalled" <> show i)|]
(normalB [e|stallC @($ty) conf $(varE $ mkName $ "stalls" <> show i)|])
[]
[d|
$[p|Circuit $(varP $ mkName $ "stalled" <> show i)|] =
stallC @($ty) conf $(varE $ mkName $ "stalls" <> show i)
|]

-- Generate the stallC expression
stallCExpr =
[e|
Circuit $ \($fwdPat0, $bwdPat0) -> $(letE stallCResultDecs [e|($(tupE fwdExpr1), $(tupE bwdExpr1))|])
|]
stallCExpr = do
stallVecs <-
concat <$> zipWithM mkStallVec [1 .. n] circTys
stallCircuits <-
concat <$> zipWithM mkStallCircuit [1 .. n] circTys
LetE (stallVecs <> stallCircuits)
<$> [e|Circuit $ \($fwdPat0, $bwdPat0) -> $circuitResExpr|]

circuitResExpr = do
stallCResultDecs <- concatMapM mkStallCResultDec [1 .. n]
LetE stallCResultDecs <$> [e|($(tupE fwdExpr1), $(tupE bwdExpr1))|]

stallCResultDecs = map mkStallCResultDec [1 .. n]
mkStallCResultDec i =
valD
(tupP [varP $ mkName $ "fwdStalled" <> show i, varP $ mkName $ "bwdStalled" <> show i])
( normalB $
appE (varE $ mkName $ "stalled" <> show i) $
tupE [varE $ mkName $ "fwd" <> show i, varE $ mkName $ "bwd" <> show i]
)
[]
[d|
$[p|
( $(varP $ mkName $ "fwdStalled" <> show i)
, $(varP $ mkName $ "bwdStalled" <> show i)
)
|] =
$(varE $ mkName $ "stalled" <> show i)
( $(varE $ mkName $ "fwd" <> show i)
, $(varE $ mkName $ "bwd" <> show i)
)
|]

drivableTupleInstances :: Int -> Int -> DecsQ
drivableTupleInstances n m = concatMapM drivableTupleInstance [n .. m]
Expand Down
Loading