Skip to content

Add LLVM range attributes to slice length parameters - #148350

Merged
bors merged 1 commit into
rust-lang:masterfrom
scottmcm:slice-range-attributes
Nov 3, 2025
Merged

bors merged 1 commit into
rust-lang:masterfrom
scottmcm:slice-range-attributes

Conversation

@scottmcm

Copy link
Copy Markdown
Member

It'll take a bunch more work to do this in layout -- because of cycles in struct Foo<'a>(&'a Foo<'a>); -- so until we figure out how to do that well, just look for slices specifically and add the proper range for the length.

@rustbot

rustbot commented Oct 31, 2025

Copy link
Copy Markdown
Collaborator

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
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 A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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 Oct 31, 2025
pub fn len_plus_ten_b(s: &[u32]) -> usize {
// CHECK: start:
// CHECK-NOT: add
// CHECK: %[[R:.+]] = add nuw nsw i{{.+}} %s.1, 10

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This file is the simplest demonstration of LLVM taking advantage of it. See https://rust.godbolt.org/z/7jxqsaaTx showing that it didn't know nuw/nsw for these things previously.

@scottmcm

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot added a commit that referenced this pull request Oct 31, 2025
Add LLVM range attributes to slice length parameters
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 31, 2025
@rust-bors

rust-bors Bot commented Nov 1, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 398e28a (398e28a631e9d9248dbf3cef0409d1b7a684b917, parent: 82ae0ee6487e93bd6c05167ccb2ef3485fdbc890)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (398e28a): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
1.0% [0.1%, 2.0%] 20
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.1%] 15
All ❌✅ (primary) -0.1% [-0.4%, 0.2%] 2

Max RSS (memory usage)

