Skip to content

Add unstable loop unrolling hint attributes#156816

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
saethlin:loop-attributes
Jun 16, 2026
Merged

Add unstable loop unrolling hint attributes#156816
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
saethlin:loop-attributes

Conversation

@saethlin

@saethlin saethlin commented May 22, 2026

Copy link
Copy Markdown
Member

View all comments

Tracking issue: #156874

This adds as new attribute #[unroll]/#[unroll(full)]/#[unroll(never)]/#[unroll(16)] (or any u32).

#[unroll] is behind a new feature gate #![feature(loop_hints)] because I intend to add an attribute for loop vectorization as well. If a user wants to turn off loop unrolling to locally minimize code size, LLVM may vectorize the loop even though it isn't unrolled which can produce a similar code size explosion.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 22, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@saethlin saethlin force-pushed the loop-attributes branch 2 times, most recently from 9c8f21c to 22381f6 Compare May 24, 2026 21:15
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@saethlin saethlin force-pushed the loop-attributes branch 2 times, most recently from 4d232fd to 9c8493b Compare May 31, 2026 20:58
@saethlin saethlin marked this pull request as ready for review May 31, 2026 20:58
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 31, 2026
@rustbot

rustbot commented May 31, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred in coverage instrumentation.

cc @Zalathar

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 31, 2026
@rustbot

rustbot commented May 31, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

@saethlin saethlin changed the title Prototype loop unrolling hint attributes Add unstable loop unrolling hint attributes May 31, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

(would like to take a look at this as well, should have time in the next few days)

@JonathanBrouwer JonathanBrouwer self-assigned this May 31, 2026
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks reasonable, I'll let Jonathan take a closer look at the attribute stuff.

Maybe I'm missing something or am just used to different parts of the code base, but some style things seemed off to me. Feel free to disregard that though, I guess.

View changes since this review

Comment thread compiler/rustc_middle/src/mir/terminator.rs Outdated
Comment thread compiler/rustc_middle/src/mir/mod.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/builder.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/builder.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/builder.rs Outdated
Comment thread compiler/rustc_attr_parsing/src/attributes/unroll.rs
Comment thread compiler/rustc_attr_parsing/src/attributes/unroll.rs Outdated
Comment thread compiler/rustc_mir_build/src/thir/cx/expr.rs Outdated
Comment thread tests/codegen-llvm/loop-attrs/unroll-for-metadata.rs
@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 2, 2026
@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@saethlin

Copy link
Copy Markdown
Member Author

The regressions in #156816 (comment) are primarily if not entirely due to the increase in the size of Terminator, but I don't see an easy way to improve that. The size of TerminatorKind is limited by the size of the Call variant, and I do not see any opportunities to make that smaller.

I could improve the size by stuffing the attributes into TerminatorKind::Goto, but I'm not sure if that is good design, and we would be right back in this situation again if anyone (like me) wants to add call site inlining attributes.

@saethlin saethlin added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 12, 2026

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, assuming we want to take the perf hit (which sounds reasonable). I think @JonathanBrouwer wanted to have another look?

View changes since this review

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

I'd like to take a look, will do so in the next few days :)

@folkertdev

Copy link
Copy Markdown
Contributor

Then for the administration, let's

r? JonathanBrouwer

@JonathanBrouwer JonathanBrouwer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah from taking a further look I think any way to gain back the perf would seriously regress code maintainablity, so

@bors r=JonathanBrouwer,folkertdev rollup=never

View changes since this review

@rust-bors

rust-bors Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 8ff48f5 has been approved by JonathanBrouwer,folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Jun 15, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 16, 2026
@rust-bors

rust-bors Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: JonathanBrouwer,folkertdev
Duration: 3h 34m 7s
Pushing b998449 to main...

@rust-bors rust-bors Bot merged commit b998449 into rust-lang:main Jun 16, 2026
13 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 16, 2026
@github-actions

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 9d862dd (parent) -> b998449 (this PR)

Test differences

Show 46 test diffs

Stage 1

  • [codegen] tests/codegen-llvm/loop-attrs/unroll-for-metadata.rs: [missing] -> pass (J0)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-for-works.rs: [missing] -> pass (J0)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-loop-metadata.rs: [missing] -> pass (J0)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-while-metadata.rs: [missing] -> pass (J0)
  • [ui] tests/ui/attributes/unroll/invalid-unroll.rs: [missing] -> pass (J3)
  • [ui] tests/ui/feature-gates/feature-gate-loop-hints.rs: [missing] -> pass (J3)
  • [ui (polonius)] tests/ui/attributes/unroll/invalid-unroll.rs: [missing] -> pass (J4)
  • [ui (polonius)] tests/ui/feature-gates/feature-gate-loop-hints.rs: [missing] -> pass (J4)

Stage 2

  • [ui] tests/ui/attributes/unroll/invalid-unroll.rs: [missing] -> pass (J1)
  • [ui] tests/ui/feature-gates/feature-gate-loop-hints.rs: [missing] -> pass (J1)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-for-metadata.rs: [missing] -> pass (J2)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-for-works.rs: [missing] -> pass (J2)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-loop-metadata.rs: [missing] -> pass (J2)
  • [codegen] tests/codegen-llvm/loop-attrs/unroll-while-metadata.rs: [missing] -> pass (J2)

Additionally, 32 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 b998449636a48e2c4a362809085b600a0174e1f2 --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. i686-gnu-1: 1h 34m -> 2h 16m (+44.3%)
  2. i686-gnu-nopt-2: 2h 27m -> 1h 25m (-42.5%)
  3. x86_64-msvc-ext1: 1h 43m -> 2h 23m (+39.2%)
  4. dist-powerpc64le-linux-gnu: 1h 47m -> 1h 6m (-38.3%)
  5. i686-msvc-1: 2h 26m -> 3h 11m (+30.2%)
  6. aarch64-apple: 2h 42m -> 3h 27m (+27.6%)
  7. test-various: 2h 21m -> 1h 46m (-24.6%)
  8. x86_64-mingw-1: 3h -> 2h 17m (-24.2%)
  9. dist-x86_64-solaris: 1h 39m -> 1h 16m (-22.4%)
  10. dist-x86_64-illumos: 1h 49m -> 1h 25m (-21.6%)
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 (b998449): comparison URL.

Overall result: ❌ regressions - please read:

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.1%, 0.3%] 17
Regressions ❌
(secondary)
0.2% [0.0%, 0.5%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.1%, 0.3%] 17

Max RSS (memory usage)

Results (primary 0.0%, secondary 1.0%)

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

mean range count
Regressions ❌
(primary)
2.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
2.0% [1.1%, 2.9%] 6
Improvements ✅
(primary)
-1.3% [-1.6%, -0.9%] 2
Improvements ✅
(secondary)
-2.0% [-2.5%, -1.5%] 2
All ❌✅ (primary) 0.0% [-1.6%, 2.6%] 3

Cycles

Results (primary 2.3%, 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)
2.3% [1.9%, 2.6%] 2
Regressions ❌
(secondary)
3.7% [2.2%, 5.8%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 1
All ❌✅ (primary) 2.3% [1.9%, 2.6%] 2

Binary size

Results (primary 0.2%, secondary 0.4%)

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

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.7%] 103
Regressions ❌
(secondary)
0.4% [0.0%, 1.3%] 70
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.0%, 0.7%] 103

Bootstrap: 519.36s -> 524.623s (1.01%)
Artifact size: 400.90 MiB -> 401.77 MiB (0.22%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. 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.

9 participants