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

Guard mux-pushing simplifications behind option #256

Merged
merged 5 commits into from
May 14, 2024

Conversation

RyanGlScott
Copy link
Contributor

@RyanGlScott RyanGlScott commented Mar 1, 2024

Add pushMuxOps and pushMuxOpsOption. If this option is enabled, What4 will push certain ExprBuilder operations (e.g., zext) down to the branches of ite expressions. In some (but not all) circumstances, this can result in operations that are easier for SMT solvers to reason about.

RyanGlScott added a commit to GaloisInc/crucible that referenced this pull request Mar 1, 2024
This also reverts the test output changes from commit
23cc439, as the option which caused the change
is no longer enabled by default.
RyanGlScott added a commit to GaloisInc/saw-script that referenced this pull request Mar 1, 2024
This bumps:

* The `crucible` submodule to bring in the changes from
  GaloisInc/crucible#1178
* The `what4` submodule to bring in the changes from
  GaloisInc/what4#256
@RyanGlScott RyanGlScott marked this pull request as ready for review May 13, 2024 20:48
@RyanGlScott RyanGlScott requested a review from kquick May 13, 2024 20:48
Copy link
Member

@kquick kquick left a comment

Choose a reason for hiding this comment

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

IIUC, this patch adds a config flag to control the push-down, and the push-down is originally added in a different patch that is also post 1.5.1. Thus, the default for most clients (unless they had updated to a commit of what4 lying within these two changes) is unchanged and the new behavior only occurs when the new config is set.

My concern here is clients like Macaw that use pattern-based recognition (especially for end-of-block dispatching) that might be broken by this push-down behavior. Unless you think this concern is unfounded, you might want to add this information to the PR description to help forensic efforts for any of this kind of downstream breakage folks might encounter.

Other than this concern and the help text, the other comments are just stylistic, so I'm approving and you are free to choose what changes you'd like to make or not make before merging.

what4/src/What4/Expr/Builder.hs Outdated Show resolved Hide resolved
what4/src/What4/Expr/Builder.hs Outdated Show resolved Hide resolved
| otherwise = do
Just LeqProof <- return $ testLeq (knownNat :: NatRepr 1) w
sbMakeExpr sym $ BVZext w x
pmo <- CFG.getOpt (sbPushMuxOps sym)
Copy link
Member

Choose a reason for hiding this comment

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

Quick check: looks like you had to do this in 6-8 other locations as well. Do you think it's reasonable to float this getOpt to the top before the primary case on expr so that the case pattern match branches can be largely as they were with just the pmo check? I'm fine either way (based on the assumption that getOpt is inexpensive), but just wanted to call this out for you to make a decision on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I understand you correctly, you are suggesting to call getOpt at the beginning of each method's implementation to minimize the diff to the actual logic of the code itself? If so, I'm not sure that that would actually have the desired effect. In particular, each method of this IsExprBuilder instance is implemented using pattern guards, so in order to call getOpt before the pattern guards, we'd likely need to do something like this:

  bvZext sym w x = do
    pmo <- CFG.getOpt (sbPushMuxOps sym)
    if | Just xv <- asBV x = do ...
       | Just (BVZext _ y) <- asApp x = do ...
       |  ...

But using MultiWayIf (or a similar refactoring) here would cause the diff to be just as large (if not larger) than it is currently. As such, I opted to localize the getOpt calls to the parts of the code that actually make use of the option.

what4/src/What4/Expr/Builder.hs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@RyanGlScott RyanGlScott left a comment

Choose a reason for hiding this comment

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

My concern here is clients like Macaw that use pattern-based recognition (especially for end-of-block dispatching) that might be broken by this push-down behavior. Unless you think this concern is unfounded, you might want to add this information to the PR description to help forensic efforts for any of this kind of downstream breakage folks might encounter.

To be clear, Macaw isn't yet making use of the new behavior, and by the time Macaw bumps its What4 submodule, the new behavior will be guarded behind an opt-in flag. As a result, Macaw's behavior won't change unless it specifically opts into the new behavior. (Indeed, this was the whole point of guarding this behavior behind a flag, as the new behavior proved problematic for certain use cases, such as in certain SAW proofs.)

what4/src/What4/Expr/Builder.hs Outdated Show resolved Hide resolved
what4/src/What4/Expr/Builder.hs Outdated Show resolved Hide resolved
| otherwise = do
Just LeqProof <- return $ testLeq (knownNat :: NatRepr 1) w
sbMakeExpr sym $ BVZext w x
pmo <- CFG.getOpt (sbPushMuxOps sym)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I understand you correctly, you are suggesting to call getOpt at the beginning of each method's implementation to minimize the diff to the actual logic of the code itself? If so, I'm not sure that that would actually have the desired effect. In particular, each method of this IsExprBuilder instance is implemented using pattern guards, so in order to call getOpt before the pattern guards, we'd likely need to do something like this:

  bvZext sym w x = do
    pmo <- CFG.getOpt (sbPushMuxOps sym)
    if | Just xv <- asBV x = do ...
       | Just (BVZext _ y) <- asApp x = do ...
       |  ...

