Skip to content

New expression: count_leading_zeros_exprt #5879

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

Merged
merged 3 commits into from
Mar 10, 2021

Conversation

tautschnig
Copy link
Collaborator

Rather than ad-hoc handling __builtin_clz and __lzcnt (and their
variants) in the C front-end, make counting-leading-zeros available
across the code base.

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

{
clz.zero_permitted(true);
const source_locationt source_location = clz.source_location();

Copy link
Member

Choose a reason for hiding this comment

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

I would go stronger than the nondet: The wording in the gcc docs is that __builtin_clz is undefined when the argument is zero. Assuming they use the meaning of "undefined" as in the ISO C standard, then this is worth an assertion, to be generated in goto_check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, true, then I could perhaps demote the ID_zero_permitted to ID_C_zero_permitted?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need it at all. We don't have anything equivalent for any of the other expressions that are only partially defined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But we need to distinguish whether we're handling MSVC's __lzcnt or __builtin_clz. The former is defined for all values, the latter isn't defined for 0.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right. Then I'd make it ID_zero_permitted; a comment would be too weak, given that the semantics do differ.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Except they only sort-of do: it seems that in practice it's almost always ok, it's just that GCC doesn't provide a guarantee: https://stackoverflow.com/questions/19527897/how-undefined-are-builtin-ctz0-or-builtin-clz0. How about the solution that I just pushed?

@codecov
Copy link

codecov bot commented Mar 2, 2021

Codecov Report

Merging #5879 (7409997) into develop (46449ee) will increase coverage by 0.18%.
The diff coverage is 94.94%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5879      +/-   ##
===========================================
+ Coverage    73.37%   73.55%   +0.18%     
===========================================
  Files         1428     1429       +1     
  Lines       155121   155219      +98     
===========================================
+ Hits        113813   114167     +354     
+ Misses       41308    41052     -256     
Impacted Files Coverage Δ
jbmc/src/java_bytecode/assignments_from_json.cpp 97.26% <ø> (ø)
jbmc/src/java_bytecode/java_class_loader.cpp 96.07% <ø> (ø)
jbmc/src/java_bytecode/java_object_factory.cpp 94.29% <ø> (ø)
src/analyses/reaching_definitions.cpp 77.58% <ø> (ø)
src/analyses/reaching_definitions.h 82.75% <ø> (ø)
...ses/variable-sensitivity/abstract_value_object.cpp 95.43% <ø> (ø)
...s/variable-sensitivity/interval_abstract_value.cpp 81.96% <ø> (ø)
...sitivity/variable_sensitivity_dependence_graph.cpp 84.58% <ø> (ø)
...variable-sensitivity/variable_sensitivity_domain.h 100.00% <ø> (ø)
...ensitivity/variable_sensitivity_object_factory.cpp 97.61% <ø> (ø)
... and 117 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0b86a2...7409997. Read the comment docs.

Copy link
Collaborator

@martin-cs martin-cs left a comment

Choose a reason for hiding this comment

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

Seems reasonable if we want to go that way.

@tautschnig tautschnig force-pushed the clz-expression branch 2 times, most recently from ab6246a to 30ba008 Compare March 10, 2021 16:35
Replaces the hard-coded string by a dstringt to avoid typos, and also
changes it to a comment: the annotation (communicating from the
front-end to goto_checkt) does not change the semantics of the
expression that has been annotated.
Expression lowering may be useful well outside the solvers/ context.
Making it a method of the class being lowered also serves as a way of
describing the semantics of the expression.

This is mainly a refactoring, no changes in behaviour are intended.
Rather than ad-hoc handling __builtin_clz and __lzcnt (and their
variants) in the C front-end, make counting-leading-zeros available
across the code base.
@tautschnig tautschnig merged commit ac1e4dd into diffblue:develop Mar 10, 2021
@tautschnig tautschnig deleted the clz-expression branch March 10, 2021 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants