Skip to content

fix: codegen symbol ordering with v0 mangling - #166449

Open
paradoxicalguy wants to merge 1 commit into
rust-lang:mainfrom
paradoxicalguy:fix-v0-symbol-order-152910
Open

paradoxicalguy wants to merge 1 commit into
rust-lang:mainfrom
paradoxicalguy:fix-v0-symbol-order-152910

Conversation

@paradoxicalguy

@paradoxicalguy paradoxicalguy commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

View all comments

fixes #152910

sort codegen items using a dedicated v0 mangling key that omits crate disambiguators, reducing the dependence of symbol ordering on -Cmetadata without changing actual linkage symbols.

adds a run-make regression test covering distinct function paths and generic arguments, verified with A/B testing against upstream.

based on the original work in #162003 by @panstromek

@rustbot

rustbot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs 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 Nov 24, 2026
@paradoxicalguy

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@paradoxicalguy: 🔑 Insufficient privileges: not in try users

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

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…<try>

fix: codegen symbol ordering with v0 mangling
@panstromek

panstromek commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

TBH, I tried this approach only to test the idea, but I don't really like it, and it was slower a bunch. I think a "proper" fix would be to sort by some precursor thing from which we compute the symbol name, before we insert the disambiguator (probably something like path?).

Also, demangling also doesn't address the issue I was trying to address - that disambiguators (hashes) and up influencing performance of code that uses symbols and transitively PGO stats (possibly). This code still has that property because we process disambiguators in mangling and demangling.

@paradoxicalguy

Copy link
Copy Markdown
Member Author

yes im tinkering w more stuff locally rn, currently trying to see if Instance printer works :)

@rust-log-analyzer

This comment has been minimized.

@petrochenkov

Copy link
Copy Markdown
Contributor

r? @panstromek

@rustbot rustbot assigned panstromek and unassigned petrochenkov Nov 24, 2026
@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a54b870 (a54b870ff7ee08a20a38c24cd76f0b03e8a62421)
Base parent: 058c701 (058c701055c4b6f60f622d616afc0349911c38c3)

@rust-timer

This comment has been minimized.

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the fix-v0-symbol-order-152910 branch from b71d09c to bff15a2 Compare November 24, 2026 12:41
@paradoxicalguy

Copy link
Copy Markdown
Member Author

replaced the demangling with a dedicated v0 mangling mode that omits crate disambiguators for the sort key. no new dependency, rustc-demangle is dropped from rustc_middle.

extended the run-make test to cover distinct generic arguments (fails on upstream main, passes here). full local ui, codegen-llvm and codegen-units pass on x86_64, including the two intrinsic tests that failed on aarch64 before.

known gaps: dyn types with multiple projection bounds can still vary with -Cmetadata (i have a repro), and every mono item is now mangled twice. i haven't measured that cost.
will have to run another perf run

@panstromek

Copy link
Copy Markdown
Contributor

Ok, this looks more like what I envisioned. I worry we're going to get a perf hit, though, let's try where we stand.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…<try>

fix: codegen symbol ordering with v0 mangling
@panstromek

Copy link
Copy Markdown
Contributor

oh, I didn't realize the previous one didn't finish, yet.

@bjorn3

bjorn3 commented Nov 24, 2026

Copy link
Copy Markdown
Member

I think you will still need to include the disambiguators in the sort key to have a consistent order when two crates with the same name but different -Cmetadata contain the same item. You can put the disambiguators at the end in that case though to only sort on them when the rest of the symbol is identical.

@paradoxicalguy

Copy link
Copy Markdown
Member Author

yes, the sort tuple is (sort key, full mangled symbol), so the disambiguators only matter as a tiebreaker when the primary keys are identical. that case (same-named crates with the same item) still depends on -Cmetadata, which i don't think we can avoid without some other way to distinguish the crates

@panstromek

Copy link
Copy Markdown
Contributor

Yes

@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 24, 2026
@panstromek

Copy link
Copy Markdown
Contributor

oh, the update try is not available for rust-timer since you pushed again, so we'll have to rerun it as well

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…<try>

fix: codegen symbol ordering with v0 mangling
@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a0f7d9d (a0f7d9de6f5c0ef0aadb44cde9e842315390ac50)
Base parent: 4cedc6b (4cedc6bd576618ca90bc479c67e4ba16892a6188)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (a0f7d9d): 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.2%, 1.7%] 71
Regressions ❌
(secondary)
0.8% [0.2%, 1.8%] 43
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 2
All ❌✅ (primary) 0.6% [0.2%, 1.7%] 71

Max RSS (memory usage)

Results (primary 1.2%, secondary -0.6%)

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

mean range count
Regressions ❌
(primary)
1.2% [1.1%, 1.3%] 2
Regressions ❌
(secondary)
5.6% [5.1%, 5.8%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-5.5%, -1.4%] 8
All ❌✅ (primary) 1.2% [1.1%, 1.3%] 2

Cycles

