Skip to content

Sort MonoItems by span instead of DefIndex. #92323

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 3 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
54 changes: 27 additions & 27 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::ItemId;
use rustc_index::vec::Idx;
use rustc_query_system::ich::StableHashingContext;
use rustc_session::config::OptLevel;
use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol;
use rustc_span::{Span, SpanData};
use std::fmt;
use std::hash::Hash;

Expand Down Expand Up @@ -348,35 +347,36 @@ impl<'tcx> CodegenUnit<'tcx> {
tcx: TyCtxt<'tcx>,
) -> Vec<(MonoItem<'tcx>, (Linkage, Visibility))> {
// 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
//
// We directly cache a SpanData to avoid having to query the interner for each comparison.
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>);
pub struct ItemSortKey<'tcx>(Option<SpanData>, 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.
InstanceDef::Item(def) => def.did.as_local().map(Idx::index),
InstanceDef::VTableShim(..)
| InstanceDef::ReifyShim(..)
| InstanceDef::Intrinsic(..)
| InstanceDef::FnPtrShim(..)
| InstanceDef::Virtual(..)
| InstanceDef::ClosureOnceShim { .. }
| InstanceDef::DropGlue(..)
| InstanceDef::CloneShim(..) => None,
}
let span = 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.
InstanceDef::Item(def) => tcx.hir().span_if_local(def.did),
InstanceDef::VTableShim(..)
| InstanceDef::ReifyShim(..)
| InstanceDef::Intrinsic(..)
| InstanceDef::FnPtrShim(..)
| InstanceDef::Virtual(..)
| InstanceDef::ClosureOnceShim { .. }
| InstanceDef::DropGlue(..)
| InstanceDef::CloneShim(..) => None,
}
MonoItem::Static(def_id) => def_id.as_local().map(Idx::index),
MonoItem::GlobalAsm(item_id) => Some(item_id.def_id.index()),
},
item.symbol_name(tcx),
)
}
MonoItem::Static(def_id) => tcx.hir().span_if_local(def_id),
MonoItem::GlobalAsm(item_id) => Some(tcx.def_span(item_id.def_id)),
};
let span = span.map(Span::data);
ItemSortKey(span, item.symbol_name(tcx))
}

let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect();
Expand Down
88 changes: 44 additions & 44 deletions src/test/assembly/asm/avr-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,6 @@ impl Copy for i32 {}
impl Copy for i64 {}
impl Copy for ptr {}

macro_rules! check {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!("mov {}, {}", lateout($class) y, in($class) x);
y
}
};
}

macro_rules! checkw {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!("movw {}, {}", lateout($class) y, in($class) x);
y
}
};
}

macro_rules! check_reg {
($func:ident $ty:ident $reg:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!(concat!("mov ", $reg, ", ", $reg), lateout($reg) y, in($reg) x);
y
}
};
}

macro_rules! check_regw {
($func:ident $ty:ident $reg:tt $reg_lit:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!(concat!("movw ", $reg_lit, ", ", $reg_lit), lateout($reg) y, in($reg) x);
y
}
};
}

extern "C" {
fn extern_func();
static extern_static: i8;
Expand Down Expand Up @@ -161,6 +117,17 @@ pub unsafe fn muls_clobber(x: i8, y: i8) -> i16 {
z
}

macro_rules! check {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!("mov {}, {}", lateout($class) y, in($class) x);
y
}
};
}

// CHECK-LABEL: reg_i8:
// CHECK: ;APP
// CHECK: mov r{{[0-9]+}}, r{{[0-9]+}}
Expand All @@ -173,6 +140,17 @@ check!(reg_i8 i8 reg);
// CHECK: ;NO_APP
check!(reg_upper_i8 i8 reg_upper);

macro_rules! checkw {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!("movw {}, {}", lateout($class) y, in($class) x);
y
}
};
}

// CHECK-LABEL: reg_pair_i16:
// CHECK: ;APP
// CHECK: movw r{{[0-9]+}}, r{{[0-9]+}}
Expand All @@ -191,6 +169,17 @@ checkw!(reg_iw_i16 i16 reg_iw);
// CHECK: ;NO_APP
checkw!(reg_ptr_i16 i16 reg_ptr);

macro_rules! check_reg {
($func:ident $ty:ident $reg:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!(concat!("mov ", $reg, ", ", $reg), lateout($reg) y, in($reg) x);
y
}
};
}

// CHECK-LABEL: r2_i8:
// CHECK: ;APP
// CHECK: mov r2, r2
Expand All @@ -209,6 +198,17 @@ check_reg!(xl_i8 i8 "XL");
// CHECK: ;NO_APP
check_reg!(xh_i8 i8 "XH");

macro_rules! check_regw {
($func:ident $ty:ident $reg:tt $reg_lit:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!(concat!("movw ", $reg_lit, ", ", $reg_lit), lateout($reg) y, in($reg) x);
y
}
};
}

// CHECK-LABEL: x_i16:
// CHECK: ;APP
// CHECK: movw r26, r26
Expand Down
44 changes: 22 additions & 22 deletions src/test/assembly/asm/bpf-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,6 @@ impl Copy for i32 {}
impl Copy for i64 {}
impl Copy for ptr {}

macro_rules! check {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!("{} = {}", out($class) y, in($class) x);
y
}
};
}

macro_rules! check_reg {
($func:ident $ty:ident $reg:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!(concat!($reg, " = ", $reg), lateout($reg) y, in($reg) x);
y
}
};
}

extern "C" {
fn extern_func();
}
Expand All @@ -69,6 +47,17 @@ pub unsafe fn sym_fn() {
asm!("call {}", sym extern_func);
}

macro_rules! check {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!("{} = {}", out($class) y, in($class) x);
y
}
};
}

// CHECK-LABEL: reg_i8:
// CHECK: #APP
// CHECK: r{{[0-9]+}} = r{{[0-9]+}}
Expand Down Expand Up @@ -111,6 +100,17 @@ check!(wreg_i16 i16 wreg);
// CHECK: #NO_APP
check!(wreg_i32 i32 wreg);

macro_rules! check_reg {
($func:ident $ty:ident $reg:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
let y;
asm!(concat!($reg, " = ", $reg), lateout($reg) y, in($reg) x);
y
}
};
}

// CHECK-LABEL: r0_i8:
// CHECK: #APP
// CHECK: r0 = r0
Expand Down
68 changes: 34 additions & 34 deletions src/test/assembly/asm/hexagon-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,6 @@ extern "C" {
static extern_static: u8;
}

macro_rules! check {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
// Hack to avoid function merging
extern "Rust" {
fn dont_merge(s: &str);
}
dont_merge(stringify!($func));

let y;
asm!("{} = {}", out($class) y, in($class) x);
y
}
};
}

macro_rules! check_reg {
($func:ident $ty:ident $reg:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
// Hack to avoid function merging
extern "Rust" {
fn dont_merge(s: &str);
}
dont_merge(stringify!($func));

let y;
asm!(concat!($reg, " = ", $reg), lateout($reg) y, in($reg) x);
y
}
};
}

// CHECK-LABEL: sym_static:
// CHECK: InlineAsm Start
// CHECK: r0 = {{#+}}extern_static
Expand Down Expand Up @@ -120,6 +86,23 @@ pub unsafe fn packet() {
}}", out(reg) _, in(reg) &val);
}

macro_rules! check {
($func:ident $ty:ident $class:ident) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
// Hack to avoid function merging
extern "Rust" {
fn dont_merge(s: &str);
}
dont_merge(stringify!($func));

let y;
asm!("{} = {}", out($class) y, in($class) x);
y
}
};
}

// CHECK-LABEL: reg_ptr:
// CHECK: InlineAsm Start
// CHECK: r{{[0-9]+}} = r{{[0-9]+}}
Expand Down Expand Up @@ -150,6 +133,23 @@ check!(reg_i8 i8 reg);
// CHECK: InlineAsm End
check!(reg_i16 i16 reg);

macro_rules! check_reg {
($func:ident $ty:ident $reg:tt) => {
#[no_mangle]
pub unsafe fn $func(x: $ty) -> $ty {
// Hack to avoid function merging
extern "Rust" {
fn dont_merge(s: &str);
}
dont_merge(stringify!($func));

let y;
asm!(concat!($reg, " = ", $reg), lateout($reg) y, in($reg) x);
y
}
};
}

// CHECK-LABEL: r0_ptr:
// CHECK: InlineAsm Start
// CHECK: r0 = r0
Expand Down
Loading