Skip to content

Commit 2d5a628

Browse files
committed
Auto merge of rust-lang#128165 - saethlin:optimize-clone-shims, r=compiler-errors
Let InstCombine remove Clone shims inside Clone shims The Clone shims that we generate tend to recurse into other Clone shims, which gets very silly very quickly. Here's our current state: https://godbolt.org/z/E69YeY8eq So I've added InstSimplify to the shims optimization passes, and improved `is_trivially_pure_clone_copy` so that it can delete those calls inside the shim. This makes the shim way smaller because most of its size is the required ceremony for unwinding. This change also completely breaks the UI test added for rust-lang#104870. With this PR, that program ICEs in MIR type checking because `is_trivially_pure_clone_copy` and the trait solver disagree on whether `*mut u8` is `Copy`. And adding the requisite `Copy` impl to make them agree makes the test not generate any diagnostics. Considering that I spent most of my time on this PR fixing `#![no_core]` tests, I would prefer to just delete this one. The maintenance burden of `#![no_core]` is uniquely high because when they break they tend to break in very confusing ways. try-job: x86_64-mingw
2 parents 355efac + a7d57aa commit 2d5a628

13 files changed

+31
-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 standard library has a blanket Copy impl for shared references and raw pointers,
1868+
// for all unsized types.
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
],

library/rtstartup/rsbegin.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ trait Copy {}
2929
#[lang = "freeze"]
3030
auto trait Freeze {}
3131

32+
impl<T: ?Sized> Copy for *mut T {}
33+
3234
#[lang = "drop_in_place"]
3335
#[inline]
3436
#[allow(unconditional_recursion)]

library/rtstartup/rsend.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ trait Copy {}
1717
#[lang = "freeze"]
1818
auto trait Freeze {}
1919

20+
impl<T: ?Sized> Copy for *mut T {}
21+
2022
#[lang = "drop_in_place"]
2123
#[inline]
2224
#[allow(unconditional_recursion)]

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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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-NOT: call <i32 as core::clone::Clone>::clone
12+
// CHECK-NOT: call <*const i32 as core::clone::Clone>::clone
13+
// CHECK: ; <(i32, i32, *const i32) as core::clone::Clone>::clone
14+
// CHECK-NOT: call <i32 as core::clone::Clone>::clone
15+
// 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)