Skip to content

Commit 0d4a3f1

Browse files
committed
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.
1 parent d7b282b commit 0d4a3f1

File tree

5 files changed

+214
-4
lines changed

5 files changed

+214
-4
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 30 additions & 0 deletions
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.const_for_ty()?.ty();
2218+
if let ty::FnDef(def_id, substs) = *const_ty.kind() { Some((def_id, substs)) } else { None }
2219+
}
21902220
}
21912221

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

compiler/rustc_mir_transform/src/instcombine.rs

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ 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
};
9-
use rustc_middle::ty::{self, TyCtxt};
9+
use rustc_middle::ty::{self, Ty, TyCtxt, TyKind};
1010

1111
pub struct InstCombine;
1212

@@ -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,80 @@ 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 !is_trivially_pure_copy(self.tcx, inner_ty) {
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+
}
204+
}
205+
206+
fn is_trivially_pure_copy<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
207+
use TyKind::*;
208+
match *ty.kind() {
209+
Bool | Char | Int(..) | Uint(..) | Float(..) => true,
210+
Array(element_ty, _len) => is_trivially_pure_copy(tcx, element_ty),
211+
Tuple(field_tys) => field_tys.iter().all(|x| is_trivially_pure_copy(tcx, x)),
212+
_ => false,
213+
}
133214
}

src/test/codegen/inline-hint.rs

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

0 commit comments

Comments
 (0)