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: Add Overflow Semantics to Addition in LLVM #480

Merged
merged 24 commits into from
Aug 8, 2024

Conversation

AtticusKuhn
Copy link
Contributor

This is a smaller, scaled back version of
#471

I am wondering if we can scope it a bit smaller and just add overflow flags for addition. I feel this would allow us to iterate quickly on the right implementation and then expand it to all ops.

https://grosser.zulipchat.com/#narrow/stream/446584-Project---Lean4---BitVectors/topic/Overflow.20Flags.20in.20LLVM/near/453334239

Copy link

Alive Statistics: 59 / 93 (34 failed)

@AtticusKuhn AtticusKuhn changed the title Add Overflow Semantics to Addition in LLVM Feat: Add Overflow Semantics to Addition in LLVM Jul 23, 2024
Copy link

Alive Statistics: 59 / 93 (34 failed)

@tobiasgrosser
Copy link
Collaborator

I am surprised to see still a need to print the default false. Is this unavoidable.

@AtticusKuhn
Copy link
Contributor Author

I am surprised to see still a need to print the default false. Is this unavoidable.

I was also surpised that Lean prints MOp.BinaryOp.add w false, i.e. Lean still prints the default parameter. I'm not sure why this is.

@AtticusKuhn
Copy link
Contributor Author

Maybe someone with more knowledge of the Lean internals can comment as to why this is?

@tobiasgrosser
Copy link
Collaborator

It might be useful to build a minimal example and ask on the official lean zulip.

Copy link

Alive Statistics: 59 / 93 (34 failed)

@AtticusKuhn
Copy link
Contributor Author

It might be useful to build a minimal example and ask on the official lean zulip.

Good idea, I posted this on the Lean Zulip
https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Why.20does.20Lean.204.20Print.20Default.20Values.3F/near/453447387

Copy link

Alive Statistics: 64 / 93 (29 failed)

1 similar comment
Copy link

Alive Statistics: 64 / 93 (29 failed)

@AtticusKuhn
Copy link
Contributor Author

AtticusKuhn commented Jul 23, 2024

I am surprised to see still a need to print the default false. Is this unavoidable.

The pretty printer only considers omitting the last optional argument. I got around this by writing

inductive MOp.BinaryOp : Type
  | add (nswnuw  : Bool × Bool := ⟨ false , false ⟩ )

@AtticusKuhn AtticusKuhn added the enhancement New feature or request label Jul 23, 2024
Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link
Collaborator

@tobiasgrosser tobiasgrosser left a comment

Choose a reason for hiding this comment

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

This looks great to me. Maybe some comments from a parsing expert such as @bollu .

Copy link

Alive Statistics: 64 / 93 (29 failed)

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.

LGTM, but it'd be nice if we can refactor the flags.

SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
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 think we need to fix the flag handling to handle cases where we have both nsw and nuw.

Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link

Alive Statistics: 64 / 93 (29 failed)

1 similar comment
Copy link

Alive Statistics: 64 / 93 (29 failed)

@AtticusKuhn
Copy link
Contributor Author

AtticusKuhn commented Jul 31, 2024

I think this PR looks good to me now.

SSA/Core/MLIRSyntax/GenericParser.lean Outdated Show resolved Hide resolved
Comment on lines 104 to 106
deriving DecidableEq, Inhabited
/-- Homogeneous, binary operations -/
inductive g : Type
Copy link
Collaborator

Choose a reason for hiding this comment

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

@AtticusKuhn you've still not added the newline inbetween these two definitions

Comment on lines 135 to 136
instance : Repr (MOp.BinaryOp) where
reprPrec op w := ((toString (aux op w)).replace "InstCombine.g" "InstCombine.MOp.BinaryOp").replace "false" ""
Copy link
Collaborator

@alexkeizer alexkeizer Aug 3, 2024

Choose a reason for hiding this comment

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

/--
The reason that I am using the admittedly hacky and ad-hoc method is that I want to preserve the guard_msgs > statements, otherwise the build will fail.
But the default Repr instance has some fancy behavior where depending on the indentation it will sometimes wrap in parentheses.
I think the only way to replicate this behavior is to have another class and piggy-back off its default Repr class
-/

Thanks for writing this comment to explain your rationale! You are right that this solution deserves some comment in the code, but first this would fit better here in the code review --- also, as an aside, code comments are best not written in the first person, since a future reader might not immediately know who actually wrote that comment.

More concretely, the fancy behaviour you refer to is simply behaviour of Std.Format. You can have a look at the derive handler of Repr to see what code it generates; in particular the line: Repr.addAppParen (Format.group (Format.nest (if prec >= max_prec then 1 else 2) ($rhs:term))) prec) is where the parenthesization happens.

We could write similar logic by hand. See for example:

