@@ -22,7 +22,8 @@ module Booster.Pattern.ApplyEquations (
22
22
handleSimplificationEquation ,
23
23
simplifyConstraint ,
24
24
simplifyConstraints ,
25
- SimplifierCache ,
25
+ SimplifierCache (.. ),
26
+ CacheTag (.. ),
26
27
evaluateConstraints ,
27
28
) where
28
29
@@ -234,19 +235,46 @@ popRecursion = do
234
235
throw $ InternalError " Trying to pop an empty recursion stack"
235
236
else eqState $ put s{recursionStack = tail s. recursionStack}
236
237
237
- toCache :: Monad io => CacheTag -> Term -> Term -> EquationT io ()
238
- toCache tag orig result = eqState . modify $ \ s -> s{cache = updateCache tag s. cache}
239
- where
240
- insertInto = Map. insert orig result
241
- updateCache LLVM cache = cache{llvm = insertInto cache. llvm}
242
- updateCache Equations cache = cache{equations = insertInto cache. equations}
243
-
244
- fromCache :: Monad io => CacheTag -> Term -> EquationT io (Maybe Term )
245
- fromCache tag t = eqState $ Map. lookup t <$> gets (select tag . (. cache))
246
- where
247
- select :: CacheTag -> SimplifierCache -> Map Term Term
248
- select LLVM = (. llvm)
249
- select Equations = (. equations)
238
+ toCache :: LoggerMIO io => CacheTag -> Term -> Term -> EquationT io ()
239
+ toCache LLVM orig result = eqState . modify $
240
+ \ s -> s{cache = s. cache{llvm = Map. insert orig result s. cache. llvm}}
241
+ toCache Equations orig result = eqState $ do
242
+ s <- get
243
+ -- Check before inserting a new result to avoid creating a
244
+ -- lookup chain e -> result -> olderResult.
245
+ newEqCache <- case Map. lookup result s. cache. equations of
246
+ Nothing ->
247
+ pure $ Map. insert orig result s. cache. equations
248
+ Just furtherResult -> do
249
+ when (result /= furtherResult) $ do
250
+ withContextFor Equations . logMessage $
251
+ " toCache shortening a chain "
252
+ <> showHashHex (getAttributes orig). hash
253
+ <> " ->"
254
+ <> showHashHex (getAttributes furtherResult). hash
255
+ pure $ Map. insert orig furtherResult s. cache. equations
256
+ put s{cache = s. cache{equations = newEqCache}}
257
+
258
+ fromCache :: LoggerMIO io => CacheTag -> Term -> EquationT io (Maybe Term )
259
+ fromCache tag t = eqState $ do
260
+ s <- get
261
+ case tag of
262
+ LLVM -> pure $ Map. lookup t s. cache. llvm
263
+ Equations -> do
264
+ case Map. lookup t s. cache. equations of
265
+ Nothing -> pure Nothing
266
+ Just t' -> case Map. lookup t' s. cache. equations of
267
+ Nothing -> pure $ Just t'
268
+ Just t'' -> do
269
+ when (t'' /= t') $ do
270
+ withContextFor Equations . logMessage $
271
+ " fromCache shortening a chain "
272
+ <> showHashHex (getAttributes t). hash
273
+ <> " ->"
274
+ <> showHashHex (getAttributes t''). hash
275
+ let newEqCache = Map. insert t t'' s. cache. equations
276
+ put s{cache = s. cache{equations = newEqCache}}
277
+ pure $ Just t''
250
278
251
279
logWarn :: LoggerMIO m => Text -> m ()
252
280
logWarn msg =
@@ -899,6 +927,12 @@ applyEquation term rule =
899
927
Left other ->
900
928
liftIO $ Exception. throw other
901
929
lift $ pushConstraints $ Set. fromList ensuredConditions
930
+ -- when a new path condition is added, invalidate the equation cache
931
+ unless (null ensuredConditions) $ do
932
+ withContextFor Equations . logMessage $
933
+ (" New ensured condition from evaluation, invalidating cache" :: Text )
934
+ lift . eqState . modify $
935
+ \ s -> s{cache = s. cache{equations = mempty }}
902
936
pure $ substituteInTerm subst rule. rhs
903
937
where
904
938
filterOutKnownConstraints :: Set Predicate -> [Predicate ] -> EquationT io [Predicate ]
0 commit comments