Skip to content

Mark functions nounwind when their body proves an unwind cannot escape - #165326

Open
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:nounwind-inference
Open

cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:nounwind-inference

Conversation

@cezarbbb

@cezarbbb cezarbbb commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This draft adds a query that walks optimized_mir. A call is judged by its callee, a drop by its glue, and the answer is a fixpoint over the call graph in which a cycle yields no proof; false means "no proof was found" rather than "this unwinds", so everything the analysis cannot resolve keeps today's behaviour. The result feeds fn_abi_of_instance, and the existing machinery does the rest: the unwind edges become unreachable and the call is emitted as a plain call carrying nounwind on both the definition and the call site. Answers reach downstream crates through the crate metadata as one bit per DefIndex, truncated after the last proven one, which bumps the metadata version; incremental builds write nothing and keep the current behaviour, as do builds without optimizations.

This version of the analysis runs on optimized generics; consequently, it gives up in three scenarios: when the arguments to a call still contain type parameters, when a type being dropped contains type parameters, or when a trait method resolution fails to yield a unique implementation. For generic functions, at least one of these conditions is almost always met, meaning that, in practice, the analysis can currently only provide proofs for non-generic functions.

I now need a rust-perf tool to assess the impact of my current changes on the performance and binary size of rustc.

This PR makes rustc infer nounwind at the MIR level, when a function's body proves that no unwind can escape it(no calls to potentially unwinding functions, no drop glue that can panic), it marks the function and its call sites nounwind, and records the fact in crate metadata so that downstream crates can use it too. Everything is behind -Z nounwind-inference and currently only runs at opt-level 1 or above with the unwinding panic strategy.

I compute this over MIR rather than leaning on LLVM because MIR is where the information is actually available. By the time LLVM sees a function it has already committed to invokes and landing pads, and without LTO it never sees the bodies of functions in other crates at all. The analysis bails on indirect calls and virtual shims and only claims the attribute where the body genuinely cannot unwind, so what lands in the metadata is a fact, not a guess.

The rust-perf's result is close to neutral and if anything slightly positive: the per-benchmark median is +0.03% on aarch64 and +0.08% on x86_64, primary benchmarks average −0.4% with more of them compiling faster than slower, and primary binary size is −0.2%. The exceptions are a few aarch64 derive-heavy benchmarks that regress hard: clap_derive and -nll are both around +23%, serde_derive +3.9%, syn +3.5%, while the same rows on x86_64 stay under +1%. I think the mechanism is the inline cost model, replacing an invoke plus its landing pad with a plain call makes a callee about 220 cheaper to inline, and for something like the Drop glue of Vec<syn::attr::Attribute> that is enough to cross the -Os/-Oz threshold of 250(465 down to 245 on aarch64, but 475 down to 255 on x86_64), which is exactly why the two targets diverge.

Below is a portion of the discussion from zulip thread: nounwind inference: aarch64 instruction delta 20x x86_64, along with my thoughts on it:

  • @panstromek said "I think we haven't been paying atteention to aarch64 results very much so far."

In my opinion, I would not read the +23% rows as a reason to shelve this. I think it still counts as an improvement, since the regression comes from an inline-threshold interaction rather than from the fact we are propagating, and that interaction should be fixed in the inline cost model itself rather than by withholding information from LLVM. It also helps that this is behind -Z for now, so nobody pays for it until we have the cost model where we want it.

  • @saethlin asked "Why doesn't LLVM already handle all of this? LLVM is in a better position to determine if a function can unwind, because you actually need to determine this recursively across the call graph and that is infeasible in MIR currently because it creates query cycles."

I think where LLVM can see a function's body it does, and there I expect the attribute adds nothing; what it cannot recover without LTO is the bodies that live in another crate, and that is the part I think justifies the feature. So even when cgu=1, the current PR can still retain some of the performance gains derived from cross-crate part.

  • @hanna-kruppe said "While we can do preliminary nounwind analysis on MIR, later optimizations often remove potential unwinding sites and make more things nounwind (e.g., eliminating bounds checks or devirtualizing an indirect call). So enabling ThinLTO nounwind inference would still be useful even if we did what you propose in rustc."