But using MultiWayIf (or a similar refactoring) here would cause the diff to be just as large (if not larger) than it is currently. As such, I opted to localize the getOpt calls to the parts of the code that actually make use of the option.

@kquick
Copy link
Member

kquick commented May 14, 2024

To be clear, Macaw isn't yet making use of the new behavior, and by the time Macaw bumps its What4 submodule, the new behavior will be guarded behind an opt-in flag. As a result, Macaw's behavior won't change unless it specifically opts into the new behavior. (Indeed, this was the whole point of guarding this behavior behind a flag, as the new behavior proved problematic for certain use cases, such as in certain SAW proofs.)

Exactly the situation I was hoping for. We probably discussed this back then but I subsequently forgot. Thanks for the confirmation.

@RyanGlScott RyanGlScott merged commit 8c9401b into master May 14, 2024
29 checks passed
@RyanGlScott RyanGlScott deleted the push-mux-ops-setting branch May 14, 2024 14:06
RyanGlScott added a commit to GaloisInc/crucible that referenced this pull request May 14, 2024
RyanGlScott added a commit to GaloisInc/crucible that referenced this pull request May 14, 2024
* Code refactoring.

* Find SMT array write of a fixed size.

* Cache tail traversal in findArrayStore.

* Load SMT array with concrete size.

* Load SMT array with 0 size.

* Add cache for base pointers with array stores.

* Add noSatisfyingWriteFreshConstant option.

* wip

* Cleanup.

* Derive Show.

* Add updateHandleMap.

* Add parentWTOComponent.

* Export writeSourceSize.

* Add runCHC and helpers.

* Bump what4.

* wip

* Fix build error with GHC 8.10

* Bump what4 submodule to incorporate GaloisInc/what4#256

This also reverts the test output changes from commit
23cc439, as the option which caused the change
is no longer enabled by default.

* Fix build warnings introduced in #1165

* crucible: Clearer error messages for runCHC

* Additional documentation

* Comment out putStrLns

* Remove redundant export

* More accurate logReason

* Remove redundant import

* Don't log everything to foo.* files

* Pass pointer size to writeSouceSize

* Replace putStrLn's with ?logMessage's

* Fix build warnings

* Rename SimpleLoopFixpoint to SimpleLoopFixpointCHC

SimpleLoopFixpointCHC is not quite suitable for being a full replacement for
SimpleLoopFixpoint as of yet. For now, we will offer the CHC functionality in a
separate module, and we will restore the old SimpleLoopFixpoint functionality
in a subsequent commit.

* Restore previous SimpleLoopFixpoint functionality

* Fix -Wunused-do-bind warning

* Bump what4 to bring in latest changes from GaloisInc/what4#256

* Remove commented-out exports

* Review comments

---------

Co-authored-by: Andrei Stefanescu <andrei@stefanescu.io>
RyanGlScott added a commit to GaloisInc/saw-script that referenced this pull request May 15, 2024
* Find SMT array write of a fixed size.

* Load SMT array with concrete size.

* Add test.

* Add noSatisfyingWriteFreshConstant option.

* Add invariant substitution to getPoststateObligations.

* Bump what4.

* wip

* wip

* Use simplified term in resolveSAWPred.

* Bump crucible.

* Remove unused sc.

* Use simplified term in resolveSAWPred.

* Bump crucible.

* Update src/SAWScript/Crucible/LLVM/Builtins.hs

Co-authored-by: Ryan Scott <rscott@galois.com>

* Fix -Wunused-matches warning

* Bump crucible, what4 submodules

This bumps:

* The `crucible` submodule to bring in the changes from
  GaloisInc/crucible#1178
* The `what4` submodule to bring in the changes from
  GaloisInc/what4#256

* Remove debugging-only code

* Bump cryptol-specs to incorporate GaloisInc/cryptol-specs#72

* Repair AES example to work with `type Nk = AES256`

* Add expert options for enabling What4-, Crucible-related SyGuS features

* Split off separate llvm_verify_fixpoint_chc_x86 command

* Only enable doPtrCmp optimizations with SimpleFixpointCHC

* crucible: Revert popFrame refactoring

* Uniformly apply pushMuxOps option to all ExprBuilders

SAW creates a variety of different ExprBuilders in the course of a typical SAW
script, but we were only applying the pushMuxOps option to some of them. This
patch makes the treatment a bit more comprehensive.

Unfortunately, doing so requires a rather uncomfortable amount of extra
plumbing in `SAWScript.Proof`, but I'm not sure how to do better without
refactoring all of `SAWScript.Proof` to use `TopLevel` instead of `IO` (and
it's unclear if that is desirable).

* Bump cryptol-specs, what4, crucible submodules to latest

* Bump what4, crucible submodules

* Adapt to recent crucible-llvm API changes

---------

Co-authored-by: Andrei Stefanescu <andrei@stefanescu.io>
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.

None yet

2 participants