Skip to content

fix bugs in the smt cannonicalazion passes for sdiv and srem #74

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dominicmkennedy
Copy link
Contributor

There are few bugs in SRemFold and SDivFold.
When the canonicalizer sees constants as operands for the SRem and SDiv operations, it tries to elide the instruction and instead just return the new constant itself, unfortunatly on the edge cases it doesn't actually match z3's semantics.

For SRem when the rhs is 0 then the result should always be the lhs.
For SDiv when there's an underflow it should return INT_MIN, and when there's a div by zero the result should either be one or neg one, depending on sign of lhs

I also took the liberty of adding passes for Udiv and URem.
Not sure if there are unit tests for this part of the codebase, but if so I'd like to add some if I could, just to be extra sure of everything

Copy link
Contributor

@math-fehr math-fehr left a comment

Choose a reason for hiding this comment

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

Thanks a lot!
I need to add the proper tests for constant folding, I have some idea that will require to essentially test all possible values with 4 bits, that should do it I think!
Otherwise, the tests are currently just input/output tests in tests/filecheck/canonicalize-smt/ Can you add one simple test just to be sure that this gets called?

Comment on lines +166 to +168
if rhs == 0 and isinstance(op.lhs, OpResult):
rewriter.replace_matched_op(op.lhs.op.clone())
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if rhs == 0 and isinstance(op.lhs, OpResult):
rewriter.replace_matched_op(op.lhs.op.clone())
return
if rhs == 0:
rewriter.replace_matched_op([], op.lhs)
return

Comment on lines +195 to +197
if rhs == 0 and isinstance(op.lhs, OpResult):
rewriter.replace_matched_op(op.lhs.op.clone())
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if rhs == 0 and isinstance(op.lhs, OpResult):
rewriter.replace_matched_op(op.lhs.op.clone())
return
if rhs == 0:
rewriter.replace_matched_op([], [op.lhs])
return

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.

2 participants