Skip to content

Commit c5a43b8

Browse files
committed
Auto merge of #94276 - scottmcm:primitive-clone, r=oli-obk
mir-opt: Replace clone on primitives with copy We can't do it for everything, but it would be nice to at least stop making calls to clone methods in debug from things like derived-clones. r? `@ghost`
2 parents 352e621 + 705b880 commit c5a43b8

17 files changed

+273
-5
lines changed

compiler/rustc_middle/src/mir/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,27 @@ impl<'tcx> Place<'tcx> {
19151915
(base, proj)
19161916
})
19171917
}
1918+
1919+
/// Generates a new place by appending `more_projections` to the existing ones
1920+
/// and interning the result.
1921+
pub fn project_deeper(self, more_projections: &[PlaceElem<'tcx>], tcx: TyCtxt<'tcx>) -> Self {
1922+
if more_projections.is_empty() {
1923+
return self;
1924+
}
1925+
1926+
let mut v: Vec<PlaceElem<'tcx>>;
1927+
1928+
let new_projections = if self.projection.is_empty() {
1929+
more_projections
1930+
} else {
1931+
v = Vec::with_capacity(self.projection.len() + more_projections.len());
1932+
v.extend(self.projection);
1933+
v.extend(more_projections);
1934+
&v
1935+
};
1936+
1937+
Place { local: self.local, projection: tcx.intern_place_elems(new_projections) }
1938+
}
19181939
}
19191940

19201941
impl From<Local> for Place<'_> {
@@ -2187,6 +2208,15 @@ impl<'tcx> Operand<'tcx> {
21872208
Operand::Copy(_) | Operand::Move(_) => None,
21882209
}
21892210
}
2211+
2212+
/// Gets the `ty::FnDef` from an operand if it's a constant function item.
2213+
///
2214+
/// While this is unlikely in general, it's the normal case of what you'll
2215+
/// find as the `func` in a [`TerminatorKind::Call`].
2216+
pub fn const_fn_def(&self) -> Option<(DefId, SubstsRef<'tcx>)> {
2217+
let const_ty = self.constant()?.literal.ty();
2218+
if let ty::FnDef(def_id, substs) = *const_ty.kind() { Some((def_id, substs)) } else { None }
2219+
}
21902220
}
21912221

21922222
///////////////////////////////////////////////////////////////////////////

compiler/rustc_middle/src/ty/sty.rs

+51
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,57 @@ impl<'tcx> Ty<'tcx> {
23802380
}
23812381
}
23822382
}
2383+
2384+
/// Fast path helper for primitives which are always `Copy` and which
2385+
/// have a side-effect-free `Clone` impl.
2386+
///
2387+
/// Returning true means the type is known to be pure and `Copy+Clone`.
2388+
/// Returning `false` means nothing -- could be `Copy`, might not be.
2389+
///
2390+
/// This is mostly useful for optimizations, as there are the types
2391+
/// on which we can replace cloning with dereferencing.
2392+
pub fn is_trivially_pure_clone_copy(self) -> bool {
2393+
match self.kind() {
2394+
ty::Bool | ty::Char | ty::Never => true,
2395+
2396+
// These aren't even `Clone`
2397+
ty::Str | ty::Slice(..) | ty::Foreign(..) | ty::Dynamic(..) => false,
2398+
2399+
ty::Int(..) | ty::Uint(..) | ty::Float(..) => true,
2400+
2401+
// The voldemort ZSTs are fine.
2402+
ty::FnDef(..) => true,
2403+
2404+
ty::Array(element_ty, _len) => element_ty.is_trivially_pure_clone_copy(),
2405+
2406+
// A 100-tuple isn't "trivial", so doing this only for reasonable sizes.
2407+
ty::Tuple(field_tys) => {
2408+
field_tys.len() <= 3 && field_tys.iter().all(Self::is_trivially_pure_clone_copy)
2409+
}
2410+
2411+
// Sometimes traits aren't implemented for every ABI or arity,
2412+
// because we can't be generic over everything yet.
2413+
ty::FnPtr(..) => false,
2414+
2415+
// Definitely absolutely not copy.
2416+
ty::Ref(_, _, hir::Mutability::Mut) => false,
2417+
2418+
// Thin pointers & thin shared references are pure-clone-copy, but for
2419+
// anything with custom metadata it might be more complicated.
2420+
ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => false,
2421+
2422+
ty::Generator(..) | ty::GeneratorWitness(..) => false,
2423+
2424+
// Might be, but not "trivial" so just giving the safe answer.
2425+
ty::Adt(..) | ty::Closure(..) | ty::Opaque(..) => false,
2426+
2427+
ty::Projection(..) | ty::Param(..) | ty::Infer(..) | ty::Error(..) => false,
2428+
2429+
ty::Bound(..) | ty::Placeholder(..) => {
2430+
bug!("`is_trivially_pure_clone_copy` applied to unexpected type: {:?}", self);
2431+
}
2432+
}
2433+
}
23832434
}
23842435

