Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 70 additions & 37 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;

use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
use rustc_attr_data_structures::InlineAttr;
use rustc_data_structures::base_n::{BaseNString, CASE_INSENSITIVE, ToBaseN};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::unord::UnordMap;
use rustc_hashes::Hash128;
use rustc_hir::ItemId;
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE};
use rustc_index::Idx;
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
use rustc_query_system::ich::StableHashingContext;
use rustc_session::config::OptLevel;
Expand Down Expand Up @@ -526,45 +526,78 @@ impl<'tcx> CodegenUnit<'tcx> {
tcx: TyCtxt<'tcx>,
) -> Vec<(MonoItem<'tcx>, MonoItemData)> {
// The codegen tests rely on items being process in the same order as
// they appear in the file, so for local items, we sort by node_id first
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>);

fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
ItemSortKey(
match item {
MonoItem::Fn(ref instance) => {
match instance.def {
// We only want to take HirIds of user-defined
// instances into account. The others don't matter for
// the codegen tests and can even make item order
// unstable.
InstanceKind::Item(def) => def.as_local().map(Idx::index),
InstanceKind::VTableShim(..)
| InstanceKind::ReifyShim(..)
| InstanceKind::Intrinsic(..)
| InstanceKind::FnPtrShim(..)
| InstanceKind::Virtual(..)
| InstanceKind::ClosureOnceShim { .. }
| InstanceKind::ConstructCoroutineInClosureShim { .. }
| InstanceKind::DropGlue(..)
| InstanceKind::CloneShim(..)
| InstanceKind::ThreadLocalShim(..)
| InstanceKind::FnPtrAddrShim(..)
| InstanceKind::AsyncDropGlue(..)
| InstanceKind::FutureDropPollShim(..)
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
}
// they appear in the file, so for local items, we sort by span and def_path first
struct ItemSortKey<'tcx>(Option<Span>, Option<String>, Option<SymbolName<'tcx>>);

// Avoids def_path querying for items that have different spans
fn item_sort<'tcx>(
tcx: TyCtxt<'tcx>,
cached_keys_map: &'_ mut FxHashMap<MonoItem<'tcx>, ItemSortKey<'tcx>>,
item1: MonoItem<'tcx>,
item2: MonoItem<'tcx>,
) -> Ordering {
let is_local1 = item1.def_id().is_local();
let is_local2 = item2.def_id().is_local();

match (is_local1, is_local2) {
(false, false) => {
cached_keys_map
.entry(item1)
.or_insert_with(|| ItemSortKey(None, None, Some(item1.symbol_name(tcx))));
cached_keys_map
.entry(item2)
.or_insert_with(|| ItemSortKey(None, None, Some(item2.symbol_name(tcx))));
let ItemSortKey(_, _, name1) = &cached_keys_map[&item1];
let ItemSortKey(_, _, name2) = &cached_keys_map[&item2];
name1.cmp(name2)
}
(false, true) => Ordering::Less,
(true, false) => Ordering::Greater,
(true, true) => {
cached_keys_map
.entry(item1)
.or_insert_with(|| ItemSortKey(item1.local_span(tcx), None, None));
cached_keys_map
.entry(item2)
.or_insert_with(|| ItemSortKey(item2.local_span(tcx), None, None));
let ItemSortKey(span1, _, _) = &cached_keys_map[&item1];
let ItemSortKey(span2, _, _) = &cached_keys_map[&item2];
let ord = span1.cmp(span2);
if ord != Ordering::Equal {
return ord;
}
MonoItem::Static(def_id) => def_id.as_local().map(Idx::index),
MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.index()),
},
item.symbol_name(tcx),
)

cached_keys_map.entry(item1).and_modify(
|ItemSortKey(_, def_path, symbol_name)| {
def_path.get_or_insert_with(|| {
tcx.def_path(item1.def_id()).to_string_no_crate_verbose()
});
symbol_name.get_or_insert_with(|| item1.symbol_name(tcx));
},
);
cached_keys_map.entry(item2).and_modify(
|ItemSortKey(_, def_path, symbol_name)| {
def_path.get_or_insert_with(|| {
tcx.def_path(item2.def_id()).to_string_no_crate_verbose()
});
symbol_name.get_or_insert_with(|| item2.symbol_name(tcx));
},
);
let ItemSortKey(_, def_path1, name1) = &cached_keys_map[&item1];
let ItemSortKey(_, def_path2, name2) = &cached_keys_map[&item2];
match def_path1.cmp(def_path2) {
Ordering::Equal => name1.cmp(name2),
other => other,
}
}
}
}

