Skip to content

Commit

Permalink
Experimenting with some limited parallelism in simple cases
Browse files Browse the repository at this point in the history
  • Loading branch information
danmatichuk committed Oct 24, 2024
1 parent e010f58 commit fcb2f7c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
54 changes: 53 additions & 1 deletion src/Pate/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ module Pate.Monad
, withSharedEnvEmit
, module PME
, atPriority, currentPriority, thisPriority
, concretizeWithSolverBatch
, concretizeWithSolverBatch
, withFreshSolver
, forkBins
, withForkedSolver
)
where

Expand Down Expand Up @@ -1611,3 +1614,52 @@ equivalenceContext = withValid $ do
PA.SomeValidArch d <- CMR.asks envValidArch
stackRegion <- CMR.asks (PMC.stackRegion . PME.envCtx)
return $ PEq.EquivContext (PA.validArchDedicatedRegisters d) stackRegion (\x -> x)


-- | Run the given continuation with a fresh solver process (without affecting the
-- current solver process). The new process will inherit the current assumption
-- context.
withFreshSolver ::
EquivM_ sym arch a ->
EquivM sym arch a
withFreshSolver f = do
vcfg <- CMR.asks envConfig
let solver = PC.cfgSolver vcfg
PSo.Sym n sym _ <- CMR.asks envValidSym
asm <- currentAsm
PSo.withOnlineSolver solver Nothing sym $ \bak ->
CMR.local (\env -> env {envValidSym = PSo.Sym n sym bak, envCurrentFrame = mempty }) $
withAssumptionSet asm $ 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 ::
EquivM_ sym arch () ->
EquivM sym arch ()
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 ()

-- | Similar to 'forBins' but runs the 'Patched' process in a separate thread
-- concurrently with a fresh solver process.
forkBins ::
(forall bin. PBi.WhichBinaryRepr bin -> EquivM_ sym arch (f bin)) ->
EquivM sym arch (PPa.PatchPair f)
forkBins f = do
(resO, resP) <- do
thistid <- IO.liftIO $ IO.myThreadId
outVar2 <- IO.liftIO $ IO.newEmptyMVar
_ <- IO.withRunInIO $ \runInIO ->
IO.forkFinally (runInIO $ withFreshSolver $ (PPa.catchPairErr (Just <$> f PBi.PatchedRepr) (return Nothing)))
(\case Left err -> IO.throwTo thistid err; Right a -> IO.putMVar outVar2 a)
resO <- PPa.catchPairErr (Just <$> f PBi.OriginalRepr) (return Nothing)
resP <- IO.liftIO $ IO.readMVar outVar2
return (resO, resP)
case (resO, resP) of
(Just vO, Just vP) -> return $ PPa.PatchPair vO vP
(Nothing, Just vP) -> return $ PPa.PatchPairPatched vP
(Just vO, Nothing) -> return $ PPa.PatchPairOriginal vO
(Nothing, Nothing) -> PPa.throwPairErr
7 changes: 5 additions & 2 deletions src/Pate/Verification/StrongestPosts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,11 @@ getTracesForPred ::
EquivM sym arch (Maybe (CE.TraceEvents sym arch), Maybe (CE.TraceEvents sym arch))
getTracesForPred scope bundle dom p = withSym $ \sym -> do
not_p <- liftIO $ W4.notPred sym p
p_pretty <- PSi.applySimpStrategy PSi.prettySimplifier p
withTracing @"expr" (Some p_pretty) $ do
-- p_pretty <- withTracing @"debug" "simplifier" $ PSi.applySimpStrategy PSi.prettySimplifier p
withTracing @"message" "Traces" $ do
withTracing @"message" "Condition" $ withForkedSolver $ do
p_pretty <- withTracing @"debug" "simplifier" $ PSi.applySimpStrategy PSi.prettySimplifier p
emitTrace @"expr" (Some p_pretty)
mtraceT <- withTracing @"message" "With condition assumed" $
withSatAssumption (PAS.fromPred p) $ do
traceT <- getSomeGroundTrace scope bundle dom Nothing
Expand Down
2 changes: 1 addition & 1 deletion src/Pate/Verification/Widening.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ getInitalAbsDomainVals bundle preDom = withTracing @"debug" "getInitalAbsDomainV
getConcreteRange <- PAD.mkGetAbsRange (\es -> TFC.fmapFC (PAD.extractAbsRange sym) <$> concretizeWithSolverBatch es)

eqCtx <- equivalenceContext
PPa.forBins $ \bin -> do
forkBins $ \bin -> do
out <- PPa.get bin (PS.simOut bundle)
pre <- PPa.get bin (PAD.absDomVals preDom)

Expand Down

0 comments on commit fcb2f7c

Please sign in to comment.