LLVM's view of a function is strictly richer than anything the MIR analysis can produce, but I think "more" is not the same as "earlier" or "everywhere". LLVM sees the body of the function being compiled and of whatever LTO hands it, not the crates it links against, and by the time it does see a body it has already chosen between invoke and call. So I see the two as complementary rather than competing, we supply a fact earlier and where LLVM cannot reach, and LLVM keeps doing all the refinement it already did.

One possible future direction is to fix the inline cost model interaction, then possibly extending the analysis to more shim kinds.

r? @hanna-kruppe (everyone pls feel free to assign it or review it:>)
cc @panstromek @workingjubilee @programmerjake @saethlin @bjorn3 @hanna-kruppe @tmiasko

LLM disclosure: the code and text are written by hand. The LLM contributed by providing optimization suggestions for certain parts of the code. Three of these suggestions were adopted: 1. In my earlier implementation, the record of whether an item could unwind relied on a dense table within define_tables!, allocating one boolean byte per DefIndex. This meant the table spanned the entire DefIndex space of the crate, consuming a byte for every definition, even those that had never been analyzed, which resulted in significant waste. I have replaced this with a LazyValue<DenseBitSet<u32>>, where each DefIndex is represented by a single bit, and the bitset is truncated to the last active member after encoding. 2. LLM ​​identified that a recursive routine I had written, intended to collect the drop glue requirements for a given type, was actually ineffective; furthermore, it repeatedly performed the same query, leading to a regression. This code has now been removed. 3. In previous commit, I mistakenly changed METADATA_VERSION to 13; in fact, adding a CrateRoot field does not require changing it. It is now reverted to 12.

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) 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 Nov 2, 2026
@adwinwhite

Copy link
Copy Markdown
Contributor

@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 2, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 2, 2026
Mark functions nounwind when their body proves an unwind cannot escape
@rust-bors

rust-bors Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a69e4b3 (a69e4b35d0d0e5667811a7ebfb07100121c5534f)
Base parent: 2a01bd6 (2a01bd6f8ba2b81badc578a7fa927d4ff00f8c71)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (a69e4b3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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.7% [0.3%, 1.9%] 8
Regressions ❌
(secondary)
0.7% [0.0%, 1.8%] 15
Improvements ✅
(primary)
-0.4% [-0.7%, -0.2%] 13
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-0.7%, 1.9%] 21

Max RSS (memory usage)

Results (primary 2.4%, secondary -0.2%)

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