23852436
/// Extra information about why we ended up with a particular variance.

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<'tcx> Ty<'tcx> {
704704
tcx_at: TyCtxtAt<'tcx>,
705705
param_env: ty::ParamEnv<'tcx>,
706706
) -> bool {
707-
tcx_at.is_copy_raw(param_env.and(self))
707+
self.is_trivially_pure_clone_copy() || tcx_at.is_copy_raw(param_env.and(self))
708708
}
709709

710710
/// Checks whether values of this type `T` have a size known at

compiler/rustc_mir_transform/src/instcombine.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::MirPass;
44
use rustc_hir::Mutability;
55
use rustc_middle::mir::{
66
BinOp, Body, Constant, LocalDecls, Operand, Place, ProjectionElem, Rvalue, SourceInfo,
7-
StatementKind, UnOp,
7+
Statement, StatementKind, Terminator, TerminatorKind, UnOp,
88
};
99
use rustc_middle::ty::{self, TyCtxt};
1010

@@ -29,6 +29,11 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
2929
_ => {}
3030
}
3131
}
32+
33+
ctx.combine_primitive_clone(
34+
&mut block.terminator.as_mut().unwrap(),
35+
&mut block.statements,
36+
);
3237
}
3338
}
3439
}
@@ -130,4 +135,70 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
130135
}
131136
}
132137
}
138+
139+
fn combine_primitive_clone(
140+
&self,
141+
terminator: &mut Terminator<'tcx>,
142+
statements: &mut Vec<Statement<'tcx>>,
143+
) {
144+
let TerminatorKind::Call { func, args, destination, .. } = &mut terminator.kind
145+
else { return };
146+
147+
// It's definitely not a clone if there are multiple arguments
148+
if args.len() != 1 {
149+
return;
150+
}
151+
152+
let Some((destination_place, destination_block)) = *destination
153+
else { return };
154+
155+
// Only bother looking more if it's easy to know what we're calling
156+
let Some((fn_def_id, fn_substs)) = func.const_fn_def()
157+
else { return };
158+
159+
// Clone needs one subst, so we can cheaply rule out other stuff
160+
if fn_substs.len() != 1 {
161+
return;
162+
}
163+
164+
// These types are easily available from locals, so check that before
165+
// doing DefId lookups to figure out what we're actually calling.
166+
let arg_ty = args[0].ty(self.local_decls, self.tcx);
167+
168+
let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind()
169+
else { return };
170+
171+
if !inner_ty.is_trivially_pure_clone_copy() {
172+
return;
173+
}
174+
175+
let trait_def_id = self.tcx.trait_of_item(fn_def_id);
176+
if trait_def_id.is_none() || trait_def_id != self.tcx.lang_items().clone_trait() {
177+
return;
178+
}
179+
180+
if !self.tcx.consider_optimizing(|| {
181+
format!(
182+
"InstCombine - Call: {:?} SourceInfo: {:?}",
183+
(fn_def_id, fn_substs),
184+
terminator.source_info
185+
)
186+
}) {
187+
return;
188+
}
189+
190+
let Some(arg_place) = args.pop().unwrap().place()
191+
else { return };
192+
193+
statements.push(Statement {
194+
source_info: terminator.source_info,
195+
kind: StatementKind::Assign(box (
196+
destination_place,
197+
Rvalue::Use(Operand::Copy(
198+
arg_place.project_deeper(&[ProjectionElem::Deref], self.tcx),
199+
)),
200+
)),
201+
});
202+
terminator.kind = TerminatorKind::Goto { target: destination_block };
203+
}
133204
}

