Skip to content

Add rustc_nounwind to various std functions - #166589

Open
nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:add-rustc_nounwind
Open

nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:add-rustc_nounwind

Conversation

@nnethercote

@nnethercote nnethercote commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

The presence of this attribute changes invoke occurrences in LLVM IR to call, which means LLVM can use the fast FastISel instruction selector instead of the SelectionDAG selector in debug builds.

Justification for these annotations is primarily semantic -- these functions should never unwind.

  • Various deref/deref_mut/as_ref: borrow existing contents.

  • Option::is_{some,none}, Vec::{len,is_empty}, NonZero::get: observe existing state.

  • Option::take, {Option,Result}::branch, mem::forget: transfer or relinquish ownership without running drop.

  • {Option,Result}::from_output, Pin::new_unchecked, {Path,OsStr}::display, Arguments::{new,from_str}: simple wrappers.

  • {Vec,String}::new: construct an empty container (no allocation).

  • {slice,Vec::IntoIter}::next: advance an iterator; exhaustion via None.

  • Argument constructors: just bundle up some arguments.

  • OsString::{index,index_mut} with RangeFull: just address manipulation.

  • fetch_add: wrapping add, no failure mode.

  • maybe_is_nonoverlapping: comparison op, already uses panic_nounwind on mul overflow.

  • must_use: literally the identity function (with special warning behaviour).

If any of these functions were ever changed such that they could unwind, these annotations mean they would instead abort. But such a change would be a bug anyway. The nounwind property is also generally clear by looking at the code inside each function.

r? @cuviper

@rustbot rustbot added A-atomic Area: Atomics, barriers, and sync primitives A-fmt Area: `core::fmt` A-option-result Area: Option and Result combinators A-path Area: `std::path` (note: `std::fs` is A-filesystem) A-rc Area: `Rc` and `Arc` A-slice-vec Area: `[T]` and `Vec<T>` A-str Area: `str` and `String` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 26, 2026
@nnethercote

Copy link
Copy Markdown
Contributor 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 Nov 26, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Add `rustc_nounwind` to various std functions
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 0b06dff (0b06dff4880609885ea2078f3cd3fc95d98bd531)
Base parent: 86e8c94 (86e8c946f12c16ad26be9e4813cec57232290d99)

@rust-timer

This comment has been minimized.

@petrochenkov

Copy link
Copy Markdown
Contributor

This looks like a subset of what #165326 is doing?
cc @cezarbbb

The presence of this attribute changes `invoke` occurrences in LLVM IR
to `call`, which means LLVM can use the fast `FastISel` instruction
selector instead of the `SelectionDAG` selector in debug builds.

Justification for these annotations is primarily semantic -- these
functions should never unwind.

- Various `deref`/`deref_mut`/`as_ref`: borrow existing contents.

- `Option::is_{some,none}`, `Vec::{len,is_empty}`, `NonZero::get`:
  observe existing state.

- `Option::take`, `{Option,Result}::branch`, `mem::forget`: transfer or
  relinquish ownership without running `drop`.

- `{Option,Result}::from_output`, `Pin::new_unchecked`,
  `{Path,OsStr}::display`, `Arguments::{new,from_str}`: simple wrappers.

- `{Vec,String}::new`: construct an empty container (no allocation).

- `{slice,Vec::IntoIter}::next`: advance an iterator; exhaustion via `None`.

- `Argument` constructors: just bundle up some arguments.

- `OsString::{index,index_mut}` with `RangeFull`: just address
  manipulation.

- `fetch_add`: wrapping add, no failure mode.

- `maybe_is_nonoverlapping`: comparison op, already uses
  `panic_nounwind` on mul overflow.

- `must_use`: literally the identity function (with special warning
  behaviour).

