You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #145932 - JamieCunliffe:target-feature-inlining, r=jackh726
Allow `inline(always)` with a target feature behind a unstable feature `target_feature_inline_always`.
Rather than adding the inline always attribute to the function definition, we add it to the callsite. We can then check that the target features match and that the call would be safe to inline. If the function isn't inlined due to a mismatch, we emit a warning informing the user that the function can't be inlined due to the target feature mismatch.
See tracking issue #145574
Copy file name to clipboardExpand all lines: compiler/rustc_lint_defs/src/builtin.rs
+54Lines changed: 54 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5141,3 +5141,57 @@ declare_lint! {
5141
5141
"detects tail calls of functions marked with `#[track_caller]`",
5142
5142
@feature_gate = explicit_tail_calls;
5143
5143
}
5144
+
declare_lint!{
5145
+
/// The `inline_always_mismatching_target_features` lint will trigger when a
5146
+
/// function with the `#[inline(always)]` and `#[target_feature(enable = "...")]`
5147
+
/// attributes is called and cannot be inlined due to missing target features in the caller.
5148
+
///
5149
+
/// ### Example
5150
+
///
5151
+
/// ```rust,ignore (fails on x86_64)
5152
+
/// #[inline(always)]
5153
+
/// #[target_feature(enable = "fp16")]
5154
+
/// unsafe fn callee() {
5155
+
/// // operations using fp16 types
5156
+
/// }
5157
+
///
5158
+
/// // Caller does not enable the required target feature
5159
+
/// fn caller() {
5160
+
/// unsafe { callee(); }
5161
+
/// }
5162
+
///
5163
+
/// fn main() {
5164
+
/// caller();
5165
+
/// }
5166
+
/// ```
5167
+
///
5168
+
/// This will produce:
5169
+
///
5170
+
/// ```text
5171
+
/// warning: call to `#[inline(always)]`-annotated `callee` requires the same target features. Function will not have `alwaysinline` attribute applied
5172
+
/// --> $DIR/builtin.rs:5192:14
5173
+
/// |
5174
+
/// 10 | unsafe { callee(); }
5175
+
/// | ^^^^^^^^
5176
+
/// |
5177
+
/// note: `fp16` target feature enabled in `callee` here but missing from `caller`
5178
+
/// --> $DIR/builtin.rs:5185:1
5179
+
/// |
5180
+
/// 3 | #[target_feature(enable = "fp16")]
5181
+
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5182
+
/// 4 | unsafe fn callee() {
5183
+
/// | ------------------
5184
+
/// = note: `#[warn(inline_always_mismatching_target_features)]` on by default
5185
+
/// warning: 1 warning emitted
5186
+
/// ```
5187
+
///
5188
+
/// ### Explanation
5189
+
///
5190
+
/// Inlining a function with a target feature attribute into a caller that
5191
+
/// lacks the corresponding target feature can lead to unsound behavior.
5192
+
/// LLVM may select the wrong instructions or registers, or reorder
5193
+
/// operations, potentially resulting in runtime errors.
5194
+
pubINLINE_ALWAYS_MISMATCHING_TARGET_FEATURES,
5195
+
Warn,
5196
+
r#"detects when a function annotated with `#[inline(always)]` and `#[target_feature(enable = "..")]` is inlined into a caller without the required target feature"#,
0 commit comments