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

Lower unchecked_div/_rem to MIR's BinOp::Div/Rem #112168

Merged
merged 5 commits into from
Jun 3, 2023

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Jun 1, 2023

As described in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.BinOp.html#variant.Div, the ordinary BinOps for these are already UB for division by zero (or overflow, demo), as MIR building is responsible for inserting code to panic for those cases regardless of whether the overflow checks are enabled.

So we can lower these in the same arm that lowers wrapping_add to MIR BinOp::Add and such, as all these cases turn into ordinary Rvalue::BinaryOps.

@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2023

r? @oli-obk

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 1, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@RalfJung
Copy link
Member

RalfJung commented Jun 1, 2023

are already UB for division by zero (or overflow

The MIR comment only mentions 0, not overflow. However the Miri implementation makes div and rem also UB on overflow. We should probably update the MIR comment then.

@rustbot
Copy link
Collaborator

rustbot commented Jun 2, 2023

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

@scottmcm
Copy link
Member Author

scottmcm commented Jun 2, 2023

Updated the comments on Div/Rem to also mention that MIN / -1 is UB, but also to clarify that division by zero is completely fine (not UB) for floating-point types.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 2, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jun 2, 2023

📌 Commit adb37d4 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 2, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 2, 2023
…mpiler-errors

Rollup of 6 pull requests

Successful merges:

 - rust-lang#109609 (Separate AnonConst from ConstBlock in HIR.)
 - rust-lang#112166 (bootstrap: Rename profile = user to profile = dist)
 - rust-lang#112168 (Lower `unchecked_div`/`_rem` to MIR's `BinOp::Div`/`Rem`)
 - rust-lang#112183 (Normalize anon consts in new solver)
 - rust-lang#112211 (pass `--lib` to `x doc`)
 - rust-lang#112223 (Don't ICE in new solver when auto traits have associated types)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 5460f92 into rust-lang:master Jun 3, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 3, 2023
@scottmcm scottmcm deleted the lower-div-rem-unchecked-to-mir branch June 3, 2023 02:16
bjorn3 pushed a commit to bjorn3/rust that referenced this pull request Jun 15, 2023
…o-mir, r=oli-obk

Lower `unchecked_div`/`_rem` to MIR's `BinOp::Div`/`Rem`

As described in <https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.BinOp.html#variant.Div>, the ordinary `BinOp`s for these are already UB for division by zero ([or overflow](https://llvm.org/docs/LangRef.html#sdiv-instruction), [demo](https://rust.godbolt.org/z/71e7P7Exh)), as MIR building is responsible for inserting code to panic for those cases regardless of whether the overflow checks are enabled.

So we can lower these in the same arm that lowers `wrapping_add` to MIR `BinOp::Add` and such, as all these cases turn into ordinary `Rvalue::BinaryOp`s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants