Skip to content

Remap local def ids for deterministic metadata encoding - #162809

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
aerooneqq:def-index-remapping
Oct 17, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
aerooneqq:def-index-remapping

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Remaps local def ids that are allocated after Definitions::commit_end_of_determinism was called.
Fixes #162202, fixes #162203.

cc #t-compiler/const-eval > DefId reproducibility with the parallel frontend

cc #t-compiler/gpgpu-backend > Deterministic encoding of offload manifest

r? @petrochenkov

@rustbot rustbot added 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 Sep 15, 2026
@aerooneqq aerooneqq changed the title Remap def indices for deterministic metadata encoing Remap def indices for deterministic metadata encoding Sep 15, 2026
@susitsm

susitsm commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Are the changes in decoder.rs and def_path_hash_map.rs needed? It seems unrelated to me.

@aerooneqq

Copy link
Copy Markdown
Contributor Author

Are the changes in decoder.rs and def_path_hash_map.rs needed? It seems unrelated to me.

We need to update map from hashes to def indices while encoding DefPathHashMap, now I inserted simple FxHashMap to test the approach, if I don't remove functions from decoder.rs there will be warnings, because they are not used anywhere else.

@petrochenkov

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

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
Remap def indices for deterministic metadata encoding
@rust-bors

rust-bors Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: ceb9839 (ceb98398abf30a57ca7f186c152d2adda7b079f9)
Base parent: 28e8a8c (28e8a8c81bf3b37909edac6c2a76e56f30cd492f)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (ceb9839): 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)
4.4% [0.1%, 164.6%] 276
Regressions ❌
(secondary)
11.8% [0.2%, 130.1%] 350
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.4% [0.1%, 164.6%] 276

Max RSS (memory usage)

Results (primary 3.0%, secondary 4.5%)

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

mean range count
Regressions ❌
(primary)
3.0% [0.8%, 9.4%] 203
Regressions ❌
(secondary)
4.6% [0.9%, 14.8%] 327
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
-3.6% [-4.0%, -2.9%] 3
All ❌✅ (primary) 3.0% [-2.3%, 9.4%] 204

Cycles

Results (primary 10.2%, secondary 14.6%)

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

mean range count
Regressions ❌
(primary)
10.2% [1.7%, 73.0%] 55
Regressions ❌
(secondary)
14.6% [1.4%, 66.2%] 179
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 10.2% [1.7%, 73.0%] 55

Binary size

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

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.4% [-0.4%, -0.3%] 8
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 4
All ❌✅ (primary) -0.4% [-0.4%, -0.3%] 8

Bootstrap: 496.137s -> 496.27s (0.03%)
Artifact size: 406.79 MiB -> 406.77 MiB (-0.01%)

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

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

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
Remap def indices for deterministic metadata encoding

