Skip to content

Commit

Permalink
Use more quotations in TH module
Browse files Browse the repository at this point in the history
A seemingly bogus warning in GHC < 9.6 forces us to write some patterns
oddly to silence the warning.

For instance, instead of writing

    [d|
      Circuit $(varP ...) = ...
      |]

we're forced to write

    [d|
      $[p| Circuit $(varP ...) |] = ...
      |]

where we wrap a pattern P as $[p| P |] .
  • Loading branch information
DigitalBrains1 committed Nov 21, 2024
1 parent f2d9e1c commit 1af0259
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
1 change: 1 addition & 0 deletions clash-protocols/src/Protocols/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ instance (Drivable a, Drivable b) => Drivable (a, b) where
)

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

0 comments on commit 1af0259

Please sign in to comment.