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

feat(BV): Only store domains on variable parts #1152

Merged
merged 15 commits into from
Aug 6, 2024

Conversation

bclement-ocp
Copy link
Collaborator

@bclement-ocp bclement-ocp commented Jun 20, 2024

This patch changes the way that bit-vector domains are stored in order to share domains between multiple bit-vectors with the same variable part, including across different bit-width. Currently, if we had terms [x], [00 @ x], and [10 @ x], we would store domains for each of these. With this patch, we only store a domain for [x] and rebuild the domains for [00 @ x] and [10 @ x] dynamically; we do this by computing normal forms composed of a variable part ([x] here) and a constant offset (either [0] or [10]).

This reduces the number of variables handled by the system (which is important for global domains such as global difference) and reduces the number of trivial propagations.

Note that this patch is not reverting #1044. In particular, we are considering [x @ y] as a unique (composite) variable built up from the atomic variables [x] and [y] (in the same way that the polynomial [x + y] is built up from the monomial [x] and [y] in the IntervalCalculus module; in fact, the implementation is intentionally abstracted in order to be useable with polynomials). This is necessary because, as noted in #1044, the interval domain for [x @ y] cannot be built reconstructed precisely from the interval domains for [x] and [y].

The patch includes a bit of refactoring of the way we handle constraints; notably, instead of aggressively substituting constraints, we now store the original constraint and call [Uf.find] at propagation time. This avoids having to define a substitution operation on normal forms and allows storing the constraint dependencies directly inside the domain (in the previous design, we could not do it easily without repeating the expensive substitution of constraint arguments).

Note: depends on #1144. Only the latest commit with title "feat(BV): Interval domains for bit-vectors" is included in this PR.

Benchmarks: This PR provides a small reasoning boost (+171-48 on the usual QF_BV subset) and a significant performance boost for bit-vector reasoning (-25% on the same QF_BV subset) compared to #1144.

@bclement-ocp
Copy link
Collaborator Author

Rebased on next. @Halbaroth ping

@bclement-ocp
Copy link
Collaborator Author

Note: this fixes rare crashes with bitvector fields in ADTs/records (previous implementation incorrectly assumed that X.leaves of a composition returned the bit-vectors in the composition, but that is not true if the composition contains ADT/record destructors). I tried to make a test case for it but couldn't figure out the exact conditions to trigger this (it depends on the way propagation is done so would be brittle anyways).

Assuming [x] and [y] are bit-vectors of width 2:
- [101 @ x] is [x + 10100] ;
- [10 @ x @ 01] is [(x @ 00) + 100001] ;
- [10 @ y<0, 0> @ y<1, 1>] is [(y<0, 0> @ y<1>1) + 1000] ;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not understand the notation x<.., ...>. Is it an extraction? I guess there is a typo with y<1>1 instead of y<1, 1>?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this is the syntax that is used elsewhere in documentation comments for extraction (and indeed there is a typo, it should be y<1, 1>). I will clarify.

Comment on lines 429 to 430
[domain]. The explanation [ex] justifies that the [domain] applies to
the entry's key.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This documentation is outdated. There is no ex argument.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True — the justification is in the domain.

src/lib/reasoners/rel_utils.ml Outdated Show resolved Hide resolved
(** [entry t k] returns the [handle] associated with [k].

There is a unique entry associated with each key [k] that is created
on-the-fly when [handle t k] is called for the first time.
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no handle function in this module. I guess you meant entry t k?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, handle was renamed to entry at some point.


The domain associated with the entry is initialized from the underlying
persistent domain the first time it is accessed, and updated with
[update]. *)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is update here? Cannot find it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It should be set_domain, will fix.

src/lib/reasoners/rel_utils.ml Outdated Show resolved Hide resolved
val add : X.t -> W.t -> t -> t
(** [add x w r] adds the tuple [(x, w)] to the relation. *)

val add_many : X.t -> W.Set.t -> t -> t
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add documentation


val add_many : X.t -> W.Set.t -> t -> t

val range : X.t -> t -> W.Set.t
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

DMC.needs_propagation t.composites ||
not (W.Set.is_empty t.triggers)

let variables { variables ; _ } = variables
Copy link
Collaborator

Choose a reason for hiding this comment

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

For this kind of function, I am used to add [@inline always], but I am not sure it is necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's probably going to be inlined anyways. I can add the annotation.


let variables { variables ; _ } = variables

let parents { parents ; _ } = parents
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question for this one

@Halbaroth
Copy link
Collaborator

Halbaroth commented Aug 1, 2024

I still believe the code would be easier to read with less functors and signatures. I understand your argument. It is probably easier to write it like this for you but reading the file Rel_utils gives me the same feeling that reading Bourbaki, a bunch of abstract axioms before reaching the real stuff.

Copy link
Collaborator Author

@bclement-ocp bclement-ocp left a comment

Choose a reason for hiding this comment

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

I still believe the code would be easier to read with less functors and signatures.

