rust/tests/codegen-units/item-collection/unsizing.rs

80 lines
1.8 KiB
Rust
Raw Normal View History

2018-05-08 08:10:16 -05:00
// compile-flags:-Zprint-mono-items=eager
rustc: Don't inline in CGUs at -O0 This commit tweaks the behavior of inlining functions into multiple codegen units when rustc is compiling in debug mode. Today rustc will unconditionally treat `#[inline]` functions by translating them into all codegen units that they're needed within, marking the linkage as `internal`. This commit changes the behavior so that in debug mode (compiling at `-O0`) rustc will instead only translate `#[inline]` functions into *one* codegen unit, forcing all other codegen units to reference this one copy. The goal here is to improve debug compile times by reducing the amount of translation that happens on behalf of multiple codegen units. It was discovered in #44941 that increasing the number of codegen units had the adverse side effect of increasing the overal work done by the compiler, and the suspicion here was that the compiler was inlining, translating, and codegen'ing more functions with more codegen units (for example `String` would be basically inlined into all codegen units if used). The strategy in this commit should reduce the cost of `#[inline]` functions to being equivalent to one codegen unit, which is only translating and codegen'ing inline functions once. Collected [data] shows that this does indeed improve the situation from [before] as the overall cpu-clock time increases at a much slower rate and when pinned to one core rustc does not consume significantly more wall clock time than with one codegen unit. One caveat of this commit is that the symbol names for inlined functions that are only translated once needed some slight tweaking. These inline functions could be translated into multiple crates and we need to make sure the symbols don't collideA so the crate name/disambiguator is mixed in to the symbol name hash in these situations. [data]: https://github.com/rust-lang/rust/issues/44941#issuecomment-334880911 [before]: https://github.com/rust-lang/rust/issues/44941#issuecomment-334583384
2017-10-06 16:59:33 -05:00
// compile-flags:-Zinline-in-all-cgus
2022-05-18 18:06:40 -05:00
// compile-flags:-Zmir-opt-level=0
#![deny(dead_code)]
#![feature(coerce_unsized)]
#![feature(unsize)]
#![feature(start)]
use std::marker::Unsize;
use std::ops::CoerceUnsized;
trait Trait {
fn foo(&self);
}
// Simple Case
impl Trait for bool {
fn foo(&self) {}
}
impl Trait for char {
fn foo(&self) {}
}
// Struct Field Case
struct Struct<T: ?Sized> {
_a: u32,
_b: i32,
_c: T
}
impl Trait for f64 {
fn foo(&self) {}
}
// Custom Coercion Case
impl Trait for u32 {
fn foo(&self) {}
}
#[derive(Clone, Copy)]
2022-07-25 15:36:03 -05:00
struct Wrapper<T: ?Sized>(#[allow(unused_tuple_struct_fields)] *const T);
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Wrapper<U>> for Wrapper<T> {}
//~ MONO_ITEM fn start
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
// simple case
let bool_sized = &true;
//~ MONO_ITEM fn std::ptr::drop_in_place::<bool> - shim(None) @@ unsizing-cgu.0[Internal]
//~ MONO_ITEM fn <bool as Trait>::foo
let _bool_unsized = bool_sized as &Trait;
let char_sized = &'a';
//~ MONO_ITEM fn std::ptr::drop_in_place::<char> - shim(None) @@ unsizing-cgu.0[Internal]
//~ MONO_ITEM fn <char as Trait>::foo
let _char_unsized = char_sized as &Trait;
// struct field
let struct_sized = &Struct {
_a: 1,
_b: 2,
_c: 3.0f64
};
//~ MONO_ITEM fn std::ptr::drop_in_place::<f64> - shim(None) @@ unsizing-cgu.0[Internal]
//~ MONO_ITEM fn <f64 as Trait>::foo
let _struct_unsized = struct_sized as &Struct<Trait>;
// custom coercion
let wrapper_sized = Wrapper(&0u32);
//~ MONO_ITEM fn std::ptr::drop_in_place::<u32> - shim(None) @@ unsizing-cgu.0[Internal]
//~ MONO_ITEM fn <u32 as Trait>::foo
let _wrapper_sized = wrapper_sized as Wrapper<Trait>;
0
}