diff options
| author | bors <bors@rust-lang.org> | 2025-08-22 07:13:07 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-08-22 07:13:07 +0000 |
| commit | 831e291d3be4cacc4fe178424c4d6baa264d8b5a (patch) | |
| tree | c39b2a0f442e66e16a753726bef01969ab8ee690 /compiler | |
| parent | d127901d940d96209fd2ae8ff6769ad2788099fb (diff) | |
| parent | 6f1c998ca9ec7e7fc7002c2de9cf1c08d9e39f05 (diff) | |
| download | rust-831e291d3be4cacc4fe178424c4d6baa264d8b5a.tar.gz rust-831e291d3be4cacc4fe178424c4d6baa264d8b5a.zip | |
Auto merge of #145358 - Kobzol:symbol-name-sort, r=nnethercote
Sort mono items by symbol name Trying to claw back cycles/branch/cache miss losses from https://github.com/rust-lang/rust/pull/144722.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_middle/src/mir/mono.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index 0e6f797b1e4..533066bdef9 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -548,10 +548,27 @@ impl<'tcx> CodegenUnit<'tcx> { let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect(); if !tcx.sess.opts.unstable_opts.codegen_source_order { - // It's already deterministic, so we can just use it. - return items; + // In this case, we do not need to keep the items in any specific order, as the input + // is already deterministic. + // + // However, it seems that moving related things (such as different + // monomorphizations of the same function) close to one another is actually beneficial + // for LLVM performance. + // LLVM will codegen the items in the order we pass them to it, and when it handles + // similar things in succession, it seems that it leads to better cache utilization, + // less branch mispredictions and in general to better performance. + // For example, if we have functions `a`, `c::<u32>`, `b`, `c::<i16>`, `d` and + // `c::<bool>`, it seems that it helps LLVM's performance to codegen the three `c` + // instantiations right after one another, as they will likely reference similar types, + // call similar functions, etc. + // + // See https://github.com/rust-lang/rust/pull/145358 for more details. + // + // Sorting by symbol name should not incur any new non-determinism. + items.sort_by_cached_key(|&(i, _)| i.symbol_name(tcx)); + } else { + items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i)); } - items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i)); items } |
