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

Remove some assumes from slice iterators that don't do anything #111282

Merged
merged 1 commit into from
May 9, 2023

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented May 6, 2023

Because the start pointer is iterators is already a NonNull, we emit the appropriate !nonnull metadata when loading the pointer to tell LLVM that it's non-null.

Probably the best way to see that it's the metadata that's important (and not the assume) is to observe that LLVM actually removes the assume from the optimized IR: https://rust.godbolt.org/z/KhE6G963n.

(I also checked that, yes, the if-not-ZST assume on end is still doing something: it's how there's a !nonnull metadata on its load, even though it's an ordinary raw pointer. The codegen test added in this PR fails if the other assume is removed.)

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2023

r? @joshtriplett

(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-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 6, 2023
@rustbot
Copy link
Collaborator

rustbot commented May 6, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@scottmcm
Copy link
Member Author

scottmcm commented May 6, 2023

Just in case, since slice iterators really matter,
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 6, 2023
@bors
Copy link
Contributor

bors commented May 6, 2023

⌛ Trying commit ec3a9bc with merge dc509d686d4afcd39ba3abd7c29cd106c2f9477b...

@bors
Copy link
Contributor

bors commented May 6, 2023

☀️ Try build successful - checks-actions
Build commit: dc509d686d4afcd39ba3abd7c29cd106c2f9477b (dc509d686d4afcd39ba3abd7c29cd106c2f9477b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (dc509d686d4afcd39ba3abd7c29cd106c2f9477b): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.0% [-3.1%, -2.9%] 2
Improvements ✅
(secondary)
-2.5% [-3.0%, -2.0%] 2
All ❌✅ (primary) -3.0% [-3.1%, -2.9%] 2

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 54
Improvements ✅
(secondary)
-0.0% [-0.1%, -0.0%] 4
All ❌✅ (primary) -0.1% [-0.2%, -0.0%] 54

Bootstrap: 656.275s -> 653.825s (-0.37%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 6, 2023
// CHECK: %[[START:.+]] = load ptr, ptr %it,
// CHECK-SAME: !nonnull
// CHECK-SAME: !noundef
// CHECK: icmp eq ptr %[[START]], %[[END]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it matter which order the ptr comparison goes in? Sometimes LLVM likes to change that up a little.

Otherwise looks pretty good!

Copy link
Member Author

Choose a reason for hiding this comment

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

No, for eq the order shouldn't matter. I wonder if I can put the [[vars]] in a regex to allow either order...

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I definitely can't use a regex to allow either, since the [[ syntax in regexes is character classes. Not sure how best to do this...

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, yeah, I asked some LLVM devs and they basically were like "oh I dunno, I don't think we support that" so I think this is something we can't harden our tests against. Which isn't awful, I've seen operand swaps happen once or twice but that doesn't mean they're incredibly common.

@workingjubilee
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 9, 2023

📌 Commit ec3a9bc has been approved by workingjubilee

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 May 9, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 9, 2023
…r=workingjubilee

Remove some `assume`s from slice iterators that don't do anything

Because the start pointer is iterators is already a `NonNull`, we emit the appropriate `!nonnull` metadata when loading the pointer to tell LLVM that it's non-null.

Probably the best way to see that it's the metadata that's important (and not the `assume`) is to observe that LLVM actually *removes* the `assume` from the optimized IR: <https://rust.godbolt.org/z/KhE6G963n>.

(I also checked that, yes, the if-not-ZST `assume` on `end` is still doing something: it's how there's a `!nonnull` metadata on its load, even though it's an ordinary raw pointer.  The codegen test added in this PR fails if the other `assume` is  removed.)
bors added a commit to rust-lang-ci/rust that referenced this pull request May 9, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#97320 (Stabilize const_ptr_read)
 - rust-lang#110770 (Limit lifetime of format_args!() with inlined args.)
 - rust-lang#111021 (Move some tests)
 - rust-lang#111215 (Various changes to name resolution of anon consts)
 - rust-lang#111242 (support set `rpath` option  for each target independently)
 - rust-lang#111282 (Remove some `assume`s from slice iterators that don't do anything)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 273fbf4 into rust-lang:master May 9, 2023
11 checks passed
@rustbot rustbot added this to the 1.71.0 milestone May 9, 2023
@scottmcm scottmcm deleted the remove-unneeded-assumes branch May 10, 2023 02:42
bors added a commit to rust-lang-ci/rust that referenced this pull request May 11, 2023
…r=the8472

Simplify the implementation of iterators over slices of ZSTs

Currently, slice iterators over ZSTs store `end = start.wrapping_byte_add(len)`.

That's slightly convenient for `is_empty`, but kinda annoying for pretty much everything else -- see bugs like rust-lang#42789, for example.

This PR instead changes it to just `end = ptr::invalid(len)` instead.

That's easier to think about (IMHO, at least) as well as easier to represent.

`next` is still to big to get inlined into the mir-opt/pre-codegen/ tests, but if I bump the inline threshold to force it to show the whole thing, this implementation is also less MIR:
```
> git diff --numstat
241     370     tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir
255     329     tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir
184     216     tests/mir-opt/pre-codegen/slice_iter.slice_iter_mut_next_back.PreCodegen.after.mir
182     254     tests/mir-opt/pre-codegen/slice_iter.slice_iter_next.PreCodegen.after.mir
```
(That's ≈70 lines less for `Iter::next`, for example.)

r? `@ghost`

~~Built atop rust-lang#111282, so draft until that lands.~~
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.

None yet

6 participants