[clang] [llvm] [Clang][X86] Resolve CPU-model target_clones calls (PR #239654)

Oleksandr Kulkov via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 08:02:29 PST 2026


https://github.com/adamant-pwn created https://github.com/llvm/llvm-project/pull/239654

Depends on #230278; the first four commits belong to that PR.

Calls between matching `target_clones("default,arch=haswell")` versions currently retain IFUNC dispatch. Represent CPU-identity predicates so GlobalOpt can resolve these calls and enable inlining, including through full LTO.

Extend X86 `fmv-features` with `arch=CPU` predicates using the runtime CPU type/subtype ABI values, including aliases. Add `fmv-priority` to CPU-model families to preserve the resolver's complete order. This matters when Haswell and `x86-64-v3` have equal base priority: reversing their source order can change the selected callee.

Leave families with missing, malformed or incompatible ordering metadata unresolved. Feature-only families using the existing metadata remain supported. Document both attributes and cover metadata emission, aliases, tie ordering, mixed legacy/new bitcode, integration and LTO.

ThinLTO retains cross-module IFUNC dispatch. Its existing duplicate-IFUNC verifier failure when multiple translation units emit the same resolver is outside this change.

For example, this scaling/clamping loop retains a helper call per element before the change; afterward the helper is inlined and the loop vectorizes:

```c
#include <stddef.h>
#define CLONES __attribute__((target_clones("default,arch=znver4")))
CLONES float calibrate(float x, float scale, float offset) {
  float y = x * scale + offset;
  return y < 0 ? 0 : y > 1 ? 1 : y;
}
CLONES void transform(const float *restrict x, float *restrict y,
                      size_t n, float scale, float offset) {
  for (size_t i = 0; i < n; ++i)
    y[i] = calibrate(x[i], scale, offset);
}
```

On a Ryzen 9 7945HX, the measured loop over 16,777,216 floats took **24.54 ms before and 5.08 ms after (4.8× faster)**, comparing Clang 24 at `e7d99bf477` and `78f7fff0ef`. Both used `-O3 -march=x86-64 -fopenmp-simd -ffp-contract=off -falign-functions=64 -fno-pie`, with the runtime-selected `znver4` clones. Inputs were bounded binary fractions, with scale 1.125 and offset −0.25. Timings are medians of four interleaved, CPU-pinned process runs, each with seven samples; outputs matched bitwise. These are loop-level timings.

Assisted-by: OpenAI Codex.


>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/5] [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/5] [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/5] [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" }

>From 9e3ae4b319a7d053e2792f1cb3bdb464124d74b5 Mon Sep 17 00:00:00 2001
From: Oleksandr Kulkov <adamant.pwn at gmail.com>
Date: Fri, 6 Nov 2026 13:09:55 +0100
Subject: [PATCH 4/5] [X86] Prototype CPU-model predicates for target_clones
 resolution

Represent runtime CPU identity separately from ISA feature predicates and preserve source-order ties in FMV priority metadata. Keep the follow-up separate from PR #230278.

Assisted-by: OpenAI Codex
---
 clang/lib/CodeGen/CodeGenModule.cpp           |  31 ++--
 .../test/CodeGen/attr-target-clones-inline.c  |  11 +-
 clang/test/CodeGen/attr-target-clones.c       |   6 +-
 .../lib/Target/X86/X86TargetTransformInfo.cpp |  47 +++++-
 .../GlobalOpt/X86/resolve-fmv-cpu-models.ll   | 135 ++++++++++++++++++
 5 files changed, 207 insertions(+), 23 deletions(-)
 create mode 100644 llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 9a258939bfd5b2..8dec71d495f7a1 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3616,15 +3616,30 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
              TC) {
     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()));
-      Attrs.addAttribute("fmv-features", Feature == "default" ? "" : Feature);
-      AddedAttr = true;
+    // Preserve CPU-identity predicates separately from feature predicates.
+    // In particular, arch=haswell must not mean "supports Haswell's ISA".
+    StringRef Feature =
+        Arch.value_or(TC->getFeatureStr(GD.getMultiVersionIndex()));
+    std::string Predicate = Arch && !Arch->starts_with("x86-64")
+                                ? ("arch=" + *Arch).str()
+                                : Feature.str();
+    Attrs.addAttribute("fmv-features", Predicate == "default" ? "" : Predicate);
+    bool HasCPUModel = false;
+    for (unsigned I = 0, E = TC->featuresStrs_size(); I != E; ++I) {
+      std::optional<StringRef> CPU = TC->getX86Architecture(I);
+      HasCPUModel |= CPU && !CPU->starts_with("x86-64");
+    }
+    if (HasCPUModel) {
+      // Runtime dispatch is stable for equal-priority predicates. Preserve the
+      // target_clones source order in the low bits for static resolution too.
+      uint64_t Priority =
+          Feature == "default"
+              ? 0
+              : getTarget().getFMVPriority({Feature}).getZExtValue();
+      Priority = (Priority << 32) | (UINT32_MAX - GD.getMultiVersionIndex());
+      Attrs.addAttribute("fmv-priority", std::to_string(Priority));
     }
+    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 a98b1af4ae05a1..fd1e5cf5dab064 100644
--- a/clang/test/CodeGen/attr-target-clones-inline.c
+++ b/clang/test/CodeGen/attr-target-clones-inline.c
@@ -17,9 +17,9 @@
 // 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.default.1({{.*}}) #[[CPU_DEFAULT:[0-9]+]]
 // ATTR: define{{.*}} i32 @cpu_callee.arch_haswell.0({{.*}}) #[[CPU:[0-9]+]]
-// ATTR: define{{.*}} i32 @cpu_caller.default.1({{.*}}) #[[DEFAULT]]
+// ATTR: define{{.*}} i32 @cpu_caller.default.1({{.*}}) #[[CPU_DEFAULT]]
 // ATTR: define{{.*}} i32 @cpu_caller.arch_haswell.0({{.*}}) #[[CPU]]
 
 #ifdef __cplusplus