let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect();
items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
let mut cached_keys_map =
FxHashMap::with_capacity_and_hasher(items.len(), Default::default());
items
.sort_by(|&(item1, _), &(item2, _)| item_sort(tcx, &mut cached_keys_map, item1, item2));
items
}

Expand Down
56 changes: 28 additions & 28 deletions tests/assembly-llvm/asm/aarch64-modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,17 @@ check!(vreg vreg "add {0}.4s, {0}.4s, {0}.4s");
// CHECK: //NO_APP
check!(vreg_b vreg "ldr {:b}, [x0]");

// CHECK-LABEL: vreg_h:
// CHECK: //APP
// CHECK: ldr h0, [x0]
// CHECK: //NO_APP
check!(vreg_h vreg "ldr {:h}, [x0]");

// CHECK-LABEL: vreg_s:
// CHECK: //APP
// CHECK: ldr s0, [x0]
// CHECK: //NO_APP
check!(vreg_s vreg "ldr {:s}, [x0]");

// CHECK-LABEL: vreg_d:
// CHECK: //APP
// CHECK: ldr d0, [x0]
// CHECK: //NO_APP
check!(vreg_d vreg "ldr {:d}, [x0]");

// CHECK-LABEL: vreg_q:
// CHECK: //APP
// CHECK: ldr q0, [x0]
// CHECK: //NO_APP
check!(vreg_q vreg "ldr {:q}, [x0]");

// CHECK-LABEL: vreg_v:
// CHECK-LABEL: vreg_h:
// CHECK: //APP
// CHECK: add v0.4s, v0.4s, v0.4s
// CHECK: ldr h0, [x0]
// CHECK: //NO_APP
check!(vreg_v vreg "add {0:v}.4s, {0:v}.4s, {0:v}.4s");
check!(vreg_h vreg "ldr {:h}, [x0]");

// CHECK-LABEL: vreg_low16:
// CHECK: //APP
Expand All @@ -97,32 +79,50 @@ check!(vreg_low16 vreg_low16 "add {0}.4s, {0}.4s, {0}.4s");
// CHECK: //NO_APP
check!(vreg_low16_b vreg_low16 "ldr {:b}, [x0]");

// CHECK-LABEL: vreg_low16_d:
// CHECK: //APP
// CHECK: ldr d0, [x0]
// CHECK: //NO_APP
check!(vreg_low16_d vreg_low16 "ldr {:d}, [x0]");

// CHECK-LABEL: vreg_low16_h:
// CHECK: //APP
// CHECK: ldr h0, [x0]
// CHECK: //NO_APP
check!(vreg_low16_h vreg_low16 "ldr {:h}, [x0]");

// CHECK-LABEL: vreg_low16_q:
// CHECK: //APP
// CHECK: ldr q0, [x0]
// CHECK: //NO_APP
check!(vreg_low16_q vreg_low16 "ldr {:q}, [x0]");

// CHECK-LABEL: vreg_low16_s:
// CHECK: //APP
// CHECK: ldr s0, [x0]
// CHECK: //NO_APP
check!(vreg_low16_s vreg_low16 "ldr {:s}, [x0]");

// CHECK-LABEL: vreg_low16_d:
// CHECK-LABEL: vreg_low16_v:
// CHECK: //APP
// CHECK: ldr d0, [x0]
// CHECK: add v0.4s, v0.4s, v0.4s
// CHECK: //NO_APP
check!(vreg_low16_d vreg_low16 "ldr {:d}, [x0]");
check!(vreg_low16_v vreg_low16 "add {0:v}.4s, {0:v}.4s, {0:v}.4s");

// CHECK-LABEL: vreg_low16_q:
// CHECK-LABEL: vreg_q:
// CHECK: //APP
// CHECK: ldr q0, [x0]
// CHECK: //NO_APP
check!(vreg_low16_q vreg_low16 "ldr {:q}, [x0]");
check!(vreg_q vreg "ldr {:q}, [x0]");

// CHECK-LABEL: vreg_low16_v:
// CHECK-LABEL: vreg_s:
// CHECK: //APP
// CHECK: ldr s0, [x0]
// CHECK: //NO_APP
check!(vreg_s vreg "ldr {:s}, [x0]");

// CHECK-LABEL: vreg_v:
// CHECK: //APP
// CHECK: add v0.4s, v0.4s, v0.4s
// CHECK: //NO_APP
check!(vreg_low16_v vreg_low16 "add {0:v}.4s, {0:v}.4s, {0:v}.4s");
check!(vreg_v vreg "add {0:v}.4s, {0:v}.4s, {0:v}.4s");
Loading
Loading