Skip to content

Commit 90e7d5f

Browse files
committed
Eliminate the separate implementation of NameMap, as it's just NameMapE applied to LiftE.
1 parent b03fdff commit 90e7d5f

File tree

7 files changed

+33
-80
lines changed

7 files changed

+33
-80
lines changed

src/lib/CheckType.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ liftTyperM cont =
5656
affineUsed :: AtomName r o -> TyperM r i o ()
5757
affineUsed name = TyperM $ do
5858
affines <- get
59-
case lookupNameMap name affines of
60-
Just n -> if n > 0 then
61-
throw TypeErr $ "Affine name " ++ pprint name ++ " used " ++ show (n + 1) ++ " times."
62-
else
63-
put $ insertNameMap name (n + 1) affines
64-
Nothing -> put $ insertNameMap name 1 affines
59+
case lookupNameMapE name affines of
60+
Just (LiftE n) ->
61+
if n > 0 then
62+
throw TypeErr $ "Affine name " ++ pprint name ++ " used " ++ show (n + 1) ++ " times."
63+
else
64+
put $ insertNameMapE name (LiftE $ n + 1) affines
65+
Nothing -> put $ insertNameMapE name (LiftE 1) affines
6566

6667
parallelAffines :: [TyperM r i o a] -> TyperM r i o [a]
6768
parallelAffines actions = TyperM $ do
@@ -77,7 +78,7 @@ parallelAffines actions = TyperM $ do
7778
result <- runTyperT' act
7879
(result,) <$> get
7980
put affines
80-
forM_ (toListNameMap $ unionsWithNameMap max isolateds) \(name, ct) ->
81+
forM_ (toListNameMapE $ unionsWithNameMapE max isolateds) \(name, (LiftE ct)) ->
8182
case ct of
8283
0 -> return ()
8384
1 -> runTyperT' $ affineUsed name

