Repository navigation
[InlineCost] Never inline functions with incompatible target features - #205113
Conversation
|
@llvm/pr-subscribers-llvm-analysis @llvm/pr-subscribers-llvm-transforms Author: Nikita Popov (nikic) ChangesIf inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set. This will likely result in a crash during instruction selection. We tried this previously in d6f994a, but the change had to be reverted because the quality of our areInlineCompatible() hooks was very bad at the time, which resulted in inlining not happening in many cases where it was safe. I think we're in a much better position now. Most notably, we now have a default areInlineCompatible() implementation that actually does something sensible (#117493), and various other target-specific issues have been fixed as well (e.g. ARM's outdated feature whitelist in #169337). The one remaining bit I'm aware of is the overly conservative ABI check on x86, which I'm fixing in #205106. 2 Files Affected:
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index d975a93e9b1fd..8114dda51e54e 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -3089,9 +3089,7 @@ static bool functionsHaveCompatibleAttributes(
// object, and always returns the same object (which is overwritten on each
// GetTLI call). Therefore we copy the first result.
auto CalleeTLI = GetTLI(*Callee);
- return (IgnoreTTIInlineCompatible ||
- TTI.areInlineCompatible(Caller, Callee)) &&
- GetTLI(*Caller).areInlineCompatible(CalleeTLI,
+ return GetTLI(*Caller).areInlineCompatible(CalleeTLI,
InlineCallerSupersetNoBuiltin) &&
AttributeFuncs::areInlineCompatible(*Caller, *Callee);
}
@@ -3214,6 +3212,13 @@ std::optional<InlineResult> llvm::getAttributeBasedInliningDecision(
" address space");
}
+ // Inlining into a function with less target features is unsound, so enforce
+ // this even if alwaysinline is used.
+ Function *Caller = Call.getCaller();
+ if (!IgnoreTTIInlineCompatible &&
+ !CalleeTTI.areInlineCompatible(Caller, Callee))
+ return InlineResult::failure("conflicting target features");
+
// Calls to functions with always-inline attributes should be inlined
// whenever possible.
if (Call.hasFnAttr(Attribute::AlwaysInline)) {
@@ -3228,7 +3233,6 @@ std::optional<InlineResult> llvm::getAttributeBasedInliningDecision(
// Never inline functions with conflicting attributes (unless callee has
// always-inline attribute).
- Function *Caller = Call.getCaller();
if (!functionsHaveCompatibleAttributes(Caller, Callee, CalleeTTI, GetTLI))
return InlineResult::failure("conflicting attributes");
diff --git a/llvm/test/Transforms/Inline/X86/always-inline-features.ll b/llvm/test/Transforms/Inline/X86/always-inline-features.ll
new file mode 100644
index 0000000000000..500356471c9ae
--- /dev/null
+++ b/llvm/test/Transforms/Inline/X86/always-inline-features.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=inline < %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; Even with alwaysinline, the callee cannot be inlined, because the target
+; features are incompatible. Inlining it would result in a selection failure
+; crash.
+
+define <16 x float> @caller(<16 x float> %a, <16 x float> %b) {
+; CHECK-LABEL: define <16 x float> @caller(
+; CHECK-SAME: <16 x float> [[A:%.*]], <16 x float> [[B:%.*]]) {
+; CHECK-NEXT: [[RES:%.*]] = call <16 x float> @callee(<16 x float> [[A]], <16 x float> [[B]]) #[[ATTR2:[0-9]+]]
+; CHECK-NEXT: ret <16 x float> [[RES]]
+;
+ %res = call <16 x float> @callee(<16 x float> %a, <16 x float> %b) alwaysinline
+ ret <16 x float> %res
+}
+
+define <16 x float> @callee(<16 x float> %a, <16 x float> %b) #0 {
+; CHECK-LABEL: define <16 x float> @callee(
+; CHECK-SAME: <16 x float> [[A:%.*]], <16 x float> [[B:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[RES:%.*]] = call <16 x float> @llvm.x86.avx512.min.ps.512(<16 x float> [[A]], <16 x float> [[B]], i32 0)
+; CHECK-NEXT: ret <16 x float> [[RES]]
+;
+ %res = call <16 x float> @llvm.x86.avx512.min.ps.512(<16 x float> %a, <16 x float> %b, i32 0)
+ ret <16 x float> %res
+}
+
+declare <16 x float> @llvm.x86.avx512.min.ps.512(<16 x float>, <16 x float>, i32 immarg)
+
+attributes #0 = { "target-features"="+avx512bw,+avx512dq,+avx512f" }
|
boomanaiden154
left a comment
There was a problem hiding this comment.
I remember there still being some issues from #65152. Quickly looking over the AArch64 areInlineCompatible implementation, it seems like we still couldn't handle the case where we want to inline a v7a callee into a v8a caller.
There were also some calls for missed inlining on always_inline due to mismatched target features to be a frontend error/warning in clang (#65205 (comment)), like we already do for some cases. My plan was to try out implementing the warning and see how much fallout there was on our codebase, but I haven't gotten around to it yet.
I'll look into this one. I think that we may want to just completely ignore these ARM architecture features for inlining? As far as I can tell, these architecture features are not actually used for anything, they just exist to imply other features that matter (like HasVxxxOps).
I haven't looked very closely, but I think that one was just a bug in X86's areInlineCompatible? It shouldn't error in the frontend, because it's supposed to inline. The underlying issue (inline assembly affecting ABI checks) has been fixed. So I'm a bit unclear on what warning you actually want to implement? Do you want to promote optimization remarks for always inline failures to compiler warnings? |
I don't think we can just completely ignore them. From my understanding, it would still be illegal to inline a function marked
Yes, the specific case there was already fixed in the X86 TTI implementation.
Yes, that was (the beginnings of) the idea. We already do it in some cases (where clang can directly see the attributes and that they are trivially incompatible). It would make diagnosing potential fallout from this change much easier. Although the implementation might be tricky because we probably don't want to diagnose things like ICP promoting |
Yes, that's correct. However, this is already handled because As far as I can tell, the |
Ah, okay. In that case, just ignoring the |
1791e31 to
f18c5ae
Compare
f18c5ae to
40c9a43
Compare
|
@boomanaiden154 Do you still have concerns about this change? The specific ARM issue is now fixed, and more generally #205348 and followups moved us away from ad-hoc areInlineCompatible() implementations towards centralized handling in TableGen. I think the situation here is about as good as it's going to get now. I'd really like to land this before the LLVM 23 release branches. |
boomanaiden154
left a comment
There was a problem hiding this comment.
LGTM.
No objections on my end. It wouldn't hurt if frontends can emit a warning/error around this, but that's a nice to have that shouldn't block a correctness fix.
Thanks for pushing this through.
If inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set.
40c9a43 to
74e0633
Compare
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/45219 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/65/builds/36766 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/154/builds/33465 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/227/builds/3072 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/229/builds/2746 Here is the relevant piece of the build log for the reference |
…tions with incompatible target features" (#208079) Reverts llvm/llvm-project#192142 Now that llvm/llvm-project#205113 has landed, we will not inline functions with incompatible target features, even if the callee is marked `alwaysinline`, so we can promote calls to such functions while also removing the extra complexity from ICP.
…tions with incompatible target features" (#208079) Reverts llvm/llvm-project#192142 Now that llvm/llvm-project#205113 has landed, we will not inline functions with incompatible target features, even if the callee is marked `alwaysinline`, so we can promote calls to such functions while also removing the extra complexity from ICP.
…tions with incompatible target features" (#208079) Reverts llvm/llvm-project#192142 Now that llvm/llvm-project#205113 has landed, we will not inline functions with incompatible target features, even if the callee is marked `alwaysinline`, so we can promote calls to such functions while also removing the extra complexity from ICP.
…rget-feature-rust-lang#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` rust-lang#2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
…#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` #2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
…rget-feature-rust-lang#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` rust-lang#2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
…#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` #2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
…#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` #2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
…rget-feature-rust-lang#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` rust-lang#2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
Rollup merge of #162460 - Jamesbarford:feat/inline-always+target-feature-#2, r=nikic,traviscross Feat - `#[inline(always)] + #[target_feature(enable = "....")]` #2 This is broadly a resurrection of `#[inline(always)] + #[target_feature(enable = "...")]`. - Applies the `alwaysinline` attribute to the function definition. - `version < LLVM23` downgrades to an `inline` hint. - Remove the check for using [target features + inline always](compiler/rustc_codegen_ssa/src/codegen_attrs.rs) This should now be sound thanks to * llvm/llvm-project#205113 * llvm/llvm-project#209345 Therefore, it should be okay to just allow this combination. But we can also have a period of unstable experimentation if t-lang prefers. Reference PR; rust-lang/reference#2391 r? @RalfJung
If inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set. This will likely result in a crash during instruction selection.
We tried this previously in d6f994a, but the change had to be reverted because the quality of our areInlineCompatible() hooks was very bad at the time, which resulted in inlining not happening in many cases where it was safe.
I think we're in a much better position now. Most notably, we now have a default areInlineCompatible() implementation that actually does something sensible (#117493), inlining compatibility for target features is now specified in TableGen (#205348). Various target-specific issues have been fixed as well, e.g. ARM's overly strict feature whitelist (#205763) and X86's overly conservative ABI compatibility checks (#205106).