Skip to content

Commit c9ab62c

Browse files
committed
Better names for combinators that manipulate the state of OCCM actions.
`censored` is inspired by `censor` from `MonadWriter`, but (i) can't reuse that name, and (ii) adverbs are better here anyway because we are modifying actions.
1 parent bac03f8 commit c9ab62c

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/lib/OccAnalysis.hs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,27 @@ countFreeVarsAsOccurrencesB obj =
144144
forM_ (freeAtomVarsList $ Abs obj UnitE) \name -> do
145145
modify (<> FV (singletonNameMapE name $ AccessInfo One accessOnce))
146146

147-
-- Run the given action with its own FV state, and return the FVs it
148-
-- accumulates for post-processing.
149-
isolated :: OCCM n a -> OCCM n (a, FV n)
150-
isolated action = do
147+
-- Run the given action with its own FV state, and return the FVs it accumulates
148+
-- for post-processing. Merging them back in is up to the caller.
149+
separately :: OCCM n a -> OCCM n (a, FV n)
150+
separately action = do
151151
r <- ask
152152
lift11 $ lift11 $ runStateT1 (runReaderT1 r action) mempty
153153

154+
-- Run the given action with its own FV state, and process its accumulated FVs
155+
-- before merging.
156+
censored :: (FV n -> FV n) -> OCCM n a -> OCCM n a
157+
censored f act = do
158+
(a, fvs) <- separately act
159+
modify (<> f fvs)
160+
return a
161+
162+
-- Run the given action with its own FV state, then merge its accumulated FVs
163+
-- afterwards. (This is only meaningful if the action reads the FV state.)
164+
isolated :: OCCM n a -> OCCM n a
165+
isolated = censored id
166+
{-# INLINE isolated #-}
167+
154168
-- Extend the IxExpr environment
155169
extend :: (BindsOneName b (AtomNameC SimpIR))
156170
=> b any n -> IxExpr n -> OCCM n a -> OCCM n a
@@ -171,8 +185,8 @@ ixExpr name = do
171185
-- including statically.
172186
inlinedLater :: (HoistableE e) => e n -> OCCM n (e n)
173187
inlinedLater obj = do
174-
(_, fvs) <- isolated $ countFreeVarsAsOccurrences obj
175-
modify (<> useManyTimesStatic (useManyTimes fvs))
188+
void $ censored (useManyTimesStatic . useManyTimes)
189+
$ countFreeVarsAsOccurrences obj
176190
return obj
177191

178192
-- === Computing IxExpr summaries ===
@@ -289,9 +303,7 @@ occNest a (Abs decls ans) = case decls of
289303
\d'@(Let b' (DeclBinding _ expr')) rest -> do
290304
exprIx <- summaryExpr $ sink expr'
291305
extend b' exprIx do
292-
(below, belowfvs) <- isolated do
293-
occNest (sink a) rest >>= wrapWithCachedFVs
294-
modify (<> belowfvs)
306+
below <- isolated $ occNest (sink a) rest >>= wrapWithCachedFVs
295307
accessInfo <- getAccessInfo $ binderName d'
296308
let usage = usageInfo accessInfo
297309
let dceAttempt = case isPureDecl of
@@ -350,7 +362,7 @@ instance HasOCC SExpr where
350362
Case scrut alts (EffTy effs ty) -> do
351363
scrut' <- occ accessOnce scrut
352364
scrutIx <- summary scrut
353-
(alts', innerFVs) <- unzip <$> mapM (isolated . occAlt a scrutIx) alts
365+
(alts', innerFVs) <- unzip <$> mapM (separately . occAlt a scrutIx) alts
354366
modify (<> foldl' Occ.max zero innerFVs)
355367
ty' <- occTy ty
356368
countFreeVarsAsOccurrences effs
@@ -399,14 +411,10 @@ instance HasOCC (Hof SimpIR) where
399411
ixDict' <- inlinedLater ixDict
400412
occWithBinder (Abs b body) \b' body' -> do
401413
extend b' (Occ.Var $ binderName b') do
402-
(body'', bodyFV) <- isolated (occNest accessOnce body')
403-
modify (<> abstractFor b' bodyFV)
414+
body'' <- censored (abstractFor b') (occNest accessOnce body')
404415
return $ For ann ixDict' (UnaryLamExpr b' body'')
405416
For _ _ _ -> error "For body should be a unary lambda expression"
406-
While body -> While <$> do
407-
(body', bodyFV) <- isolated $ occNest accessOnce body
408-
modify (<> useManyTimes bodyFV)
409-
return body'
417+
While body -> While <$> censored useManyTimes (occNest accessOnce body)
410418
RunReader ini bd -> do
411419
iniIx <- summary ini
412420
bd' <- oneShot a [Deterministic [], iniIx] bd
@@ -466,7 +474,7 @@ occWithBinder
466474
-> (forall l. DExt n l => Binder SimpIR n l -> e l -> OCCM l a)
467475
-> OCCM n a
468476
occWithBinder (Abs (b:>ty) body) cont = do
469-
(ty', fvs) <- isolated $ occTy ty
477+
(ty', fvs) <- separately $ occTy ty
470478
ans <- refreshAbs (Abs (b:>ty') body) cont
471479
modify (<> fvs)
472480
return ans

0 commit comments

Comments
 (0)