Skip to content

Commit 63cc5f9

Browse files
committed
make gc root and STG state reference fact collection reusable
1 parent efd3adc commit 63cc5f9

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

external-stg-interpreter/lib/Stg/Interpreter/GC/LiveDataAnalysis.hs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ runLiveDataAnalysis extraGCRoots stgState = Souffle.runSouffle ExtStgGC $ \maybe
9696
---------------------------
9797

9898
addGCRootFacts :: Souffle.Handle ExtStgGC -> StgState -> [Atom] -> SouffleM ()
99-
addGCRootFacts prog StgState{..} localGCRoots = do
100-
let addGCRoot :: GCSymbol -> SouffleM ()
101-
addGCRoot s = Souffle.addFact prog $ GCRoot $ BS8.unpack $ unGCSymbol s
99+
addGCRootFacts prog stgState localGCRoots = withGCRootFacts stgState localGCRoots $ \_msg s -> do
100+
Souffle.addFact prog $ GCRoot $ BS8.unpack $ unGCSymbol s
101+
102+
addReferenceFacts :: Souffle.Handle ExtStgGC -> StgState -> SouffleM ()
103+
addReferenceFacts prog stgState = withReferenceFacts stgState $ \from to -> do
104+
Souffle.addFact prog $ Reference (BS8.unpack $ unGCSymbol from) (BS8.unpack $ unGCSymbol to)
105+
106+
withGCRootFacts :: Monad m => StgState -> [Atom] -> (String -> GCSymbol -> m ()) -> m ()
107+
withGCRootFacts StgState{..} localGCRoots addGCRoot = do
102108

103109
-- HINT: the following can be GC roots
104110
{-
@@ -111,36 +117,33 @@ addGCRootFacts prog StgState{..} localGCRoots = do
111117
-}
112118

113119
-- local
114-
visitGCRef addGCRoot localGCRoots
120+
visitGCRef (addGCRoot "local") localGCRoots
115121

116122
-- stable pointer values
117-
visitGCRef addGCRoot [PtrAtom (StablePtr idx) (intPtrToPtr $ IntPtr idx) | idx <- IntMap.keys ssStablePointers]
123+
visitGCRef (addGCRoot "stable pointer") [PtrAtom (StablePtr idx) (intPtrToPtr $ IntPtr idx) | idx <- IntMap.keys ssStablePointers]
118124

119125
-- CAFs
120-
visitGCRef addGCRoot $ map HeapPtr $ IntSet.toList ssCAFSet
126+
visitGCRef (addGCRoot "CAF") $ map HeapPtr $ IntSet.toList ssCAFSet
121127

122128
-- stack continuations of live threads
123129
forM_ (IntMap.toList ssThreads) $ \(tid, ts) -> case tsStatus ts of
124130
ThreadFinished -> pure ()
125131
ThreadDied -> pure ()
126-
ThreadRunning -> addGCRoot $ encodeRef tid NS_Thread
132+
ThreadRunning -> addGCRoot "thread" $ encodeRef tid NS_Thread
127133
ThreadBlocked r -> case r of
128134
BlockedOnMVar{} -> pure () -- will be referred by the mvar wait queue
129135
BlockedOnMVarRead{} -> pure () -- will be referred by the mvar wait queue
130136
BlockedOnBlackHole{} -> pure () -- will be referred by the BlockedOnBlackHole ADDR thread status
131137
BlockedOnThrowAsyncEx{} -> pure () -- will be referred by the target thread's blocked exceptions queue
132138
BlockedOnSTM{} -> pure () -- will be referred by the tvar wait queue
133139
BlockedOnForeignCall{} -> error "not implemented yet"
134-
BlockedOnRead{} -> addGCRoot $ encodeRef tid NS_Thread
135-
BlockedOnWrite{} -> addGCRoot $ encodeRef tid NS_Thread
136-
BlockedOnDelay{} -> addGCRoot $ encodeRef tid NS_Thread
137-
138-
addReferenceFacts :: Souffle.Handle ExtStgGC -> StgState -> SouffleM ()
139-
addReferenceFacts prog StgState{..} = do
140-
let addReference :: GCSymbol -> GCSymbol -> SouffleM ()
141-
addReference from i = Souffle.addFact prog $ Reference (BS8.unpack $ unGCSymbol from) (BS8.unpack $ unGCSymbol i)
140+
BlockedOnRead{} -> addGCRoot "thread" $ encodeRef tid NS_Thread
141+
BlockedOnWrite{} -> addGCRoot "thread" $ encodeRef tid NS_Thread
142+
BlockedOnDelay{} -> addGCRoot "thread" $ encodeRef tid NS_Thread
142143

143-
addRefs :: VisitGCRef a => IntMap a -> RefNamespace -> SouffleM ()
144+
withReferenceFacts :: forall m . Monad m => StgState -> (GCSymbol -> GCSymbol -> m ()) -> m ()
145+
withReferenceFacts StgState{..} addReference = do
146+
let addRefs :: (VisitGCRef a, Monad m) => IntMap a -> RefNamespace -> m ()
144147
addRefs im ns = do
145148
let l = IntMap.toList im
146149
forM_ l $ \(i, v) -> visitGCRef (addReference (encodeRef i ns)) v

0 commit comments

Comments
 (0)