Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable -Zshare-generics for inline(never) functions #123244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Mark-Simulacrum
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum commented Mar 30, 2024

This avoids inlining cross-crate generic items when possible that are
already marked inline(never), implying that the author is not intending
for the function to be inlined by callers. As such, having a local copy
may make it easier for LLVM to optimize but mostly just adds to binary
bloat and codegen time. In practice our benchmarks indicate this is
indeed a win for larger compilations, where the extra cost in dynamic
linking to these symbols is diminished compared to the advantages in
fewer copies that need optimizing in each binary.

It might also make sense it expand this with other heuristics (e.g.,
#[cold]) in the future, but this seems like a good starting point.

FWIW, I expect that doing cleanup in where we make the decision
what should/shouldn't be shared is also a good idea. Way too
much code needed to be tweaked to check this. But I'm hoping
to leave that for a follow-up PR rather than blocking this on it.

@rustbot rustbot added 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 Mar 30, 2024
@Mark-Simulacrum
Copy link
Member 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 Mar 30, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 30, 2024
…enerics, r=<try>

Enable -Zshare-generics for inline(never) functions

This avoids inlining cross-crate generic items when possible that are already marked inline(never), implying that the author is not intending for the function to be inlined by callers. As such, having a local copy may make it easier for LLVM to optimize but mostly just adds to binary bloat and codegen time (in theory, TBD on in practice).

It might also make sense it expand this with other heuristics (e.g., #[cold]).

FWIW, I expect that doing cleanup in where we make the decision what should/shouldn't be shared is also a good idea. Way too much code needed to be tweaked to check this.

r? `@Mark-Simulacrum` for perf at first
@bors
Copy link
Contributor

bors commented Mar 30, 2024

⌛ Trying commit 5702d83 with merge 1f2a5ec...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 30, 2024

☀️ Try build successful - checks-actions
Build commit: 1f2a5ec (1f2a5ecd17d6b0415946e70d192b88b566dc73f8)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1f2a5ec): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.4% [0.3%, 18.5%] 203
Regressions ❌
(secondary)
2.7% [0.3%, 16.2%] 207
Improvements ✅
(primary)
-1.2% [-4.1%, -0.3%] 28
Improvements ✅
(secondary)
-3.9% [-6.8%, -2.7%] 4
All ❌✅ (primary) 1.1% [-4.1%, 18.5%] 231

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.5% [0.6%, 3.9%] 16
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.7% [-4.5%, -1.0%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.7% [-4.5%, 3.9%] 19

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.6% [0.9%, 11.5%] 73
Regressions ❌
(secondary)
4.5% [1.2%, 14.3%] 119
Improvements ✅
(primary)
-1.8% [-3.8%, -0.9%] 12
Improvements ✅
(secondary)
-4.4% [-7.8%, -2.7%] 4
All ❌✅ (primary) 2.0% [-3.8%, 11.5%] 85

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.3%] 15
Regressions ❌
(secondary)
0.2% [0.0%, 0.3%] 38
Improvements ✅
(primary)
-0.7% [-2.3%, -0.0%] 77
Improvements ✅
(secondary)
-8.7% [-18.9%, -0.3%] 14
All ❌✅ (primary) -0.6% [-2.3%, 0.3%] 92

Bootstrap: 667.994s -> 656.525s (-1.72%)
Artifact size: 315.79 MiB -> 314.40 MiB (-0.44%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 31, 2024
@Mark-Simulacrum Mark-Simulacrum added the perf-regression-triaged The performance regression has been triaged. label Mar 31, 2024
@Mark-Simulacrum
Copy link
Member Author

It looks like the majority of the additional cost comes from additional indirection in calls to standard library functions, e.g., a diff like this:

-  f61064:       e8 73 f4 49 03          call   44004dc <_RINvNtCsavh3npScQaX_5alloc7raw_vec11finish_growNtNtB4_5alloc6GlobalECs1UKmS5rlRwk_21rustc_trait_selection>
+  ffe57a:       ff 15 a0 24 03 03       call   *0x30324a0(%rip)        # 4030a20 <_ZN5alloc7raw_vec11finish_grow17h78aea5cebcfaa28aE@Base>

This means more work, particularly for short-lived compilations, since the symbol needs to get resolved at runtime now. That cost should be eliminated with #122362, which might take some time to land but is making progress now. Most downstream programs also don't pay it since they're not linking std dynamically.

A little of the extra cost from there seems to be due to these non-inlined functions now being codegen'd with frame pointers (due to #122646). That doesn't seem like something worth worrying about, we accepted some regression from those changes already.

My sense is that something like this is probably still a good idea despite the regressions. We do see good improvements in binary sizes (including >1MB of librustc_driver.so), and bootstrap times are significantly reduced. That suggests that this is a pretty good win for the larger crates while being a slight loss for smaller crates (instruction count timings are down for some of the larger primary crates as well, e.g., ripgrep and cranelift). That seems consistent with the loss due to additional indirection due to librustc_driver dynamically linking with the standard library.

Going to mark as ready for review as such.

r? compiler

@rustbot rustbot assigned fmease and unassigned Mark-Simulacrum Mar 31, 2024
@Mark-Simulacrum Mark-Simulacrum marked this pull request as ready for review March 31, 2024 13:33
@rustbot

This comment was marked as resolved.

@fmease
Copy link
Member

fmease commented Apr 3, 2024

r? compiler

@rustbot rustbot assigned wesleywiser and unassigned fmease Apr 3, 2024
@Mark-Simulacrum
Copy link
Member Author

cc #14527

@Mark-Simulacrum
Copy link
Member Author

Poking @wesleywiser as it's been ~3 weeks here.

@saethlin
Copy link
Member

saethlin commented May 25, 2024

It's been another 3 weeks, and this looks really interesting.

r? saethlin

@rustbot rustbot assigned saethlin and unassigned wesleywiser May 25, 2024
Comment on lines +22 to +23
// This is generic, but it's only instantiated with a u32 argument and that instantiation is present
// in the local crate (see F above).
Copy link
Member

Choose a reason for hiding this comment

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

Is this testing what we want? I was expecting to see a cross-crate call to a #[inline(never)] generic function in a test, because I think the point of this PR is to change the behavior for such calls, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is indeed checking that behavior, though it's rather obscure. As per the comment just above ("These should not contribute...") we are implicitly checking that these functions are absent in the mono-items of the downstream crate (tests/codegen-units/partitioning/extern-generic.rs) since they're not listed as MONO-ITEM declarations in that file. They are referenced though via the foo function above which has to get codegen'd in the downstream crate since it is generic and never called in this one.

I suppose we could try to write a more direct test case? But this also tests that the property holds transitively (i.e., we don't codegen the called function twice with inline(never) even if it's not directly being called).

IIRC, before my changes, this would fail.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see! This is very interconnected, and I should really be used to that by now.

Copy link
Member

Choose a reason for hiding this comment

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

Also I checked and yes the test does fail without the rest of this PR.

@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum removed the perf-regression-triaged The performance regression has been triaged. label Nov 24, 2024
@Mark-Simulacrum
Copy link
Member Author

Last perf run in #123244 (comment) continues to look pretty reasonable to me. We see some regressions (as expected), but primarily those are in smaller benchmarks. For larger benchmarks, e.g., the compiler crates, we see sizeable improvements in wall times and binary sizes. It seems likely this still remains a good idea.

@saethlin, you previously already approved this, so probably makes sense for you to take another look? The core changes should be the same as before (many months ago :)

@rustbot ready

@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 24, 2024
@saethlin
Copy link
Member

Heh, I was already looking at this again because I'm quite excited to land it.

@rustbot label: +perf-regression-triaged
The binary size improvements are quite nice, and the PR has mixed icount changes, but all the regressions over 0.5% are to helloworld. Many but not all of these tiny regressions seem related to green queries. I suspect there's one or two functions in the query system that are #[inline(never)] but were getting some useful optimizations from having internal linkage. I think root-causing the regressions further is more trouble than it's worth, given all the other compile time improvement opportunities we have.

@bors r+

@bors
Copy link
Contributor

bors commented Nov 24, 2024

📌 Commit 2a0fcc7 has been approved by saethlin

It is now in the queue for this repository.

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 24, 2024
@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 24, 2024
@bors
Copy link
Contributor

bors commented Nov 25, 2024

⌛ Testing commit 2a0fcc7 with merge b773e21...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 25, 2024
…enerics, r=saethlin

Enable -Zshare-generics for inline(never) functions

This avoids inlining cross-crate generic items when possible that are
already marked inline(never), implying that the author is not intending
for the function to be inlined by callers. As such, having a local copy
may make it easier for LLVM to optimize but mostly just adds to binary
bloat and codegen time. In practice our benchmarks indicate this is
indeed a win for larger compilations, where the extra cost in dynamic
linking to these symbols is diminished compared to the advantages in
fewer copies that need optimizing in each binary.

It might also make sense it expand this with other heuristics (e.g.,
`#[cold]`) in the future, but this seems like a good starting point.

FWIW, I expect that doing cleanup in where we make the decision
what should/shouldn't be shared is also a good idea. Way too
much code needed to be tweaked to check this. But I'm hoping
to leave that for a follow-up PR rather than blocking this on it.
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 25, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 25, 2024
@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum
Copy link
Member Author

Some sort of randomness with the begin_panic symbol, seemingly for the same builder sometimes showing begin_panic::<&str> and sometimes begin_panic. I'm pretty confused because AFAICT, they should have identical CI configurations. Maybe we're running into pre-existing instability/non-determinism with -Csymbol-mangling-version and -Zshare-generics?

@Mark-Simulacrum
Copy link
Member Author

OK, the issue is that if std is built with v0 mangling, then the test output differs. That only happens in PR CI today (due to

ENABLE_GCC_CODEGEN: "1"
), I started a Zulip thread about that difference.

For this PR I'll probably just normalize away the difference in the small number of affected tests.

@rust-log-analyzer

This comment has been minimized.

This reduces code sizes and better respects programmer intent when
marking inline(never). Previously such a marking was essentially ignored
for generic functions, as we'd still inline them in remote crates.
@Mark-Simulacrum
Copy link
Member Author

@bors r=saethlin

@bors
Copy link
Contributor

bors commented Nov 27, 2024

📌 Commit efc4eb3 has been approved by saethlin

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 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.