If any of these functions were ever changed such that they could unwind,
these annotations mean they would instead abort. But such a change would
be a bug anyway. The nounwind property is also generally clear by
looking at the code inside each function.
@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (0b06dff): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never rustc-perf
@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.3% [0.3%, 0.3%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.7% [-1.6%, -0.2%] 26
Improvements ✅
(secondary)
-0.8% [-1.6%, -0.4%] 7
All ❌✅ (primary) -0.6% [-1.6%, 0.3%] 28

Max RSS (memory usage)

Results (primary 1.3%, secondary 0.5%)

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

mean range count
Regressions ❌
(primary)
2.1% [1.4%, 2.5%] 4
Regressions ❌
(secondary)
1.1% [0.4%, 2.6%] 8
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
-1.8% [-2.5%, -1.1%] 2
All ❌✅ (primary) 1.3% [-2.2%, 2.5%] 5

Cycles

Results (primary -2.3%, secondary -1.2%)

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

Binary size

Results (primary -0.2%, 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.1% [0.1%, 0.2%] 2
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.2% [-0.6%, -0.0%] 62
Improvements ✅
(secondary)
-0.3% [-0.9%, -0.0%] 32
All ❌✅ (primary) -0.2% [-0.6%, 0.2%] 64

Bootstrap: 461.669s -> 462.551s (0.19%)
Artifact size: 405.59 MiB -> 405.56 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 26, 2026
@cezarbbb

Copy link
Copy Markdown
Contributor

This looks like a subset of what #165326 is doing? cc @cezarbbb

Thanks for the cc:> Hmm... that doesn't seem to be entirely the subset. In my view, adding the nounwind guarantee to these functions would be quite beneficial for #165326, at the very least, it allows LLVM to emit more accurate call instructions instead of redundant invoke instructions. However, I’m not very certain about the correctness of applying this attribute (I’m not yet that familiar with std😂sorry), as doing so relies heavily on humans guarentee.

@hanna-kruppe

hanna-kruppe commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

It does feel like this is something we should be able to have the compiler infer. But my initial impression of #165326 is that it won’t be an easy or quick review. Also, the current state of that PR is trying to do much more than should be necessary for this PR: most if not all functions annotated here should be leaf functions (after MIR inlining their callees), so a shallow analysis that side-steps complications like query cycles should be sufficient.

So I don’t think we necessarily need to block the perf wins here on #165326 (if we’d otherwise be happy with the manual annotations). On the contrary, it might be useful to pick the low hanging fruit like this first and see if that changes the cost/benefit analysis for the bigger, more general hammer.

@cezarbbb

Copy link
Copy Markdown
Contributor

On the contrary, it might be useful to pick the low hanging fruit like this first and see if that changes the cost/benefit analysis for the bigger, more general hammer.

I totally agree with that haha:> My guess is that if this PR gets merged first, then when #165326 is subsequently benchmarked via rust-perf, we might see some "free" performance gains, I suppose with these functions marked NEVER_UNWIND, the analysis treats them as leaves and can prove their callers without doing any work for the callees themselves.

@nnethercote
nnethercote marked this pull request as ready for review November 26, 2026 22:31
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in library/core/src/fmt/rt.rs

cc @m-ou-se

@rustbot rustbot 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 Nov 26, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

I updated the mir-opt expected output, which should fix those test failures.

I didn't know about #165326 until @petrochenkov mentioned it. The annotation-by-hand approach is definitely the quick and dirty win; I just annotated the functions that were showing up the most in a couple of benchmarks that I tried.

Sounds like people aren't opposed to this PR, so I have moved it out of draft state and asked @cuviper for review, but if there's a more appropriate reviewer please let me know.

This branch has not been deployed

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

Labels

A-atomic Area: Atomics, barriers, and sync primitives A-fmt Area: `core::fmt` A-option-result Area: Option and Result combinators A-path Area: `std::path` (note: `std::fs` is A-filesystem) A-rc Area: `Rc` and `Arc` A-slice-vec Area: `[T]` and `Vec<T>` A-str Area: `str` and `String` 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants