Skip to content

Commit 88796a3

Browse files
committed
Refactor move_copy_annotation_instance
Just pass the `PlaceRef` and defer anything possibly expensive until after we've decided we're going to do the annotation. Flip the representation check to only instrument BackendRepr::Memory represented objects.
1 parent 2d68efe commit 88796a3

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,11 +1043,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10431043
mir::Operand::Copy(_) => sym::compiler_copy,
10441044
_ => unreachable!(),
10451045
};
1046-
let ty = self.monomorphized_place_ty(place.as_ref());
1047-
let layout = bx.cx().layout_of(ty);
10481046

10491047
// Check if we should annotate this move/copy for profiling
1050-
let move_annotation = self.move_copy_annotation_instance(bx, ty, layout, kind);
1048+
let move_annotation = self.move_copy_annotation_instance(bx, place.as_ref(), kind);
10511049

10521050
OperandRef { move_annotation, ..self.codegen_consume(bx, place.as_ref()) }
10531051
}
@@ -1076,44 +1074,44 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10761074

10771075
/// Creates an `Instance` for annotating a move/copy operation at codegen time.
10781076
///
1079-
/// Returns `Some(instance)` if the operation should be annotated with debug info,
1080-
/// `None` otherwise. The instance represents a monomorphized `compiler_move<T, SIZE>`
1081-
/// or `compiler_copy<T, SIZE>` function that can be used to create debug scopes.
1077+
/// Returns `Some(instance)` if the operation should be annotated with debug info, `None`
1078+
/// otherwise. The instance represents a monomorphized `compiler_move<T, SIZE>` or
1079+
/// `compiler_copy<T, SIZE>` function that can be used to create debug scopes.
10821080
///
1083-
/// Annotation criteria:
1084-
/// - Codegen annotation flag must be enabled
1085-
/// - Debug info must be enabled
1086-
/// - Type must not be ZST
1087-
/// - Type size must meet threshold
1088-
/// - Type must not be scalar/scalar-pair/SIMD (no memcpy generated)
1081+
/// There are a number of conditions that must be met for an annotation to be created, but aside
1082+
/// from the basics (annotation is enabled, we're generating debuginfo), the primary concern is
1083+
/// moves/copies which could result in a real `memcpy`. So we check for the size limit, but also
1084+
/// that the underlying representation of the type is in memory.
10891085
fn move_copy_annotation_instance(
10901086
&self,
10911087
bx: &Bx,
1092-
ty: Ty<'tcx>,
1093-
layout: TyAndLayout<'tcx>,
1088+
place: mir::PlaceRef<'tcx>,
10941089
kind: Symbol,
10951090
) -> Option<ty::Instance<'tcx>> {
10961091
let tcx = bx.tcx();
10971092
let sess = tcx.sess;
10981093

1099-
// Check if annotation is enabled and get size limit
1094+
// Skip if we're not generating debuginfo
1095+
if sess.opts.debuginfo == DebugInfo::None {
1096+
return None;
1097+
}
1098+
1099+
// Check if annotation is enabled and get size limit (otherwise skip)
11001100
let size_limit = match sess.opts.unstable_opts.annotate_moves {
11011101
AnnotateMoves::Disabled => return None,
11021102
AnnotateMoves::Enabled(None) => MOVE_ANNOTATION_DEFAULT_LIMIT,
11031103
AnnotateMoves::Enabled(Some(limit)) => limit,
11041104
};
1105-
let size = layout.size.bytes();
1106-
// Skip if we're not generating debug info, or if ty is zst, or the size limit isn't met, or
1107-
// if the layout is not moved by memcpy.
1108-
if sess.opts.debuginfo == DebugInfo::None
1109-
|| layout.is_zst()
1110-
|| size < size_limit
1111-
|| matches!(
1112-
layout.backend_repr,
1113-
BackendRepr::Scalar(_)
1114-
| BackendRepr::ScalarPair(_, _)
1115-
| BackendRepr::SimdVector { .. }
1116-
)
1105+
1106+
let ty = self.monomorphized_place_ty(place);
1107+
let layout = bx.cx().layout_of(ty);
1108+
let ty_size = layout.size.bytes();
1109+
1110+
// Only annotate if type has a memory representation and exceeds size limit (and has a
1111+
// non-zero size)
1112+
if layout.is_zst()
1113+
|| ty_size < size_limit
1114+
|| !matches!(layout.backend_repr, BackendRepr::Memory { .. })
11171115
{
11181116
return None;
11191117
}
@@ -1123,7 +1121,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11231121
let def_id = tcx.get_diagnostic_item(kind)?;
11241122

11251123
// Create generic args: compiler_move<T, SIZE> or compiler_copy<T, SIZE>
1126-
let size_const = ty::Const::from_target_usize(tcx, size);
1124+
let size_const = ty::Const::from_target_usize(tcx, ty_size);
11271125
let generic_args = tcx.mk_args(&[ty.into(), size_const.into()]);
11281126

11291127
// Create the Instance

0 commit comments

Comments
 (0)