From ab91a63d403b0105cacd72809cd292a72984ed99 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 26 Aug 2025 21:08:03 -0400 Subject: [PATCH] Ignore intrinsic calls in cross-crate-inlining cost model --- .../rustc_mir_transform/src/cross_crate_inline.rs | 11 ++++++++++- tests/assembly-llvm/breakpoint.rs | 1 + tests/assembly-llvm/simd/reduce-fadd-unordered.rs | 1 + .../cross-crate-inlining/auxiliary/leaf.rs | 5 +++++ .../cross-crate-inlining/leaf-inlining.rs | 7 +++++++ tests/codegen-llvm/default-visibility.rs | 1 + 6 files changed, 25 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_transform/src/cross_crate_inline.rs b/compiler/rustc_mir_transform/src/cross_crate_inline.rs index b186c2bd775..df98c07f549 100644 --- a/compiler/rustc_mir_transform/src/cross_crate_inline.rs +++ b/compiler/rustc_mir_transform/src/cross_crate_inline.rs @@ -135,7 +135,16 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> { } } } - TerminatorKind::Call { unwind, .. } => { + TerminatorKind::Call { ref func, unwind, .. } => { + // We track calls because they make our function not a leaf (and in theory, the + // number of calls indicates how likely this function is to perturb other CGUs). + // But intrinsics don't have a body that gets assigned to a CGU, so they are + // ignored. + if let Some((fn_def_id, _)) = func.const_fn_def() + && self.tcx.has_attr(fn_def_id, sym::rustc_intrinsic) + { + return; + } self.calls += 1; if let UnwindAction::Cleanup(_) = unwind { self.landing_pads += 1; diff --git a/tests/assembly-llvm/breakpoint.rs b/tests/assembly-llvm/breakpoint.rs index e0cc2d1eebb..d119b68e899 100644 --- a/tests/assembly-llvm/breakpoint.rs +++ b/tests/assembly-llvm/breakpoint.rs @@ -9,6 +9,7 @@ // CHECK-LABEL: use_bp // aarch64: brk #0xf000 // x86_64: int3 +#[inline(never)] pub fn use_bp() { core::arch::breakpoint(); } diff --git a/tests/assembly-llvm/simd/reduce-fadd-unordered.rs b/tests/assembly-llvm/simd/reduce-fadd-unordered.rs index e872826f6ef..fdd03639da0 100644 --- a/tests/assembly-llvm/simd/reduce-fadd-unordered.rs +++ b/tests/assembly-llvm/simd/reduce-fadd-unordered.rs @@ -16,6 +16,7 @@ use std::simd::*; // It would emit about an extra fadd, depending on the architecture. // CHECK-LABEL: reduce_fadd_negative_zero +#[inline(never)] pub unsafe fn reduce_fadd_negative_zero(v: f32x4) -> f32 { // x86_64: addps // x86_64-NEXT: movshdup diff --git a/tests/codegen-llvm/cross-crate-inlining/auxiliary/leaf.rs b/tests/codegen-llvm/cross-crate-inlining/auxiliary/leaf.rs index d059a3d0a73..7b5679c3f4d 100644 --- a/tests/codegen-llvm/cross-crate-inlining/auxiliary/leaf.rs +++ b/tests/codegen-llvm/cross-crate-inlining/auxiliary/leaf.rs @@ -18,3 +18,8 @@ pub fn stem_fn() -> String { fn inner() -> String { String::from("test") } + +// This function's optimized MIR contains a call, but it is to an intrinsic. +pub fn leaf_with_intrinsic(a: &[u64; 2], b: &[u64; 2]) -> bool { + a == b +} diff --git a/tests/codegen-llvm/cross-crate-inlining/leaf-inlining.rs b/tests/codegen-llvm/cross-crate-inlining/leaf-inlining.rs index 37132312ca9..5e7912791ad 100644 --- a/tests/codegen-llvm/cross-crate-inlining/leaf-inlining.rs +++ b/tests/codegen-llvm/cross-crate-inlining/leaf-inlining.rs @@ -18,3 +18,10 @@ pub fn stem_outer() -> String { // CHECK: call {{.*}}stem_fn leaf::stem_fn() } + +// Check that we inline functions that call intrinsics +#[no_mangle] +pub fn leaf_with_intrinsic_outer(a: &[u64; 2], b: &[u64; 2]) -> bool { + // CHECK-NOT: call {{.*}}leaf_with_intrinsic + leaf::leaf_with_intrinsic(a, b) +} diff --git a/tests/codegen-llvm/default-visibility.rs b/tests/codegen-llvm/default-visibility.rs index 88ff9fee254..28238e5ef12 100644 --- a/tests/codegen-llvm/default-visibility.rs +++ b/tests/codegen-llvm/default-visibility.rs @@ -32,6 +32,7 @@ pub static tested_symbol: [u8; 6] = *b"foobar"; // INTERPOSABLE: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant // DEFAULT: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant +#[inline(never)] pub fn do_memcmp(left: &[u8], right: &[u8]) -> i32 { left.cmp(right) as i32 }