Results (secondary 1.8%)

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)
2.9% [2.0%, 4.2%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.8% [-2.8%, -2.8%] 1
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 461.503s -> 469.097s (1.65%)
Artifact size: 406.39 MiB -> 406.51 MiB (0.03%)

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

Copy link
Copy Markdown
Member Author

looked at the self-profiles. in cargo debug/full, symbol_sort_key takes 1.02% of total cpu time with 83,624 executions and only 112 cache hits. there are also ~2.4m additional query cache hits overall.
next-solver-stress regresses even in check builds without executing the new query, so there may be indirect effects too.

i'll look into avoiding the redundant mangling/query work.

@panstromek

Copy link
Copy Markdown
Contributor

The next-solver-stress is noise. This is a new one, see https://rust-lang.zulipchat.com/#narrow/channel/247081-t-compiler.2Fperformance/topic/New.20artifact.20noise.20in.20.60next-solver-stress.60/with/642541041

btw. if there's a weird result like that, it's good to run the provided cachegrind command and just double check that the diff is where you expect it to be.

@paradoxicalguy

Copy link
Copy Markdown
Member Author

=O good to know! i'll keep that in mind and use cachegrind for remaining regressions

@paradoxicalguy

Copy link
Copy Markdown
Member Author

ran cachegrind on cargo debug/full and reproduced the regression locally (+1.49%). V0SymbolMangler::print_def_path alone accounts for ~52% of the net instruction increase (1.84b -> 2.78b).
looks like avoiding query caching alone won't be sufficient; the redundant mangling traversal is the main thing to address.

💤 for now 🛌

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the fix-v0-symbol-order-152910 branch from 85a10ea to 451e542 Compare November 25, 2026 14:59
@paradoxicalguy

paradoxicalguy commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

6 cachegrinds later, regression is now +0.23% on Cargo (Debug/Full). All 1,268 selected tests pass, and linkage symbols remain unchanged in the tested workload.
need a perf run now cc @panstromek

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

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 25, 2026
…<try>

fix: codegen symbol ordering with v0 mangling
@rust-bors

rust-bors Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 20458e2 (20458e22a806e22446c1e5acd2467183820319d4)
Base parent: bc9132d (bc9132daa5c0be572ebb81304f6039d947298e67)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (20458e2): comparison URL.

Overall result: ❌ regressions - 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.4% [0.2%, 1.5%] 25
Regressions ❌
(secondary)
0.5% [0.2%, 1.5%] 19
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.2%, 1.5%] 25

Max RSS (memory usage)

Results (primary 1.9%, secondary -2.2%)

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

mean range count
Regressions ❌
(primary)
1.9% [0.8%, 3.2%] 9
Regressions ❌
(secondary)
4.1% [1.8%, 5.3%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.1% [-7.2%, -1.3%] 10
All ❌✅ (primary) 1.9% [0.8%, 3.2%] 9

Cycles

Results (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)
- - 0
Regressions ❌
(secondary)
3.4% [3.3%, 3.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.4% [-2.4%, -2.4%] 2
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 462.454s -> 463.061s (0.13%)
Artifact size: 405.60 MiB -> 405.67 MiB (0.02%)

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

Copy link
Copy Markdown
Member Author

incr comp reaching heights unlike me 🙏

// correct symbol name of instances from upstream crates.
fn symbol_name_provider<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::SymbolName<'tcx> {
let symbol_name = compute_symbol_name(tcx, instance, || {
tcx.symbol_name_and_sort_key(instance).0

@cezarbbb cezarbbb Nov 26, 2026 •

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.

Does symbol_name need to build the sort key at all? afaik it's queried from places that never sort like symbol export, coverage, or diagnostics, why all of them now need to pay for the extra string?

View changes since the review

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.

This might have introduced some part of unnecessary regressions I guess.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

in a local prototype symbol_name is now just an accessor on the combined query (no wrapper query), which cuts the regex enum-reorder regression from +1.67% to ~+0.13%. but linkage-only callers still pay for the sort key. i'm looking at making that lazy without a second full mangle

@paradoxicalguy

Copy link
Copy Markdown
Member Author

i've managed to reduce the local regex enum-reorder regression from +1.67% to +0.135% by consolidating both queries into a single disk-cached query. cargo incremental still shows ~0.4–0.5% overhead.
i tried different caching strategies, independent queries, optional keys, mangler specialization, arena allocation and buffer optimizations, but none provided substantial further improvements.
the remaining overhead seems to come partly from hashing and serialization. the main blocker is making sorting-key generation lazy without duplicating mangling work or complicating incremental query dependencies.

correctness tests and linkage comparison passed locally. official perf is still pending.
i'd appreciate any suggestions on how to approach this, particularly the query architecture, or whether the remaining overhead is an acceptable trade-off.

@cezarbbb

Copy link
Copy Markdown
Contributor

Could the query only return the linkage plus the ranges where the disambiguators were written, instead of a second buffer? In your pr I found the key only differs in the stable_crate_id hashes and the instantiating crate suffix, so building it lazily from those spans keeps mangling just a single write, doesn't it?

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-run-make Area: port run-make Makefiles to rmake.rs 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.

Symbol order in executable unstable with v0 mangling when -Cmetadata changes

8 participants