Skip to content

Fix parallel rustc not being reproducible due to unstable sorts of items - #144576

Closed
ywxt wants to merge 8 commits into
rust-lang:masterfrom
ywxt:parallel-reproducibility
Closed

Fix parallel rustc not being reproducible due to unstable sorts of items#144576
ywxt wants to merge 8 commits into
rust-lang:masterfrom
ywxt:parallel-reproducibility

Conversation

@ywxt

@ywxtywxt commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

Currently, A tuple (DefId, SymbolName) is used to determine the order of items in the final binary. However DefId is expected as non-deterministic, which leads to some not reproducible issues under parallel compilation. (See #140425 (comment))

Here, we use Span replacing DefId to make the order deterministic.

Items generated by a macro have a same span. But for codegen tests, items are expected the same order between in binary and source. Unluckily, SymbolName isn't predictive. So we use DefPath to distinguish items generated by a same macro.

At the cost of it, we have to manually sort items generated by macros for codegen tests.

This PR is purposed to fix#140425, but seemly works on #140413 too.

This behavior hasn't added into any test until we have a test suit for the parallel frontend. (See #143953)

Update #113349

r? @oli-obk
cc @matthiaskrgr@Zoxc@SparrowLii

@rustbot

Copy link
Copy Markdown
Collaborator

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@rustbotrustbot 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 Jul 28, 2025
@matthiaskrgr

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

rust-borsBot commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit 030a674 with merge 2e20b9f

To cancel the try build, run the command @bors try cancel.

rust-borsBot added a commit that referenced this pull request Jul 28, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 28, 2025
@bjorn3

bjorn3 commented Jul 28, 2025

Copy link
Copy Markdown
Member

I don't think this is a proper fix. Multiple items can have the exact same span. And ignoring all spans is valid too. Try ordering based on the DefPathHash instead of the DefId as @Zoxc was alluding to.

Edit: Never mind. That code is actually intended to keep a stable order based on the source location across rustc versions. Maybe only do the sorting based on span when doing codegen tests would be a good idea? I also think span order comparison could be non-deterministic with parallel rustc once we do parallel macro expansion as that can cause the source map order to be non-deterministic.

@ywxt

ywxt commented Jul 28, 2025

Copy link
Copy Markdown
ContributorAuthor

I don't think this is a proper fix. Multiple items can have the exact same span. And ignoring all spans is valid too. Try ordering based on the DefPathHash instead of the DefId as @Zoxc was alluding to.

Edit: Never mind. That code is actually intended to keep a stable order based on the source location across rustc versions. Maybe only do the sorting based on span when doing codegen tests would be a good idea? I also think span order comparison could be non-deterministic with parallel rustc once we do parallel macro expansion as that can cause the source map order to be non-deterministic.

You are right. I agree that using span only for codegen tests is a good idea, but we need a brief way to enable/disable it. An environment variable or flag?

@ywxt

ywxt commented Jul 28, 2025

Copy link
Copy Markdown
ContributorAuthor

Also A hash would make a completely different order that is unrelated to the source order. Maybe is it confusing?

@rust-bors

rust-borsBot commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2e20b9f (2e20b9f3cfa74d3a05a9d641c94db30bc451dfbb, parent: d242a8bd5a73f633ba1ec5aacf19acf35a3c747d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2e20b9f): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@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.

meanrangecount
Regressions ❌
(primary)
2.0%[0.2%, 61.0%]42
Regressions ❌
(secondary)
0.3%[0.2%, 1.2%]28
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
-0.7%[-0.9%, -0.2%]4
All ❌✅ (primary)2.0%[0.2%, 61.0%]42

Max RSS (memory usage)

Results (primary 4.7%, secondary 4.0%)

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

meanrangecount
Regressions ❌
(primary)
4.7%[3.3%, 6.1%]2
Regressions ❌
(secondary)
4.0%[2.3%, 6.7%]3
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)4.7%[3.3%, 6.1%]2

Cycles

Results (primary 20.0%, secondary 4.9%)

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

meanrangecount
Regressions ❌
(primary)
20.0%[2.5%, 60.1%]4
Regressions ❌
(secondary)
6.1%[2.5%, 8.7%]7
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
-3.1%[-3.1%, -3.1%]1
All ❌✅ (primary)20.0%[2.5%, 60.1%]4

Binary size

Results (secondary 0.0%)

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

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
0.0%[0.0%, 0.0%]3
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)--0

