Let InstCombine remove Clone shims inside Clone shims

Co-authored-by: scottmcm <scottmcm@users.noreply.github.com>
This commit is contained in:
Ben Kimock 2024-07-24 19:12:50 -04:00
parent 92c6c03805
commit a7d57aa7c8
13 changed files with 31 additions and 45 deletions

View File

@ -1864,9 +1864,9 @@ impl<'tcx> Ty<'tcx> {
// Definitely absolutely not copy.
ty::Ref(_, _, hir::Mutability::Mut) => false,
// Thin pointers & thin shared references are pure-clone-copy, but for
// anything with custom metadata it might be more complicated.
ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => false,
// The standard library has a blanket Copy impl for shared references and raw pointers,
// for all unsized types.
ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => true,
ty::Coroutine(..) | ty::CoroutineWitness(..) => false,

View File

@ -17,7 +17,7 @@ use std::iter;
use crate::{
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator,
mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
};
use rustc_middle::mir::patch::MirPatch;
use rustc_mir_dataflow::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
@ -154,6 +154,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
&deref_separator::Derefer,
&remove_noop_landing_pads::RemoveNoopLandingPads,
&simplify::SimplifyCfg::MakeShim,
&instsimplify::InstSimplify,
&abort_unwinding_calls::AbortUnwindingCalls,
&add_call_guards::CriticalCallEdges,
],

View File

@ -29,6 +29,8 @@ trait Copy {}
#[lang = "freeze"]
auto trait Freeze {}
impl<T: ?Sized> Copy for *mut T {}
#[lang = "drop_in_place"]
#[inline]
#[allow(unconditional_recursion)]

View File

@ -17,6 +17,8 @@ trait Copy {}
#[lang = "freeze"]
auto trait Freeze {}
impl<T: ?Sized> Copy for *mut T {}
#[lang = "drop_in_place"]
#[inline]
#[allow(unconditional_recursion)]

View File

@ -18,6 +18,7 @@ pub trait Sized {}
#[lang = "copy"]
trait Copy {}
impl<T: ?Sized> Copy for *const T {}
#[repr(simd)]
pub struct i8x16([i8; 16]);

View File

@ -18,6 +18,7 @@ pub trait Sized {}
#[lang = "copy"]
trait Copy {}
impl<T: ?Sized> Copy for *mut T {}
#[repr(simd)]
pub struct i8x16([i8; 16]);

View File

@ -17,6 +17,7 @@
pub trait Sized {}
#[lang = "copy"]
pub trait Copy {}
impl<T: ?Sized> Copy for *const T {}
#[lang = "receiver"]
pub trait Receiver {}
#[lang = "tuple_trait"]

View File

@ -0,0 +1,15 @@
// Clone shims for aggregates are generated by just calling the Clone shims for all their members.
// Those calls generate a lot of unnecessary IR if the members are Copy. This test ensures that we
// optimize away those inner calls without needing to inline them.
//@ compile-flags: -Cno-prepopulate-passes -Csymbol-mangling-version=v0 -Zinline-mir=no
#![crate_type = "lib"]
pub type Test = (i32, i32, *const i32);
pub static TEST: fn(&Test) -> Test = <Test as core::clone::Clone>::clone;
// CHECK-NOT: call <i32 as core::clone::Clone>::clone
// CHECK-NOT: call <*const i32 as core::clone::Clone>::clone
// CHECK: ; <(i32, i32, *const i32) as core::clone::Clone>::clone
// CHECK-NOT: call <i32 as core::clone::Clone>::clone
// CHECK-NOT: call <*const i32 as core::clone::Clone>::clone

View File

@ -16,6 +16,8 @@ trait Freeze {}
#[lang = "copy"]
trait Copy {}
impl<T> Copy for *mut T {}
#[rustc_intrinsic]
fn size_of<T>() -> usize {
loop {}

View File

@ -18,6 +18,7 @@ impl Copy for i64 {}
impl Copy for u64 {}
impl Copy for f32 {}
impl Copy for f64 {}
impl<T> Copy for *mut T {}
// CHECK: define void @f_void()
#[no_mangle]

View File

@ -15,6 +15,7 @@
trait Sized {}
#[lang = "copy"]
trait Copy {}
impl<T: ?Sized> Copy for &T {}
#[lang = "receiver"]
trait Receiver {}
#[lang = "dispatch_from_dyn"]

View File

@ -1,20 +0,0 @@
// Avoid panicking if the Clone trait is not found while building error suggestions
// See #104870
#![feature(no_core, lang_items)]
#![no_core]
#[lang = "sized"]
trait Sized {}
#[lang = "copy"]
trait Copy {}
fn g<T>(x: T) {}
fn f(x: *mut u8) {
g(x);
g(x); //~ ERROR use of moved value: `x`
}
fn main() {}

View File

@ -1,21 +0,0 @@
error[E0382]: use of moved value: `x`
--> $DIR/missing-clone-for-suggestion.rs:17:7
|
LL | fn f(x: *mut u8) {
| - move occurs because `x` has type `*mut u8`, which does not implement the `Copy` trait
LL | g(x);
| - value moved here
LL | g(x);
| ^ value used here after move
|
note: consider changing this parameter type in function `g` to borrow instead if owning the value isn't necessary
--> $DIR/missing-clone-for-suggestion.rs:13:12
|
LL | fn g<T>(x: T) {}
| - ^ this parameter takes ownership of the value
| |
| in this function
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0382`.