open Std (Format) in
def reprWithoutFlags (op : MOp.BinaryOp) (prec : Nat) : Format :=
  let op := match op with
    | .and    => "and"
    | .or     => "or"
    | .xor    => "xor"
    | .shl    => "shl"
    | .lshr   => "lshr"
    | .ashr   => "ashr"
    | .urem   => "urem"
    | .srem   => "srem"
    | .add _  => "add"
    | .mul    => "mul"
    | .sub    => "sub"
    | .sdiv   => "sdiv"
    | .udiv   => "udiv"
  Repr.addAppParen (Format.group (Format.nest
    (if prec >= max_prec then 1 else 2) f!"InstCombine.MOp.BinaryOp.{op}"))
    prec

Finally, and the reason I do consider this one a blocker for merging: your current Repr instance completely ignores flags, all four of the following lines print exactly the same (InstCombine.MOp.BinaryOp.add)

#eval repr (MOp.BinaryOp.add ⟨false, false⟩)
#eval repr (MOp.BinaryOp.add ⟨false, true⟩)
#eval repr (MOp.BinaryOp.add ⟨true, true⟩)
#eval repr (MOp.BinaryOp.add ⟨true, false⟩)

Copy link
Collaborator

@alexkeizer alexkeizer left a comment

Choose a reason for hiding this comment

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

I'd say this PR is not ready to be merged yet, some of my earlier comments still stand

SSA/Projects/InstCombine/Base.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/LLVM/Semantics.lean Outdated Show resolved Hide resolved
SSA/Projects/InstCombine/TacticAuto.lean Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Aug 5, 2024

Alive Statistics: 64 / 93 (29 failed)

3 similar comments
Copy link

github-actions bot commented Aug 5, 2024

Alive Statistics: 64 / 93 (29 failed)

Copy link

github-actions bot commented Aug 5, 2024

Alive Statistics: 64 / 93 (29 failed)

Copy link

github-actions bot commented Aug 5, 2024

Alive Statistics: 64 / 93 (29 failed)

Avoid writing code comments in the first person
| .urem => "urem"
| .srem => "srem"
| .add ⟨false, false⟩ => "add"
| .add ⟨nsw, nuw ⟩ => toString f!"add {nsw} {nuw}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
| .add ⟨nsw, nuw ⟩ => toString f!"add {nsw} {nuw}"
| .add ⟨nsw, nuw⟩ => toString f!"add {nsw} {nuw}"

Please be more careful with your whitespace!

Copy link

github-actions bot commented Aug 8, 2024

Alive Statistics: 64 / 93 (29 failed)

Copy link
Collaborator

@alexkeizer alexkeizer left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments @AtticusKuhn, I think this is good to merge now!

Nevertheless, some general remarks: I like your code style generally, but I find you tend to be a bit sloppy when it comes to whitespace (both in terms of putting newlines in between definitions, and where to put spaces inside a line). I appreciate that there is no clear style guide and things are quite vague, but do try to give your diff a thorough read yourself to catch things that are obviously wrong! It might also help to ask @Equilibris to give your code a look for general style, before waiting on me. I've left one suggestion above, please do accept that before merging.

A minor nit: comments with /-- ... -/ are generally intended for so-called docstrings. That is, documentation of a function that will show up when you hover over a call of that function. You have some comments of this form which talk about the position of the function in the file, which is not really interesting outside of the context of that file, and should probably be a -- or /- ... -/ comment instead (neither of which will show up as documentation).

This last point is an exceedingly nitpicky comment, though. Feel free to merge as-is, or to address the points yourself and merge afterwards

@alexkeizer
Copy link
Collaborator

@bollu I think you also need to "approve these changes" to unblock this PR

@AtticusKuhn
Copy link
Contributor Author

Nevertheless, some general remarks: I like your code style generally, but I find you tend to be a bit sloppy when it comes to whitespace (both in terms of putting newlines in between definitions, and where to put spaces inside a line). I appreciate that there is no clear style guide and things are quite vague, but do try to give your diff a thorough read yourself to catch things that are obviously wrong! It might also help to ask @Equilibris to give your code a look for general style, before waiting on me. I've left one suggestion above, please do accept that before merging.

Thank you for your review. I'll be sure to be more attentive in the future.

@AtticusKuhn AtticusKuhn enabled auto-merge August 8, 2024 08:29
@tobiasgrosser
Copy link
Collaborator

Great to see this merged. 🎉

Copy link

github-actions bot commented Aug 8, 2024

Alive Statistics: 64 / 93 (29 failed)

@AtticusKuhn AtticusKuhn added this pull request to the merge queue Aug 8, 2024
Merged via the queue into main with commit 4b13fba Aug 8, 2024
2 checks passed
@AtticusKuhn AtticusKuhn deleted the feat/addition_overflow_flags branch August 8, 2024 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants