[llvm] [InlineCost] Never inline functions with incompatible target features (PR #205113)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 07:27:25 PDT 2026
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/205113
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 https://github.com/llvm/llvm-project/commit/d6f994acb3d545b80161e24ab742c9c69d4bbf33, 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 (https://github.com/llvm/llvm-project/pull/117493), and various other target-specific issues have been fixed as well (e.g. ARM's outdated feature whitelist in https://github.com/llvm/llvm-project/pull/169337). The one remaining bit I'm aware of is the overly conservative ABI check on x86, which I'm fixing in https://github.com/llvm/llvm-project/pull/205106.
>From 1791e319f6ae8a4e4ef03674d4b577c2f18a0aa9 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] [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 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" }
More information about the llvm-commits
mailing list