#[inline]
fn map_index(&self, index: DefIndex) -> DefIndex {
self.def_indexes_remapping.get(&index).copied().unwrap_or(index)

@susitsm susitsm Sep 16, 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.

This could be donewith a vec for better performance. Since we know that these indices are dense in a range. Either a full IndexVec mapping every LocalDefId, or a shorter Vec, that only contains the remapped ones, indexed by remapped_index - min_remapped_index

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is very strong assumption that def ids that are needed to be remapped will be in the end, now it should be true, because resolution and AST -> HIR lowering is executed without any parallelism, however AST -> HIR lowering became more incremental recently by introducing lower_to_hir query which is very easy to parallelize in the beginning of the run_required_analysis function (now it is executed sequentially as we prefetch hir_crate_items which is a sequential HIR visit). If it is parallelized then from each lower_to_hir execution we may invoke arbitrary code that can allocate def ids that do not need to be remapped.

But I like the idea, I was thinking about something similar in a bit different direction: create separate IndexVecs in Definitions struct for different stages of compilation, thus we can maybe pre-allocate and remap ids only for selected parts.

I think that it is reasonable to track the last def id that is allocated during resolution stage, because it is not likely to get parallelized in the nearest time (I hope so) and then to remap all def ids that are allocated after it, I will try it tomorrow.

let mut to_remap = vec![];
let mut def_ids = vec![];
for idx in 0..defs.num_definitions() {
let def_id = LocalDefId { local_def_index: idx.into() };

@susitsm susitsm Sep 16, 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.

I think this could iterate backwards, stopping when it hits an index not needing remapping. Remapped indices should be at the end

View changes since the review

@rust-bors

rust-bors Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 8bcd37a (8bcd37a7bc50acb44dc7181f601e93127ebf19af)
Base parent: 28e8a8c (28e8a8c81bf3b37909edac6c2a76e56f30cd492f)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (8bcd37a): comparison URL.

Overall result: ❌ regressions - BENCHMARK(S) FAILED

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

❗ ❗ ❗ ❗ ❗
Warning ⚠️: The following benchmark(s) failed to build:

  • Job failure
  • cranelift-codegen-0.119.0

❗ ❗ ❗ ❗ ❗

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.3% [0.1%, 0.6%] 60
Regressions ❌
(secondary)
0.2% [0.1%, 0.3%] 13
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [0.1%, 0.6%] 60

Max RSS (memory usage)

Results (secondary -4.1%)

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
Improvements ✅
(secondary)
-4.1% [-5.1%, -3.3%] 3
All ❌✅ (primary) - - 0

Cycles

Results (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
Regressions ❌
(secondary)
4.6% [4.6%, 4.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.7%, -2.3%] 2
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 496.137s -> 495.635s (-0.10%)
Artifact size: 406.79 MiB -> 406.74 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 16, 2026
Comment thread compiler/rustc_hir_id/src/definitions.rs Outdated
Comment thread compiler/rustc_hir_id/src/definitions.rs
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 13, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs
@rust-log-analyzer

This comment has been minimized.

@aerooneqq
aerooneqq force-pushed the def-index-remapping branch from 2e0d0ff to e1a82ef Compare October 13, 2026 14:40
@rust-log-analyzer

This comment has been minimized.

@aerooneqq
aerooneqq force-pushed the def-index-remapping branch from e1a82ef to 79353ac Compare October 13, 2026 16:31
@rust-log-analyzer

This comment has been minimized.

@aerooneqq
aerooneqq force-pushed the def-index-remapping branch from 79353ac to 5bc2553 Compare October 14, 2026 10:05
@aerooneqq

Copy link
Copy Markdown
Contributor Author

The problem was that we have impl FixedSizeEncoding for Option<RawDefId> which does not respect remapping. It worked earlier because I remapped RawDefId explicitly and when I changed it to impl Encodable<EncodeContext<'_, '_>> for RawDefId and removed manual remappings it stopped working in cases where we use record_some! macro, as set_some in Table uses FixedSizeEncoding::write_to_bytes to encode values. Need to take care of it too.

While I was debugging this yesterday I also learned about existence of offload manifest which writes ty::Instance, seems like we need a remapping here too, as those instances can be decoded and then used during collect_crate_mono_items.

@aerooneqq
aerooneqq force-pushed the def-index-remapping branch 2 times, most recently from 857fb2e to ab3da1b Compare October 14, 2026 14:54
@aerooneqq

aerooneqq commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

@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 Oct 15, 2026
@petrochenkov petrochenkov removed their assignment Oct 15, 2026
@oli-obk

oli-obk commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

@bors squash msg="Remap local def ids for deterministic metadata encoding"

@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

🔨 14 commits were squashed into d8980d3.

@rust-bors
rust-bors Bot force-pushed the def-index-remapping branch from 8c692ca to d8980d3 Compare October 17, 2026 13:59
@oli-obk

oli-obk commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

📌 Commit d8980d3 has been tentatively approved by oli-obk

It will be put into the queue for this repository once PR CI succeeds.

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors rollup=iffy

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

parallel compiler: static mut refs in lib not reproducible parallel compiler: async fns not reproducible

8 participants