Bootstrap: 465.115s -> 468.017s (0.62%)
Artifact size: 376.87 MiB -> 376.82 MiB (-0.01%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 28, 2025
@SparrowLii

SparrowLii commented Jul 29, 2025

Copy link
Copy Markdown
Member

I guess that parallel macro expansion should not affect the order of source maps? We may confirm this with @petrochenkov .

In this pr's sorting, def_span is redundant in most cases, and we should consider supplementing def_span only if spans are same.

@cjgillot

Copy link
Copy Markdown
Contributor

Could we stop sorting altogether? We would rely on the order output by the collector, which should be deterministic. Probably far from source order, but deterministic.

@ywxt

ywxt commented Jul 29, 2025

Copy link
Copy Markdown
ContributorAuthor

Could we stop sorting altogether? We would rely on the order output by the collector, which should be deterministic. Probably far from source order, but deterministic.

The key is codegen tests need a predictive and deterministic order.

@cjgillot

Copy link
Copy Markdown
Contributor

Should we adapt the tests or restrict sorting to codegen tests?

@ywxt

ywxt commented Jul 29, 2025

Copy link
Copy Markdown
ContributorAuthor

I prefer to adapt the tests, but it may need more effort and process. A more ideal resolution is to avoid regressions in sorting.

@ywxt
ywxtforce-pushed the parallel-reproducibility branch from 030a674 to f7fba9aCompareJuly 29, 2025 12:42
@SparrowLii

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

rust-borsBot commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit f7fba9a with merge e972d15

To cancel the try build, run the command @bors try cancel.

rust-borsBot added a commit that referenced this pull request Jul 29, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
@rust-bors

rust-borsBot commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: e972d15 (e972d15b963bba39b5608d11ff9eb2851d7af217, parent: 552904134b564a74489db50aebe7070fdcce895c)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (e972d15): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@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.

meanrangecount
Regressions ❌
(primary)
2.3%[0.2%, 60.7%]35
Regressions ❌
(secondary)
0.3%[0.1%, 1.2%]30
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)2.3%[0.2%, 60.7%]35

Max RSS (memory usage)

Results (primary 4.4%, secondary 2.5%)

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

meanrangecount
Regressions ❌
(primary)
4.4%[2.9%, 5.8%]2
Regressions ❌
(secondary)
2.5%[2.4%, 2.6%]2
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)4.4%[2.9%, 5.8%]2

Cycles

Results (primary 24.9%, secondary 0.5%)

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

meanrangecount
Regressions ❌
(primary)
24.9%[3.1%, 58.1%]3
Regressions ❌
(secondary)
3.7%[3.4%, 4.0%]2
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
-2.8%[-3.1%, -2.4%]2
All ❌✅ (primary)24.9%[3.1%, 58.1%]3

Binary size

Results (secondary 0.0%)

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

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
0.0%[0.0%, 0.0%]3
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)--0

