From 95b6747a9de4d30b328c3300a27622b15c1c83e5 Mon Sep 17 00:00:00 2001 From: Jamie Cunliffe Date: Mon, 27 Oct 2025 13:49:23 +0000 Subject: [PATCH 1/4] Broken test to show alwaysinline attribute not being applied --- tests/codegen-llvm/inline-always-callsite.rs | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/codegen-llvm/inline-always-callsite.rs diff --git a/tests/codegen-llvm/inline-always-callsite.rs b/tests/codegen-llvm/inline-always-callsite.rs new file mode 100644 index 0000000000000..3000ceb1bbc4f --- /dev/null +++ b/tests/codegen-llvm/inline-always-callsite.rs @@ -0,0 +1,37 @@ +//@ add-core-stubs +//@ compile-flags: --target aarch64-unknown-linux-gnu -Zinline-mir=no -C no-prepopulate-passes +//@ needs-llvm-components: aarch64 + +#![crate_type = "lib"] +#![feature(no_core, lang_items, target_feature_inline_always)] +#![no_core] + +extern crate minicore; +use minicore::*; + +#[inline(always)] +#[target_feature(enable = "neon")] +#[no_mangle] +pub fn single_target_feature() -> i32 { + 42 +} + +#[inline(always)] +#[target_feature(enable = "neon,i8mm")] +#[no_mangle] +// CHECK: define noundef i32 @multiple_target_features() unnamed_addr #1 { +pub fn multiple_target_features() -> i32 { + // CHECK: %_0 = call noundef i32 @single_target_feature() #3 + single_target_feature() +} + +#[no_mangle] +// CHECK: define noundef i32 @inherits_from_global() unnamed_addr #2 { +pub fn inherits_from_global() -> i32 { + unsafe { + // CHECK: %_0 = call noundef i32 @single_target_feature() #3 + single_target_feature() + } +} + +// CHECK: attributes #3 = { nounwind } From 0cf4fd651c281c8fb3985640ecb6b4482f38eceb Mon Sep 17 00:00:00 2001 From: Jamie Cunliffe Date: Mon, 27 Oct 2025 14:09:17 +0000 Subject: [PATCH 2/4] Fix issue with callsite inline attribute not being applied sometimes. If the calling function had more target features enabled than the callee than the attribute wasn't being applied as the arguments for the check had been swapped round. Also includes target features that are part of the global set as the warning was checking those but when adding the attribute they were not checked. Add a codegen-llvm test to check that the attribute is actually applied as previously only the warning was being checked. --- compiler/rustc_codegen_llvm/src/builder.rs | 10 +++++++--- tests/codegen-llvm/inline-always-callsite.rs | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index b10a1282f4dd0..3442abece2f76 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -16,7 +16,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef; use rustc_codegen_ssa::traits::*; use rustc_data_structures::small_c_str::SmallCStr; use rustc_hir::def_id::DefId; -use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; +use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature, TargetFeatureKind}; use rustc_middle::ty::layout::{ FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers, TyAndLayout, @@ -1405,14 +1405,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { // Attributes on the function definition being called let fn_defn_attrs = self.cx.tcx.codegen_fn_attrs(instance.def_id()); if let Some(fn_call_attrs) = fn_call_attrs - && !fn_call_attrs.target_features.is_empty() // If there is an inline attribute and a target feature that matches // we will add the attribute to the callsite otherwise we'll omit // this and not add the attribute to prevent soundness issues. && let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, instance) && self.cx.tcx.is_target_feature_call_safe( - &fn_call_attrs.target_features, &fn_defn_attrs.target_features, + &fn_call_attrs.target_features.iter().cloned().chain( + self.cx.tcx.sess.target_features.iter().map(|feat| TargetFeature { + name: *feat, + kind: TargetFeatureKind::Implied, + }) + ).collect::>(), ) { attributes::apply_to_callsite( diff --git a/tests/codegen-llvm/inline-always-callsite.rs b/tests/codegen-llvm/inline-always-callsite.rs index 3000ceb1bbc4f..229c85049d782 100644 --- a/tests/codegen-llvm/inline-always-callsite.rs +++ b/tests/codegen-llvm/inline-always-callsite.rs @@ -1,4 +1,4 @@ -//@ add-core-stubs +//@ add-minicore //@ compile-flags: --target aarch64-unknown-linux-gnu -Zinline-mir=no -C no-prepopulate-passes //@ needs-llvm-components: aarch64 @@ -34,4 +34,4 @@ pub fn inherits_from_global() -> i32 { } } -// CHECK: attributes #3 = { nounwind } +// CHECK: attributes #3 = { alwaysinline nounwind } From 0490b5000c74fa128d6581267c811704ee0dcd19 Mon Sep 17 00:00:00 2001 From: Jamie Cunliffe Date: Wed, 19 Nov 2025 11:26:53 +0000 Subject: [PATCH 3/4] Review comments --- compiler/rustc_middle/src/ty/context.rs | 2 ++ tests/codegen-llvm/inline-always-callsite.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 3092c6e76c030..0cd36d5e971de 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2090,6 +2090,8 @@ impl<'tcx> TyCtxt<'tcx> { self.sess.dcx() } + /// Checks to see if the caller (`body_features`) has all the features required by the callee + /// (`callee_features`). pub fn is_target_feature_call_safe( self, callee_features: &[TargetFeature], diff --git a/tests/codegen-llvm/inline-always-callsite.rs b/tests/codegen-llvm/inline-always-callsite.rs index 229c85049d782..2821c606aa0ee 100644 --- a/tests/codegen-llvm/inline-always-callsite.rs +++ b/tests/codegen-llvm/inline-always-callsite.rs @@ -34,4 +34,8 @@ pub fn inherits_from_global() -> i32 { } } +// Attribute #3 requires the alwaysinline attribute, the alwaysinline attribute is not emitted on a +// function definition when target features are present, rather it will be moved onto the function +// call, if the features match up. +// // CHECK: attributes #3 = { alwaysinline nounwind } From ae078da9c24f3cc2accb689e837fab8512fd732d Mon Sep 17 00:00:00 2001 From: Jamie Cunliffe Date: Wed, 19 Nov 2025 11:26:53 +0000 Subject: [PATCH 4/4] Fix test failure --- tests/codegen-llvm/inline-always-callsite.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/codegen-llvm/inline-always-callsite.rs b/tests/codegen-llvm/inline-always-callsite.rs index 2821c606aa0ee..ad133391ce267 100644 --- a/tests/codegen-llvm/inline-always-callsite.rs +++ b/tests/codegen-llvm/inline-always-callsite.rs @@ -19,17 +19,17 @@ pub fn single_target_feature() -> i32 { #[inline(always)] #[target_feature(enable = "neon,i8mm")] #[no_mangle] -// CHECK: define noundef i32 @multiple_target_features() unnamed_addr #1 { +// CHECK: define{{( noundef)?}} i32 @multiple_target_features() unnamed_addr #1 { pub fn multiple_target_features() -> i32 { - // CHECK: %_0 = call noundef i32 @single_target_feature() #3 + // CHECK: %_0 = call{{( noundef)?}} i32 @single_target_feature() #3 single_target_feature() } #[no_mangle] -// CHECK: define noundef i32 @inherits_from_global() unnamed_addr #2 { +// CHECK: define{{( noundef)?}} i32 @inherits_from_global() unnamed_addr #2 { pub fn inherits_from_global() -> i32 { unsafe { - // CHECK: %_0 = call noundef i32 @single_target_feature() #3 + // CHECK: %_0 = call{{( noundef)?}} i32 @single_target_feature() #3 single_target_feature() } }