@@ -46,7 +46,7 @@ double widen(_Float16 x) { return (double)x; }
 __attribute__((target_clones("default,arch=x86-64-v3")))
 double wide_caller(_Float16 x) { return widen(x); }
 
-// CPU-model predicates are deliberately left to the runtime resolver.
+// CPU-model predicates retain CPU identity and the exact resolver priority.
 __attribute__((target_clones("default,arch=haswell")))
 int cpu_callee(int x) { return x + 2; }
 
@@ -67,6 +67,5 @@ int dynamic_init = callee(0);
 // 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: }
+// ATTR: attributes #[[CPU_DEFAULT]] = { {{.*}}"fmv-features" "fmv-priority"="{{[0-9]+}}"
+// ATTR: attributes #[[CPU]] = { {{.*}}"fmv-features"="arch=haswell" "fmv-priority"="{{[0-9]+}}"
diff --git a/clang/test/CodeGen/attr-target-clones.c b/clang/test/CodeGen/attr-target-clones.c
index ccf9b5615bde46..06dff45aa3a84d 100644
--- a/clang/test/CodeGen/attr-target-clones.c
+++ b/clang/test/CodeGen/attr-target-clones.c
@@ -318,7 +318,5 @@ int isa_level(int) { return 0; }
 
 
 // CHECK: attributes #[[ATTR_RESOLVER]] = { disable_sanitizer_instrumentation }
-// CHECK: attributes #[[SSE42]] =
-// CHECK-SAME: "target-features"="+crc32,+cx8,+mmx,+popcnt,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87"
-// CHECK: attributes #[[SB]] =
-// CHECK-SAME: "target-features"="+avx,+cmov,+crc32,+cx16,+cx8,+fxsr,+mmx,+pclmul,+popcnt,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave,+xsaveopt"
+// CHECK-DAG: attributes #[[SSE42]] = {{.*}} "target-features"="+crc32,+cx8,+mmx,+popcnt,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87"
+// CHECK-DAG: attributes #[[SB]] = {{.*}} "target-features"="+avx,+cmov,+crc32,+cx16,+cx8,+fxsr,+mmx,+pclmul,+popcnt,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave,+xsaveopt"
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index eef748e80b5bc0..ca8135b32e046d 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -50,6 +50,7 @@
 
 #include "X86TargetTransformInfo.h"
 #include "llvm/ADT/SmallBitVector.h"
+#include "llvm/ADT/StringSwitch.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/Analysis.h"
 #include "llvm/CodeGen/BasicTTIImpl.h"
@@ -78,24 +79,60 @@ static SmallVector<StringRef, 8> getFMVFeatures(const Function &F) {
   return Features;
 }
 
+// CPU predicates use the same (field, ABI value) identity as __builtin_cpu_is.
+// Distinct bits make no assumptions about CPU/ISA implication or exclusivity.
+static constexpr unsigned CPUTypeBits = 1 + std::max({
+                                                0u,
+#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) ABI_VALUE##u,
+#include "llvm/TargetParser/X86TargetParser.def"
+                                            });
+static constexpr unsigned CPUSubtypeBits = 1 + std::max({
+                                                   0u,
+#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE) ABI_VALUE##u,
+#include "llvm/TargetParser/X86TargetParser.def"
+                                               });
+
 APInt X86TTIImpl::getFeatureMask(const Function &F) const {
-  auto Words = X86::getCpuSupportsMask(getFMVFeatures(F));
-  APInt Mask(32 * Words.size(), 0);
+  SmallVector<StringRef, 8> Features;
+  APInt Mask(128 + CPUTypeBits + CPUSubtypeBits, 0);
+  for (StringRef Predicate : getFMVFeatures(F)) {
+    if (!Predicate.consume_front("arch=")) {
+      Features.push_back(Predicate);
+      continue;
+    }
+    unsigned Bit = StringSwitch<unsigned>(Predicate)
+#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) .Case(STR, 128 + ABI_VALUE)
+#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE)                                  \
+  .Case(STR, 128 + CPUTypeBits + ABI_VALUE)
+#include "llvm/TargetParser/X86TargetParser.def"
+                       .Default(~0u);
+    assert(Bit != ~0u && "Unknown CPU identity predicate");
+    Mask.setBit(Bit);
+  }
+  auto Words = X86::getCpuSupportsMask(Features);
   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)));
+  if (F.hasFnAttribute("fmv-priority"))
+    return APInt(64, F.getFnAttribute("fmv-priority").getValueAsString(), 10);
+  return APInt(64, X86::getFMVPriority(getFMVFeatures(F))) << 32;
 }
 
 bool X86TTIImpl::isMultiversionedFunction(const Function &F) const {
-  return F.hasFnAttribute("fmv-features");
+  if (!F.hasFnAttribute("fmv-features"))
+    return false;
+  // CPU predicates require the resolver's exact ordering, including ties.
+  if (F.getFnAttribute("fmv-features").getValueAsString().contains("arch="))
+    return F.hasFnAttribute("fmv-priority");
+  return true;
 }
 
 using CostKindTblEntry = CostTblEntryT<CostKindCosts>;
