Skip to content

Commit 775c468

Browse files
committed
Let InstCombine remove Clone shims inside Clone shims
1 parent 92c6c03 commit 775c468

11 files changed

+25
-45
lines changed

compiler/rustc_middle/src/ty/sty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1864,9 +1864,9 @@ impl<'tcx> Ty<'tcx> {
18641864
// Definitely absolutely not copy.
18651865
ty::Ref(_, _, hir::Mutability::Mut) => false,
18661866

1867-
// Thin pointers & thin shared references are pure-clone-copy, but for
1868-
// anything with custom metadata it might be more complicated.
1869-
ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => false,
1867+
// The library has a blanket Copy impl for shared references and raw pointers,
1868+
// regardless of metadata.
1869+
ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => true,
18701870

18711871
ty::Coroutine(..) | ty::CoroutineWitness(..) => false,
18721872

compiler/rustc_mir_transform/src/shim.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::iter;
1717

1818
use crate::{
1919
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator,
20-
mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
20+
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
2121
};
2222
use rustc_middle::mir::patch::MirPatch;
2323
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<
154154
&deref_separator::Derefer,
155155
&remove_noop_landing_pads::RemoveNoopLandingPads,
156156
&simplify::SimplifyCfg::MakeShim,
157+
&instsimplify::InstSimplify,
157158
&abort_unwinding_calls::AbortUnwindingCalls,
158159
&add_call_guards::CriticalCallEdges,
159160
],

tests/assembly/simd-intrinsic-mask-load.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub trait Sized {}
1818

1919
#[lang = "copy"]
2020
trait Copy {}
21+
impl<T: ?Sized> Copy for *const T {}
2122

2223
#[repr(simd)]
2324
pub struct i8x16([i8; 16]);

tests/assembly/simd-intrinsic-mask-store.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub trait Sized {}
1818

1919
#[lang = "copy"]
2020
trait Copy {}
21+
impl<T: ?Sized> Copy for *mut T {}
2122

2223
#[repr(simd)]
2324
pub struct i8x16([i8; 16]);

tests/codegen/avr/avr-func-addrspace.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
pub trait Sized {}
1818
#[lang = "copy"]
1919
pub trait Copy {}
20+
impl<T: ?Sized> Copy for *const T {}
2021
#[lang = "receiver"]
2122
pub trait Receiver {}
2223
#[lang = "tuple_trait"]

tests/codegen/clone-shims.rs

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

tests/codegen/emcripten-catch-unwind.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ trait Freeze {}
1616
#[lang = "copy"]
1717
trait Copy {}
1818

19+
impl<T> Copy for *mut T {}
20+
1921
#[rustc_intrinsic]
2022
fn size_of<T>() -> usize {
2123
loop {}

tests/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl Copy for i64 {}
1818
impl Copy for u64 {}
1919
impl Copy for f32 {}
2020
impl Copy for f64 {}
21+
impl<T> Copy for *mut T {}
2122

2223
// CHECK: define void @f_void()
2324
#[no_mangle]

tests/codegen/sanitizer/kcfi/emit-type-metadata-trait-objects.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
trait Sized {}
1616
#[lang = "copy"]
1717
trait Copy {}
18+
impl<T: ?Sized> Copy for &T {}
1819
#[lang = "receiver"]
1920
trait Receiver {}
2021
#[lang = "dispatch_from_dyn"]

tests/ui/lang-items/missing-clone-for-suggestion.rs

-20
This file was deleted.

tests/ui/lang-items/missing-clone-for-suggestion.stderr

-21
This file was deleted.

0 commit comments

Comments
 (0)