Skip to content

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

Draft
cezarbbb wants to merge 3 commits into
rust-lang:mainfrom
cezarbbb:nounwind-inference
Draft

cezarbbb wants to merge 3 commits 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.

@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
Collaborator

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
Collaborator

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
Collaborator

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

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-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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants