[clang] [llvm] [Clang][X86] Resolve target_clones calls through GlobalOpt (PR #230278)
Oleksandr Kulkov via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 03:49:00 PST 2026
https://github.com/adamant-pwn updated https://github.com/llvm/llvm-project/pull/230278
>From 9bf19c5b5eeaec759a359ee416e0652361fd8d88 Mon Sep 17 00:00:00 2001
From: Oleksandr Kulkov <adamant.pwn at gmail.com>
Date: Mon, 26 Oct 2026 18:14:43 +0100
Subject: [PATCH 1/3] [Clang][X86] Add baseline tests for target_clones call
resolution
Record the existing behavior before the implementation change.
Assisted-by: OpenAI Codex.
---
.../test/CodeGen/attr-target-clones-inline.c | 77 +++++++++++
.../GlobalOpt/X86/resolve-fmv-ifunc.ll | 126 ++++++++++++++++++
2 files changed, 203 insertions(+)
create mode 100644 clang/test/CodeGen/attr-target-clones-inline.c
create mode 100644 llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll
diff --git a/clang/test/CodeGen/attr-target-clones-inline.c b/clang/test/CodeGen/attr-target-clones-inline.c
new file mode 100644
index 00000000000000..8a5b8ffa0d2c52
--- /dev/null
+++ b/clang/test/CodeGen/attr-target-clones-inline.c
@@ -0,0 +1,77 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefix=ATTR
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O2 -emit-llvm %s -o - | FileCheck %s --check-prefix=OPT
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefix=ATTR
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -O2 -emit-llvm %s -o - | FileCheck %s --check-prefixes=OPT,CXX
+// RUN: %clang_cc1 -triple x86_64-windows-pc -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefix=WINDOWS
+
+// WINDOWS-NOT: "fmv-features"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+__attribute__((target_clones("default,sse4.2,avx2")))
+int callee(int x) { return x + 1; }
+
+// Before resolution, calls through matching clone families retain IFUNC dispatch.
+__attribute__((target_clones("avx2,default,sse4.2")))
+int caller(int x) {
+ int (*p)(int) = callee;
+ return p(x);
+}
+
+// OPT-LABEL: define{{.*}} i32 @caller.avx2.
+// OPT: call{{.*}} i32 @callee(
+// OPT: ret i32
+// OPT-LABEL: define{{.*}} i32 @caller.default.
+// OPT: call{{.*}} i32 @callee(
+// OPT: ret i32
+// OPT-LABEL: define{{.*}} i32 @caller.sse4.2.
+// OPT: call{{.*}} i32 @callee(
+// OPT: ret i32
+
+// The runtime resolver can still choose callee.avx2 from either caller here.
+__attribute__((target_clones("default,sse4.2")))
+int limited(int x) { return callee(x); }
+
+// OPT-LABEL: define{{.*}} i32 @limited.default.
+// OPT: call i32 @callee(
+// OPT-LABEL: define{{.*}} i32 @limited.sse4.2.
+// OPT: call i32 @callee(
+
+__attribute__((target_clones("default,arch=x86-64-v3")))
+double widen(_Float16 x) { return (double)x; }
+
+__attribute__((target_clones("default,arch=x86-64-v3")))
+double wide_caller(_Float16 x) { return widen(x); }
+
+// OPT-LABEL: define{{.*}} double @wide_caller.default.
+// OPT: call double @widen(
+// OPT: ret double
+// OPT-LABEL: define{{.*}} double @wide_caller.arch_x86-64-v3.
+// OPT: call double @widen(
+// OPT: ret double
+
+// CPU-model predicates are deliberately left to the runtime resolver.
+__attribute__((target_clones("default,arch=haswell")))
+int cpu_callee(int x) { return x + 2; }
+
+__attribute__((target_clones("default,arch=haswell")))
+int cpu_caller(int x) { return cpu_callee(x); }
+
+// OPT-LABEL: define{{.*}} i32 @cpu_caller.default.
+// OPT: call i32 @cpu_callee(
+// OPT-LABEL: define{{.*}} i32 @cpu_caller.arch_haswell.
+// OPT: call i32 @cpu_callee(
+
+#ifdef __cplusplus
+}
+// There is no enclosing FunctionDecl while emitting a global initializer.
+int dynamic_init = callee(0);
+// CXX-LABEL: define internal void @_GLOBAL__sub_I_
+// CXX: call i32 @callee(
+#endif
+
+// Dispatch predicate metadata is not emitted before the implementation change.
+// ATTR-NOT: "fmv-features"
diff --git a/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll
new file mode 100644
index 00000000000000..902907bb7aa693
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll
@@ -0,0 +1,126 @@
+; NOTE: This test uses separate bits for the runtime predicates, including the
+; x86-64-v3 level. They are not expanded sets of code-generation features.
+; REQUIRES: x86-registered-target
+; RUN: opt -passes=globalopt -S %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+ at features = external global i32
+ at callee = weak_odr ifunc i32 (i32), ptr @callee.resolver
+ at caller = weak_odr ifunc i32 (i32), ptr @caller.resolver
+ at limited = weak_odr ifunc i32 (i32), ptr @limited.resolver
+ at preemptible = weak ifunc i32 (i32), ptr @callee.resolver
+ at cpu_callee = weak_odr ifunc i32 (i32), ptr @cpu_callee.resolver
+
+define ptr @callee.resolver() {
+ %features = load i32, ptr @features
+ %avx2 = icmp ne i32 %features, 0
+ %result = select i1 %avx2, ptr @callee.avx2, ptr @callee.default
+ ret ptr %result
+}
+
+define ptr @caller.resolver() {
+ %features = load i32, ptr @features
+ %avx2 = icmp ne i32 %features, 0
+ %result = select i1 %avx2, ptr @caller.avx2, ptr @caller.default
+ ret ptr %result
+}
+
+define ptr @limited.resolver() {
+ ret ptr @limited.default
+}
+
+define ptr @cpu_callee.resolver() {
+ %features = load i32, ptr @features
+ %cpu = icmp ne i32 %features, 0
+ %result = select i1 %cpu, ptr @callee.cpu, ptr @callee.default
+ ret ptr %result
+}
+
+declare i32 @callee.avx2(i32) #1
+declare i32 @callee.default(i32) #0
+declare i32 @callee.cpu(i32) #2
+
+define i32 @caller.avx2(i32 %x) #1 {
+; CHECK-LABEL: define i32 @caller.avx2(
+; CHECK: call i32 @callee(
+; CHECK: call i32 @preemptible(
+; CHECK: call i32 @cpu_callee(
+ %a = call i32 @callee(i32 %x)
+ %b = call i32 @preemptible(i32 %a)
+ %c = call i32 @cpu_callee(i32 %b)
+ ret i32 %c
+}
+
+define i32 @caller.default(i32 %x) #0 {
+; CHECK-LABEL: define i32 @caller.default(
+; CHECK: call i32 @callee(
+ %a = call i32 @callee(i32 %x)
+ ret i32 %a
+}
+
+define i32 @limited.default(i32 %x) #0 {
+; CHECK-LABEL: define i32 @limited.default(
+; CHECK: call i32 @callee(
+ %a = call i32 @callee(i32 %x)
+ ret i32 %a
+}
+
+define i32 @ordinary(i32 %x) #3 {
+; CHECK-LABEL: define i32 @ordinary(
+; CHECK: call i32 @callee(
+ %a = call i32 @callee(i32 %x)
+ ret i32 %a
+}
+
+ at levels = weak_odr ifunc i32 (i32), ptr @levels.resolver
+ at level_caller = weak_odr ifunc i32 (i32), ptr @level_caller.resolver
+
+define ptr @levels.resolver() {
+ %features = load i32, ptr @features
+ %v3 = icmp eq i32 %features, 2
+ %avx2 = icmp ne i32 %features, 0
+ %low = select i1 %avx2, ptr @level.avx2, ptr @level.default
+ %result = select i1 %v3, ptr @level.v3, ptr %low
+ ret ptr %result
+}
+
+define ptr @level_caller.resolver() {
+ %features = load i32, ptr @features
+ %v3 = icmp eq i32 %features, 2
+ %avx2 = icmp ne i32 %features, 0
+ %low = select i1 %avx2, ptr @level_caller.avx2, ptr @level_caller.default
+ %result = select i1 %v3, ptr @level_caller.v3, ptr %low
+ ret ptr %result
+}
+
+declare i32 @level.default(i32) #0
+declare i32 @level.avx2(i32) #1
+declare i32 @level.v3(i32) #4
+
+define i32 @level_caller.default(i32 %x) #0 {
+; CHECK-LABEL: define i32 @level_caller.default(
+; CHECK: call i32 @levels(
+ %a = call i32 @levels(i32 %x)
+ ret i32 %a
+}
+
+define i32 @level_caller.avx2(i32 %x) #1 {
+; CHECK-LABEL: define i32 @level_caller.avx2(
+; CHECK: call i32 @levels(
+ %a = call i32 @levels(i32 %x)
+ ret i32 %a
+}
+
+define i32 @level_caller.v3(i32 %x) #4 {
+; CHECK-LABEL: define i32 @level_caller.v3(
+; CHECK: call i32 @levels(
+ %a = call i32 @levels(i32 %x)
+ ret i32 %a
+}
+
+attributes #0 = { "fmv-features" }
+attributes #1 = { "fmv-features"="avx2" }
+attributes #2 = { "target-cpu"="haswell" }
+attributes #3 = { "target-features"="+avx2" }
+attributes #4 = { "fmv-features"="x86-64-v3" }
>From 53f1a73d04327c305477fb4fa1d0e5df76df1611 Mon Sep 17 00:00:00 2001
From: Oleksandr Kulkov <adamant.pwn at gmail.com>
Date: Mon, 26 Oct 2026 18:14:43 +0100
Subject: [PATCH 2/3] [Clang][X86] Resolve target_clones calls through
GlobalOpt
Apply the implementation and update expectations from the preceding
baseline tests. The final tree is unchanged from the prior PR tip.
Assisted-by: OpenAI Codex.
---
clang/lib/Basic/Targets/X86.cpp | 31 +----------------
clang/lib/CodeGen/CodeGenModule.cpp | 12 ++++++-
.../test/CodeGen/attr-target-clones-inline.c | 24 +++++++++-----
.../llvm/TargetParser/X86TargetParser.h | 3 ++
.../lib/Target/X86/X86TargetTransformInfo.cpp | 25 ++++++++++++++
llvm/lib/Target/X86/X86TargetTransformInfo.h | 4 +++
llvm/lib/TargetParser/X86TargetParser.cpp | 33 +++++++++++++++++++
.../GlobalOpt/X86/resolve-fmv-ifunc.ll | 10 +++---
8 files changed, 98 insertions(+), 44 deletions(-)
diff --git a/clang/lib/Basic/Targets/X86.cpp b/clang/lib/Basic/Targets/X86.cpp
index 670c85a769d71b..718ac419f6c817 100644
--- a/clang/lib/Basic/Targets/X86.cpp
+++ b/clang/lib/Basic/Targets/X86.cpp
@@ -1349,37 +1349,8 @@ bool X86TargetInfo::validateCpuSupports(StringRef FeatureStr) const {
.Default(false);
}
-static llvm::X86::ProcessorFeatures getFeature(StringRef Name) {
- return llvm::StringSwitch<llvm::X86::ProcessorFeatures>(Name)
-#define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY, ABI_VALUE) \
- .Case(STR, llvm::X86::FEATURE_##ENUM)
-
-#include "llvm/TargetParser/X86TargetParser.def"
- ;
- // Note, this function should only be used after ensuring the value is
- // correct, so it asserts if the value is out of range.
-}
-
llvm::APInt X86TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
- auto getPriority = [](StringRef Feature) -> unsigned {
- // Valid CPUs have a 'key feature' that compares just better than its key
- // feature.
- using namespace llvm::X86;
- CPUKind Kind = parseArchX86(Feature);
- if (Kind != CK_None) {
- ProcessorFeatures KeyFeature = getKeyFeature(Kind);
- return (getFeaturePriority(KeyFeature) << 1) + 1;
- }
- // Now we know we have a feature, so get its priority and shift it a few so
- // that we have sufficient room for the CPUs (above).
- return getFeaturePriority(getFeature(Feature)) << 1;
- };
-
- unsigned Priority = 0;
- for (StringRef Feature : Features)
- if (!Feature.empty())
- Priority = std::max(Priority, getPriority(Feature));
- return llvm::APInt(32, Priority);
+ return llvm::APInt(32, llvm::X86::getFMVPriority(Features));
}
bool X86TargetInfo::validateCPUSpecificCPUDispatch(StringRef Name) const {
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 62ef11a697b1c2..3e71525027b1c0 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3533,7 +3533,7 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
AddedAttr = true;
}
}
- // Add metadata for AArch64 Function Multi Versioning.
+ // Add metadata for Function Multi Versioning.
if (getTarget().getTriple().isAArch64()) {
llvm::SmallVector<StringRef, 8> Feats;
bool IsDefault = false;
@@ -3556,6 +3556,16 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
Attrs.addAttribute("fmv-features", FMVFeatures.substr(1));
AddedAttr = true;
}
+ } else if (getTarget().getTriple().isX86() && getTarget().supportsIFunc() &&
+ TC) {
+ auto Arch = TC->getX86Architecture(GD.getMultiVersionIndex());
+ // CPU-model checks cannot be represented by feature masks.
+ if (!Arch || Arch->starts_with("x86-64")) {
+ StringRef Feature =
+ Arch.value_or(TC->getFeatureStr(GD.getMultiVersionIndex()));
+ Attrs.addAttribute("fmv-features", Feature == "default" ? "" : Feature);
+ AddedAttr = true;
+ }
}
return AddedAttr;
}
diff --git a/clang/test/CodeGen/attr-target-clones-inline.c b/clang/test/CodeGen/attr-target-clones-inline.c
index 8a5b8ffa0d2c52..0e4a930a408be1 100644
--- a/clang/test/CodeGen/attr-target-clones-inline.c
+++ b/clang/test/CodeGen/attr-target-clones-inline.c
@@ -14,7 +14,7 @@ extern "C" {
__attribute__((target_clones("default,sse4.2,avx2")))
int callee(int x) { return x + 1; }
-// Before resolution, calls through matching clone families retain IFUNC dispatch.
+// Reordering versions and calling through a local pointer still allow inlining.
__attribute__((target_clones("avx2,default,sse4.2")))
int caller(int x) {
int (*p)(int) = callee;
@@ -22,13 +22,16 @@ int caller(int x) {
}
// OPT-LABEL: define{{.*}} i32 @caller.avx2.
-// OPT: call{{.*}} i32 @callee(
+// OPT-NOT: call
+// OPT: add nsw i32
// OPT: ret i32
// OPT-LABEL: define{{.*}} i32 @caller.default.
-// OPT: call{{.*}} i32 @callee(
+// OPT-NOT: call
+// OPT: add nsw i32
// OPT: ret i32
// OPT-LABEL: define{{.*}} i32 @caller.sse4.2.
-// OPT: call{{.*}} i32 @callee(
+// OPT-NOT: call
+// OPT: add nsw i32
// OPT: ret i32
// The runtime resolver can still choose callee.avx2 from either caller here.
@@ -47,10 +50,12 @@ __attribute__((target_clones("default,arch=x86-64-v3")))
double wide_caller(_Float16 x) { return widen(x); }
// OPT-LABEL: define{{.*}} double @wide_caller.default.
-// OPT: call double @widen(
+// OPT-NOT: call
+// OPT: fpext half
// OPT: ret double
// OPT-LABEL: define{{.*}} double @wide_caller.arch_x86-64-v3.
-// OPT: call double @widen(
+// OPT-NOT: call
+// OPT: fpext half
// OPT: ret double
// CPU-model predicates are deliberately left to the runtime resolver.
@@ -73,5 +78,8 @@ int dynamic_init = callee(0);
// CXX: call i32 @callee(
#endif
-// Dispatch predicate metadata is not emitted before the implementation change.
-// ATTR-NOT: "fmv-features"
+// Dispatch predicates, rather than the expanded ISA features, describe clones.
+// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features" {{.*}} }
+// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features"="sse4.2" {{.*}} }
+// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features"="avx2" {{.*}} }
+// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features"="x86-64-v3" {{.*}} }
diff --git a/llvm/include/llvm/TargetParser/X86TargetParser.h b/llvm/include/llvm/TargetParser/X86TargetParser.h
index 4552287507fd3c..7ef2f2c26a2023 100644
--- a/llvm/include/llvm/TargetParser/X86TargetParser.h
+++ b/llvm/include/llvm/TargetParser/X86TargetParser.h
@@ -177,6 +177,9 @@ LLVM_ABI std::array<uint32_t, 4>
getCpuSupportsMask(ArrayRef<StringRef> FeatureStrs);
LLVM_ABI unsigned getFeaturePriority(ProcessorFeatures Feat);
+/// Return the runtime function-multiversioning priority of features or CPUs.
+LLVM_ABI unsigned getFMVPriority(ArrayRef<StringRef> Features);
+
} // namespace X86
} // namespace llvm
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index b1bac7fe4b25a2..1122c1ebe823f5 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -57,6 +57,7 @@
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/TargetParser/X86TargetParser.h"
#include <optional>
using namespace llvm;
@@ -69,6 +70,30 @@ using namespace llvm;
//
//===----------------------------------------------------------------------===//
+static SmallVector<StringRef, 8> getFMVFeatures(const Function &F) {
+ SmallVector<StringRef, 8> Features;
+ F.getFnAttribute("fmv-features")
+ .getValueAsString()
+ .split(Features, ",", -1, false);
+ return Features;
+}
+
+APInt X86TTIImpl::getFeatureMask(const Function &F) const {
+ auto Words = X86::getCpuSupportsMask(getFMVFeatures(F));
+ APInt Mask(32 * Words.size(), 0);
+ for (auto [I, Word] : enumerate(Words))
+ Mask |= APInt(Mask.getBitWidth(), Word) << (32 * I);
+ return Mask;
+}
+
+APInt X86TTIImpl::getPriorityMask(const Function &F) const {
+ return APInt(32, X86::getFMVPriority(getFMVFeatures(F)));
+}
+
+bool X86TTIImpl::isMultiversionedFunction(const Function &F) const {
+ return F.hasFnAttribute("fmv-features");
+}
+
// Helper struct to store/access costs for each cost kind.
// TODO: Move this to allow other targets to use it?
struct CostKindCosts {
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.h b/llvm/lib/Target/X86/X86TargetTransformInfo.h
index 160a71e1affbb4..5df3098703f0a0 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.h
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.h
@@ -41,6 +41,10 @@ class X86TTIImpl final : public BasicTTIImplBase<X86TTIImpl> {
: BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),
TLI(ST->getTargetLowering()) {}
+ APInt getFeatureMask(const Function &F) const override;
+ APInt getPriorityMask(const Function &F) const override;
+ bool isMultiversionedFunction(const Function &F) const override;
+
/// \name Scalar TTI Implementations
/// @{
TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
diff --git a/llvm/lib/TargetParser/X86TargetParser.cpp b/llvm/lib/TargetParser/X86TargetParser.cpp
index 384687702c7c25..0b2aef130432f0 100644
--- a/llvm/lib/TargetParser/X86TargetParser.cpp
+++ b/llvm/lib/TargetParser/X86TargetParser.cpp
@@ -829,3 +829,36 @@ unsigned llvm::X86::getFeaturePriority(ProcessorFeatures Feat) {
llvm_unreachable("No Feature Priority for non-CPUSupports Features");
}
}
+
+static llvm::X86::ProcessorFeatures getFeature(StringRef Name) {
+ return llvm::StringSwitch<llvm::X86::ProcessorFeatures>(Name)
+#define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY, ABI_VALUE) \
+ .Case(STR, llvm::X86::FEATURE_##ENUM)
+
+#include "llvm/TargetParser/X86TargetParser.def"
+ ;
+ // Note, this function should only be used after ensuring the value is
+ // correct, so it asserts if the value is out of range.
+}
+
+unsigned llvm::X86::getFMVPriority(ArrayRef<StringRef> Features) {
+ auto getPriority = [](StringRef Feature) -> unsigned {
+ // Valid CPUs have a 'key feature' that compares just better than its key
+ // feature.
+ using namespace llvm::X86;
+ CPUKind Kind = parseArchX86(Feature);
+ if (Kind != CK_None) {
+ ProcessorFeatures KeyFeature = getKeyFeature(Kind);
+ return (getFeaturePriority(KeyFeature) << 1) + 1;
+ }
+ // Now we know we have a feature, so get its priority and shift it a few so
+ // that we have sufficient room for the CPUs (above).
+ return getFeaturePriority(getFeature(Feature)) << 1;
+ };
+
+ unsigned Priority = 0;
+ for (StringRef Feature : Features)
+ if (!Feature.empty())
+ Priority = std::max(Priority, getPriority(Feature));
+ return Priority;
+}
diff --git a/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll
index 902907bb7aa693..b78b3644105442 100644
--- a/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll
+++ b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-ifunc.ll
@@ -43,7 +43,7 @@ declare i32 @callee.cpu(i32) #2
define i32 @caller.avx2(i32 %x) #1 {
; CHECK-LABEL: define i32 @caller.avx2(
-; CHECK: call i32 @callee(
+; CHECK: call i32 @callee.avx2(
; CHECK: call i32 @preemptible(
; CHECK: call i32 @cpu_callee(
%a = call i32 @callee(i32 %x)
@@ -54,7 +54,7 @@ define i32 @caller.avx2(i32 %x) #1 {
define i32 @caller.default(i32 %x) #0 {
; CHECK-LABEL: define i32 @caller.default(
-; CHECK: call i32 @callee(
+; CHECK: call i32 @callee.default(
%a = call i32 @callee(i32 %x)
ret i32 %a
}
@@ -100,21 +100,21 @@ declare i32 @level.v3(i32) #4
define i32 @level_caller.default(i32 %x) #0 {
; CHECK-LABEL: define i32 @level_caller.default(
-; CHECK: call i32 @levels(
+; CHECK: call i32 @level.default(
%a = call i32 @levels(i32 %x)
ret i32 %a
}
define i32 @level_caller.avx2(i32 %x) #1 {
; CHECK-LABEL: define i32 @level_caller.avx2(
-; CHECK: call i32 @levels(
+; CHECK: call i32 @level.avx2(
%a = call i32 @levels(i32 %x)
ret i32 %a
}
define i32 @level_caller.v3(i32 %x) #4 {
; CHECK-LABEL: define i32 @level_caller.v3(
-; CHECK: call i32 @levels(
+; CHECK: call i32 @level.v3(
%a = call i32 @levels(i32 %x)
ret i32 %a
}
>From 1861baf78e5d1f5505914e77f521c4f2853c9bd0 Mon Sep 17 00:00:00 2001
From: Oleksandr Kulkov <adamant.pwn at gmail.com>
Date: Fri, 6 Nov 2026 12:19:14 +0100
Subject: [PATCH 3/3] [Clang][X86] Separate target_clones attribute and
optimizer tests
Bind frontend checks to each clone and verify CPU-model clones lack fmv-features. Move optimizer coverage to LLVM IR and clarify runtime-dispatch comments.
Assisted-by: OpenAI Codex
---
clang/lib/CodeGen/CodeGenModule.cpp | 7 +-
.../test/CodeGen/attr-target-clones-inline.c | 77 +++---
.../GlobalOpt/X86/target-clones-inline.ll | 236 ++++++++++++++++++
3 files changed, 273 insertions(+), 47 deletions(-)
create mode 100644 llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 3e71525027b1c0..cc6c3700ce05e6 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3558,8 +3558,11 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
}
} else if (getTarget().getTriple().isX86() && getTarget().supportsIFunc() &&
TC) {
- auto Arch = TC->getX86Architecture(GD.getMultiVersionIndex());
- // CPU-model checks cannot be represented by feature masks.
+ std::optional<StringRef> Arch =
+ TC->getX86Architecture(GD.getMultiVersionIndex());
+ // A CPU-model predicate such as arch=haswell tests CPU identity, not just
+ // supported ISA features. Leave it to runtime dispatch: fmv-features only
+ // represents feature predicates and x86-64 ISA levels.
if (!Arch || Arch->starts_with("x86-64")) {
StringRef Feature =
Arch.value_or(TC->getFeatureStr(GD.getMultiVersionIndex()));
diff --git a/clang/test/CodeGen/attr-target-clones-inline.c b/clang/test/CodeGen/attr-target-clones-inline.c
index 0e4a930a408be1..a98b1af4ae05a1 100644
--- a/clang/test/CodeGen/attr-target-clones-inline.c
+++ b/clang/test/CodeGen/attr-target-clones-inline.c
@@ -1,12 +1,27 @@
// REQUIRES: x86-registered-target
// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefix=ATTR
-// RUN: %clang_cc1 -triple x86_64-linux-gnu -O2 -emit-llvm %s -o - | FileCheck %s --check-prefix=OPT
-// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefix=ATTR
-// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -O2 -emit-llvm %s -o - | FileCheck %s --check-prefixes=OPT,CXX
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefixes=ATTR,INIT
// RUN: %clang_cc1 -triple x86_64-windows-pc -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s --check-prefix=WINDOWS
// WINDOWS-NOT: "fmv-features"
+// ATTR: define{{.*}} i32 @callee.default.2({{.*}}) #[[DEFAULT:[0-9]+]]
+// ATTR: define{{.*}} i32 @callee.sse4.2.0({{.*}}) #[[SSE42:[0-9]+]]
+// ATTR: define{{.*}} i32 @callee.avx2.1({{.*}}) #[[AVX2:[0-9]+]]
+// ATTR: define{{.*}} i32 @caller.avx2.0({{.*}}) #[[AVX2]]
+// ATTR: define{{.*}} i32 @caller.default.2({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} i32 @caller.sse4.2.1({{.*}}) #[[SSE42]]
+// ATTR: define{{.*}} i32 @limited.default.1({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} i32 @limited.sse4.2.0({{.*}}) #[[SSE42]]
+// ATTR: define{{.*}} double @widen.default.1({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} double @widen.arch_x86-64-v3.0({{.*}}) #[[V3:[0-9]+]]
+// ATTR: define{{.*}} double @wide_caller.default.1({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} double @wide_caller.arch_x86-64-v3.0({{.*}}) #[[V3]]
+// ATTR: define{{.*}} i32 @cpu_callee.default.1({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} i32 @cpu_callee.arch_haswell.0({{.*}}) #[[CPU:[0-9]+]]
+// ATTR: define{{.*}} i32 @cpu_caller.default.1({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} i32 @cpu_caller.arch_haswell.0({{.*}}) #[[CPU]]
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -14,50 +29,23 @@ extern "C" {
__attribute__((target_clones("default,sse4.2,avx2")))
int callee(int x) { return x + 1; }
-// Reordering versions and calling through a local pointer still allow inlining.
+// Attribute values follow each clone, including reordered version lists.
__attribute__((target_clones("avx2,default,sse4.2")))
int caller(int x) {
int (*p)(int) = callee;
return p(x);
}
-// OPT-LABEL: define{{.*}} i32 @caller.avx2.
-// OPT-NOT: call
-// OPT: add nsw i32
-// OPT: ret i32
-// OPT-LABEL: define{{.*}} i32 @caller.default.
-// OPT-NOT: call
-// OPT: add nsw i32
-// OPT: ret i32
-// OPT-LABEL: define{{.*}} i32 @caller.sse4.2.
-// OPT-NOT: call
-// OPT: add nsw i32
-// OPT: ret i32
-
-// The runtime resolver can still choose callee.avx2 from either caller here.
+// A caller with fewer versions still gets its own dispatch predicates.
__attribute__((target_clones("default,sse4.2")))
int limited(int x) { return callee(x); }
-// OPT-LABEL: define{{.*}} i32 @limited.default.
-// OPT: call i32 @callee(
-// OPT-LABEL: define{{.*}} i32 @limited.sse4.2.
-// OPT: call i32 @callee(
-
__attribute__((target_clones("default,arch=x86-64-v3")))
double widen(_Float16 x) { return (double)x; }
__attribute__((target_clones("default,arch=x86-64-v3")))
double wide_caller(_Float16 x) { return widen(x); }
-// OPT-LABEL: define{{.*}} double @wide_caller.default.
-// OPT-NOT: call
-// OPT: fpext half
-// OPT: ret double
-// OPT-LABEL: define{{.*}} double @wide_caller.arch_x86-64-v3.
-// OPT-NOT: call
-// OPT: fpext half
-// OPT: ret double
-
// CPU-model predicates are deliberately left to the runtime resolver.
__attribute__((target_clones("default,arch=haswell")))
int cpu_callee(int x) { return x + 2; }
@@ -65,21 +53,20 @@ int cpu_callee(int x) { return x + 2; }
__attribute__((target_clones("default,arch=haswell")))
int cpu_caller(int x) { return cpu_callee(x); }
-// OPT-LABEL: define{{.*}} i32 @cpu_caller.default.
-// OPT: call i32 @cpu_callee(
-// OPT-LABEL: define{{.*}} i32 @cpu_caller.arch_haswell.
-// OPT: call i32 @cpu_callee(
-
#ifdef __cplusplus
}
-// There is no enclosing FunctionDecl while emitting a global initializer.
+// A global initializer must call the runtime-dispatched function, since it
+// has no selected clone whose features could determine the callee version.
+// INIT-LABEL: define internal void @__cxx_global_var_init(
+// INIT: call i32 @callee(
int dynamic_init = callee(0);
-// CXX-LABEL: define internal void @_GLOBAL__sub_I_
-// CXX: call i32 @callee(
#endif
-// Dispatch predicates, rather than the expanded ISA features, describe clones.
-// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features" {{.*}} }
-// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features"="sse4.2" {{.*}} }
-// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features"="avx2" {{.*}} }
-// ATTR-DAG: attributes #{{[0-9]+}} = { {{.*}}"fmv-features"="x86-64-v3" {{.*}} }
+// Dispatch predicates describe the runtime selection of each clone.
+// ATTR: attributes #[[DEFAULT]] = { {{.*}}"fmv-features" {{.*}} }
+// ATTR: attributes #[[SSE42]] = { {{.*}}"fmv-features"="sse4.2" {{.*}} }
+// ATTR: attributes #[[AVX2]] = { {{.*}}"fmv-features"="avx2" {{.*}} }
+// ATTR: attributes #[[V3]] = { {{.*}}"fmv-features"="x86-64-v3" {{.*}} }
+// ATTR: attributes #[[CPU]] = {
+// ATTR-NOT: "fmv-features"
+// ATTR-SAME: }
diff --git a/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll b/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll
new file mode 100644
index 00000000000000..1f2280e4a9c3ef
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll
@@ -0,0 +1,236 @@
+; REQUIRES: x86-registered-target
+; RUN: opt -passes='default<O2>' -S %s | FileCheck %s
+;
+; Matching clone families allow inlining, including calls through a local
+; function pointer and x86-64 ISA-level predicates. A caller with fewer clones,
+; a CPU-model predicate, or a global initializer must retain runtime dispatch.
+; The resolvers use separate bits for AVX2, SSE4.2 and the x86-64-v3 predicate.
+
+target triple = "x86_64-unknown-linux-gnu"
+
+ at dynamic_init = global i32 0, align 4
+ at features = external global i32
+ at cpu_model = external global i32
+ at llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @global_init, ptr null }]
+
+ at callee = weak_odr ifunc i32 (i32), ptr @callee.resolver
+ at caller = weak_odr ifunc i32 (i32), ptr @caller.resolver
+ at limited = weak_odr ifunc i32 (i32), ptr @limited.resolver
+ at widen = weak_odr ifunc double (half), ptr @widen.resolver
+ at wide_caller = weak_odr ifunc double (half), ptr @wide_caller.resolver
+ at cpu_callee = weak_odr ifunc i32 (i32), ptr @cpu_callee.resolver
+ at cpu_caller = weak_odr ifunc i32 (i32), ptr @cpu_caller.resolver
+
+define i32 @callee.default.2(i32 %x) #0 {
+entry:
+ %add = add nsw i32 %x, 1
+ ret i32 %add
+}
+
+define i32 @callee.sse4.2.0(i32 %x) #1 {
+entry:
+ %add = add nsw i32 %x, 1
+ ret i32 %add
+}
+
+define i32 @callee.avx2.1(i32 %x) #2 {
+entry:
+ %add = add nsw i32 %x, 1
+ ret i32 %add
+}
+
+define ptr @callee.resolver() {
+ %features = load i32, ptr @features
+ %mask0 = and i32 %features, 1
+ %cond0 = icmp ne i32 %mask0, 0
+ %mask1 = and i32 %features, 2
+ %cond1 = icmp ne i32 %mask1, 0
+ %choice1 = select i1 %cond1, ptr @callee.sse4.2.0, ptr @callee.default.2
+ %choice0 = select i1 %cond0, ptr @callee.avx2.1, ptr %choice1
+ ret ptr %choice0
+}
+
+; CHECK-LABEL: define{{.*}} i32 @caller.avx2.0(
+; CHECK-NOT: call
+; CHECK: add nsw i32
+; CHECK-NEXT: ret i32
+define i32 @caller.avx2.0(i32 %x) #2 {
+entry:
+ %p = alloca ptr
+ store ptr @callee, ptr %p
+ %fn = load ptr, ptr %p
+ %call = call i32 %fn(i32 %x)
+ ret i32 %call
+}
+
+; CHECK-LABEL: define{{.*}} i32 @caller.default.2(
+; CHECK-NOT: call
+; CHECK: add nsw i32
+; CHECK-NEXT: ret i32
+define i32 @caller.default.2(i32 %x) #0 {
+entry:
+ %p = alloca ptr
+ store ptr @callee, ptr %p
+ %fn = load ptr, ptr %p
+ %call = call i32 %fn(i32 %x)
+ ret i32 %call
+}
+
+; CHECK-LABEL: define{{.*}} i32 @caller.sse4.2.1(
+; CHECK-NOT: call
+; CHECK: add nsw i32
+; CHECK-NEXT: ret i32
+define i32 @caller.sse4.2.1(i32 %x) #1 {
+entry:
+ %p = alloca ptr
+ store ptr @callee, ptr %p
+ %fn = load ptr, ptr %p
+ %call = call i32 %fn(i32 %x)
+ ret i32 %call
+}
+
+define ptr @caller.resolver() {
+ %features = load i32, ptr @features
+ %mask0 = and i32 %features, 1
+ %cond0 = icmp ne i32 %mask0, 0
+ %mask1 = and i32 %features, 2
+ %cond1 = icmp ne i32 %mask1, 0
+ %choice1 = select i1 %cond1, ptr @caller.sse4.2.1, ptr @caller.default.2
+ %choice0 = select i1 %cond0, ptr @caller.avx2.0, ptr %choice1
+ ret ptr %choice0
+}
+
+; CHECK-LABEL: define{{.*}} i32 @limited.default.1(
+; CHECK: call i32 @callee(
+define i32 @limited.default.1(i32 %x) #0 {
+entry:
+ %call = call i32 @callee(i32 %x)
+ ret i32 %call
+}
+
+; CHECK-LABEL: define{{.*}} i32 @limited.sse4.2.0(
+; CHECK: call i32 @callee(
+define i32 @limited.sse4.2.0(i32 %x) #1 {
+entry:
+ %call = call i32 @callee(i32 %x)
+ ret i32 %call
+}
+
+define ptr @limited.resolver() {
+ %features = load i32, ptr @features
+ %mask0 = and i32 %features, 2
+ %cond0 = icmp ne i32 %mask0, 0
+ %choice0 = select i1 %cond0, ptr @limited.sse4.2.0, ptr @limited.default.1
+ ret ptr %choice0
+}
+
+define double @widen.default.1(half %x) #0 {
+entry:
+ %conv = fpext half %x to double
+ ret double %conv
+}
+
+define double @widen.arch_x86-64-v3.0(half %x) #5 {
+entry:
+ %conv = fpext half %x to double
+ ret double %conv
+}
+
+define ptr @widen.resolver() {
+ %features = load i32, ptr @features
+ %mask0 = and i32 %features, 4
+ %cond0 = icmp ne i32 %mask0, 0
+ %choice0 = select i1 %cond0, ptr @widen.arch_x86-64-v3.0, ptr @widen.default.1
+ ret ptr %choice0
+}
+
+; CHECK-LABEL: define{{.*}} double @wide_caller.default.1(
+; CHECK-NOT: call
+; CHECK: fpext half
+; CHECK-NEXT: ret double
+define double @wide_caller.default.1(half %x) #0 {
+entry:
+ %call = call double @widen(half %x)
+ ret double %call
+}
+
+; CHECK-LABEL: define{{.*}} double @wide_caller.arch_x86-64-v3.0(
+; CHECK-NOT: call
+; CHECK: fpext half
+; CHECK-NEXT: ret double
+define double @wide_caller.arch_x86-64-v3.0(half %x) #5 {
+entry:
+ %call = call double @widen(half %x)
+ ret double %call
+}
+
+define ptr @wide_caller.resolver() {
+ %features = load i32, ptr @features
+ %mask0 = and i32 %features, 4
+ %cond0 = icmp ne i32 %mask0, 0
+ %choice0 = select i1 %cond0, ptr @wide_caller.arch_x86-64-v3.0, ptr @wide_caller.default.1
+ ret ptr %choice0
+}
+
+define i32 @cpu_callee.default.1(i32 %x) #0 {
+entry:
+ %add = add nsw i32 %x, 2
+ ret i32 %add
+}
+
+define i32 @cpu_callee.arch_haswell.0(i32 %x) #6 {
+entry:
+ %add = add nsw i32 %x, 2
+ ret i32 %add
+}
+
+define ptr @cpu_callee.resolver() {
+ %cpu = load i32, ptr @cpu_model
+ %haswell = icmp eq i32 %cpu, 13
+ %result = select i1 %haswell, ptr @cpu_callee.arch_haswell.0, ptr @cpu_callee.default.1
+ ret ptr %result
+}
+
+; CHECK-LABEL: define{{.*}} i32 @cpu_caller.default.1(
+; CHECK: call i32 @cpu_callee(
+define i32 @cpu_caller.default.1(i32 %x) #0 {
+entry:
+ %call = call i32 @cpu_callee(i32 %x)
+ ret i32 %call
+}
+
+; CHECK-LABEL: define{{.*}} i32 @cpu_caller.arch_haswell.0(
+; CHECK: call i32 @cpu_callee(
+define i32 @cpu_caller.arch_haswell.0(i32 %x) #6 {
+entry:
+ %call = call i32 @cpu_callee(i32 %x)
+ ret i32 %call
+}
+
+define ptr @cpu_caller.resolver() {
+ %cpu = load i32, ptr @cpu_model
+ %haswell = icmp eq i32 %cpu, 13
+ %result = select i1 %haswell, ptr @cpu_caller.arch_haswell.0, ptr @cpu_caller.default.1
+ ret ptr %result
+}
+
+define internal void @__cxx_global_var_init() {
+entry:
+ %call = call i32 @callee(i32 0)
+ store i32 %call, ptr @dynamic_init, align 4
+ ret void
+}
+
+; CHECK-LABEL: define{{.*}} void @global_init(
+; CHECK: call i32 @callee(
+define internal void @global_init() {
+entry:
+ call void @__cxx_global_var_init()
+ ret void
+}
+
+attributes #0 = { "fmv-features" }
+attributes #1 = { "fmv-features"="sse4.2" }
+attributes #2 = { "fmv-features"="avx2" }
+attributes #5 = { "fmv-features"="x86-64-v3" }
+attributes #6 = { "target-cpu"="haswell" }
More information about the cfe-commits
mailing list