Skip to content

Output deterministic AllocIds when emitting MIR - #161768

Closed
aerooneqq wants to merge 4 commits into
rust-lang:mainfrom
aerooneqq:deterministic-alloc-ids
Closed

Output deterministic AllocIds when emitting MIR#161768
aerooneqq wants to merge 4 commits into
rust-lang:mainfrom
aerooneqq:deterministic-alloc-ids

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #154278. Blocked by #161450.

r? @petrochenkov

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs 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 Aug 25, 2026
@rust-log-analyzer

This comment has been minimized.

@aerooneqq
aerooneqq force-pushed the deterministic-alloc-ids branch from e8fc29e to 058b2a1 Compare August 25, 2026 14:51
@aerooneqq
aerooneqq force-pushed the deterministic-alloc-ids branch from 058b2a1 to 6e7643a Compare August 25, 2026 15:03
writeln!(w, "// HINT: See also -Z dump-mir for MIR at specific points during compilation.")?;
writeln!(
w,
"// WARNING: Allocation ids were remapped for deterministic output, they may be differ from real ones."

@bjorn3 bjorn3 Aug 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How would you show the original alloc ids if you need them for debugging? Also this would lose the ability to tell alloc ids between different mir bodies apart, right?

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.

How would you show the original alloc ids if you need them for debugging?

Maybe with special --emit mode (like --emit=mir-original).

Also this would lose the ability to tell alloc ids between different mir bodies apart, right?

Yes, and I am now working on it by trying to overwrite tcx.alloc_map.to_alloc for consistent remapping across different bodies and for correct access to allocations through tcx.try_get_global_alloc (unfortunately AllocId can randomly appear in Debug implementations and maybe somewhere else; if it tries to access allocation through remapped allocation id this will lead to an error), however at this stage I don't like the resulting solution from many points of view, so I am not sure that this problem can be solved with this approach.

@petrochenkov petrochenkov 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 Aug 27, 2026
@aerooneqq

aerooneqq commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

After some experiments from technical side I don't see adequate solution for now, as during MIR dump we use MIR models directly without prior creation of DTO models specifically for printing/dumping, thus we need to adjust regular data structures in the compiler to remap allocation ids, more specifically tcx.alloc_map.to_alloc. We can add API to edit this map inplace, however it is not clear what is the lifetime of this remap and how long we should keep remapped allocation ids in this map. Adding second map which will track remappings and add two-map search for allocation may cause perf regression and it leaves the same question: how long should we keep remapping alive. One approach is to remove it on drop of MirWriter but this will cause remapping to be re-applied for every instance of MirWriter, and I am not even considering possible cases of parallel dumping/writing of MIR.

From logical point of view it seems like we do not need such a remapping, because non-deterministic MIR output is not that critical compared to deterministic binaries or metadata, next for debug purposes it is better to view original MIR that can be obtained from non-parallel compiler. Finally, in tests we have normalization which removes allocation ids, so remapping is not needed.

Other option is to make creation of allocation ids deterministic in its core, now it is just a number which gets atomically incremented from different threads, so for determinism we need to alter its structure, which likely to cause perf regressions and seems like not worth doing for the purpose of fixing #154278 (at least for now).

@aerooneqq aerooneqq closed this Aug 27, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 27, 2026
@petrochenkov

petrochenkov commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Finally, in tests we have normalization which removes allocation ids, so remapping is not needed.

This can potentially be done in the rustc instead of compiletest.
With -Zui-testing various numbers like diagnostic line numbers are already normalized to LL, we could do the same thing with allocation IDs in diagnostics and dumps, so they are "anonimized" if UI testing is enabled.
-Zui-testing=false would then act as the --emit=mir-original suggestion from #161768 (comment), except that it is the default behavior.

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 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: --emit=mir not reproducible (alloc ids)

5 participants