mean range count
Regressions ❌
(primary)
2.4% [2.4%, 2.4%] 1
Regressions ❌
(secondary)
4.2% [2.2%, 5.5%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.9% [-8.9%, -1.1%] 6
All ❌✅ (primary) 2.4% [2.4%, 2.4%] 1

Cycles

Results (primary 2.3%, 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)
2.3% [2.3%, 2.3%] 1
Regressions ❌
(secondary)
4.1% [1.3%, 6.6%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.3% [-6.7%, -2.6%] 5
All ❌✅ (primary) 2.3% [2.3%, 2.3%] 1

Binary size

Results (primary -0.5%)

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

Bootstrap: 487.539s -> 489.35s (0.37%)
Artifact size: 406.62 MiB -> 406.92 MiB (0.07%)

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

cezarbbb commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

It appears that excluding generic functions results in negligible size gains, while also introducing numerous regressions. I am currently extending the analysis to include generic functions; once I have the results from another rust-perf run, I will convert this into a formal PR.

@mu001999

mu001999 commented Nov 5, 2026

Copy link
Copy Markdown
Member

@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 5, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 5, 2026
Mark functions nounwind when their body proves an unwind cannot escape
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 19464f8 (19464f8b32af9f779d837154c2233a2697c0c1b8)
Base parent: 2d20b42 (2d20b42b883dcfc014f3c2c56081eeb6091c2179)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (19464f8): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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.8% [0.2%, 1.6%] 9
Regressions ❌
(secondary)
0.6% [0.1%, 1.6%] 28
Improvements ✅
(primary)
-0.6% [-1.6%, -0.2%] 27
Improvements ✅
(secondary)
-0.6% [-1.4%, -0.3%] 5
All ❌✅ (primary) -0.2% [-1.6%, 1.6%] 36

Max RSS (memory usage)

Results (primary 2.4%, secondary -1.5%)

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

mean range count
Regressions ❌
(primary)
2.4% [2.4%, 2.4%] 1
Regressions ❌
(secondary)
4.0% [2.4%, 6.3%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.2% [-10.8%, -1.8%] 9
All ❌✅ (primary) 2.4% [2.4%, 2.4%] 1

Cycles

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

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)
6.3% [5.2%, 7.5%] 2
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
-6.3% [-7.7%, -4.4%] 7
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

Binary size

Results (primary -0.4%, 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.7% [0.2%, 1.4%] 4
Regressions ❌
(secondary)
0.7% [0.2%, 1.4%] 3
Improvements ✅
(primary)
-0.6% [-1.8%, -0.0%] 19
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.2%] 44
All ❌✅ (primary) -0.4% [-1.8%, 1.4%] 23

Bootstrap: 480.462s -> 480.233s (-0.05%)
Artifact size: 406.22 MiB -> 406.33 MiB (0.03%)

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

cezarbbb commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

A few results on aarch64 are not quite as good as expected. I need to investigate whether certain call sites along the aarch64 code path are repeatedly querying the function ABI, or if the cache for drop glue is being rebuilt within each individual instance.

@cezarbbb

cezarbbb commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

In my local testing, I discovered an issue likely caused by the inlining threshold.

The values ​​shifted from 465 to 245 on aarch64, and from 475 to 255 on x86_64. Since the cutoff for inlining is 250, this 220-unit reduction resulted in one case falling below the threshold, while the other remained above it.

For further details, please refer to the discussion on Zulip.

@panstromek

Copy link
Copy Markdown
Contributor

@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 9, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 9, 2026
Mark functions nounwind when their body proves an unwind cannot escape
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: f1af851 (f1af85156593c6a59c79bb405f8dc5e2441da410)
Base parent: fd4400d (fd4400dce238d263ff4239ac9eb902cb9547daf2)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (f1af851): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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.6% [0.3%, 1.0%] 7
Regressions ❌
(secondary)
0.4% [0.1%, 0.9%] 28
Improvements ✅
(primary)
-0.6% [-1.6%, -0.2%] 30
Improvements ✅
(secondary)
-0.4% [-1.5%, -0.2%] 14
All ❌✅ (primary) -0.4% [-1.6%, 1.0%] 37

Max RSS (memory usage)

Results (primary 2.2%, secondary -0.8%)

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

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
4.4% [2.4%, 6.0%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.9% [-11.3%, -2.2%] 6
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 1

Cycles

Results (primary -2.2%, secondary -1.7%)

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.2% [1.6%, 7.3%] 4
Improvements ✅
(primary)
-2.2% [-2.5%, -1.8%] 6
Improvements ✅
(secondary)
-5.0% [-7.0%, -3.0%] 7
All ❌✅ (primary) -2.2% [-2.5%, -1.8%] 6

Binary size

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

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

mean range count
Regressions ❌
(primary)
0.7% [0.2%, 1.4%] 4
Regressions ❌
(secondary)
0.7% [0.2%, 1.4%] 3
Improvements ✅
(primary)
-0.3% [-1.8%, -0.0%] 34
Improvements ✅
(secondary)
-0.2% [-0.5%, -0.0%] 97
All ❌✅ (primary) -0.2% [-1.8%, 1.4%] 38

Bootstrap: 485.883s -> 481.907s (-0.82%)
Artifact size: 406.18 MiB -> 406.25 MiB (0.02%)

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

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

hanna-kruppe is currently at their maximum review capacity.
They may take a while to respond.

@cezarbbb
cezarbbb marked this pull request as ready for review November 26, 2026 03:34
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 26, 2026
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

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

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-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) 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