From a7d57aa7c8f42dda6d33e0ac349cf834eaf041b8 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Wed, 24 Jul 2024 19:12:50 -0400 Subject: [PATCH] Let InstCombine remove Clone shims inside Clone shims Co-authored-by: scottmcm --- compiler/rustc_middle/src/ty/sty.rs | 6 +++--- compiler/rustc_mir_transform/src/shim.rs | 3 ++- library/rtstartup/rsbegin.rs | 2 ++ library/rtstartup/rsend.rs | 2 ++ tests/assembly/simd-intrinsic-mask-load.rs | 1 + tests/assembly/simd-intrinsic-mask-store.rs | 1 + tests/codegen/avr/avr-func-addrspace.rs | 1 + tests/codegen/clone-shims.rs | 15 +++++++++++++ tests/codegen/emcripten-catch-unwind.rs | 2 ++ .../riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs | 1 + .../kcfi/emit-type-metadata-trait-objects.rs | 1 + .../missing-clone-for-suggestion.rs | 20 ------------------ .../missing-clone-for-suggestion.stderr | 21 ------------------- 13 files changed, 31 insertions(+), 45 deletions(-) create mode 100644 tests/codegen/clone-shims.rs delete mode 100644 tests/ui/lang-items/missing-clone-for-suggestion.rs delete mode 100644 tests/ui/lang-items/missing-clone-for-suggestion.stderr diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index d2b444a066bcc..da98e3b9f4613 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -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, diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index 6835a39cf3624..19e3bf5a59939 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -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, ], diff --git a/library/rtstartup/rsbegin.rs b/library/rtstartup/rsbegin.rs index 14bce2bbeee2b..9a3d95bd8ddfb 100644 --- a/library/rtstartup/rsbegin.rs +++ b/library/rtstartup/rsbegin.rs @@ -29,6 +29,8 @@ trait Copy {} #[lang = "freeze"] auto trait Freeze {} +impl Copy for *mut T {} + #[lang = "drop_in_place"] #[inline] #[allow(unconditional_recursion)] diff --git a/library/rtstartup/rsend.rs b/library/rtstartup/rsend.rs index 714643c83866f..2514eb0034402 100644 --- a/library/rtstartup/rsend.rs +++ b/library/rtstartup/rsend.rs @@ -17,6 +17,8 @@ trait Copy {} #[lang = "freeze"] auto trait Freeze {} +impl Copy for *mut T {} + #[lang = "drop_in_place"] #[inline] #[allow(unconditional_recursion)] diff --git a/tests/assembly/simd-intrinsic-mask-load.rs b/tests/assembly/simd-intrinsic-mask-load.rs index d537c143d365b..b650e1cee3036 100644 --- a/tests/assembly/simd-intrinsic-mask-load.rs +++ b/tests/assembly/simd-intrinsic-mask-load.rs @@ -18,6 +18,7 @@ pub trait Sized {} #[lang = "copy"] trait Copy {} +impl Copy for *const T {} #[repr(simd)] pub struct i8x16([i8; 16]); diff --git a/tests/assembly/simd-intrinsic-mask-store.rs b/tests/assembly/simd-intrinsic-mask-store.rs index 5d4c00c382337..95a3b28b96796 100644 --- a/tests/assembly/simd-intrinsic-mask-store.rs +++ b/tests/assembly/simd-intrinsic-mask-store.rs @@ -18,6 +18,7 @@ pub trait Sized {} #[lang = "copy"] trait Copy {} +impl Copy for *mut T {} #[repr(simd)] pub struct i8x16([i8; 16]); diff --git a/tests/codegen/avr/avr-func-addrspace.rs b/tests/codegen/avr/avr-func-addrspace.rs index 708347075640b..7f9a7e6e81103 100644 --- a/tests/codegen/avr/avr-func-addrspace.rs +++ b/tests/codegen/avr/avr-func-addrspace.rs @@ -17,6 +17,7 @@ pub trait Sized {} #[lang = "copy"] pub trait Copy {} +impl Copy for *const T {} #[lang = "receiver"] pub trait Receiver {} #[lang = "tuple_trait"] diff --git a/tests/codegen/clone-shims.rs b/tests/codegen/clone-shims.rs new file mode 100644 index 0000000000000..06c959f9ee7db --- /dev/null +++ b/tests/codegen/clone-shims.rs @@ -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 = ::clone; + +// CHECK-NOT: call ::clone +// CHECK-NOT: call <*const i32 as core::clone::Clone>::clone +// CHECK: ; <(i32, i32, *const i32) as core::clone::Clone>::clone +// CHECK-NOT: call ::clone +// CHECK-NOT: call <*const i32 as core::clone::Clone>::clone diff --git a/tests/codegen/emcripten-catch-unwind.rs b/tests/codegen/emcripten-catch-unwind.rs index 6cda8c6799f42..35444db9558df 100644 --- a/tests/codegen/emcripten-catch-unwind.rs +++ b/tests/codegen/emcripten-catch-unwind.rs @@ -16,6 +16,8 @@ trait Freeze {} #[lang = "copy"] trait Copy {} +impl Copy for *mut T {} + #[rustc_intrinsic] fn size_of() -> usize { loop {} diff --git a/tests/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs b/tests/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs index ed0af90aaafdd..520192b5d5915 100644 --- a/tests/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs +++ b/tests/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs @@ -18,6 +18,7 @@ impl Copy for i64 {} impl Copy for u64 {} impl Copy for f32 {} impl Copy for f64 {} +impl Copy for *mut T {} // CHECK: define void @f_void() #[no_mangle] diff --git a/tests/codegen/sanitizer/kcfi/emit-type-metadata-trait-objects.rs b/tests/codegen/sanitizer/kcfi/emit-type-metadata-trait-objects.rs index fc4d570dc2ebe..c1967e55e75b4 100644 --- a/tests/codegen/sanitizer/kcfi/emit-type-metadata-trait-objects.rs +++ b/tests/codegen/sanitizer/kcfi/emit-type-metadata-trait-objects.rs @@ -15,6 +15,7 @@ trait Sized {} #[lang = "copy"] trait Copy {} +impl Copy for &T {} #[lang = "receiver"] trait Receiver {} #[lang = "dispatch_from_dyn"] diff --git a/tests/ui/lang-items/missing-clone-for-suggestion.rs b/tests/ui/lang-items/missing-clone-for-suggestion.rs deleted file mode 100644 index e8290c0098afd..0000000000000 --- a/tests/ui/lang-items/missing-clone-for-suggestion.rs +++ /dev/null @@ -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(x: T) {} - -fn f(x: *mut u8) { - g(x); - g(x); //~ ERROR use of moved value: `x` -} - -fn main() {} diff --git a/tests/ui/lang-items/missing-clone-for-suggestion.stderr b/tests/ui/lang-items/missing-clone-for-suggestion.stderr deleted file mode 100644 index 0187f965b5c00..0000000000000 --- a/tests/ui/lang-items/missing-clone-for-suggestion.stderr +++ /dev/null @@ -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(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`.