Skip to content

Commit

Permalink
Auto merge of rust-lang#128165 - saethlin:optimize-clone-shims, r=com…
Browse files Browse the repository at this point in the history
…piler-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
  • Loading branch information
bors committed Jul 26, 2024
2 parents 355efac + a7d57aa commit 2d5a628
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 45 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
],
Expand Down
2 changes: 2 additions & 0 deletions library/rtstartup/rsbegin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions library/rtstartup/rsend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions tests/assembly/simd-intrinsic-mask-load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions tests/assembly/simd-intrinsic-mask-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/avr/avr-func-addrspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
15 changes: 15 additions & 0 deletions tests/codegen/clone-shims.rs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tests/codegen/emcripten-catch-unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ trait Freeze {}
#[lang = "copy"]
trait Copy {}

impl<T> Copy for *mut T {}

#[rustc_intrinsic]
fn size_of<T>() -> usize {
loop {}
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
trait Sized {}
#[lang = "copy"]
trait Copy {}
impl<T: ?Sized> Copy for &T {}
#[lang = "receiver"]
trait Receiver {}
#[lang = "dispatch_from_dyn"]
Expand Down
20 changes: 0 additions & 20 deletions tests/ui/lang-items/missing-clone-for-suggestion.rs

This file was deleted.

21 changes: 0 additions & 21 deletions tests/ui/lang-items/missing-clone-for-suggestion.stderr

This file was deleted.

0 comments on commit 2d5a628

Please sign in to comment.