-
Notifications
You must be signed in to change notification settings - Fork 44
Recover from TooManyIterations
when simplifying during rewriting
#3989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
No difference in KEVM performance as of commit |
No significant difference in Kontrol performance either. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test code not adapted yet.
where | ||
-- when TooManyIterations exception occurred while evaluating the top-level term, | ||
-- i.e. not in a recursive evaluation of a side-condition, | ||
-- it is safe to keep the partial result and ignore the exception. | ||
-- Otherwise we would be throwing away useful work. | ||
keepTopLevelResults :: LoggerMIO io => EquationFailure -> EquationT io Term | ||
keepTopLevelResults = \case | ||
err@(TooManyIterations recursionDepth _ _ partialResult) -> | ||
case recursionDepth of | ||
0 -> pure partialResult | ||
_ -> throw err | ||
err -> throw err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After checking the code where we recurse into the evaluation,
haskell-backend/booster/library/Booster/Pattern/ApplyEquations.hs
Lines 931 to 939 in 034078d
checkConstraint whenBottom (Predicate p) = withContext CtxConstraint $ do | |
let fallBackToUnsimplifiedOrBottom :: EquationFailure -> EquationT io Term | |
fallBackToUnsimplifiedOrBottom = \case | |
UndefinedTerm{} -> pure FalseBool | |
_ -> pure p | |
-- exceptions need to be handled differently in the recursion, | |
-- falling back to the unsimplified constraint instead of aborting. | |
simplified <- | |
lift $ simplifyConstraint' True p `catch_` fallBackToUnsimplifiedOrBottom |
I think it is probably fine without this additional
catch_
. Of course it is safer the way you propose it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still need the catch_
to get the simplified term, but I do agree that the recursion depth check is redundant. I've removed it.
getRecusionDepth :: LoggerMIO io => EquationT io Int | ||
getRecusionDepth = (length . (.recursionStack)) <$> getState |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getRecusionDepth :: LoggerMIO io => EquationT io Int | |
getRecusionDepth = (length . (.recursionStack)) <$> getState | |
getRecursionDepth :: LoggerMIO io => EquationT io Int | |
getRecursionDepth = (length . (.recursionStack)) <$> getState |
@@ -331,6 +335,10 @@ iterateEquations direction preference startTerm = do | |||
config <- getConfig | |||
currentCount <- countSteps | |||
when (coerce currentCount > config.maxIterations) $ do | |||
currentRecursionDepth <- getRecusionDepth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentRecursionDepth <- getRecusionDepth | |
currentRecursionDepth <- getRecursionDepth |
-- FIXME if this exception is caught in evaluatePattern', | ||
-- then CtxAbort is a wrong context for it. | ||
-- We should emit this log entry somewhere else. | ||
withContext CtxAbort $ do | ||
logWarn $ | ||
renderOneLineText $ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably log this in evaluatePattern'
where we decide whether or not to use the final term.
The warning will still show up for recursive evaluations where we catch the exception, too. Maybe not useless to notify the user about it, though? (so we could just use CtxWarn
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed CtxAbort
, so these will show up as warnings now.
b0c52a7
to
a3cc6a7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Booster's rewriting algorithm, upon getting stuck (or aborting due to
RuleApplicationUnclear
), will try so simplify the configuration once and retry applying rules. When we cannot not finish simplifying the term due to aTooManyIterations
, we throw the partial result away and fail.This PR handles the
TooManyIterations
exception and attempts applying rewrite rules to the partially simplified term.