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

Add assumes to slice length calls #122926

Closed
wants to merge 1 commit into from

Conversation

scottmcm
Copy link
Member

Since .len() on slices is safe, let's see how this impacts things vs what we could do with #121965 and LLVM 19

@rustbot
Copy link
Collaborator

rustbot commented Mar 23, 2024

r? @wesleywiser

rustbot has assigned @wesleywiser.
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 23, 2024
// CHECK: %[[J:.+]] = phi [[USIZE]]
// CHECK: %[[I:.+]] = phi [[USIZE]]
// CHECK-NOT: phi
// CHECK: add nuw nsw [[USIZE]] %[[I]], %[[J]]
Copy link
Member Author

Choose a reason for hiding this comment

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

Demo that this is neither nuw nor nsw today: https://rust.godbolt.org/z/ne1bMPqv9

  %_16.us = add i64 %iter2.sroa.0.09.us, %iter.sroa.0.011.us, !dbg !56
  tail call void @do_something(i64 noundef %_16.us), !dbg !58

Since `.len()` on slices is safe, let's see how this impacts things vs what we could do with 121965 and LLVM 19
if let Some(elem_bytes) = std::num::NonZeroU64::new(elem_ty.size.bytes()) {
let isize_max = (1_u64 << (bx.sess().target.pointer_width - 1)) - 1;
let len_max = isize_max / elem_bytes;
let limit = bx.icmp(IntPredicate::IntULE, length, bx.const_usize(len_max));
Copy link
Member Author

Choose a reason for hiding this comment

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

I first tried doing this with assume(sge(len * elem_bytes, 0)), but got worse results -- I think LLVM is confused by the SGE limit compared with other positive things, even though it actually simplifies ule(x, isize::MAX) into sgt(x, -1).

@scottmcm
Copy link
Member Author

@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 23, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 23, 2024
Add `assume`s to slice length calls

Since `.len()` on slices is safe, let's see how this impacts things vs what we could do with rust-lang#121965 and LLVM 19
@bors
Copy link
Contributor

bors commented Mar 23, 2024

⌛ Trying commit eed1ccd with merge b6cd5cf...

@bors
Copy link
Contributor

bors commented Mar 23, 2024

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b6cd5cf): comparison URL.

Overall result: ❌✅ regressions and improvements - 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.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@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.6% [0.2%, 2.5%] 111
Regressions ❌
(secondary)
0.5% [0.2%, 2.5%] 40
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.7%, -0.3%] 12
All ❌✅ (primary) 0.6% [0.2%, 2.5%] 111

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)
3.3% [3.3%, 3.3%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.3% [-4.3%, -4.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.5% [-4.3%, 3.3%] 2

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)
1.4% [0.8%, 2.6%] 26
Regressions ❌
(secondary)
2.1% [1.6%, 2.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.0% [-1.0%, -1.0%] 1
All ❌✅ (primary) 1.4% [0.8%, 2.6%] 26

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.0% [0.0%, 0.0%] 4
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 11
Improvements ✅
(primary)
-0.2% [-0.3%, -0.2%] 8
Improvements ✅
(secondary)
-0.6% [-0.6%, -0.6%] 3
All ❌✅ (primary) -0.1% [-0.3%, 0.0%] 12

Bootstrap: 669.588s -> 679.345s (1.46%)
Artifact size: 315.05 MiB -> 312.86 MiB (-0.70%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 23, 2024
@saethlin
Copy link
Member

Wow it looks like that knocked a fair chunk of code out of librustc_driver.so
The compile time cost looks a bit steep but this is clearly doing something

@the8472
Copy link
Member

the8472 commented Mar 24, 2024

See also #116542, which will add range metadata and niches to the slice length. If I can get it to work.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 4, 2024

I think I'll close it in this form, though -- the perf costs are pretty high.

Hopefully #121965 lets this just be parameter metadata to get more benefits at lower cost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants