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

Use nuw when calculating slice lengths from Ranges #108763

Merged
merged 1 commit into from
Mar 7, 2023

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Mar 5, 2023

An assume would definitely not be worth it, but since the flag is almost free we might as well tell LLVM this, especially on _unchecked calls where there's no obvious way for it to deduce it.

(Today neither safe nor unsafe indexing gets it: https://rust.godbolt.org/z/G1jYT548s)

@rustbot
Copy link
Collaborator

rustbot commented Mar 5, 2023

r? @cuviper

(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 Mar 5, 2023
@rustbot

This comment was marked as resolved.

@scottmcm
Copy link
Member Author

scottmcm commented Mar 5, 2023

To double-check that the flag really is cheap to add,
@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 Mar 5, 2023
@bors
Copy link
Contributor

bors commented Mar 5, 2023

⌛ Trying commit be2d01aaf9721c0bc7fc0c8cbc622bcc3e98f98d with merge 430c05dbb9a23df78dfb78aedbdedfd47710f194...

@scottmcm
Copy link
Member Author

scottmcm commented Mar 5, 2023

Conveniently this also lets us catch some unsafe precondition violations we miss today:

const A: [(); 5] = [(), (), (), (), ()];
const B: &[()] = unsafe { A.get_unchecked(3..1) };
dbg!(B.len()); // 18446744073709551614 today, uhoh

https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=66d31ea59577a0cbef82e10eb8d5c02d

I'll add a UI test for that once perf is done.

@bors
Copy link
Contributor

bors commented Mar 5, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (430c05dbb9a23df78dfb78aedbdedfd47710f194): comparison URL.

Overall result: ✅ improvements - 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 is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-1.1%, -0.9%] 2
Improvements ✅
(secondary)
-1.7% [-2.6%, -0.3%] 8
All ❌✅ (primary) -1.0% [-1.1%, -0.9%] 2

Max RSS (memory usage)

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

Cycles

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)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 5, 2023
@scottmcm
Copy link
Member Author

scottmcm commented Mar 5, 2023

I was just hoping for neutral, so getting a bunch of green is a pleasant surprise. Awesome that this apparently helps something in the compiler.

I've also pushed the UI test to confirm that CTFE now catches more UB (though it's only reachable in const with a feature flag).

@bors

This comment was marked as resolved.

An `assume` would definitely not be worth it, but since the flag is almost free we might as well tell LLVM this, especially on `_unchecked` calls where there's no obvious way for it to deduce it.

(Today neither safe nor unsafe indexing gets it: <https://rust.godbolt.org/z/G1jYT548s>)
Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

🚢 it

@cuviper
Copy link
Member

cuviper commented Mar 7, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Mar 7, 2023

📌 Commit 3554036 has been approved by cuviper

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 Mar 7, 2023
@bors
Copy link
Contributor

bors commented Mar 7, 2023

⌛ Testing commit 3554036 with merge 160c2eb...

@bors
Copy link
Contributor

bors commented Mar 7, 2023

☀️ Test successful - checks-actions
Approved by: cuviper
Pushing 160c2eb to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 7, 2023
@bors bors merged commit 160c2eb into rust-lang:master Mar 7, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 7, 2023
@scottmcm scottmcm deleted the indexing-nuw-lengths branch March 7, 2023 16:59
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (160c2eb): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -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.9% [0.9%, 0.9%] 1
Regressions ❌
(secondary)
1.0% [1.0%, 1.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.2% [-1.2%, -1.2%] 1
All ❌✅ (primary) 0.9% [0.9%, 0.9%] 1

Cycles

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
Improvements ✅
(secondary)
-2.3% [-2.3%, -2.3%] 1
All ❌✅ (primary) - - 0

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2024
…scottmcm

Use unchecked_sub in str indexing

rust-lang#108763 applied this logic to indexing for slices, but of course `str` has its own separate impl.

Found this by skimming over the codegen for https://github.com/oxidecomputer/hubris/; their dist builds enable overflow checks so the lack of `unchecked_sub` was producing an impossible-to-hit overflow check and also inhibiting some inlining.

r? scottmcm
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Apr 15, 2024
Use unchecked_sub in str indexing

rust-lang/rust#108763 applied this logic to indexing for slices, but of course `str` has its own separate impl.

Found this by skimming over the codegen for https://github.com/oxidecomputer/hubris/; their dist builds enable overflow checks so the lack of `unchecked_sub` was producing an impossible-to-hit overflow check and also inhibiting some inlining.

r? scottmcm
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 4, 2024
…_use_unchecked, r=Nilstrieb

Use `unchecked_sub` in `split_at`

LLVM currently isn't figuring it out on its own, even in the checked version where it hypothetically could.

Before: <https://rust.godbolt.org/z/PEY38YrKs>
```llvm
bb1:                                              ; preds = %start
  %4 = getelementptr inbounds float, ptr %x.0, i64 %n
  %5 = sub i64 %x.1, %n
```

After:
```llvm
bb1:                                              ; preds = %start
  %4 = getelementptr inbounds float, ptr %x.0, i64 %n
  %5 = sub nuw i64 %x.1, %n
```

This is not using the wrapper because there's already a ubcheck covering it, so I don't want this to get a second one once rust-lang#121571 lands.

---

This is basically the same as rust-lang#108763, since `split_at` is essentially doing two `get_unchecked`s.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 4, 2024
…_use_unchecked, r=Nilstrieb

Use `unchecked_sub` in `split_at`

LLVM currently isn't figuring it out on its own, even in the checked version where it hypothetically could.

Before: <https://rust.godbolt.org/z/PEY38YrKs>
```llvm
bb1:                                              ; preds = %start
  %4 = getelementptr inbounds float, ptr %x.0, i64 %n
  %5 = sub i64 %x.1, %n
```

After:
```llvm
bb1:                                              ; preds = %start
  %4 = getelementptr inbounds float, ptr %x.0, i64 %n
  %5 = sub nuw i64 %x.1, %n
```

This is not using the wrapper because there's already a ubcheck covering it, so I don't want this to get a second one once rust-lang#121571 lands.

---

This is basically the same as rust-lang#108763, since `split_at` is essentially doing two `get_unchecked`s.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 4, 2024
Rollup merge of rust-lang#124699 - scottmcm:split_at_unchecked_should_use_unchecked, r=Nilstrieb

Use `unchecked_sub` in `split_at`

LLVM currently isn't figuring it out on its own, even in the checked version where it hypothetically could.

Before: <https://rust.godbolt.org/z/PEY38YrKs>
```llvm
bb1:                                              ; preds = %start
  %4 = getelementptr inbounds float, ptr %x.0, i64 %n
  %5 = sub i64 %x.1, %n
```

After:
```llvm
bb1:                                              ; preds = %start
  %4 = getelementptr inbounds float, ptr %x.0, i64 %n
  %5 = sub nuw i64 %x.1, %n
```

This is not using the wrapper because there's already a ubcheck covering it, so I don't want this to get a second one once rust-lang#121571 lands.

---

This is basically the same as rust-lang#108763, since `split_at` is essentially doing two `get_unchecked`s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

6 participants