src/test/assembly/sparc-struct-abi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
pub trait Sized {}
1414
#[lang = "copy"]
1515
pub trait Copy {}
16+
impl Copy for f32 {}
1617

1718
#[repr(C)]
1819
pub struct Franta {

src/test/assembly/target-feature-multiple.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
trait Sized {}
2424
#[lang = "copy"]
2525
trait Copy {}
26+
impl Copy for u32 {}
2627

2728
// Use of these requires target features to be enabled
2829
extern "unadjusted" {

src/test/codegen/abi-sysv64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
trait Sized {}
1414
#[lang = "copy"]
1515
trait Copy {}
16+
impl Copy for i64 {}
1617

1718
// CHECK: define x86_64_sysvcc i64 @has_sysv64_abi
1819
#[no_mangle]

src/test/codegen/abi-x86-interrupt.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
trait Sized {}
1414
#[lang = "copy"]
1515
trait Copy {}
16+
impl Copy for i64 {}
1617

1718
// CHECK: define x86_intrcc i64 @has_x86_interrupt_abi
1819
#[no_mangle]

src/test/codegen/frame-pointer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
trait Sized { }
1818
#[lang="copy"]
1919
trait Copy { }
20-
20+
impl Copy for u32 {}
2121

2222

2323
// CHECK: define i32 @peach{{.*}}[[PEACH_ATTRS:\#[0-9]+]] {

src/test/codegen/inline-hint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
pub fn f() {
88
let a = A;
9-
let b = (0i32, 1i32, 2i32, 3i32);
9+
let b = (0i32, 1i32, 2i32, 3 as *const i32);
1010
let c = || {};
1111

1212
a(String::new(), String::new());
@@ -21,7 +21,7 @@ struct A(String, String);
2121
// CHECK-NOT: inlinehint
2222
// CHECK-SAME: {{$}}
2323

24-
// CHECK: ; <(i32, i32, i32, i32) as core::clone::Clone>::clone
24+
// CHECK: ; <(i32, i32, i32, *const i{{16|32|64}}) as core::clone::Clone>::clone
2525
// CHECK-NEXT: ; Function Attrs: inlinehint
2626

2727
// CHECK: ; inline_hint::f::{closure#0}

src/test/codegen/riscv-abi/riscv64-lp64-lp64f-lp64d-abi.rs

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
trait Sized {}
1111
#[lang = "copy"]
1212
trait Copy {}
13+
impl Copy for bool {}
14+
impl Copy for i8 {}
15+
impl Copy for u8 {}
16+
impl Copy for i32 {}
17+
impl Copy for i64 {}
18+
impl Copy for u64 {}
19+
impl Copy for f32 {}
20+
impl Copy for f64 {}
1321

1422
// CHECK: define void @f_void()
1523
#[no_mangle]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: -C opt-level=0 -Z inline_mir=no
2+
// ignore-wasm32 compiled with panic=abort by default
3+
4+
// EMIT_MIR combine_clone_of_primitives.{impl#0}-clone.InstCombine.diff
5+
6+
#[derive(Clone)]
7+
struct MyThing<T> {
8+
v: T,
9+
i: u64,
10+
a: [f32; 3],
11+
}
12+
13+
fn main() {
14+
let x = MyThing::<i16> { v: 2, i: 3, a: [0.0; 3] };
15+
let y = x.clone();
16+
17+
assert_eq!(y.v, 2);
18+
assert_eq!(y.i, 3);
19+
assert_eq!(y.a, [0.0; 3]);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone` before InstCombine
2+
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone` after InstCombine
3+
4+
fn <impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone(_1: &MyThing<T>) -> MyThing<T> {
5+
debug self => _1; // in scope 0 at $DIR/combine_clone_of_primitives.rs:6:10: 6:15
6+
let mut _0: MyThing<T>; // return place in scope 0 at $DIR/combine_clone_of_primitives.rs:6:10: 6:15
7+
let _2: &T; // in scope 0 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
8+
let _3: &u64; // in scope 0 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
9+
let _4: &[f32; 3]; // in scope 0 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
10+
let mut _5: T; // in scope 0 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
11+
let mut _6: &T; // in scope 0 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
12+
let _7: &T; // in scope 0 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
13+
let mut _8: u64; // in scope 0 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
14+
let mut _9: &u64; // in scope 0 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
15+
let _10: &u64; // in scope 0 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
16+
let mut _11: [f32; 3]; // in scope 0 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
17+
let mut _12: &[f32; 3]; // in scope 0 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
18+
let _13: &[f32; 3]; // in scope 0 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
19+
scope 1 {
20+
debug __self_0_0 => _2; // in scope 1 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
21+
debug __self_0_1 => _3; // in scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
22+
debug __self_0_2 => _4; // in scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
23+
}
24+
25+
bb0: {
26+
_2 = &((*_1).0: T); // scope 0 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
27+
_3 = &((*_1).1: u64); // scope 0 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
28+
_4 = &((*_1).2: [f32; 3]); // scope 0 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
29+
- _7 = &(*_2); // scope 1 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
30+
- _6 = &(*_7); // scope 1 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
31+
+ _7 = _2; // scope 1 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
32+
+ _6 = _7; // scope 1 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
33+
_5 = <T as Clone>::clone(move _6) -> bb1; // scope 1 at $DIR/combine_clone_of_primitives.rs:8:5: 8:9
34+
// mir::Constant
35+
// + span: $DIR/combine_clone_of_primitives.rs:8:5: 8:9
36+
// + literal: Const { ty: for<'r> fn(&'r T) -> T {<T as Clone>::clone}, val: Value(Scalar(<ZST>)) }
37+
}
38+
39+
bb1: {
40+
- _10 = &(*_3); // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
41+
- _9 = &(*_10); // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
42+
- _8 = <u64 as Clone>::clone(move _9) -> [return: bb2, unwind: bb4]; // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
43+
- // mir::Constant
44+
- // + span: $DIR/combine_clone_of_primitives.rs:9:5: 9:11
45+
- // + literal: Const { ty: for<'r> fn(&'r u64) -> u64 {<u64 as Clone>::clone}, val: Value(Scalar(<ZST>)) }
46+
+ _10 = _3; // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
47+
+ _9 = _10; // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
48+
+ _8 = (*_9); // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
49+
+ goto -> bb2; // scope 1 at $DIR/combine_clone_of_primitives.rs:9:5: 9:11
50+
}
51+
52+
bb2: {
53+
- _13 = &(*_4); // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
54+
- _12 = &(*_13); // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
55+
- _11 = <[f32; 3] as Clone>::clone(move _12) -> [return: bb3, unwind: bb4]; // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
56+
- // mir::Constant
57+
- // + span: $DIR/combine_clone_of_primitives.rs:10:5: 10:16
58+
- // + literal: Const { ty: for<'r> fn(&'r [f32; 3]) -> [f32; 3] {<[f32; 3] as Clone>::clone}, val: Value(Scalar(<ZST>)) }
59+
+ _13 = _4; // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
60+
+ _12 = _13; // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
61+
+ _11 = (*_12); // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
62+
+ goto -> bb3; // scope 1 at $DIR/combine_clone_of_primitives.rs:10:5: 10:16
63+
}
64+
65+
bb3: {
66+
(_0.0: T) = move _5; // scope 1 at $DIR/combine_clone_of_primitives.rs:6:10: 6:15
67+
(_0.1: u64) = move _8; // scope 1 at $DIR/combine_clone_of_primitives.rs:6:10: 6:15
68+
(_0.2: [f32; 3]) = move _11; // scope 1 at $DIR/combine_clone_of_primitives.rs:6:10: 6:15
69+
return; // scope 0 at $DIR/combine_clone_of_primitives.rs:6:15: 6:15
70+
}
71+
72+
bb4 (cleanup): {
73+
drop(_5) -> bb5; // scope 1 at $DIR/combine_clone_of_primitives.rs:6:14: 6:15
74+
}
75+
76+
bb5 (cleanup): {
77+
resume; // scope 0 at $DIR/combine_clone_of_primitives.rs:6:10: 6:15
78+
}
79+
}
80+

src/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-registers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub trait Sized { }
88
#[lang="copy"]
99
pub trait Copy { }
10+
impl Copy for u32 {}
1011

1112
extern "rust-intrinsic" {
1213
pub fn transmute<T, U>(e: T) -> U;

src/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub trait Sized { }
88
#[lang="copy"]
99
pub trait Copy { }
10+
impl Copy for u32 {}
1011

1112
extern "rust-intrinsic" {
1213
pub fn transmute<T, U>(e: T) -> U;

0 commit comments

Comments
 (0)