src/lib/Core.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,5 +482,3 @@ freshNameM hint = do
482482
Distinct <- getDistinct
483483
return $ withFresh hint scope \b -> Abs b (binderName b)
484484
{-# INLINE freshNameM #-}
485-
486-
type AtomNameMap r = NameMap (AtomNameC r)

src/lib/Lower.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,18 @@ lowerCase maybeDest scrut alts resultTy = do
217217
-- so that it never allocates scratch space for its result, but will put it directly in
218218
-- the corresponding slice of the full 2D buffer.
219219

220-
type DestAssignment (i'::S) (o::S) = AtomNameMap SimpIR (ProjDest o) i'
220+
type DestAssignment (i'::S) (o::S) = NameMap (AtomNameC SimpIR) (ProjDest o) i'
221221

222222
data ProjDest o
223223
= FullDest (Dest SimpIR o)
224224
| ProjDest (NE.NonEmpty Projection) (Dest SimpIR o) -- dest corresponds to the projection applied to name
225+
deriving (Show)
225226

226227
instance SinkableE ProjDest where
227228
sinkingProofE = todoSinkableProof
228229

229230
lookupDest :: DestAssignment i' o -> SAtomName i' -> Maybe (ProjDest o)
230-
lookupDest = flip lookupNameMap
231+
lookupDest dests = fmap fromLiftE . flip lookupNameMapE dests
231232

232233
-- Matches up the free variables of the atom, with the given dest. For example, if the
233234
-- atom is a pair of two variables, the dest might be split into per-component dests,
@@ -238,10 +239,10 @@ lookupDest = flip lookupNameMap
238239
-- XXX: When adding more cases, be careful about potentially repeated vars in the output!
239240
decomposeDest :: Emits o => Dest SimpIR o -> SAtom i' -> LowerM i o (Maybe (DestAssignment i' o))
240241
decomposeDest dest = \case
241-
Var v -> return $ Just $ singletonNameMap (atomVarName v) $ FullDest dest
242+
Var v -> return $ Just $ singletonNameMapE (atomVarName v) $ LiftE $ FullDest dest
242243
ProjectElt _ p x -> do
243244
(ps, v) <- return $ asNaryProj p x
244-
return $ Just $ singletonNameMap (atomVarName v) $ ProjDest ps dest
245+
return $ Just $ singletonNameMapE (atomVarName v) $ LiftE $ ProjDest ps dest
245246
_ -> return Nothing
246247

247248
lowerBlockWithDest :: Emits o => Dest SimpIR o -> SBlock i -> LowerM i o (SAtom o)
@@ -258,7 +259,7 @@ lowerBlockWithDest dest (Abs decls ans) = do
258259
Just DistinctBetween -> do
259260
s' <- traverseDeclNestWithDestS destMap s decls
260261
-- But we have to emit explicit writes, for all the vars that are not defined in decls!
261-
forM_ (toListNameMap $ hoistFilterNameMap decls destMap) \(n, d) -> do
262+
forM_ (toListNameMapE $ hoistNameMap decls destMap) \(n, (LiftE d)) -> do
262263
x <- case s ! n of
263264
Rename v -> Var <$> toAtomVar v
264265
SubstVal a -> return a

src/lib/MTL1.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ instance HoistableState UnitE where
223223
hoistState _ _ UnitE = UnitE
224224
{-# INLINE hoistState #-}
225225

226-
instance HoistableState (NameMap c a) where
227-
hoistState _ b m = hoistFilterNameMap b m
226+
instance Show a => HoistableState (NameMap c a) where
227+
hoistState _ b m = hoistNameMap b m
228228
{-# INLINE hoistState #-}
229229

230230
-------------------- ScopedT1 --------------------

src/lib/Name.hs

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ newtype NonEmptyListE (e::E) (n::S) = NonEmptyListE { fromNonEmptyListE :: NonEm
481481
deriving (Show, Eq, Generic)
482482

483483
newtype LiftE (a:: *) (n::S) = LiftE { fromLiftE :: a }
484-
deriving (Show, Eq, Generic, Monoid, Semigroup)
484+
deriving (Show, Eq, Ord, Generic, Monoid, Semigroup)
485485

486486
newtype ComposeE (f :: * -> *) (e::E) (n::S) =
487487
ComposeE { fromComposeE :: (f (e n)) }
@@ -3262,60 +3262,6 @@ instance HoistableB b => HoistableB (WithAttrB a b) where
32623262

32633263
-- Hoisting the map removes entries that are no longer in scope.
32643264

3265-
newtype NameMap (c::C) (a:: *) (n::S) = UnsafeNameMap (RawNameMap a)
3266-
deriving (Eq, Semigroup, Monoid, Store)
3267-
3268-
hoistFilterNameMap :: BindsNames b => b n l -> NameMap c a l -> NameMap c a n
3269-
hoistFilterNameMap b (UnsafeNameMap raw) =
3270-
UnsafeNameMap $ raw `R.difference` frag
3271-
where UnsafeMakeScopeFrag frag = toScopeFrag b
3272-
{-# INLINE hoistFilterNameMap #-}
3273-
3274-
insertNameMap :: Name c n -> a -> NameMap c a n -> NameMap c a n
3275-
insertNameMap (UnsafeMakeName n) x (UnsafeNameMap raw) = UnsafeNameMap $ R.insert n x raw
3276-
{-# INLINE insertNameMap #-}
3277-
3278-
lookupNameMap :: Name c n -> NameMap c a n -> Maybe a
3279-
lookupNameMap (UnsafeMakeName n) (UnsafeNameMap raw) = R.lookup n raw
3280-
{-# INLINE lookupNameMap #-}
3281-
3282-
singletonNameMap :: Name c n -> a -> NameMap c a n
3283-
singletonNameMap (UnsafeMakeName n) x = UnsafeNameMap $ R.singleton n x
3284-
{-# INLINE singletonNameMap #-}
3285-
3286-
toListNameMap :: NameMap c a n -> [(Name c n, a)]
3287-
toListNameMap (UnsafeNameMap raw) = R.toList raw <&> \(r, x) -> (UnsafeMakeName r, x)
3288-
{-# INLINE toListNameMap #-}
3289-
3290-
unionWithNameMap :: (a -> a -> a) -> NameMap c a n -> NameMap c a n -> NameMap c a n
3291-
unionWithNameMap f (UnsafeNameMap raw1) (UnsafeNameMap raw2) =
3292-
UnsafeNameMap $ R.unionWith f raw1 raw2
3293-
{-# INLINE unionWithNameMap #-}
3294-
3295-
unionsWithNameMap :: (Foldable f) => (a -> a -> a) -> f (NameMap c a n) -> NameMap c a n
3296-
unionsWithNameMap func maps =
3297-
foldl' (unionWithNameMap func) mempty maps
3298-
{-# INLINE unionsWithNameMap #-}
3299-
3300-
traverseNameMap :: (Applicative f) => (a -> f b)
3301-
-> NameMap c a n -> f (NameMap c b n)
3302-
traverseNameMap f (UnsafeNameMap raw) = UnsafeNameMap <$> traverse f raw
3303-
{-# INLINE traverseNameMap #-}
3304-
3305-
mapNameMap :: (a -> b) -> NameMap c a n -> (NameMap c b n)
3306-
mapNameMap f (UnsafeNameMap raw) = UnsafeNameMap $ fmap f raw
3307-
{-# INLINE mapNameMap #-}
3308-
3309-
keysNameMap :: NameMap c a n -> [Name c n]
3310-
keysNameMap = map fst . toListNameMap
3311-
{-# INLINE keysNameMap #-}
3312-
3313-
keySetNameMap :: (Color c) => NameMap c a n -> NameSet n
3314-
keySetNameMap nmap = freeVarsE $ ListE $ keysNameMap nmap
3315-
3316-
instance SinkableE (NameMap c a) where
3317-
sinkingProofE = undefined
3318-
33193265
newtype NameMapE (c::C) (e:: E) (n::S) = UnsafeNameMapE (RawNameMap (e n))
33203266
deriving (Eq, Semigroup, Monoid, Store)
33213267

@@ -3353,6 +3299,11 @@ unionWithNameMapE f (UnsafeNameMapE raw1) (UnsafeNameMapE raw2) =
33533299
UnsafeNameMapE $ R.unionWith f raw1 raw2
33543300
{-# INLINE unionWithNameMapE #-}
33553301

3302+
unionsWithNameMapE :: (Foldable f) => (e n -> e n -> e n) -> f (NameMapE c e n) -> NameMapE c e n
3303+
unionsWithNameMapE func maps =
3304+
foldl' (unionWithNameMapE func) mempty maps
3305+
{-# INLINE unionsWithNameMapE #-}
3306+
33563307
traverseNameMapE :: (Applicative f) => (e1 n -> f (e2 n))
33573308
-> NameMapE c e1 n -> f (NameMapE c e2 n)
33583309
traverseNameMapE f (UnsafeNameMapE raw) = UnsafeNameMapE <$> traverse f raw
@@ -3379,6 +3330,13 @@ instance RenameE e => RenameE (NameMapE c e) where
33793330
instance HoistableE e => HoistableE (NameMapE c e) where
33803331
freeVarsE = undefined
33813332

3333+
type NameMap (c::C) (a:: *) = NameMapE c (LiftE a)
3334+
3335+
hoistNameMap :: (BindsNames b, Show a)
3336+
=> b n l -> NameMap c a l -> (NameMap c a n)
3337+
hoistNameMap b = ignoreHoistFailure . hoistNameMapE b
3338+
{-# INLINE hoistNameMap #-}
3339+
33823340
-- === E-kinded IR coercions ===
33833341

33843342
-- XXX: the intention is that we won't have to use this much

src/lib/Occurrence.hs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ class MaxPlus a where
8888
max :: a -> a -> a
8989
plus :: a -> a -> a
9090

91-
instance (MaxPlus a) => MaxPlus (NameMap c a n) where
92-
zero = mempty
93-
max = unionWithNameMap max
94-
plus = unionWithNameMap plus
95-
9691
instance (MaxPlus (e n)) => MaxPlus (NameMapE c e n) where
9792
zero = mempty
9893
max = unionWithNameMapE max

src/lib/Vectorize.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ askVectorByteWidth :: TopVectorizeM i o Word32
131131
askVectorByteWidth = TopVectorizeM $ SubstReaderT $ lift $ lift11 (fromLiftE <$> ask)
132132

133133
extendCommuteMap :: AtomName SimpIR o -> MonoidCommutes -> TopVectorizeM i o a -> TopVectorizeM i o a
134-
extendCommuteMap name commutativity = local $ insertNameMap name commutativity
134+
extendCommuteMap name commutativity = local $ insertNameMapE name $ LiftE commutativity
135135

136136
vectorizeLoopsDestBlock :: DestBlock i
137137
-> TopVectorizeM i o (DestBlock o)
@@ -309,9 +309,9 @@ vectorSafeEffect (EffectRow effs NoTail) = allM safe $ eSetToList effs where
309309
safe (RWSEffect Writer (Var h)) = do
310310
h' <- renameM $ atomVarName h
311311
commuteMap <- ask
312-
case lookupNameMap h' commuteMap of
313-
Just Commutes -> return True
314-
Just DoesNotCommute -> return False
312+
case lookupNameMapE h' commuteMap of
313+
Just (LiftE Commutes) -> return True
314+
Just (LiftE DoesNotCommute) -> return False
315315
Nothing -> error $ "Handle " ++ pprint h ++ " not present in commute map?"
316316
safe _ = return False
317317

0 commit comments

Comments
 (0)