Skip to content

Commit 6608a17

Browse files
committed
Sort mono items by symbol name
1 parent 350d0ef commit 6608a17

File tree

1 file changed

+14
-3
lines changed
  • compiler/rustc_middle/src/mir

1 file changed

+14
-3
lines changed

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,21 @@ impl<'tcx> CodegenUnit<'tcx> {
566566

567567
let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect();
568568
if !tcx.sess.opts.unstable_opts.codegen_source_order {
569-
// It's already deterministic, so we can just use it.
570-
return items;
569+
// In this case, we do not need to keep the items in any specific order, as the input
570+
// is already deterministic.
571+
// However, it seems that actually moving related things (such as different
572+
// monomorphizations of the same function) close to one another is actually beneficial
573+
// for LLVM performance.
574+
// LLVM will codegen the items in the order we pass them to it, and when it handles
575+
// similar things in succession, it seems that it leads to better cache utilization,
576+
// less branch mispredictions and in general to better performance.
577+
// See https://github.com/rust-lang/rust/pull/145358 for more details.
578+
//
579+
// Sorting by symbol name should not incur any new non-determinism.
580+
items.sort_by_cached_key(|&(i, _)| i.symbol_name(tcx));
581+
} else {
582+
items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
571583
}
572-
items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
573584
items
574585
}
575586

0 commit comments

Comments
 (0)