I have:

  • Removed the EphemeralMap functor that turned out not to be useful
  • Merged the OffsetDomain and CompositeDomain signature into the Domain signature (the goal was to be able to extend the code to domains such as ADT constructors, which don't have an offset and are never composite, but that is not really an useful generalization at the moment)
  • Merged AtomicType and CompositeType into the NormalForm signature (I intended to do this one prior to submitting the PR but forgot to actually do it)
  • Removed the UfHandle functor in favor of a Canon sub-module in Domains_make.Ephemeral
  • Inlined the HandleNotations functor into the EphemeralDomainMap signature (I was not really happy with this functor in the first place; the goal was to avoid code duplication for the definition of update but all solutions I found, including HandleNotations, were too clunky and verbose, and the code for update is small, so I think it's OK to duplicate here)
  • Renamed the BinRel functor to WatchMap to make it less abstract, and removed some functions from it that were unused.

I think the remaining abstractions (except for WatchMap) are necessary to be able to share the implementation between interval/bitlist domains and (in the future) between bit-vector/arithmetic normal forms. I am not too happy with the remaining WatchMap functor but I don't have a good solution to avoid it without significant changes.

It is probably easier to write it like this for you

It is not that it is easier to write but that I find it easier to understand code with small encapsulated abstractions. I agree I went a little overboard here though, hopefully it is better now.

reading the file Rel_utils gives me the same feeling that reading Bourbaki, a bunch of abstract axioms before reaching the real stuff.

That is a common and unfortunate side-effect of OCaml processing definitions in order, and the solution is to read from the bottom up — OCaml code reads like a research paper, out of order. I have moved the signature definitions to a new Domains_intf module, and the implementation itself to a new Domains module.

Assuming [x] and [y] are bit-vectors of width 2:
- [101 @ x] is [x + 10100] ;
- [10 @ x @ 01] is [(x @ 00) + 100001] ;
- [10 @ y<0, 0> @ y<1, 1>] is [(y<0, 0> @ y<1>1) + 1000] ;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this is the syntax that is used elsewhere in documentation comments for extraction (and indeed there is a typo, it should be y<1, 1>). I will clarify.

Comment on lines 429 to 430
[domain]. The explanation [ex] justifies that the [domain] applies to
the entry's key.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True — the justification is in the domain.

(** [entry t k] returns the [handle] associated with [k].

There is a unique entry associated with each key [k] that is created
on-the-fly when [handle t k] is called for the first time.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, handle was renamed to entry at some point.


The domain associated with the entry is initialized from the underlying
persistent domain the first time it is accessed, and updated with
[update]. *)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It should be set_domain, will fix.

DMC.needs_propagation t.composites ||
not (W.Set.is_empty t.triggers)

let variables { variables ; _ } = variables
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's probably going to be inlined anyways. I can add the annotation.

bclement-ocp and others added 8 commits August 2, 2024 12:07
This patch changes the way that bit-vector domains are stored in order
to share domains between multiple bit-vectors with the same variable
part, including across different bit-width. Currently, if we had terms
[x], [00 @ x], and [10 @ x], we would store domains for each of these.
With this patch, we only store a domain for [x] and rebuild the domains
for [00 @ x] and [10 @ x] dynamically; we do this by computing normal
forms composed of a variable part ([x] here) and a constant offset
(either [0] or [10]).

This reduces the number of variables handled by the system (which is
important for global domains such as global difference) and reduces the
number of trivial propagations.

Note that this patch is not reverting OCamlPro#1044. In particular, we are
considering [x @ y] as a unique (composite) variable built up from the
atomic variables [x] and [y] (in the same way that the polynomial
[x + y] is built up from the monomial [x] and [y] in the
IntervalCalculus module; in fact, the implementation is intentionally
abstracted in order to be useable with polynomials). This is necessary
because, as noted in OCamlPro#1044, the interval domain for [x @ y] cannot be
built reconstructed precisely from the interval domains for [x] and [y].

The patch includes a bit of refactoring of the way we handle
constraints; notably, instead of aggressively substituting constraints,
we now store the original constraint and call [Uf.find] at propagation
time. This avoids having to define a substitution operation on normal
forms and allows storing the constraint dependencies directly inside the
domain (in the previous design, we could not do it easily without
repeating the expensive substitution of constraint arguments).
@bclement-ocp
Copy link
Collaborator Author

Benchmarks: This PR provides a small reasoning boost (+171-48 on the usual QF_BV subset) and a significant performance boost for bit-vector reasoning (-25% on the same QF_BV subset) compared to #1144.

In the current state of both the PR and the next branch, this is now a smaller reasoning boost (+19-14, essentially a wash) and performance boost (10%, still nothing to sneer at) on the QF_BV dataset. On the J3 dataset it gives a small reasoning boost (+51-6) and larger performance boost (15%).

@bclement-ocp
Copy link
Collaborator Author

I am not too happy with the remaining WatchMap functor but I don't have a good solution to avoid it without significant changes.

Actually, I can just fold WatchMap to DomainsMap. Pushed a last commit that does just that. This should be ready for round 2 now @Halbaroth .

@Halbaroth
Copy link
Collaborator

Thanks for your effort, I will do a second pass now and we can merge :)

@bclement-ocp bclement-ocp merged commit 6424e77 into OCamlPro:next Aug 6, 2024
15 checks passed
@bclement-ocp bclement-ocp deleted the nf3 branch August 6, 2024 07:48
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