Skip to content

Commit

Permalink
allow forked solver instances to share the sat/unsat cache
Browse files Browse the repository at this point in the history
a forked instance of the solver has the same assumption context,
and so any results about satisfiability/unsatisfiability can
be re-used by other threads
  • Loading branch information
danmatichuk committed Oct 25, 2024
1 parent 65a18a9 commit d19920a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/Pate/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module Pate.Monad
, withFreshSolver
, forkBins
, withForkedSolver
, withForkedSolver_
)
where

Expand Down Expand Up @@ -1627,22 +1628,40 @@ withFreshSolver f = do
let solver = PC.cfgSolver vcfg
PSo.Sym n sym _ <- CMR.asks envValidSym
asm <- currentAsm
PSo.withOnlineSolver solver Nothing sym $ \bak ->
PSo.withOnlineSolver solver Nothing sym $ \bak -> do
unsatCacheRef <- asks envUnsatCacheRef
satCache <- asks envSatCacheRef

CMR.local (\env -> env {envValidSym = PSo.Sym n sym bak, envCurrentFrame = mempty }) $
withAssumptionSet asm $ f
withAssumptionSet asm $ CMR.local (\env -> env { envUnsatCacheRef = unsatCacheRef, envSatCacheRef = satCache }) f

-- | Run the given computation in a forked thread with a fresh solver process.
-- This is non-blocking and the current solver process is left unmodified.
withForkedSolver ::

-- | Similar to 'withForkedSolver' but does not return a result.
withForkedSolver_ ::
EquivM_ sym arch () ->
EquivM sym arch ()
withForkedSolver f = do
withForkedSolver_ f = do
thistid <- IO.liftIO $ IO.myThreadId
_ <- IO.withRunInIO $ \runInIO ->
IO.forkFinally (runInIO $ withFreshSolver f)
(\case Left err -> IO.throwTo thistid err; Right () -> return ())
return ()

-- | Run the given function in a forked thread with a fresh solver process.
-- This is non-blocking and the current solver process is left unmodified.
-- The returned IO action will block until the given function has finished.
withForkedSolver ::
EquivM_ sym arch a ->
EquivM sym arch (IO a)
withForkedSolver f = do
thistid <- IO.liftIO $ IO.myThreadId
outVar <- IO.liftIO $ IO.newEmptyMVar
_ <- IO.withRunInIO $ \runInIO ->
IO.forkFinally (runInIO $ withFreshSolver f)
(\case Left err -> IO.throwTo thistid err; Right a -> IO.putMVar outVar a)
return $ IO.readMVar outVar


-- | Similar to 'forBins' but runs the 'Patched' process in a separate thread
-- concurrently with a fresh solver process.
forkBins ::
Expand Down
2 changes: 1 addition & 1 deletion src/Pate/Verification/StrongestPosts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ getTracesForPred ::
getTracesForPred scope bundle dom p = withSym $ \sym -> do
not_p <- liftIO $ W4.notPred sym p
withTracing @"expr" (Some p) $ do
withTracing @"message" "Simplified Condition" $ withForkedSolver $ do
withTracing @"message" "Simplified Condition" $ withForkedSolver_ $ do
p_pretty <- withTracing @"debug" "simplifier" $ PSi.applySimpStrategy PSi.prettySimplifier p
emitTrace @"expr" (Some p_pretty)
mtraceT <- withTracing @"message" "With condition assumed" $
Expand Down

0 comments on commit d19920a

Please sign in to comment.