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

Llvm sem #130

Merged
merged 12 commits into from
Nov 9, 2023
Merged

Llvm sem #130

merged 12 commits into from
Nov 9, 2023

Conversation

goens
Copy link
Collaborator

@goens goens commented Nov 7, 2023

Check our denotation with the LLVM semantics (and document those).

The only thing I'm not 100% sure is what happens with constants when they exceed the bitwidth (will check that with examples later)

Copy link
Collaborator

@bollu bollu 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 for doing the hard work of checking these andres! I have a single flavour of question about the bits, and one that I have left unreviewed because I don't trust myself enough tonight.

SSA/Projects/InstCombine/LLVM/Semantics.lean Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
Comment on lines 116 to 123
def srem? {w : Nat} (x y : BitVec w) : Option <| BitVec w :=
if y.toInt = 0
then none
else
let div := (x.toInt / y.toInt)
if div < Int.ofNat (2^w)
then some $ BitVec.ofInt w (x.toInt.rem y.toInt)
else none
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be reviewed carefully, but I lack the mental acuity today to do so. I'm postponing this to tomorrow, I'm afraid :)

Copy link
Collaborator

@bollu bollu left a comment

Choose a reason for hiding this comment

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

I believe your answers to my previous questions, and left one more type of question: I wonder if we should also check for negative overflow, with div >= - Int.ofNat (2^w)

SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
then none
else
let div := (x.toInt / y.toInt)
if div < Int.ofNat (2^w)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we not need to check overflow on the negative side as well?

Suggested change
if div < Int.ofNat (2^w)
if div < Int.ofNat (2^w) && div >= - (Int.ofNat (2^w))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@bollu good point! but then it should be < intMin or > intMax, right? (in particular, that's 2^(w-1), no?)

Copy link
Collaborator

@bollu bollu Nov 8, 2023

Choose a reason for hiding this comment

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

You're right, in 2s complement, something of width w goes from $[-2^{w-1}, 2^{w-1})$

SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
then none
else
let div := (x.toInt / y.toInt)
if div < Int.ofNat (2^w)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we not need to check the other side of overflow as well?

Suggested change
if div < Int.ofNat (2^w)
if div < Int.ofNat (2^w) && div >= - (Int.ofNat (2^w))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same as above

Copy link
Collaborator

@bollu bollu left a comment

Choose a reason for hiding this comment

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

I wonder if there is some kind of unit test we can write for these, since it seems quite error prone to get these right.

It all looks consistent to me now, so feel invited to merge!

@goens
Copy link
Collaborator Author

goens commented Nov 9, 2023

I wonder if there is some kind of unit test we can write for these, since it seems quite error prone to get these right.

It all looks consistent to me now, so feel invited to merge!

yeah, ideally we should use the mlirnatural entry point and run a bunch of tests to compare with LLVM/MLIR. I'll add an issue for that.

@goens goens merged commit 060464a into main Nov 9, 2023
1 check passed
@goens goens deleted the llvm-sem branch November 9, 2023 12:37
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