-
Notifications
You must be signed in to change notification settings - Fork 273
Refactoring in goto_symex #3988
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
Refactoring in goto_symex #3988
Conversation
4701b68
to
c3bede4
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.
🚫
This PR failed Diffblue compatibility checks (cbmc commit: f47879d).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/98997921
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: c3bede4).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/99004545
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.
The pointer-analysis changes seem fine. I am unsure about the value of moving to the functional "lots of small static function" from the OO "protected utility functions". This seems an issue of style and one that risks flip-flopping depending on who is working on the code. I'm not sure here is the place to have that discussion.
src/goto-symex/goto_symex_state.cpp
Outdated
@@ -690,18 +690,15 @@ void goto_symex_statet::rename( | |||
} | |||
} | |||
|
|||
if(type.id()==ID_array) | |||
if(const auto &array_type = type_try_dynamic_cast<array_typet>(type)) |
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 guess this fits more with the current way of doing things, so, yes.
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 am no fan -- this doesn't improve execution speeds, but is less readable.
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.
It provides extra validation beyond just checking the id, which depends on the type you are casting to
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 am in favour of using type_try_dynamic_cast
, because it means that you only need to know the name of the type you want to cast to, you don't need to know that the .id()
is supposed to be ID_array
for an array_typet
, in order to do the cast. It also reduces the likely-hood of mismatches between the check and the cast. There is little chance of checking for ID_array
and casting to bool_typet
, when both operations are done by the try_cast
. I would however be in favour of renaming type_try_dynamic_cast
to something less verbose.
UNREACHABLE; | ||
auto emplace_result = state.level1.current_names.emplace( | ||
lhs.get_l1_object_identifier(), std::make_pair(lhs, 0)); | ||
CHECK_RETURN(emplace_result.second); |
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.
Definitely.
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'm in favour of small static functions for a class that is not going to be inherited from. It makes the class interface easier to read (even if it is a drop in the ocean in goto-symex).
src/goto-symex/goto_symex_state.cpp
Outdated
static void set_l2_indices( | ||
symex_level0t level0, | ||
symex_level1t level1, | ||
symex_level2t level2, |
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.
Can we please pass all these as const references? That will also show that their operator()
should have been marked const
(or maybe even static
?), which will necessitate another commit to be added.
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.
Done and I've marked the operator const for symex_level1t, and replaced symex_level0t by a function so it doesn't need to be passed as an argument here.
src/goto-symex/goto_symex_state.cpp
Outdated
/// When the type of the operands of a `with_exprt` or `if_exprt` have been | ||
/// renamed they could end up being different from that of the expression. We | ||
/// fix that by propagating the type of its operands to \p expr. | ||
static void fix_type(exprt &expr) |
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 do note that I consider this a workaround for a genuine bug, and I'd be curious to see what happens if instead of replacing we actually checked that the types already are consistent?
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.
This should really not be necessary. We need to get rid of that renaming.
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.
Indeed it doesn't seem necessary. I've tested adding a invariant that checks that this does not do anything and all tests in regression/cbmc passed so there doesn't seem to be a bug we need to workaround. I will remove that part.
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.
As a matter of procedure, would you mind just dropping the commit from this PR, and create a separate PR that removes that code and replaces it by the invariant you've played with? Thank you!!
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.
made the PR there: #3993 and dropped the commit from here rebased this one on it
src/goto-symex/symex_dead.cpp
Outdated
else | ||
rhs=exprt(ID_invalid); | ||
exprt rhs = [&]() -> exprt { | ||
if(auto failed = get_failed_symbol(to_symbol_expr(code.symbol()), ns)) |
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.
The to_symbol_expr
is actually unnecessary, code.symbol()
already returns a symbol_exprt
.
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.
done
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.
This needs another go -- I am in particularly concerned about the "renaming fix".
src/goto-symex/goto_symex_state.cpp
Outdated
return ns.lookup(to_struct_tag_type(type)).type; | ||
return {}; | ||
} | ||
|
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.
This should not be necessary.
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.
If you mean "renaming is never needed for tagged types," I think that's untrue when we have structs with a flexible array member to rename.
src/goto-symex/goto_symex_state.cpp
Outdated
@@ -27,6 +27,14 @@ Author: Daniel Kroening, [email protected] | |||
|
|||
static void get_l1_name(exprt &expr); | |||
|
|||
static void set_l2_indices( | |||
symex_level0t level0, |
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.
Commented on the other PR, but can you explain the by-value passing here?
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.
the reference was omitted by mistake, but this reveals that this level0 is actually constant, I going to replace the symex_level0t by a function
f5d3a86
to
466b69a
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.
🚫
This PR failed Diffblue compatibility checks (cbmc commit: f5d3a86).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/99114133
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.
51d7934
to
c4d9ca4
Compare
@kroening Is this looking better? |
c4d9ca4
to
02c7e5b
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: 02c7e5b).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/99164643
02c7e5b
to
ce7a898
Compare
b55e0aa
to
beb87d3
Compare
I agree with @LAJW comment on that, plus it is generally recommended to make functions non-member whenever possible
See @NathanJPhillips comment on that, and renaming it would be outside the scope of this PR.
I removed these two lambda's.
I removed this commit, though I don't see the value of this, the inheritance is not used, and it initializes a map that is never used. |
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: beb87d3).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/100460459
That could be considered a matter of taste. But, still, there are clear advantages of using static functions:
|
beb87d3
to
569ecb6
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: 569ecb6).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/100766529
src/goto-symex/goto_symex_state.cpp
Outdated
@@ -231,32 +239,41 @@ void goto_symex_statet::assignment( | |||
#endif | |||
} | |||
|
|||
void goto_symex_statet::set_l0_indices( | |||
static void set_l0_indices( | |||
symex_level0t &level0, |
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.
This can surely be const
src/goto-symex/goto_symex_state.cpp
Outdated
rename(type, l1_identifier, ns, level); | ||
} | ||
else if(type.id() == ID_union_tag) | ||
else if(const auto &followed_type = type_of_symbol_or_tag(type, ns)) |
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.
This isn't a reference
src/goto-symex/goto_symex_state.cpp
Outdated
/// Return the type corresponding to a symbol or tag. Empty optional if \p type | ||
/// is neither a symbol or a tag. | ||
static optionalt<typet> | ||
type_of_symbol_or_tag(const typet &type, const namespacet &ns) |
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 think you could replace all this with type = ns.follow(type);
, at the top of rename_type
, as that's a no-op for non-tags.
src/goto-symex/goto_symex_state.cpp
Outdated
return ns.lookup(to_struct_tag_type(type)).type; | ||
return {}; | ||
} | ||
|
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.
If you mean "renaming is never needed for tagged types," I think that's untrue when we have structs with a flexible array member to rename.
If I were you I'd drop the static-ifcation and have the |
569ecb6
to
9d0dea9
Compare
I've dropped the dynamic_cast change, and applied your suggestion for the |
9d0dea9
to
19805b7
Compare
src/goto-symex/goto_symex_state.cpp
Outdated
@@ -688,6 +688,8 @@ void goto_symex_statet::rename( | |||
const namespacet &ns, | |||
levelt level) | |||
{ | |||
type = ns.follow(type); |
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'm not even sure that compiles, but even when you make it compile: I think it should be done after requires_renaming
to make sure we don't unnecessarily break sharing.
std::make_pair(lhs, 0))).second) | ||
UNREACHABLE; | ||
auto emplace_result = state.level1.current_names.emplace( | ||
lhs.get_l1_object_identifier(), std::make_pair(lhs, 0)); |
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.
Until we have C++17 use:
auto emplace_result = state.level1.current_names.emplace(
std::piecewise_construct,
std::forward_as_tuple(lhs.get_l1_object_identifier()),
std::forward_as_tuple(lhs, 0));
to avoid even constructing the second pair if the key already exists.
19805b7
to
5e95d69
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.
🚫
This PR failed Diffblue compatibility checks (cbmc commit: 5e95d69).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/100977333
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: 5e95d69).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/100977333
Make obvious the operator doesn't change the state of the object.
5e95d69
to
6bd85cf
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: 6bd85cf).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/101311049
src/goto-symex/goto_symex_state.cpp
Outdated
@@ -675,6 +675,8 @@ void goto_symex_statet::rename( | |||
if(!requires_renaming(type, ns)) | |||
return; // no action | |||
|
|||
type = ns.follow(type); | |||
|
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.
Is that not expanding some tag types?
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.
Intentionally so, with the idea to replace the expansion below (lines 728ff have been removed). But I might be missing something.
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.
Ok -- I'd move below the following if
block, and then add a comment such as
// expand struct and union tag types
@romainbrenguier I talked to @kroening on Slack, he said this is ok once the |
This avoids a disjunction of cases for the different type of tags and makes the rename function simpler.
Using emplace avoids a call to make_pair, and CHECK_RETURN is more adapted to the situation were we check the return value of insert/emplace.
This is more precise on the type of object returned.
6bd85cf
to
2c74ebe
Compare
@romainbrenguier Many thanks for the extra effort! |
This are small refactoring commits in preparation for #3986