Results (secondary -1.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.1% [-4.1%, -4.1%] 1
All ❌✅ (primary) - - 0

Cycles

Results (secondary 0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.7% [2.1%, 3.8%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.9%, -2.1%] 2
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.1%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Bootstrap: 475.716s -> 474.193s (-0.32%)
Artifact size: 390.89 MiB -> 390.71 MiB (-0.04%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 1, 2025
@scottmcm

scottmcm commented Nov 1, 2025

Copy link
Copy Markdown
Member Author

The size numbers are really interesting here. 112KB off librustc_driver.so, which has to be coming from a whole bunch of stuff optimizing out from knowing more about the lengths, since there's no other changes here.

I think instruction-wide it's close enough to neutral to be tolerable. Especially since the bootstrap time is green.

@Kobzol

Kobzol commented Nov 1, 2025 •

Copy link
Copy Markdown
Member

Note that the compiler toolchain size sometimes changes e.g. by tens of KiB even on no-op/README changes :( Probably due to BOLT/PGO.

@JonathanBrouwer

Copy link
Copy Markdown
Member

I've reviewed the code and it is correct, the tests also look good.

I'm however struggling to interpret the performance results. From my experience the bootstrap time & toolchain size are so noisy that I don't even look at them. The benchmarks are a bit more red than I like (and expect from this change) tho, I don't really understand why. Especially check builds should not be affected by this change, since this code does not even run for them. Out of interest, lets rerun perf and see how reproducible the results are: @rust-timer queue

(To other reviewers, feel free to r=me if you can explain the perf)

@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 Nov 1, 2025
@rust-lang rust-lang deleted a comment from rust-bors Bot Nov 1, 2025
@JonathanBrouwer

Copy link
Copy Markdown
Member

Do we need to make a new build?
@bors try

rust-bors Bot added a commit that referenced this pull request Nov 1, 2025
Add LLVM range attributes to slice length parameters
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 1, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2d5ecaf (2d5ecaf90d3d6ce5f705cf9a7cd2b5821e180a9d, parent: d85276b256a8ab18e03b6394b4f7a7b246176db7)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2d5ecaf): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
1.0% [0.1%, 1.9%] 20
Improvements ✅
(primary)
-1.2% [-3.0%, -0.3%] 3
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.2%] 17
All ❌✅ (primary) -0.9% [-3.0%, 0.2%] 4

Max RSS (memory usage)

Results (primary 7.6%, secondary -3.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
7.6% [7.6%, 7.6%] 1
Regressions ❌
(secondary)
3.1% [3.1%, 3.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.6% [-5.7%, -0.9%] 12
All ❌✅ (primary) 7.6% [7.6%, 7.6%] 1

Cycles

Results (primary -2.8%, secondary 0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.1% [0.8%, 8.6%] 6
Improvements ✅
(primary)
-2.8% [-2.8%, -2.8%] 1
Improvements ✅
(secondary)
-4.2% [-7.0%, -1.5%] 5
All ❌✅ (primary) -2.8% [-2.8%, -2.8%] 1

Binary size

Results (primary -0.2%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.2% [-1.1%, -0.0%] 16
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 37
All ❌✅ (primary) -0.2% [-1.1%, -0.0%] 16

Bootstrap: 476.797s -> 476.639s (-0.03%)
Artifact size: 390.82 MiB -> 388.62 MiB (-0.56%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 1, 2025
@JonathanBrouwer

JonathanBrouwer commented Nov 1, 2025 •

Copy link
Copy Markdown
Member

Ok so it is reproducible, good to know :)

Still very confusing perf tho. I think that both improvements in the primary benchmarks may actually be noise, looking at the history graph.

I think the perf is probably good enough to merge regardless? I'm not sure though, re-assigning to someone else to make the final decision r? compiler

r=me,you if you agree performance is acceptable, I already reviewed the code and that looks good

@rustbot rustbot assigned davidtwco and unassigned JonathanBrouwer Nov 1, 2025

@davidtwco davidtwco left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the perf results are okay, the primary benchmarks basically just see small improvements and its only really secondary benchmarks that have small regressions. More information is typically good and could be used more by LLVM in future, so I'd prefer to output this.

View changes since this review

@davidtwco

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Nov 3, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 0ba7bcf has been approved by davidtwco

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 Nov 3, 2025
@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors r=JonathanBrouwer,davidtwco rollup=never

@bors

bors commented Nov 3, 2025

Copy link
Copy Markdown
Collaborator

💡 This pull request was already approved, no need to approve it again.

@bors

bors commented Nov 3, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 0ba7bcf has been approved by JonathanBrouwer,davidtwco

It is now in the queue for this repository.

@bors

bors commented Nov 3, 2025

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 0ba7bcf with merge 20383c9...

@bors

bors commented Nov 3, 2025

Copy link
Copy Markdown
Collaborator

☀️ Test successful - checks-actions
Approved by: JonathanBrouwer,davidtwco
Pushing 20383c9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 3, 2025
@bors
bors merged commit 20383c9 into rust-lang:master Nov 3, 2025
1 check passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 3, 2025
@github-actions

github-actions Bot commented Nov 3, 2025

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 1f880d9 (parent) -> 20383c9 (this PR)

Test differences

Show 4 test diffs

Stage 1

  • [codegen] tests/codegen-llvm/slice-len-math.rs: [missing] -> pass (J0)

Stage 2

  • [codegen] tests/codegen-llvm/slice-len-math.rs: [missing] -> pass (J1)

Additionally, 2 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 20383c9f1d84eb9b9c6668a1668ef68a81eae274 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-apple: 8534.6s -> 6996.5s (-18.0%)
  2. dist-apple-various: 4254.6s -> 3609.7s (-15.2%)
  3. x86_64-gnu-llvm-20-3: 6505.1s -> 5581.8s (-14.2%)
  4. pr-check-1: 1710.1s -> 1482.8s (-13.3%)
  5. aarch64-apple: 10260.3s -> 8956.0s (-12.7%)
  6. dist-loongarch64-musl: 5787.4s -> 5141.4s (-11.2%)
  7. armhf-gnu: 5139.6s -> 4589.6s (-10.7%)
  8. dist-x86_64-windows-gnullvm: 5314.8s -> 4780.0s (-10.1%)
  9. x86_64-gnu-llvm-21-3: 6246.9s -> 5679.4s (-9.1%)
  10. dist-powerpc64-linux: 5682.5s -> 5168.6s (-9.0%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (20383c9): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.7% [0.1%, 2.0%] 29
Improvements ✅
(primary)
-0.3% [-0.5%, -0.1%] 6
Improvements ✅
(secondary)
-0.3% [-0.7%, -0.1%] 27
All ❌✅ (primary) -0.3% [-0.5%, 0.2%] 7

Max RSS (memory usage)

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

Cycles

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

Binary size

Results (primary -0.1%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.1% [-0.2%, -0.1%] 14
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 37
All ❌✅ (primary) -0.1% [-0.2%, -0.1%] 14

Bootstrap: 474.478s -> 472.257s (-0.47%)
Artifact size: 390.89 MiB -> 390.71 MiB (-0.05%)

@scottmcm
scottmcm deleted the slice-range-attributes branch November 6, 2025 07:25
makai410 pushed a commit to makai410/rust that referenced this pull request Nov 8, 2025
…onathanBrouwer,davidtwco

Add LLVM range attributes to slice length parameters

It'll take a bunch more work to do this in layout -- because of cycles in `struct Foo<'a>(&'a Foo<'a>);` -- so until we figure out how to do that well, just look for slices specifically and add the proper range for the length.
makai410 pushed a commit to makai410/rust that referenced this pull request Nov 10, 2025
…onathanBrouwer,davidtwco

Add LLVM range attributes to slice length parameters

It'll take a bunch more work to do this in layout -- because of cycles in `struct Foo<'a>(&'a Foo<'a>);` -- so until we figure out how to do that well, just look for slices specifically and add the proper range for the length.
HaoZeke added a commit to OmniPotentRPC/rgmin that referenced this pull request Nov 16, 2026
rustc 1.93 and 1.94 segfault in LoopVectorizePass on scale_cap, the
step cap of the FIRE loop under Solver::run_fill, at opt-level 2 and
3. cargo build --release fails with them, and so does the Meson build
that the gpridge easyconfig of EasyBuild runs. Debug builds, rustc
1.92 and older, and rustc 1.95 and newer compile it.

Both toolchains ship LLVM 21.1.8. LLVM 21 vectorizes a maxnum
reduction without fast-math flags by leaving the vector loop when a
lane is NaN, and the scalar loop then resumes from the start of the
last vector iteration. The end value of an induction other than the
canonical counter, here the index i that steps by 3 and is read after
the loop, becomes the step times a select of that iteration and the
vector trip count. LLVM 21 does not treat a select of scalars as a
scalar, so it asks for the last vector lane of that end value, which
was never computed, and dereferences a null pointer. LLVM 22 has the
missing case (llvm/llvm-project@ce44e0b63ad2); no 21.x release got it.
Rust 1.93 is the first release to vectorize the loop: the range
attributes it puts on slice lengths (rust-lang/rust#148350) make the
bounds-check exits of the loop countable. rustc 1.92 reports an early
exit and leaves the loop scalar.

scale_cap now splits trial and origin into atoms with as_chunks::<3>,
copies each atom out as an array, and folds f64::max over the atom
distances and then the distance of the tail. No index outlives a loop.
LLVM 21 compiles it at every opt-level and target-cpu tried, on x86-64
and aarch64, and so do rustc 1.88.0, 1.89.0, stable and nightly.

The copies keep the max vectorized. Over references to the atoms, the
inlined loop keeps llvm.experimental.noalias.scope.decl calls. With
them, the cost model of LLVM 22 (rustc 1.95.0, and opt 22.1.8) finds
vectorizing unprofitable, and that cap ran 1.5 to 1.9 times slower
than the indexed loop there. By value, LLVM 21, 22 and 23 vectorize
the max, and LLVM 20 vectorizes neither form. In a microbenchmark of a
4096-atom per-atom cap, the old and new code take 13.6 and 13.7 us
with rustc 1.88.0, 13.8 and 9.1 us with 1.92.0, 9.0 and 8.9 us with
1.95.0, and 8.8 us each with 1.100.0. With 1.93 and 1.94 the new code
takes 8.8 us.

The bits are the same. Each atom computes dx * dx + dy * dy + dz * dz
as before. The tail and whole-vector sums now start from -0.0 instead
of 0.0, which no square changes. f64::max skips a NaN distance as the
old loop did, and since no distance is -0.0 the vector lanes reach the
same maximum in any order. A zero size can now be -0.0, and scale_cap
rescales by neither zero. The rescale is unchanged, and an origin
shorter than the trial still panics.

New tests: a copy of the indexed loops writes the same bits as
scale_cap for zero, -0.0 and trial == origin displacements, caps of 0,
-0, -1, 1e-300, 0.05, 0.2, 1, 1e300, inf and NaN, and each of ten
extreme and non-finite values at every position of trial and of
origin. They cover lengths 0 to 13, 26 and 112 at six displacement
scales, the last two long enough for the vectorized max to run several
vector iterations, and 2000 random cases of up to 400 coordinates, per
atom and over the whole vector. The copy reads its loop bound through
black_box, or LLVM 21 would crash on the test build in release. Both
panic on a short origin.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. 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.

7 participants