Skip to content

Commit 5b32515

Browse files
committed
wait using MVars
1 parent f39fb98 commit 5b32515

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

clash-lib/src/Clash/Core/VarEnv.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ module Clash.Core.VarEnv
3838
-- ** Conversions
3939
-- *** Lists
4040
, eltsVarEnv
41+
, toListVarEnv
42+
, listToVarEnv
4143
-- * Sets of variables
4244
, VarSet
4345
-- ** Construction
@@ -261,6 +263,15 @@ eltsVarEnv
261263
-> [a]
262264
eltsVarEnv = eltsUniqMap
263265

266+
toListVarEnv :: VarEnv a -> [(Unique, a)]
267+
toListVarEnv = toListUniqMap
268+
269+
listToVarEnv
270+
:: Uniquable a
271+
=> [(a, b)]
272+
-> VarEnv b
273+
listToVarEnv = listToUniqMap
274+
264275
-- | Does the variable exist in the environment
265276
elemVarEnv
266277
:: Var a

clash-lib/src/Clash/Normalize.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ import Clash.Core.Type (isPolyTy)
7070
import Clash.Core.Var (Id, varName, varType)
7171
import Clash.Core.VarEnv
7272
(VarEnv, VarSet, elemVarSet, eltsVarEnv, emptyInScopeSet, emptyVarEnv, emptyVarSet,
73-
extendVarEnv, extendVarSet, lookupVarEnv, mapVarEnv, mapMaybeVarEnv,
74-
mkVarEnv, mkVarSet, notElemVarEnv, notElemVarSet, nullVarEnv)
73+
extendVarEnv, extendVarSet, lookupVarEnv, mapMaybeVarEnv,
74+
mkVarEnv, mkVarSet, notElemVarEnv, notElemVarSet, nullVarEnv,
75+
listToVarEnv, toListVarEnv)
7576
import Clash.Debug (traceIf)
7677
import Clash.Driver.Types
7778
(BindingMap, Binding(..), DebugOpts(..), ClashEnv(..))
@@ -234,11 +235,11 @@ normalize' nm q = do
234235
normV <- Lens.use (extra.normalized)
235236

236237
toNormalize <-
237-
MVar.withMVar normV $ \norm ->
238-
let prevNorm = mapVarEnv bindingId norm
239-
toNormalize = filter (`notElemVarSet` topEnts)
238+
MVar.withMVar normV $ \norm -> do
239+
prevNorm <- listToVarEnv <$> traverse (\(k, v) -> (k,) . bindingId <$> MVar.readMVar v) (toListVarEnv norm)
240+
let toNormalize = filter (`notElemVarSet` topEnts)
240241
$ filter (`notElemVarEnv` extendVarEnv nm nm prevNorm) usedBndrs
241-
in pure toNormalize
242+
in pure toNormalize
242243

243244
traverse_ (Monad.liftIO . MS.pushL q) toNormalize
244245
pure (nm, tmNorm)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import Clash.Core.Term (Term)
2323
import Clash.Core.Type (Type)
2424
import Clash.Core.Var (Id)
2525
import Clash.Core.VarEnv (VarEnv)
26-
import Clash.Driver.Types (BindingMap)
26+
import Clash.Driver.Types (Binding)
2727
import Clash.Rewrite.Types (Rewrite, RewriteMonad)
2828

2929
-- | State of the 'NormalizeMonad'
3030
data NormalizeState
3131
= NormalizeState
32-
{ _normalized :: MVar BindingMap
32+
{ _normalized :: MVar (VarEnv (MVar (Binding Term)))
3333
-- ^ Global binders
3434
, _specialisationCache :: MVar (Map (Id,Int,Either Term Type) Id)
3535
-- ^ Cache of previously specialized functions:

clash-lib/src/Clash/Normalize/Util.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,19 @@ normalizeTopLvlBndr
424424
normalizeTopLvlBndr isTop nm (Binding nm' sp inl pr tm _) = do
425425
normalizedV <- Lens.use (extra.normalized)
426426

427-
mValue <- MVar.withMVar normalizedV (pure . lookupVarEnv nm)
428-
429427
-- TODO This was a call to makeCachedU, but since there was no variation
430428
-- for MVar, I unrolled everything. Maybe there should be MVar versions of
431429
-- the makeCachedX functions needed in normalization.
432-
case mValue of
433-
Just value ->
434-
pure value
435430

431+
cache <- MVar.takeMVar normalizedV
432+
case lookupVarEnv nm cache of
433+
Just vMVar -> do
434+
MVar.putMVar normalizedV cache
435+
MVar.readMVar vMVar
436436
Nothing -> do
437+
tmp <- MVar.newEmptyMVar
438+
MVar.putMVar normalizedV (extendVarEnv nm tmp cache)
439+
437440
tcm <- Lens.view tcCache
438441
let nmS = showPpr (varName nm)
439442
-- We deshadow the term because sometimes GHC gives us
@@ -454,8 +457,8 @@ normalizeTopLvlBndr isTop nm (Binding nm' sp inl pr tm _) = do
454457
let r' = nm' `globalIdOccursIn` tm3
455458
let value = Binding nm'{varType = ty'} sp inl pr tm3 r'
456459

457-
MVar.modifyMVar normalizedV $ \binders ->
458-
pure (extendVarEnv nm value binders, value)
460+
MVar.putMVar tmp value
461+
pure value
459462

460463
-- | Turn type equality constraints into substitutions and apply them.
461464
--

0 commit comments

Comments
 (0)