[llvm] [InlineCost] Never inline functions with incompatible target features (PR #205113)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 3 01:35:18 PDT 2026


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/205113

>From 2b1b39804d12b30ad130a836f6bf489870fd981d Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 22 Jun 2026 16:08:45 +0200
Subject: [PATCH 1/3] [InlineCost] Never inline functions with incompatible
 target features

If inlining is unsound due to incompatible target feature attributes,
we should not inline the call even if alwaysinline is set.
---
 llvm/lib/Analysis/InlineCost.cpp              | 12 ++++---
 .../Inline/X86/always-inline-features.ll      | 32 +++++++++++++++++++
 2 files changed, 40 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/Transforms/Inline/X86/always-inline-features.ll

diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index a2e321884e708..e2870afe234cf 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -3088,9 +3088,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);
 }
@@ -3213,6 +3211,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)) {
@@ -3227,7 +3232,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" }

>From 34bf6b9f15bd37f9e4ed6f0e318ede01c27944d2 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 1 Jul 2026 12:19:44 +0200
Subject: [PATCH 2/3] Remove unused TTI parameter

---
 llvm/lib/Analysis/InlineCost.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index e2870afe234cf..c3823931a7bcb 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -3081,7 +3081,7 @@ LLVM_DUMP_METHOD void InlineCostCallAnalyzer::dump() { print(dbgs()); }
 /// Test that there are no attribute conflicts between Caller and Callee
 ///        that prevent inlining.
 static bool functionsHaveCompatibleAttributes(
-    Function *Caller, Function *Callee, TargetTransformInfo &TTI,
+    Function *Caller, Function *Callee,
     function_ref<const TargetLibraryInfo &(Function &)> &GetTLI) {
   // Note that CalleeTLI must be a copy not a reference. The legacy pass manager
   // caches the most recently created TLI in the TargetLibraryInfoWrapperPass
@@ -3232,7 +3232,7 @@ std::optional<InlineResult> llvm::getAttributeBasedInliningDecision(
 
   // Never inline functions with conflicting attributes (unless callee has
   // always-inline attribute).
-  if (!functionsHaveCompatibleAttributes(Caller, Callee, CalleeTTI, GetTLI))
+  if (!functionsHaveCompatibleAttributes(Caller, Callee, GetTLI))
     return InlineResult::failure("conflicting attributes");
 
   // Flatten: inline all viable calls from flatten functions regardless of cost.

>From 40c9a4315abee275ebce79e1bfbb3077148d6e36 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 3 Jul 2026 10:34:52 +0200
Subject: [PATCH 3/3] Add release note

---
 llvm/docs/ReleaseNotes.md | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index cbec4d5b912d0..6c6ede5db2842 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -115,6 +115,9 @@ Makes programs 10x faster by doing Special New Thing.
   `target_cpu` properties. This resolves errors during LTO on some
   architectures.
 
+* `alwaysinline` no longer bypasses inlining compatibility checks based on
+  target features. Inlining will only be performed if it is safe to do so.
+
 ### Changes to LLVM infrastructure
 
 * Removed ``Constant::isZeroValue``. It was functionally identical to



More information about the llvm-commits mailing list