-using TypeConversionCostKindTblEntry = TypeConversionCostTblEntryT<CostKindCosts>;
+using TypeConversionCostKindTblEntry =
+    TypeConversionCostTblEntryT<CostKindCosts>;
 
 TargetTransformInfo::PopcntSupportKind
 X86TTIImpl::getPopcntSupport(unsigned TyWidth) const {
diff --git a/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll
new file mode 100644
index 00000000000000..0ecd5fccdd643e
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll
@@ -0,0 +1,135 @@
+; REQUIRES: x86-registered-target
+; RUN: opt -passes=globalopt -S %s | FileCheck %s
+;
+; CPU predicates describe runtime identity, independently of ISA features.
+; fmv-priority records the resolver's order, including source-order ties.
+
+target triple = "x86_64-unknown-linux-gnu"
+ at haswell = external global i1
+ at v3 = external global i1
+ at atom = external global i1
+
+ at unordered = weak_odr ifunc i32 (i32), ptr @unordered.resolver
+ at simple = weak_odr ifunc i32 (i32), ptr @simple.resolver
+ at simple_caller = weak_odr ifunc i32 (i32), ptr @simple_caller.resolver
+ at mixed = weak_odr ifunc i32 (i32), ptr @mixed.resolver
+ at mixed_caller = weak_odr ifunc i32 (i32), ptr @mixed_caller.resolver
+ at alias_callee = weak_odr ifunc i32 (i32), ptr @alias_callee.resolver
+ at alias_caller = weak_odr ifunc i32 (i32), ptr @alias_caller.resolver
+
+declare i32 @default(i32) #0
+declare i32 @haswell_version(i32) #1
+declare i32 @v3_version(i32) #2
+declare i32 @bonnell_version(i32) #3
+declare i32 @unordered_haswell(i32) #7
+
+; Without exact ordering metadata, leave a CPU-model family unresolved.
+define ptr @unordered.resolver() {
+  %h = load i1, ptr @haswell
+  %r = select i1 %h, ptr @unordered_haswell, ptr @default
+  ret ptr %r
+}
+
+define ptr @simple.resolver() {
+  %h = load i1, ptr @haswell
+  %r = select i1 %h, ptr @haswell_version, ptr @default
+  ret ptr %r
+}
+
+define ptr @simple_caller.resolver() {
+  %h = load i1, ptr @haswell
+  %r = select i1 %h, ptr @simple_haswell, ptr @simple_default
+  ret ptr %r
+}
+
+; Both matching clones resolve, including the default version.
+define i32 @simple_haswell(i32 %x) #1 {
+; CHECK-LABEL: define i32 @simple_haswell(
+; CHECK: call i32 @haswell_version(
+; CHECK: call i32 @unordered(
+  %r = call i32 @simple(i32 %x)
+  %u = call i32 @unordered(i32 %r)
+  ret i32 %u
+}
+
+define i32 @simple_default(i32 %x) #0 {
+; CHECK-LABEL: define i32 @simple_default(
+; CHECK: call i32 @default(
+  %r = call i32 @simple(i32 %x)
+  ret i32 %r
+}
+
+; Haswell and x86-64-v3 have equal base priority. The callee lists v3 first;
+; the caller lists Haswell first. Both predicates can hold at runtime.
+define ptr @mixed.resolver() {
+  %h = load i1, ptr @haswell
+  %v = load i1, ptr @v3
+  %lo = select i1 %h, ptr @haswell_version, ptr @default
+  %r = select i1 %v, ptr @v3_version, ptr %lo
+  ret ptr %r
+}
+
+define ptr @mixed_caller.resolver() {
+  %h = load i1, ptr @haswell
+  %v = load i1, ptr @v3
+  %lo = select i1 %v, ptr @mixed_v3, ptr @mixed_default
+  %r = select i1 %h, ptr @mixed_haswell, ptr %lo
+  ret ptr %r
+}
+
+define i32 @mixed_haswell(i32 %x) #4 {
+; CHECK-LABEL: define i32 @mixed_haswell(
+; CHECK: call i32 @mixed(
+  %r = call i32 @mixed(i32 %x)
+  ret i32 %r
+}
+
+define i32 @mixed_v3(i32 %x) #5 {
+; CHECK-LABEL: define i32 @mixed_v3(
+; CHECK: call i32 @v3_version(
+  %r = call i32 @mixed(i32 %x)
+  ret i32 %r
+}
+
+define i32 @mixed_default(i32 %x) #0 {
+; CHECK-LABEL: define i32 @mixed_default(
+; CHECK: call i32 @default(
+  %r = call i32 @mixed(i32 %x)
+  ret i32 %r
+}
+
+; Alias spellings refer to the same runtime CPU field and ABI value.
+define ptr @alias_callee.resolver() {
+  %a = load i1, ptr @atom
+  %r = select i1 %a, ptr @bonnell_version, ptr @default
+  ret ptr %r
+}
+
+define ptr @alias_caller.resolver() {
+  %a = load i1, ptr @atom
+  %r = select i1 %a, ptr @atom_caller, ptr @alias_default
+  ret ptr %r
+}
+
+define i32 @atom_caller(i32 %x) #6 {
+; CHECK-LABEL: define i32 @atom_caller(
+; CHECK: call i32 @bonnell_version(
+  %r = call i32 @alias_callee(i32 %x)
+  ret i32 %r
+}
+
+define i32 @alias_default(i32 %x) #0 {
+; CHECK-LABEL: define i32 @alias_default(
+; CHECK: call i32 @default(
+  %r = call i32 @alias_callee(i32 %x)
+  ret i32 %r
+}
+
+attributes #0 = { "fmv-features" "fmv-priority"="1" }
+attributes #1 = { "fmv-features"="arch=haswell" "fmv-priority"="20" }
+attributes #2 = { "fmv-features"="x86-64-v3" "fmv-priority"="30" }
+attributes #3 = { "fmv-features"="arch=bonnell" "fmv-priority"="20" }
+attributes #4 = { "fmv-features"="arch=haswell" "fmv-priority"="30" }
+attributes #5 = { "fmv-features"="x86-64-v3" "fmv-priority"="20" }
+attributes #6 = { "fmv-features"="arch=atom" "fmv-priority"="20" }
+attributes #7 = { "fmv-features"="arch=haswell" }

>From 78f7fff0ef6f8aed067c3067012abdcbf5395e3d Mon Sep 17 00:00:00 2001
From: Oleksandr Kulkov <adamant.pwn at gmail.com>
Date: Fri, 6 Nov 2026 15:03:38 +0100
Subject: [PATCH 5/5] [X86] Validate CPU-model FMV ordering through integration
 and LTO

Document the metadata contract, reject malformed or incompatible priority encodings, refresh priority attributes on redeclarations, and cover source-order ties and full/ThinLTO behavior.

Assisted-by: OpenAI Codex
---
 clang/lib/CodeGen/CodeGenModule.cpp           |   1 +
 .../CodeGen/attr-target-clones-cpu-models.c   |  37 +++
 .../Integration/target-clones-cpu-models.c    |  32 +++
 llvm/docs/LangRef.md                          |  30 +++
 .../llvm/Analysis/TargetTransformInfo.h       |   4 +-
 .../lib/Target/X86/X86TargetTransformInfo.cpp |  48 ++--
 llvm/lib/Transforms/IPO/GlobalOpt.cpp         |  12 +-
 llvm/test/LTO/X86/fmv-cpu-models.ll           | 229 ++++++++++++++++++
 .../GlobalOpt/X86/resolve-fmv-cpu-models.ll   |  52 +++-
 9 files changed, 426 insertions(+), 19 deletions(-)
 create mode 100644 clang/test/CodeGen/attr-target-clones-cpu-models.c
 create mode 100644 clang/test/Integration/target-clones-cpu-models.c
 create mode 100644 llvm/test/LTO/X86/fmv-cpu-models.ll

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 8dec71d495f7a1..29f1434a913e2a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3679,6 +3679,7 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
         RemoveAttrs.addAttribute("target-cpu");
         RemoveAttrs.addAttribute("target-features");
         RemoveAttrs.addAttribute("fmv-features");
+        RemoveAttrs.addAttribute("fmv-priority");
         RemoveAttrs.addAttribute("tune-cpu");
         F->removeFnAttrs(RemoveAttrs);
         F->addFnAttrs(Attrs);
diff --git a/clang/test/CodeGen/attr-target-clones-cpu-models.c b/clang/test/CodeGen/attr-target-clones-cpu-models.c
new file mode 100644
index 00000000000000..97d8aa147239ec
--- /dev/null
+++ b/clang/test/CodeGen/attr-target-clones-cpu-models.c
@@ -0,0 +1,37 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -triple i386-linux-gnu -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
+// 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"
+// WINDOWS-NOT: "fmv-priority"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Haswell and v3 have equal runtime priority. Their source order breaks ties.
+// A declaration followed by a definition must retain the same metadata.
+__attribute__((target_clones("default,arch=haswell,arch=x86-64-v3")))
+int haswell_first(int);
+int haswell_first(int x) { return x; }
+
+__attribute__((target_clones("default,arch=x86-64-v3,arch=haswell")))
+int v3_first(int x) { return x; }
+
+#ifdef __cplusplus
+}
+#endif
+
+// CHECK: define{{.*}} i32 @haswell_first.default.2({{.*}}) #[[DEFAULT:[0-9]+]]
+// CHECK: define{{.*}} i32 @haswell_first.arch_haswell.0({{.*}}) #[[H_FIRST:[0-9]+]]
+// CHECK: define{{.*}} i32 @haswell_first.arch_x86-64-v3.1({{.*}}) #[[V_SECOND:[0-9]+]]
+// CHECK: define{{.*}} i32 @v3_first.default.2({{.*}}) #[[DEFAULT]]
+// CHECK: define{{.*}} i32 @v3_first.arch_x86-64-v3.0({{.*}}) #[[V_FIRST:[0-9]+]]
+// CHECK: define{{.*}} i32 @v3_first.arch_haswell.1({{.*}}) #[[H_SECOND:[0-9]+]]
+// CHECK: attributes #[[DEFAULT]] = { {{.*}}"fmv-features" "fmv-priority"="4294967295"
+// CHECK: attributes #[[H_FIRST]] = { {{.*}}"fmv-features"="arch=haswell" "fmv-priority"="[[#PRIORITY:]]"
+// CHECK: attributes #[[V_SECOND]] = { {{.*}}"fmv-features"="x86-64-v3" "fmv-priority"="[[#PRIORITY - 1]]"
+// CHECK: attributes #[[V_FIRST]] = { {{.*}}"fmv-features"="x86-64-v3" "fmv-priority"="[[#PRIORITY]]"
+// CHECK: attributes #[[H_SECOND]] = { {{.*}}"fmv-features"="arch=haswell" "fmv-priority"="[[#PRIORITY - 1]]"
diff --git a/clang/test/Integration/target-clones-cpu-models.c b/clang/test/Integration/target-clones-cpu-models.c
new file mode 100644
index 00000000000000..facf8aab14f2e0
--- /dev/null
+++ b/clang/test/Integration/target-clones-cpu-models.c
@@ -0,0 +1,32 @@
+// REQUIRES: x86-registered-target
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O2 -emit-llvm %t/same.c -o - | FileCheck %s
+// RUN: %clang_cc1 -triple i386-linux-gnu -O2 -emit-llvm %t/same.c -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -disable-llvm-passes -emit-llvm-bc %t/callee.c -o %t/callee.bc
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -disable-llvm-passes -emit-llvm-bc %t/caller.c -o %t/caller.bc
+// RUN: llvm-lto -O2 -save-merged-module -exported-symbol=caller.default.1 -exported-symbol=caller.arch_haswell.0 -o %t/lto.o %t/caller.bc %t/callee.bc
+// RUN: llvm-dis %t/lto.o.merged.bc -o - | FileCheck %s
+
+// Matching CPU-model clone families should inline through the complete
+// frontend/optimizer pipeline, both within a module and after LTO linking.
+// CHECK-LABEL: define{{.*}} i32 @caller.default.1(
+// CHECK-NOT: call
+// CHECK: add nsw i32
+// CHECK-NEXT: ret i32
+// CHECK-LABEL: define{{.*}} i32 @caller.arch_haswell.0(
+// CHECK-NOT: call
+// CHECK: add nsw i32
+// CHECK-NEXT: ret i32
+
+//--- callee.c
+__attribute__((target_clones("default,arch=haswell")))
+int callee(int x) { return x + 1; }
+
+//--- caller.c
+int callee(int);
+__attribute__((target_clones("default,arch=haswell")))
+int caller(int x) { return callee(x); }
+
+//--- same.c
+#include "callee.c"
+#include "caller.c"
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index 9190d60092aa5d..5cc24cfabbd3d9 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -2248,6 +2248,36 @@ define void @f() "no-sse" { ... }
     be replaced with jumps to externally-defined architecture-specific symbols.
     For X86, this symbol's identifier is `__x86_return_thunk`.
 
+`"fmv-features"="PREDICATES"`
+:   Describes a conjunction of target-specific runtime predicates for a function
+    version selected by an indirect function (IFUNC) resolver. An empty value
+    denotes an unconditional predicate, as used by a fallback version. Static
+    IFUNC resolution uses these predicates together with the resolver's version
+    priorities. The predicates must describe the conditions used by the resolver.
+
+    On X86, a comma-separated list can contain runtime feature or x86-64 ISA-level
+    names recognized by `__builtin_cpu_supports`, and `arch=CPU` predicates with
+    the CPU-identity semantics of `__builtin_cpu_is`. CPU type and subtype tests
+    are distinct. Alias names that test the same runtime field and value denote
+    the same predicate. These predicates are separate from the code-generation
+    settings in `target-cpu` and `target-features`.
+
+`"fmv-priority"="N"`
+:   On X86, supplies an unsigned 64-bit decimal priority for static IFUNC
+    resolution. Larger values precede smaller values. Priorities must preserve
+    the resolver's complete selection order, including tie-breaking, and are
+    compared only between versions of the same IFUNC. Each version in an IFUNC
+    family containing a CPU-identity predicate must provide this attribute.
+    Static resolution is disabled for families mixing explicit ordering with
+    legacy feature priorities, or when a CPU version lacks this attribute.
+
+    Clang encodes the runtime feature/CPU priority in the high 32 bits and the
+    inverse source-list index in the low 32 bits for `target_clones` families
+    containing CPU-model versions. This preserves source order when runtime
+    priorities tie. Feature-only families without this attribute continue to
+    use the target's feature priority. Unparseable or out-of-range values disable
+    static resolution for that version's IFUNC family.
+
 `"frame-pointer"`
 :   This attribute tells the code generator whether the function
     should keep the frame pointer. The code generator may emit the frame pointer
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 21b623211d627e..8302b2acb53bd4 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -2175,7 +2175,9 @@ class TargetTransformInfo {
   LLVM_ABI APInt getFeatureMask(const Function &F) const;
 
   /// Returns a bitmask constructed from the target-features or fmv-features
-  /// metadata of a function corresponding to its FMV priority.
+  /// metadata of a function corresponding to its FMV priority. Results with
+  /// different bit widths use incompatible encodings and cannot be ordered
+  /// together for static IFUNC resolution.
   LLVM_ABI APInt getPriorityMask(const Function &F) const;
 
   /// Returns true if this is an instance of a function with multiple versions.
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index ca8135b32e046d..c5b04700656e66 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -60,6 +60,7 @@
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/TargetParser/X86TargetParser.h"
 #include <optional>
+#include <tuple>
 
 using namespace llvm;
 
@@ -81,29 +82,41 @@ static SmallVector<StringRef, 8> getFMVFeatures(const Function &F) {
 
 // CPU predicates use the same (field, ABI value) identity as __builtin_cpu_is.
 // Distinct bits make no assumptions about CPU/ISA implication or exclusivity.
-static constexpr unsigned CPUTypeBits = 1 + std::max({
-                                                0u,
-#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) ABI_VALUE##u,
+static constexpr unsigned FeatureBits =
+    32 * std::tuple_size_v<decltype(X86::getCpuSupportsMask({}))>;
+static constexpr unsigned CPUTypeBits = [] {
+  unsigned Max = 0;
+#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) Max = std::max(Max, ABI_VALUE##u);
 #include "llvm/TargetParser/X86TargetParser.def"
-                                            });
-static constexpr unsigned CPUSubtypeBits = 1 + std::max({
-                                                   0u,
-#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE) ABI_VALUE##u,
+  return Max + 1;
+}();
+static constexpr unsigned CPUSubtypeBits = [] {
+  unsigned Max = 0;
+#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE) Max = std::max(Max, ABI_VALUE##u);
 #include "llvm/TargetParser/X86TargetParser.def"
-                                               });
+  return Max + 1;
+}();
+
+static std::optional<uint64_t> getFMVPriority(const Function &F) {
+  Attribute Attr = F.getFnAttribute("fmv-priority");
+  uint64_t Priority;
+  if (!Attr.isValid() || Attr.getValueAsString().getAsInteger(10, Priority))
+    return std::nullopt;
+  return Priority;
+}
 
 APInt X86TTIImpl::getFeatureMask(const Function &F) const {
   SmallVector<StringRef, 8> Features;
-  APInt Mask(128 + CPUTypeBits + CPUSubtypeBits, 0);
+  APInt Mask(FeatureBits + CPUTypeBits + CPUSubtypeBits, 0);
   for (StringRef Predicate : getFMVFeatures(F)) {
     if (!Predicate.consume_front("arch=")) {
       Features.push_back(Predicate);
       continue;
     }
     unsigned Bit = StringSwitch<unsigned>(Predicate)
-#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) .Case(STR, 128 + ABI_VALUE)
+#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) .Case(STR, FeatureBits + ABI_VALUE)
 #define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE)                                  \
-  .Case(STR, 128 + CPUTypeBits + ABI_VALUE)
+  .Case(STR, FeatureBits + CPUTypeBits + ABI_VALUE)
 #include "llvm/TargetParser/X86TargetParser.def"
                        .Default(~0u);
     assert(Bit != ~0u && "Unknown CPU identity predicate");
@@ -116,14 +129,18 @@ APInt X86TTIImpl::getFeatureMask(const Function &F) const {
 }
 
 APInt X86TTIImpl::getPriorityMask(const Function &F) const {
-  if (F.hasFnAttribute("fmv-priority"))
-    return APInt(64, F.getFnAttribute("fmv-priority").getValueAsString(), 10);
-  return APInt(64, X86::getFMVPriority(getFMVFeatures(F))) << 32;
+  if (std::optional<uint64_t> Priority = getFMVPriority(F))
+    return APInt(64, *Priority);
+  // Legacy feature-only IR has no source-order tie breaker. Keep its
+  // encoding distinct so mixed families cannot be ordered speculatively.
+  return APInt(32, X86::getFMVPriority(getFMVFeatures(F)));
 }
 
 bool X86TTIImpl::isMultiversionedFunction(const Function &F) const {
   if (!F.hasFnAttribute("fmv-features"))
     return false;
+  if (F.hasFnAttribute("fmv-priority") && !getFMVPriority(F))
+    return false;
   // CPU predicates require the resolver's exact ordering, including ties.
   if (F.getFnAttribute("fmv-features").getValueAsString().contains("arch="))
     return F.hasFnAttribute("fmv-priority");
@@ -131,8 +148,7 @@ bool X86TTIImpl::isMultiversionedFunction(const Function &F) const {
 }
 
 using CostKindTblEntry = CostTblEntryT<CostKindCosts>;
-using TypeConversionCostKindTblEntry =
-    TypeConversionCostTblEntryT<CostKindCosts>;
+using TypeConversionCostKindTblEntry = TypeConversionCostTblEntryT<CostKindCosts>;
 
 TargetTransformInfo::PopcntSupportKind
 X86TTIImpl::getPopcntSupport(unsigned TyWidth) const {
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 06821ef4141632..a7b806a97760f3 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2216,7 +2216,6 @@ static bool OptimizeNonTrivialIFuncs(
       continue;
 
     for (Function *V : Versions) {
-      VersionOf.insert({V, &IF});
       auto [FeatIt, FeatInserted] = FeatureMask.try_emplace(V);
       if (FeatInserted)
         FeatIt->second = GetTTI(*V).getFeatureMask(*V);
@@ -2225,6 +2224,17 @@ static bool OptimizeNonTrivialIFuncs(
         PriorIt->second = GetTTI(*V).getPriorityMask(*V);
     }
 
+    // Different priority encodings cannot be compared. For example, LTO can
+    // combine versions with and without target-specific resolver ordering.
+    unsigned PriorityWidth = PriorityMask[Versions.front()].getBitWidth();
+    if (any_of(Versions, [&](Function *V) {
+          return PriorityMask[V].getBitWidth() != PriorityWidth;
+        }))
+      continue;
+
+    for (Function *V : Versions)
+      VersionOf.insert({V, &IF});
+
     // Sort function versions in decreasing priority order.
     sort(Versions, [&](auto *LHS, auto *RHS) {
       return PriorityMask[LHS].ugt(PriorityMask[RHS]);
diff --git a/llvm/test/LTO/X86/fmv-cpu-models.ll b/llvm/test/LTO/X86/fmv-cpu-models.ll
new file mode 100644
index 00000000000000..a376c10d665005
--- /dev/null
+++ b/llvm/test/LTO/X86/fmv-cpu-models.ll
@@ -0,0 +1,229 @@
+; REQUIRES: x86-registered-target
+; RUN: split-file %s %t
+; RUN: llvm-as %t/caller.ll -o %t/caller.bc
+; RUN: llvm-as %t/callee.ll -o %t/callee.bc
+; RUN: llvm-lto2 run %t/caller.bc %t/callee.bc -o %t/full -save-temps \
+; RUN:   -r=%t/caller.bc,haswell,l \
+; RUN:   -r=%t/caller.bc,v3,l \
+; RUN:   -r=%t/caller.bc,caller_matching,plx \
+; RUN:   -r=%t/caller.bc,caller_tied,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.default,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.haswell,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.resolver,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.default,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.haswell,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.v3,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.resolver,plx \
+; RUN:   -r=%t/caller.bc,callee_matching,l \
+; RUN:   -r=%t/caller.bc,callee_tied,l \
+; RUN:   -r=%t/callee.bc,haswell,l \
+; RUN:   -r=%t/callee.bc,v3,l \
+; RUN:   -r=%t/callee.bc,callee_matching,plx \
+; RUN:   -r=%t/callee.bc,callee_tied,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.default,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.haswell,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.resolver,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.default,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.haswell,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.v3,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.resolver,plx
+; RUN: llvm-dis %t/full.0.4.opt.bc -o - | FileCheck %s --check-prefix=FULL
+; RUN: opt -module-summary %t/caller.ll -o %t/caller.bc
+; RUN: opt -module-summary %t/callee.ll -o %t/callee.bc
+; RUN: llvm-lto2 run %t/caller.bc %t/callee.bc -o %t/thin -save-temps \
+; RUN:   -r=%t/caller.bc,haswell,l \
+; RUN:   -r=%t/caller.bc,v3,l \
+; RUN:   -r=%t/caller.bc,caller_matching,plx \
+; RUN:   -r=%t/caller.bc,caller_tied,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.default,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.haswell,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.resolver,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.default,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.haswell,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.v3,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.resolver,plx \
+; RUN:   -r=%t/caller.bc,callee_matching,l \
+; RUN:   -r=%t/caller.bc,callee_tied,l \
+; RUN:   -r=%t/callee.bc,haswell,l \
+; RUN:   -r=%t/callee.bc,v3,l \
+; RUN:   -r=%t/callee.bc,callee_matching,plx \
+; RUN:   -r=%t/callee.bc,callee_tied,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.default,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.haswell,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.resolver,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.default,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.haswell,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.v3,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.resolver,plx
+; RUN: llvm-dis %t/thin.1.4.opt.bc -o - | FileCheck %s --check-prefix=UNRESOLVED
+; RUN: llvm-as %t/caller.ll -o %t/caller.bc
+; RUN: sed 's/"fmv-priority"="1"//' %t/callee.ll | llvm-as -o %t/callee.bc
+; RUN: llvm-lto2 run %t/caller.bc %t/callee.bc -o %t/mixed -save-temps \
+; RUN:   -r=%t/caller.bc,haswell,l \
+; RUN:   -r=%t/caller.bc,v3,l \
+; RUN:   -r=%t/caller.bc,caller_matching,plx \
+; RUN:   -r=%t/caller.bc,caller_tied,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.default,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.haswell,plx \
+; RUN:   -r=%t/caller.bc,caller_matching.resolver,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.default,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.haswell,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.v3,plx \
+; RUN:   -r=%t/caller.bc,caller_tied.resolver,plx \
+; RUN:   -r=%t/caller.bc,callee_matching,l \
+; RUN:   -r=%t/caller.bc,callee_tied,l \
+; RUN:   -r=%t/callee.bc,haswell,l \
+; RUN:   -r=%t/callee.bc,v3,l \
+; RUN:   -r=%t/callee.bc,callee_matching,plx \
+; RUN:   -r=%t/callee.bc,callee_tied,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.default,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.haswell,plx \
+; RUN:   -r=%t/callee.bc,callee_matching.resolver,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.default,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.haswell,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.v3,plx \
+; RUN:   -r=%t/callee.bc,callee_tied.resolver,plx
+; RUN: llvm-dis %t/mixed.0.4.opt.bc -o - | FileCheck %s --check-prefix=UNRESOLVED
+;
+; Full LTO resolves and inlines matching CPU-model clones across modules.
+; Reversed Haswell/v3 tie order must retain dispatch where either callee can win.
+; ThinLTO retains the cross-module IFUNC calls; metadata must survive without
+; changing their semantics or producing invalid IR. Mixed legacy/new priority
+; encodings must also retain dispatch after full LTO linking.
+;
+; FULL-LABEL: define{{.*}} i32 @caller_matching.default(
+; FULL-NOT: call
+; FULL: add i32 %x, 1
+; FULL-NEXT: ret i32
+; UNRESOLVED-LABEL: define{{.*}} i32 @caller_matching.default(
+; UNRESOLVED: call i32 @callee_matching(
+; FULL-LABEL: define{{.*}} i32 @caller_matching.haswell(
+; FULL-NOT: call
+; FULL: add i32 %x, 2
+; FULL-NEXT: ret i32
+; UNRESOLVED-LABEL: define{{.*}} i32 @caller_matching.haswell(
+; UNRESOLVED: call i32 @callee_matching(
+; FULL-LABEL: define{{.*}} i32 @caller_tied.default(
+; FULL-NOT: call
+; FULL: add i32 %x, 1
+; FULL-NEXT: ret i32
+; UNRESOLVED-LABEL: define{{.*}} i32 @caller_tied.default(
+; UNRESOLVED: call i32 @callee_tied(
+; FULL-LABEL: define{{.*}} i32 @caller_tied.haswell(
+; FULL: call i32 @callee_tied(
+; UNRESOLVED-LABEL: define{{.*}} i32 @caller_tied.haswell(
+; UNRESOLVED: call i32 @callee_tied(
+; FULL-LABEL: define{{.*}} i32 @caller_tied.v3(
+; FULL-NOT: call
+; FULL: add i32 %x, 3
+; FULL-NEXT: ret i32
+; UNRESOLVED-LABEL: define{{.*}} i32 @caller_tied.v3(
+; UNRESOLVED: call i32 @callee_tied(
+
+;--- caller.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+ at haswell = external global i1
+ at v3 = external global i1
+ at caller_matching = weak_odr ifunc i32 (i32), ptr @caller_matching.resolver
+declare i32 @callee_matching(i32)
+
+define i32 @caller_matching.default(i32 %x) #0 {
+  %r = call i32 @callee_matching(i32 %x)
+  ret i32 %r
+}
+
+define i32 @caller_matching.haswell(i32 %x) #1 {
+  %r = call i32 @callee_matching(i32 %x)
+  ret i32 %r
+}
+
+define ptr @caller_matching.resolver() {
+  %h = load i1, ptr @haswell
+  %r = select i1 %h, ptr @caller_matching.haswell, ptr @caller_matching.default
+  ret ptr %r
+}
+ at caller_tied = weak_odr ifunc i32 (i32), ptr @caller_tied.resolver
+declare i32 @callee_tied(i32)
+
+define i32 @caller_tied.default(i32 %x) #0 {
+  %r = call i32 @callee_tied(i32 %x)
+  ret i32 %r
+}
+
+define i32 @caller_tied.haswell(i32 %x) #1 {
+  %r = call i32 @callee_tied(i32 %x)
+  ret i32 %r
+}
+
+define i32 @caller_tied.v3(i32 %x) #2 {
+  %r = call i32 @callee_tied(i32 %x)
+  ret i32 %r
+}
+
+define ptr @caller_tied.resolver() {
+  %h = load i1, ptr @haswell
+  %v = load i1, ptr @v3
+  %lo = select i1 %v, ptr @caller_tied.v3, ptr @caller_tied.default
+  %r = select i1 %h, ptr @caller_tied.haswell, ptr %lo
+  ret ptr %r
+}
+
+attributes #0 = { "fmv-features" "fmv-priority"="1" }
+attributes #1 = { "fmv-features"="arch=haswell" "fmv-priority"="30" }
+attributes #2 = { "fmv-features"="x86-64-v3" "fmv-priority"="20" }
+attributes #3 = { "fmv-features"="arch=haswell" "fmv-priority"="20" }
+attributes #4 = { "fmv-features"="x86-64-v3" "fmv-priority"="30" }
+
+;--- callee.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+ at haswell = external global i1
+ at v3 = external global i1
+ at callee_matching = weak_odr ifunc i32 (i32), ptr @callee_matching.resolver
+
+define i32 @callee_matching.default(i32 %x) #0 {
+  %r = add i32 %x, 1
+  ret i32 %r
+}
+
+define i32 @callee_matching.haswell(i32 %x) #1 {
+  %r = add i32 %x, 2
+  ret i32 %r
+}
+
+define ptr @callee_matching.resolver() {
+  %h = load i1, ptr @haswell
+  %r = select i1 %h, ptr @callee_matching.haswell, ptr @callee_matching.default
+  ret ptr %r
+}
+ at callee_tied = weak_odr ifunc i32 (i32), ptr @callee_tied.resolver
+
+define i32 @callee_tied.default(i32 %x) #0 {
+  %r = add i32 %x, 1
+  ret i32 %r
+}
+
+define i32 @callee_tied.haswell(i32 %x) #3 {
+  %r = add i32 %x, 2
+  ret i32 %r
+}
+
+define i32 @callee_tied.v3(i32 %x) #4 {
+  %r = add i32 %x, 3
+  ret i32 %r
+}
+
+define ptr @callee_tied.resolver() {
+  %h = load i1, ptr @haswell
+  %v = load i1, ptr @v3
+  %lo = select i1 %h, ptr @callee_tied.haswell, ptr @callee_tied.default
+  %r = select i1 %v, ptr @callee_tied.v3, ptr %lo
+  ret ptr %r
+}
+
+attributes #0 = { "fmv-features" "fmv-priority"="1" }
+attributes #1 = { "fmv-features"="arch=haswell" "fmv-priority"="30" }
+attributes #2 = { "fmv-features"="x86-64-v3" "fmv-priority"="20" }
+attributes #3 = { "fmv-features"="arch=haswell" "fmv-priority"="20" }
+attributes #4 = { "fmv-features"="x86-64-v3" "fmv-priority"="30" }
diff --git a/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll
index 0ecd5fccdd643e..214667970d5d23 100644
--- a/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll
+++ b/llvm/test/Transforms/GlobalOpt/X86/resolve-fmv-cpu-models.ll
@@ -8,6 +8,44 @@ target triple = "x86_64-unknown-linux-gnu"
 @haswell = external global i1
 @v3 = external global i1
 @atom = external global i1
+ at goldmont = external global i1
+
+; Missing or malformed ordering must disable resolution. CPU type and subtype
+; ABI values also occupy distinct namespaces (goldmont-plus and Haswell are 13).
+ at malformed = weak_odr ifunc i32 (i32), ptr @malformed.resolver
+declare i32 @malformed_version(i32) #8
+define ptr @malformed.resolver() {
+  %c = load i1, ptr @haswell
+  %r = select i1 %c, ptr @malformed_version, ptr @default
+  ret ptr %r
+}
+
+ at overflow = weak_odr ifunc i32 (i32), ptr @overflow.resolver
+declare i32 @overflow_version(i32) #9
+define ptr @overflow.resolver() {
+  %c = load i1, ptr @haswell
+  %r = select i1 %c, ptr @overflow_version, ptr @default
+  ret ptr %r
+}
+
+ at type13 = weak_odr ifunc i32 (i32), ptr @type13.resolver
+declare i32 @type13_version(i32) #10
+define ptr @type13.resolver() {
+  %c = load i1, ptr @goldmont
+  %r = select i1 %c, ptr @type13_version, ptr @default
+  ret ptr %r
+}
+
+; Mixed old/new bitcode cannot establish the complete resolver order.
+ at mixed_encoding = weak_odr ifunc i32 (i32), ptr @mixed_encoding.resolver
+declare i32 @legacy_v3(i32) #11
+define ptr @mixed_encoding.resolver() {
+  %h = load i1, ptr @haswell
+  %v = load i1, ptr @v3
+  %lo = select i1 %v, ptr @legacy_v3, ptr @default
+  %r = select i1 %h, ptr @haswell_version, ptr %lo
+  ret ptr %r
+}
 
 @unordered = weak_odr ifunc i32 (i32), ptr @unordered.resolver
 @simple = weak_odr ifunc i32 (i32), ptr @simple.resolver
@@ -47,9 +85,17 @@ define i32 @simple_haswell(i32 %x) #1 {
 ; CHECK-LABEL: define i32 @simple_haswell(
 ; CHECK: call i32 @haswell_version(
 ; CHECK: call i32 @unordered(
+; CHECK: call i32 @malformed(
+; CHECK: call i32 @overflow(
+; CHECK: call i32 @type13(
+; CHECK: call i32 @mixed_encoding(
   %r = call i32 @simple(i32 %x)
   %u = call i32 @unordered(i32 %r)
-  ret i32 %u
+  %m = call i32 @malformed(i32 %u)
+  %o = call i32 @overflow(i32 %m)
+  %t = call i32 @type13(i32 %o)
+  %e = call i32 @mixed_encoding(i32 %t)
+  ret i32 %e
 }
 
 define i32 @simple_default(i32 %x) #0 {
@@ -133,3 +179,7 @@ attributes #4 = { "fmv-features"="arch=haswell" "fmv-priority"="30" }
 attributes #5 = { "fmv-features"="x86-64-v3" "fmv-priority"="20" }
 attributes #6 = { "fmv-features"="arch=atom" "fmv-priority"="20" }
 attributes #7 = { "fmv-features"="arch=haswell" }
+attributes #8 = { "fmv-features"="arch=haswell" "fmv-priority"="invalid" }
+attributes #9 = { "fmv-features"="arch=haswell" "fmv-priority"="18446744073709551616" }
+attributes #10 = { "fmv-features"="arch=goldmont-plus" "fmv-priority"="20" }
+attributes #11 = { "fmv-features"="x86-64-v3" }



More information about the cfe-commits mailing list