Skip to content
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

ApplyEquations refactor #555

Merged
merged 4 commits into from
Mar 22, 2024
Merged

Conversation

goodlyrottenapple
Copy link
Contributor

Incremental change towards design discussed in runtimeverification/haskell-backend#3772

Refactor definitions of applyTerm and llvmSimplify


llvmEval :: MonadLoggerIO io => KoreDefinition -> LLVM.API -> Term -> EquationT io Term
llvmEval definition api = eval
let simp = cached LLVM $ evalLlvm config.definition api $ traverseTerm BottomUp simp pure
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BottomUp or TopDown are equivalent if onEval is just pure:

            | direction == BottomUp -> do
                -- evaluate arguments first
                args' <- mapM onRecurse args
                -- then try to apply equations
                pure $ SymbolApplication sym sorts args'
            | otherwise {- direction == TopDown -} -> do
                -- try to apply equations
                t <- pure app
                SymbolApplication sym sorts
                            <$> mapM onRecurse args

Copy link
Member

@jberthold jberthold Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed they are... but somehow I don't see how the cut-off is implemented.
If we have nested concrete terms, e.g., +Int(2, +Int(3, 4)) ,Is the llvm backend going to be called twice, for 3 + 4 and then 2 + 7, or is it called just once as a top-down traversal with cut-off would?

EDIT: with the recursion in what is called cb , this is indeed cutting the traversal short.
I concur we should call cb a different name, and leave a comment for posterity.

@goodlyrottenapple
Copy link
Contributor Author

No difference in performance in KEVM

@goodlyrottenapple goodlyrottenapple marked this pull request as ready for review March 19, 2024 15:19
geo2a
geo2a previously approved these changes Mar 20, 2024
Copy link
Contributor

@geo2a geo2a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left three suggestions, but they are minor. Looks good to me otherwise!

when (cached /= t) setChanged >> pure cached

eval' t@(Term attributes _)
evalLlvm definition api cb t@(Term attributes _)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function deserves to have:

  • an explicit type signature
  • a better name for the cb parameter, together with the comment on what the callback does. How about we call it traversalAction or something along these lines?

-- Bottom-up evaluation traverses AST nodes in post-order but finds work top-down
-- Top-down evaluation traverses AST nodes in pre-order
apply config = \case
traverseTerm ::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we expand the documentation of this function to reference it's two current instantiations, llvmSimplify and iterateEquations.

Just cachedTerm -> do
when (t /= cachedTerm) $ do
setChanged
emitEquationTrace t Nothing (Just "Cache") Nothing $ Success cachedTerm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to specify which cache the equation application came from:

Suggested change
emitEquationTrace t Nothing (Just "Cache") Nothing $ Success cachedTerm
emitEquationTrace t Nothing (Just ("Cache" <> show cacheTag)) Nothing $ Success cachedTerm

@geo2a geo2a dismissed their stale review March 20, 2024 09:45

Actually, we do want to have the Kontrol performance tests results as well.

@goodlyrottenapple
Copy link
Contributor Author

Kontrol

Test sam-apply-equations-refactor time master-88ffab5 time (sam-apply-equations-refactor/master-88ffab5) time
LabelTest.testLabel() 33.67 37.23 0.9043781896320173
PlainPrankTest.test_startPrankWithOrigin_true() 168.71 176.36 0.9566228169652983
FeeTest.test_fee_setup() 75.97 79.39 0.9569215266406348
ERC20.sol 46.58 48.35 0.9633919338159255
MethodDisambiguateTest.test_method_call() 30.03 29.0 1.0355172413793103
Setup2Test.test_setup() 36.69 35.43 1.035563082133785
AssumeTest.testFail_assume_true(uint256,uint256) 70.95 68.47 1.0362202424419453
StoreTest.testStoreLoad() 70.89 68.12 1.040663534938344
AllowChangesTest.testAllow() 125.11 120.11 1.0416285072017317
StoreTest.testGasLoadWarmUp() 78.76 75.44 1.0440084835630965
StructTypeTest.test_vars((uint8,uint32,bytes32)) 42.84 40.77 1.0507726269315674
src/tests/integration/test_foundry_prove.py::test_foundry_merge_nodes 46.21 43.9 1.052619589977221
src/tests/integration/test_foundry_prove.py::test_foundry_xml_report 29.86 27.53 1.0846349436977842
src/tests/integration/test_foundry_prove.py::test_foundry_remove_node 49.64 44.7 1.1105145413870245
GasTest.testSetGas() 38.07 33.98 1.1203649205414952
TOTAL 943.9800000000001 928.7800000000001 1.016365554813842

@goodlyrottenapple goodlyrottenapple merged commit 78c457e into main Mar 22, 2024
5 checks passed
@goodlyrottenapple goodlyrottenapple deleted the sam/apply-equations-refactor branch March 22, 2024 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants