Skip to content

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

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ fn test_unstable_options_tracking_hash() {
// Make sure that changing an [UNTRACKED] option leaves the hash unchanged.
// tidy-alphabetical-start
untracked!(assert_incr_state, Some(String::from("loaded")));
untracked!(codegen_source_order, true);
untracked!(deduplicate_diagnostics, false);
untracked!(dump_dep_graph, true);
untracked!(dump_mir, Some(String::from("abc")));
Expand Down
67 changes: 36 additions & 31 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc_hashes::Hash128;
use rustc_hir::ItemId;
use rustc_hir::attrs::InlineAttr;
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,44 +525,50 @@ 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
// they appear in the file, so for local items, we sort by span first
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>);

struct ItemSortKey<'tcx>(Option<Span>, SymbolName<'tcx>);

// We only want to take HirIds of user-defines instances into account.
// The others don't matter for the codegen tests and can even make item
// order unstable.
fn local_item_id<'tcx>(item: MonoItem<'tcx>) -> Option<DefId> {
match item {
MonoItem::Fn(ref instance) => match instance.def {
InstanceKind::Item(def) => def.as_local().map(|_| def),
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,
},
MonoItem::Static(def_id) => def_id.as_local().map(|_| def_id),
MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.to_def_id()),
}
}
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,
}
}
MonoItem::Static(def_id) => def_id.as_local().map(Idx::index),
MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.index()),
},
local_item_id(item)
.map(|def_id| tcx.def_span(def_id).find_ancestor_not_from_macro())
.flatten(),
item.symbol_name(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;
}
items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
items
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,8 @@ options! {
"hash algorithm of source files used to check freshness in cargo (`blake3` or `sha256`)"),
codegen_backend: Option<String> = (None, parse_opt_string, [TRACKED],
"the backend to use"),
codegen_source_order: bool = (false, parse_bool, [UNTRACKED],
"emit mono items in the order of spans in source files (default: no)"),
contract_checks: Option<bool> = (None, parse_opt_bool, [TRACKED],
"emit runtime checks for contract pre- and post-conditions (default: no)"),
coverage_options: CoverageOptions = (CoverageOptions::default(), parse_coverage_options, [TRACKED],
Expand Down
12 changes: 12 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/codegen-source-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `codegen-source-order`

---

This feature allows you to have a predictive and
deterministic order for items after codegen, which
is the same as in source code.

For every `CodegenUnit`, local `MonoItem`s would
be sorted by `(Span, SymbolName)`, which
makes codegen tests rely on the order of items in
source files work.
4 changes: 4 additions & 0 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,10 @@ impl<'test> TestCx<'test> {
}
TestMode::Assembly | TestMode::Codegen => {
rustc.arg("-Cdebug-assertions=no");
// For assembly and codegen tests, we want to use the same order
// of the items of a codegen unit as the source order, so that
// we can compare the output with the source code through filecheck.
rustc.arg("-Zcodegen-source-order");
}
TestMode::Crashes => {
set_mir_dump_dir(&mut rustc);
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/intrinsics/bad-intrinsic-monomorphization.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
error[E0511]: invalid monomorphization of `cttz` intrinsic: expected basic integer type, found `Foo`
--> $DIR/bad-intrinsic-monomorphization.rs:16:5
error[E0511]: invalid monomorphization of `simd_add` intrinsic: expected SIMD input type, found non-SIMD `Foo`
--> $DIR/bad-intrinsic-monomorphization.rs:26:5
|
LL | intrinsics::cttz(v)
| ^^^^^^^^^^^^^^^^^^^
LL | intrinsics::simd::simd_add(a, b)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `fadd_fast` intrinsic: expected basic float type, found `Foo`
--> $DIR/bad-intrinsic-monomorphization.rs:21:5
|
LL | intrinsics::fadd_fast(a, b)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `simd_add` intrinsic: expected SIMD input type, found non-SIMD `Foo`
--> $DIR/bad-intrinsic-monomorphization.rs:26:5
error[E0511]: invalid monomorphization of `cttz` intrinsic: expected basic integer type, found `Foo`
--> $DIR/bad-intrinsic-monomorphization.rs:16:5
|
LL | intrinsics::simd::simd_add(a, b)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | intrinsics::cttz(v)
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

Expand Down
88 changes: 44 additions & 44 deletions tests/ui/intrinsics/non-integer-atomic.stderr
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:15:5
error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `&dyn Fn()`
--> $DIR/non-integer-atomic.rs:55:5
|
LL | intrinsics::atomic_load::<_, { SeqCst }>(p);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:20:5
error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:35:5
|
LL | intrinsics::atomic_load::<_, { SeqCst }>(p);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `&dyn Fn()`
--> $DIR/non-integer-atomic.rs:60:5
|
LL | intrinsics::atomic_store::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:25:5
error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:85:5
|
LL | intrinsics::atomic_xchg::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:30:5
error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `&dyn Fn()`
--> $DIR/non-integer-atomic.rs:70:5
|
LL | intrinsics::atomic_cxchg::<_, { SeqCst }, { SeqCst }>(p, v, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:35:5
|
LL | intrinsics::atomic_load::<_, { SeqCst }>(p);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:40:5
|
LL | intrinsics::atomic_store::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:45:5
|
LL | intrinsics::atomic_xchg::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:50:5
error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:90:5
|
LL | intrinsics::atomic_cxchg::<_, { SeqCst }, { SeqCst }>(p, v, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `&dyn Fn()`
--> $DIR/non-integer-atomic.rs:55:5
error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:80:5
|
LL | intrinsics::atomic_load::<_, { SeqCst }>(p);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | intrinsics::atomic_store::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `&dyn Fn()`
--> $DIR/non-integer-atomic.rs:60:5
error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:20:5
|
LL | intrinsics::atomic_store::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -64,36 +58,42 @@ error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basi
LL | intrinsics::atomic_xchg::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `&dyn Fn()`
--> $DIR/non-integer-atomic.rs:70:5
|
LL | intrinsics::atomic_cxchg::<_, { SeqCst }, { SeqCst }>(p, v, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:75:5
|
LL | intrinsics::atomic_load::<_, { SeqCst }>(p);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_store` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:80:5
error[E0511]: invalid monomorphization of `atomic_load` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:15:5
|
LL | intrinsics::atomic_store::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | intrinsics::atomic_load::<_, { SeqCst }>(p);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:85:5
error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:30:5
|
LL | intrinsics::atomic_xchg::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | intrinsics::atomic_cxchg::<_, { SeqCst }, { SeqCst }>(p, v, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `[u8; 100]`
--> $DIR/non-integer-atomic.rs:90:5
error[E0511]: invalid monomorphization of `atomic_cxchg` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:50:5
|
LL | intrinsics::atomic_cxchg::<_, { SeqCst }, { SeqCst }>(p, v, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basic integer type, found `Foo`
--> $DIR/non-integer-atomic.rs:45:5
|
LL | intrinsics::atomic_xchg::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0511]: invalid monomorphization of `atomic_xchg` intrinsic: expected basic integer type, found `bool`
--> $DIR/non-integer-atomic.rs:25:5
|
LL | intrinsics::atomic_xchg::<_, { SeqCst }>(p, v);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 16 previous errors

For more information about this error, try `rustc --explain E0511`.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
//@ build-fail
//@ aux-build:def_colliding_external.rs
// FIXME(#83838) codegen-units=1 triggers llvm asserts
//@ compile-flags: -Ccodegen-units=16
// FIXME(#144940) `-Zcodegen-source-order` avoids `_rust_extern_with_linkage_collision`
// sorted after the main, otherwise it causes an ICE. Remove this option
// once this issue fixed.
//@ compile-flags: -Ccodegen-units=16 -Zcodegen-source-order

extern crate def_colliding_external as dep1;

Expand All @@ -19,7 +22,7 @@ mod dep2 {

fn main() {
unsafe {
println!("{:p}", &dep1::collision);
println!("{:p}", &dep1::collision);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//@ build-fail
// FIXME(#83838) codegen-units=1 triggers llvm asserts
//@ compile-flags: -Ccodegen-units=16
// FIXME(#144940) `-Zcodegen-source-order` avoids `_rust_extern_with_linkage_collision`
// sorted after the main, otherwise it causes an ICE. Remove this option
// once this issue fixed.
//@ compile-flags: -Ccodegen-units=16 -Zcodegen-source-order
#![feature(linkage)]

mod dep1 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: symbol `collision` is already defined
--> $DIR/linkage-detect-local-generated-name-collision.rs:10:9
--> $DIR/linkage-detect-local-generated-name-collision.rs:13:9
|
LL | pub static collision: *const i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading