Skip to content

Commit 3d9fafa

Browse files
committed
Sort items only within codegen tests
1 parent 38f68dc commit 3d9fafa

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ pub(crate) fn compile_codegen_unit(
8484
let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
8585
{
8686
let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
87-
let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
87+
let mono_items = if tcx.sess.opts.unstable_opts.codegen_source_order {
88+
cx.codegen_unit.items_in_deterministic_order(cx.tcx)
89+
} else {
90+
// The `items` has a deterministic order, so we can use it directly.
91+
cx.codegen_unit.items().iter().cloned().collect()
92+
};
8893
for &(mono_item, data) in &mono_items {
8994
mono_item.predefine::<Builder<'_, '_, '_>>(
9095
&mut cx,

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -528,37 +528,42 @@ impl<'tcx> CodegenUnit<'tcx> {
528528
// The codegen tests rely on items being process in the same order as
529529
// they appear in the file, so for local items, we sort by node_id first
530530
#[derive(PartialEq, Eq, PartialOrd, Ord)]
531-
struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>);
532-
531+
struct ItemSortKey<'tcx>(Option<Span>, Option<String>, SymbolName<'tcx>);
532+
533+
// We only want to take HirIds of user-defined
534+
// instances into account. The others don't matter for
535+
// the codegen tests and can even make item order
536+
// unstable.
537+
fn local_item_query<'tcx, T>(
538+
item: MonoItem<'tcx>,
539+
op: impl FnOnce(DefId) -> T,
540+
) -> Option<T> {
541+
match item {
542+
MonoItem::Fn(ref instance) => match instance.def {
543+
InstanceKind::Item(def) => def.as_local().map(op),
544+
InstanceKind::VTableShim(..)
545+
| InstanceKind::ReifyShim(..)
546+
| InstanceKind::Intrinsic(..)
547+
| InstanceKind::FnPtrShim(..)
548+
| InstanceKind::Virtual(..)
549+
| InstanceKind::ClosureOnceShim { .. }
550+
| InstanceKind::ConstructCoroutineInClosureShim { .. }
551+
| InstanceKind::DropGlue(..)
552+
| InstanceKind::CloneShim(..)
553+
| InstanceKind::ThreadLocalShim(..)
554+
| InstanceKind::FnPtrAddrShim(..)
555+
| InstanceKind::AsyncDropGlue(..)
556+
| InstanceKind::FutureDropPollShim(..)
557+
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
558+
},
559+
MonoItem::Static(def_id) => def_id.as_local().map(op),
560+
MonoItem::GlobalAsm(item_id) => Some(op(item_id.owner_id.def_id)),
561+
}
562+
}
533563
fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
534564
ItemSortKey(
535-
match item {
536-
MonoItem::Fn(ref instance) => {
537-
match instance.def {
538-
// We only want to take HirIds of user-defined
539-
// instances into account. The others don't matter for
540-
// the codegen tests and can even make item order
541-
// unstable.
542-
InstanceKind::Item(def) => def.as_local().map(Idx::index),
543-
InstanceKind::VTableShim(..)
544-
| InstanceKind::ReifyShim(..)
545-
| InstanceKind::Intrinsic(..)
546-
| InstanceKind::FnPtrShim(..)
547-
| InstanceKind::Virtual(..)
548-
| InstanceKind::ClosureOnceShim { .. }
549-
| InstanceKind::ConstructCoroutineInClosureShim { .. }
550-
| InstanceKind::DropGlue(..)
551-
| InstanceKind::CloneShim(..)
552-
| InstanceKind::ThreadLocalShim(..)
553-
| InstanceKind::FnPtrAddrShim(..)
554-
| InstanceKind::AsyncDropGlue(..)
555-
| InstanceKind::FutureDropPollShim(..)
556-
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
557-
}
558-
}
559-
MonoItem::Static(def_id) => def_id.as_local().map(Idx::index),
560-
MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.index()),
561-
},
565+
local_item_query(item, |def_id| tcx.def_span(def_id)),
566+
local_item_query(item, |def_id| tcx.def_path(id).to_string_no_crate_verbose()),
562567
item.symbol_name(tcx),
563568
)
564569
}

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,12 +2168,12 @@ options! {
21682168
"hash algorithm of source files used to check freshness in cargo (`blake3` or `sha256`)"),
21692169
codegen_backend: Option<String> = (None, parse_opt_string, [TRACKED],
21702170
"the backend to use"),
2171+
codegen_source_order: bool = (false, parse_bool, [UNTRACKED],
2172+
"emit mono items in the order of spans in source files (default: no)"),
21712173
contract_checks: Option<bool> = (None, parse_opt_bool, [TRACKED],
21722174
"emit runtime checks for contract pre- and post-conditions (default: no)"),
21732175
coverage_options: CoverageOptions = (CoverageOptions::default(), parse_coverage_options, [TRACKED],
21742176
"control details of coverage instrumentation"),
2175-
codegen_source_order: bool = (false, parse_bool, [UNTRACKED],
2176-
"emit mono items in the order of spans in source files (default: no)"),
21772177
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
21782178
"inject the given attribute in the crate"),
21792179
cross_crate_inline_threshold: InliningThreshold = (InliningThreshold::Sometimes(100), parse_inlining_threshold, [TRACKED],

src/tools/compiletest/src/runtest.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,10 @@ impl<'test> TestCx<'test> {
16941694
}
16951695
TestMode::Assembly | TestMode::Codegen => {
16961696
rustc.arg("-Cdebug-assertions=no");
1697+
// For assembly and codegen tests, we want to use the same order
1698+
// of the items of a codegen unit as the source order, so that
1699+
// we can compare the output with the source code through filecheck.
1700+
rustc.arg("-Zcodegen-source-order")
16971701
}
16981702
TestMode::Crashes => {
16991703
set_mir_dump_dir(&mut rustc);

0 commit comments

Comments
 (0)