Bootstrap: 468.885s -> 468.757s (-0.03%)
Artifact size: 376.79 MiB -> 376.86 MiB (0.02%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
@SparrowLii

SparrowLii commented Jul 30, 2025

Copy link
Copy Markdown
Member

Could we stop sorting altogether? We would rely on the order output by the collector, which should be deterministic. Probably far from source order, but deterministic.

We collect items in parallel (here), so the order is not deterministic.

However, during the collecting, we have already obtained Spans for all items, which we can utilize to avoid regression caused by getting them again when sorting.

@ywxt

ywxt commented Jul 30, 2025

Copy link
Copy Markdown
ContributorAuthor

I'm aware of the simpler way to add a flag to enable sorting(by Span and DefPath) only within codegen tests. Also, the items collected are deterministic already and don't need be sorted again.

@ywxt

ywxt commented Jul 31, 2025

Copy link
Copy Markdown
ContributorAuthor

Hey, all. I have open a new PR to solve it. See #144722 please.

We add a new internal option to only enable sorting for codegen tests, which doesn't influence the normal path.

@ywxtywxt closed this Jul 31, 2025
@rustbotrustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 31, 2025
@ywxt
ywxt deleted the parallel-reproducibility branch August 4, 2025 06:52
bors added a commit that referenced this pull request Aug 8, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
Currently, A tuple `(DefId, SymbolName)` is used to determine the order of items in the final binary. However `DefId` is expected as non-deterministic, which leads to some not reproducible issues under parallel compilation. (See #140425 (comment))
Theoretically, we don't need the sorting because the order of these items is already deterministic.
However, codegen tests reply on the same order of items between in binary and source.
So here we added a new option `codegen-source-order` to indicate whether sorting based on the order in source. For codegen tests, items are sorted according to the order in the source code, whereas in the normal path, no sorting is performed.
Specially, for codegen tests, in preparation for parallel compilation potentially being enabled by default in the future, we use `Span` replacing `DefId` to make the order deterministic.
This PR is purposed to fix#140425, but seemly works on #140413 too.
This behavior hasn't added into any test until we have a test suit for the parallel frontend. (See #143953)
Related discussion: [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/187679-t-compiler.2Fparallel-rustc/topic/Async.20closures.20not.20reproducible.28.23140425.29) #144576
Update #113349
r? `@oli-obk`
cc `@lqd` `@cramertj` `@matthiaskrgr` `@Zoxc` `@SparrowLii` `@bjorn3` `@cjgillot` `@joshtriplett`
bors added a commit that referenced this pull request Aug 9, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
Currently, A tuple `(DefId, SymbolName)` is used to determine the order of items in the final binary. However `DefId` is expected as non-deterministic, which leads to some not reproducible issues under parallel compilation. (See #140425 (comment))
Theoretically, we don't need the sorting because the order of these items is already deterministic.
However, codegen tests reply on the same order of items between in binary and source.
So here we added a new option `codegen-source-order` to indicate whether sorting based on the order in source. For codegen tests, items are sorted according to the order in the source code, whereas in the normal path, no sorting is performed.
Specially, for codegen tests, in preparation for parallel compilation potentially being enabled by default in the future, we use `Span` replacing `DefId` to make the order deterministic.
This PR is purposed to fix#140425, but seemly works on #140413 too.
This behavior hasn't added into any test until we have a test suit for the parallel frontend. (See #143953)
Related discussion: [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/187679-t-compiler.2Fparallel-rustc/topic/Async.20closures.20not.20reproducible.28.23140425.29) #144576
Update #113349
r? `@oli-obk`
cc `@lqd` `@cramertj` `@matthiaskrgr` `@Zoxc` `@SparrowLii` `@bjorn3` `@cjgillot` `@joshtriplett`
bors added a commit that referenced this pull request Aug 12, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
Currently, A tuple `(DefId, SymbolName)` is used to determine the order of items in the final binary. However `DefId` is expected as non-deterministic, which leads to some not reproducible issues under parallel compilation. (See #140425 (comment))
Theoretically, we don't need the sorting because the order of these items is already deterministic.
However, codegen tests reply on the same order of items between in binary and source.
So here we added a new option `codegen-source-order` to indicate whether sorting based on the order in source. For codegen tests, items are sorted according to the order in the source code, whereas in the normal path, no sorting is performed.
Specially, for codegen tests, in preparation for parallel compilation potentially being enabled by default in the future, we use `Span` replacing `DefId` to make the order deterministic.
This PR is purposed to fix#140425, but seemly works on #140413 too.
This behavior hasn't added into any test until we have a test suit for the parallel frontend. (See #143953)
Related discussion: [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/187679-t-compiler.2Fparallel-rustc/topic/Async.20closures.20not.20reproducible.28.23140425.29) #144576
Update #113349
r? `@oli-obk`
cc `@lqd` `@cramertj` `@matthiaskrgr` `@Zoxc` `@SparrowLii` `@bjorn3` `@cjgillot` `@joshtriplett`
bors added a commit that referenced this pull request Aug 13, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
Currently, A tuple `(DefId, SymbolName)` is used to determine the order of items in the final binary. However `DefId` is expected as non-deterministic, which leads to some not reproducible issues under parallel compilation. (See #140425 (comment))
Theoretically, we don't need the sorting because the order of these items is already deterministic.
However, codegen tests reply on the same order of items between in binary and source.
So here we added a new option `codegen-source-order` to indicate whether sorting based on the order in source. For codegen tests, items are sorted according to the order in the source code, whereas in the normal path, no sorting is performed.
Specially, for codegen tests, in preparation for parallel compilation potentially being enabled by default in the future, we use `Span` replacing `DefId` to make the order deterministic.
This PR is purposed to fix#140425, but seemly works on #140413 too.
This behavior hasn't added into any test until we have a test suit for the parallel frontend. (See #143953)
Related discussion: [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/187679-t-compiler.2Fparallel-rustc/topic/Async.20closures.20not.20reproducible.28.23140425.29) #144576
Update #113349
r? `@oli-obk`
cc `@lqd` `@cramertj` `@matthiaskrgr` `@Zoxc` `@SparrowLii` `@bjorn3` `@cjgillot` `@joshtriplett`
github-actionsBot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Aug 18, 2025
Fix parallel rustc not being reproducible due to unstable sorts of items
Currently, A tuple `(DefId, SymbolName)` is used to determine the order of items in the final binary. However `DefId` is expected as non-deterministic, which leads to some not reproducible issues under parallel compilation. (See rust-lang/rust#140425 (comment))
Theoretically, we don't need the sorting because the order of these items is already deterministic.
However, codegen tests reply on the same order of items between in binary and source.
So here we added a new option `codegen-source-order` to indicate whether sorting based on the order in source. For codegen tests, items are sorted according to the order in the source code, whereas in the normal path, no sorting is performed.
Specially, for codegen tests, in preparation for parallel compilation potentially being enabled by default in the future, we use `Span` replacing `DefId` to make the order deterministic.
This PR is purposed to fixrust-lang/rust#140425, but seemly works on rust-lang/rust#140413 too.
This behavior hasn't added into any test until we have a test suit for the parallel frontend. (See rust-lang/rust#143953)
Related discussion: [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/187679-t-compiler.2Fparallel-rustc/topic/Async.20closures.20not.20reproducible.28.23140425.29) rust-lang/rust#144576
Update rust-lang/rust#113349
r? `@oli-obk`
cc `@lqd` `@cramertj` `@matthiaskrgr` `@Zoxc` `@SparrowLii` `@bjorn3` `@cjgillot` `@joshtriplett`
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regressionPerformance regression.T-compilerRelevant 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.

parallel rustc: async closures not reproducible

8 participants

@ywxt@rustbot@matthiaskrgr@rust-timer@bjorn3@SparrowLii@cjgillot@oli-obk