Skip to content

Commit ecfa5d2

Browse files
committed
Catch TooManyIterations in evaluatePattern', and only there
1 parent 49516fc commit ecfa5d2

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

booster/library/Booster/Pattern/ApplyEquations.hs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ popRecursion = do
234234
throw $ InternalError "Trying to pop an empty recursion stack"
235235
else eqState $ put s{recursionStack = tail s.recursionStack}
236236

237+
getRecusionDepth :: LoggerMIO io => EquationT io Int
238+
getRecusionDepth = (length . (.recursionStack)) <$> getState
239+
237240
toCache :: Monad io => CacheTag -> Term -> Term -> EquationT io ()
238241
toCache tag orig result = eqState . modify $ \s -> s{cache = updateCache tag s.cache}
239242
where
@@ -331,6 +334,9 @@ iterateEquations direction preference startTerm = do
331334
config <- getConfig
332335
currentCount <- countSteps
333336
when (coerce currentCount > config.maxIterations) $ do
337+
-- FIXME if this exception is caught in evaluatePattern',
338+
-- then CtxAbort is a wrong context for it.
339+
-- We should emit this log entry somewhere else.
334340
withContext CtxAbort $ do
335341
logWarn $
336342
renderOneLineText $
@@ -409,7 +415,8 @@ evaluateTerm' ::
409415
Direction ->
410416
Term ->
411417
EquationT io Term
412-
evaluateTerm' direction = iterateEquations direction PreferFunctions
418+
evaluateTerm' direction =
419+
iterateEquations direction PreferFunctions
413420

414421
{- | Simplify a Pattern, processing its constraints independently.
415422
Returns either the first failure or the new pattern if no failure was encountered
@@ -431,13 +438,24 @@ evaluatePattern' ::
431438
Pattern ->
432439
EquationT io Pattern
433440
evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
434-
newTerm <- withTermContext term $ evaluateTerm' BottomUp term
441+
newTerm <- withTermContext term $ evaluateTerm' BottomUp term `catch_` keepTopLevelResults
435442
-- after evaluating the term, evaluate all (existing and
436443
-- newly-acquired) constraints, once
437444
traverse_ simplifyAssumedPredicate . predicates =<< getState
438445
-- this may yield additional new constraints, left unevaluated
439446
evaluatedConstraints <- predicates <$> getState
440447
pure Pattern{constraints = evaluatedConstraints, term = newTerm, ceilConditions}
448+
where
449+
-- when TooManyIterations exception occurs while evaluating the top-level term (not a side-condition),
450+
-- it is safe to keep the partial result and ignore the exception. Otherwise we
451+
-- would be throwing away useful work.
452+
keepTopLevelResults :: LoggerMIO io => EquationFailure -> EquationT io Term
453+
keepTopLevelResults = \case
454+
err@(TooManyIterations _ _ partialResult) ->
455+
getRecusionDepth >>= \case
456+
0 -> pure partialResult
457+
_ -> throw err
458+
err -> throw err
441459

442460
-- evaluate the given predicate assuming all others
443461
simplifyAssumedPredicate :: LoggerMIO io => Predicate -> EquationT io ()

booster/library/Booster/Pattern/Rewrite.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,6 @@ performRewrite doTracing def mLlvmLibrary mSolver mbMaxDepth cutLabels terminalL
761761
Left r@UndefinedTerm{} -> do
762762
emitRewriteTrace $ RewriteSimplified (Just r)
763763
pure Nothing
764-
Left r@(TooManyIterations _ _ end) -> do
765-
emitRewriteTrace $ RewriteSimplified (Just r)
766-
pure $ Just p{term = end}
767764
Left other -> do
768765
emitRewriteTrace $ RewriteSimplified (Just other)
769766
pure $ Just p

0 commit comments

Comments
 (0)