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

Don't emit divide-by-zero panic paths in StepBy::len #123564

Merged
merged 1 commit into from
Apr 8, 2024

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Apr 6, 2024

I happened to notice today that there's actually two such calls emitted in the assembly: https://rust.godbolt.org/z/1Wbbd3Ts6

Since they're impossible, hopefully telling LLVM that will also help optimizations elsewhere.

I happened to notice today that there's actually two such calls emitted in the assembly: <https://rust.godbolt.org/z/1Wbbd3Ts6>

Since they're impossible, hopefully telling LLVM that will also help optimizations elsewhere.
@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2024

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 6, 2024
iter: &mut I,
step_minus_one: usize,
) -> impl FnMut() -> Option<I::Item> + '_ {
move || iter.nth(step_minus_one)
Copy link
Member Author

Choose a reason for hiding this comment

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

As I went through I also tried to be consistent about naming things step_minus_one when they're not the original step, since it was confusing to have the mix.

@@ -500,7 +518,7 @@ macro_rules! spec_int_ranges_r {
fn spec_next_back(&mut self) -> Option<Self::Item>
where Range<$t>: DoubleEndedIterator + ExactSizeIterator,
{
let step = (self.step + 1) as $t;
let step = self.original_step().get() as $t;
Copy link
Member Author

Choose a reason for hiding this comment

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

Like how here the two steps in the previous code meant very different things, but since the let step is the width (not the "what to pass to nth") it's still called step.

fn first_size(step: usize) -> impl Fn(usize) -> usize {
move |n| if n == 0 { 0 } else { 1 + (n - 1) / (step + 1) }
fn first_size(step: NonZeroUsize) -> impl Fn(usize) -> usize {
move |n| if n == 0 { 0 } else { 1 + (n - 1) / step }
Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you, Div<NonZeroUsize>, for just magically doing the right thing here 🎉

Copy link
Contributor

@joboet joboet left a comment

Choose a reason for hiding this comment

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

Looks very good to me! I'm not a big fan of step_minus_one, but couldn't come up with a better name. The comment about nth made it click, so it's fine.

tests/codegen/step_by-overflow-checks.rs Show resolved Hide resolved
@joboet
Copy link
Contributor

joboet commented Apr 8, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Apr 8, 2024

📌 Commit 00bd247 has been approved by joboet

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 Apr 8, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#122781 (Fix argument ABI for overaligned structs on ppc64le)
 - rust-lang#123367 (Safe Transmute: Compute transmutability from `rustc_target::abi::Layout`)
 - rust-lang#123518 (Fix `ByMove` coroutine-closure shim (for 2021 precise closure capturing behavior))
 - rust-lang#123547 (bootstrap: remove unused pub fns)
 - rust-lang#123564 (Don't emit divide-by-zero panic paths in `StepBy::len`)
 - rust-lang#123578 (Restore `pred_known_to_hold_modulo_regions`)
 - rust-lang#123591 (Remove unnecessary cast from `LLVMRustGetInstrProfIncrementIntrinsic`)
 - rust-lang#123632 (parser: reduce visibility of unnecessary public `UnmatchedDelim`)
 - rust-lang#123635 (CFI: Fix ICE in KCFI non-associated function pointers)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9570ac4 into rust-lang:master Apr 8, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 8, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2024
Rollup merge of rust-lang#123564 - scottmcm:step-by-div-zero, r=joboet

Don't emit divide-by-zero panic paths in `StepBy::len`

I happened to notice today that there's actually two such calls emitted in the assembly: <https://rust.godbolt.org/z/1Wbbd3Ts6>

Since they're impossible, hopefully telling LLVM that will also help optimizations elsewhere.
@scottmcm scottmcm deleted the step-by-div-zero branch April 9, 2024 01:55
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-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants