https://github.com/adamant-pwn updated https://github.com/llvm/llvm-project/pull/239683
>From 9bf19c5b5eeaec759a359ee416e0652361fd8d88 Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov <[email protected]> Date: Mon, 26 Oct 2026 18:14:43 +0100 Subject: [PATCH 1/6] [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" + +@features = external global i32 +@callee = weak_odr ifunc i32 (i32), ptr @callee.resolver +@caller = weak_odr ifunc i32 (i32), ptr @caller.resolver +@limited = weak_odr ifunc i32 (i32), ptr @limited.resolver +@preemptible = weak ifunc i32 (i32), ptr @callee.resolver +@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 +} + +@levels = weak_odr ifunc i32 (i32), ptr @levels.resolver +@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 <[email protected]> Date: Mon, 26 Oct 2026 18:14:43 +0100 Subject: [PATCH 2/6] [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 <[email protected]> Date: Fri, 6 Nov 2026 12:19:14 +0100 Subject: [PATCH 3/6] [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" + +@dynamic_init = global i32 0, align 4 +@features = external global i32 +@cpu_model = external global i32 [email protected]_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @global_init, ptr null }] + +@callee = weak_odr ifunc i32 (i32), ptr @callee.resolver +@caller = weak_odr ifunc i32 (i32), ptr @caller.resolver +@limited = weak_odr ifunc i32 (i32), ptr @limited.resolver +@widen = weak_odr ifunc double (half), ptr @widen.resolver +@wide_caller = weak_odr ifunc double (half), ptr @wide_caller.resolver +@cpu_callee = weak_odr ifunc i32 (i32), ptr @cpu_callee.resolver +@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 369d5eeb9d1fdc8d27208be9e6d86d91a2dd6fef Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov <[email protected]> Date: Fri, 6 Nov 2026 17:37:27 +0100 Subject: [PATCH 4/6] [GlobalOpt] Resolve equivalent FMV bodies for profitable inlining Allow calls through an FMV IFUNC to use a target-compatible version when all resolver choices have equivalent small scalar integer bodies and the inline cost model predicts a profitable inlining. Preserve existing compatible direct calls and reject unsupported bodies and callsites. Assisted-by: OpenAI Codex. --- llvm/lib/Transforms/IPO/GlobalOpt.cpp | 141 +++++- .../GlobalOpt/X86/equivalent-fmv-inline.ll | 71 +++ .../GlobalOpt/X86/resolve-equivalent-fmv.ll | 416 ++++++++++++++++++ .../GlobalOpt/X86/target-clones-inline.ll | 18 +- 4 files changed, 637 insertions(+), 9 deletions(-) create mode 100644 llvm/test/Transforms/GlobalOpt/X86/equivalent-fmv-inline.ll create mode 100644 llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index 06821ef4141632..2d22d653a5d775 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -21,8 +21,10 @@ #include "llvm/ADT/Statistic.h" #include "llvm/ADT/Twine.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/BlockFrequencyInfo.h" #include "llvm/Analysis/ConstantFolding.h" +#include "llvm/Analysis/InlineCost.h" #include "llvm/Analysis/MemoryBuiltins.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" @@ -2152,6 +2154,85 @@ collectVersions(Value *V, SmallVectorImpl<Function *> &Versions, return true; } +// Limit equivalence-based resolution to small scalar integer expressions. In +// particular, do not infer equivalence from a common IFUNC or source function: +// target-specific operations and calls can give versions different behavior. +static bool isSimpleIntegerVersion(const Function &F) { + if (F.isDeclaration() || F.isInterposable() || F.size() != 1 || + F.isVarArg() || !F.getReturnType()->isIntegerTy() || + F.hasPersonalityFn() || F.hasPrefixData() || F.hasPrologueData() || + F.hasGC() || F.hasFnAttribute(Attribute::NoInline) || + F.hasFnAttribute(Attribute::OptimizeNone) || F.front().size() > 32) + return false; + if (any_of(F.args(), + [](const Argument &A) { return !A.getType()->isIntegerTy(); })) + return false; + for (const Instruction &I : F.front()) { + if (I.hasMetadataOtherThanDebugLoc()) + return false; + switch (I.getOpcode()) { + case Instruction::Add: + case Instruction::Sub: + case Instruction::Mul: + case Instruction::And: + case Instruction::Or: + case Instruction::Xor: + case Instruction::Shl: + case Instruction::LShr: + case Instruction::AShr: + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::ICmp: + case Instruction::Select: + case Instruction::Ret: + break; + default: + return false; + } + if (!I.getType()->isIntegerTy() && !isa<ReturnInst>(I)) + return false; + for (const Value *Op : I.operands()) + if (!Op->getType()->isIntegerTy() || + (!isa<Argument, Instruction, ConstantInt>(Op))) + return false; + } + return true; +} + +static AttributeList getVersionSemanticAttrs(const Function &F) { + AttributeList Attrs = F.getAttributes(); + for (StringRef Name : {"target-cpu", "target-features", "tune-cpu", + "fmv-features", "fmv-priority"}) + Attrs = Attrs.removeFnAttribute(F.getContext(), Name); + return Attrs; +} + +static bool haveSameIntegerBody(const Function &L, const Function &R) { + if (L.getFunctionType() != R.getFunctionType() || + L.getCallingConv() != R.getCallingConv() || + getVersionSemanticAttrs(L) != getVersionSemanticAttrs(R) || + L.front().size() != R.front().size()) + return false; + DenseMap<const Value *, const Value *> Values; + for (auto [LA, RA] : zip(L.args(), R.args())) + Values[&LA] = &RA; + for (auto [LI, RI] : zip(L.front(), R.front())) { + if (!LI.isSameOperationAs(&RI) || + LI.getRawSubclassOptionalData() != RI.getRawSubclassOptionalData()) + return false; + for (auto [LO, RO] : zip(LI.operands(), RI.operands())) { + if (isa<ConstantInt>(LO)) { + if (LO != RO) + return false; + } else if (Values.lookup(LO) != RO) + return false; + } + Values[&LI] = &RI; + } + return true; +} + // Try to statically resolve calls to versioned functions when possible. First // we identify the function versions which are associated with an IFUNC symbol. // We do that by examining the resolver function of the IFUNC. Once we have @@ -2174,7 +2255,9 @@ collectVersions(Value *V, SmallVectorImpl<Function *> &Versions, // Presentation in EuroLLVM2025: // https://www.youtube.com/watch?v=k54MFimPz-A&t=867s static bool OptimizeNonTrivialIFuncs( - Module &M, function_ref<TargetTransformInfo &(Function &)> GetTTI) { + Module &M, function_ref<TargetTransformInfo &(Function &)> GetTTI, + function_ref<TargetLibraryInfo &(Function &)> GetTLI, + function_ref<AssumptionCache &(Function &)> GetAC) { const IPOOptions &Opts = getIPOOptions(M); bool Changed = false; @@ -2369,6 +2452,54 @@ static bool OptimizeNonTrivialIFuncs( staticallyResolveCalls(Callers, Callees, /*CallerIsFMV=*/true); } + // Exact resolver selection may leave an indirect call, or select a version + // whose target requirements prevent inlining. If every possible version + // computes the same integer expression, a compatible version is sufficient. + // Keep an already compatible direct call. Otherwise choose a compatible + // body only when the inline cost model predicts a profitable inlining. + if (all_of(Callees, + [](Function *F) { return isSimpleIntegerVersion(*F); }) && + all_of(drop_begin(Callees), [&](Function *F) { + return haveSameIntegerBody(*Callees.front(), *F); + })) { + for (auto &Entry : CallSites) { + Function *Caller = Entry.first; + TargetTransformInfo &TTI = GetTTI(*Caller); + for (CallBase *CS : Entry.second) { + Function *Current = CS->getCalledFunction(); + if ((Current && + AttributeFuncs::areInlineCompatible(*Caller, *Current) && + TTI.areInlineCompatible(Caller, Current)) || + CS->isNoInline() || CS->hasOperandBundles() || + CS->isMustTailCall()) + continue; + Function *Replacement = nullptr; + int BestCost = 0; + for (Function *F : Callees) { + if (CS->getFunctionType() != F->getFunctionType() || + CS->getCallingConv() != F->getCallingConv()) + continue; + InlineCost Cost = + getInlineCost(*CS, F, getInlineParams(), TTI, GetAC, GetTLI); + if (!Cost) + continue; + if (Cost.isAlways()) { + Replacement = F; + break; + } + if (!Replacement || Cost.getCost() < BestCost) { + Replacement = F; + BestCost = Cost.getCost(); + } + } + if (Replacement) { + CS->setCalledOperand(Replacement); + Changed = true; + } + } + } + } + if (CalleeIF->use_empty() || all_of(CalleeIF->users(), [](User *U) { return isa<GlobalAlias>(U); })) NumIFuncsResolved++; @@ -2380,6 +2511,7 @@ static bool optimizeGlobalsInModule(Module &M, const DataLayout &DL, function_ref<TargetLibraryInfo &(Function &)> GetTLI, function_ref<TargetTransformInfo &(Function &)> GetTTI, + function_ref<AssumptionCache &(Function &)> GetAC, function_ref<BlockFrequencyInfo &(Function &)> GetBFI, function_ref<DominatorTree &(Function &)> LookupDomTree, function_ref<void(Function &F)> ChangedCFGCallback, @@ -2443,7 +2575,7 @@ optimizeGlobalsInModule(Module &M, const DataLayout &DL, LocalChange |= OptimizeStaticIFuncs(M); // Optimize IFuncs based on the target features of the caller. - LocalChange |= OptimizeNonTrivialIFuncs(M, GetTTI); + LocalChange |= OptimizeNonTrivialIFuncs(M, GetTTI, GetTLI, GetAC); // Remove any IFuncs that are now dead. LocalChange |= DeleteDeadIFuncs(M, NotDiscardableComdats); @@ -2471,6 +2603,9 @@ PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) { return FAM.getResult<TargetIRAnalysis>(F); }; + auto GetAC = [&FAM](Function &F) -> AssumptionCache & { + return FAM.getResult<AssumptionAnalysis>(F); + }; auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & { return FAM.getResult<BlockFrequencyAnalysis>(F); }; @@ -2479,7 +2614,7 @@ PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) { }; auto DeleteFnCallback = [&FAM](Function &F) { FAM.clear(F, F.getName()); }; - if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree, + if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetAC, GetBFI, LookupDomTree, ChangedCFGCallback, DeleteFnCallback)) return PreservedAnalyses::all(); diff --git a/llvm/test/Transforms/GlobalOpt/X86/equivalent-fmv-inline.ll b/llvm/test/Transforms/GlobalOpt/X86/equivalent-fmv-inline.ll new file mode 100644 index 00000000000000..f111088787aca5 --- /dev/null +++ b/llvm/test/Transforms/GlobalOpt/X86/equivalent-fmv-inline.ll @@ -0,0 +1,71 @@ +; REQUIRES: x86-registered-target +; RUN: opt -passes='default<O3>' -S %s | FileCheck %s +; RUN: opt -passes='lto<O3>' -S %s | FileCheck %s +; +; An AVX2 loop cannot inline the AVX512 helper selected on some machines. +; All helper bodies are equivalent, so a compatible body can inline and expose +; integer arithmetic to the loop vectorizer. + +target triple = "x86_64-unknown-linux-gnu" +@features = external global i32 +@mix = weak_odr ifunc i32 (i32), ptr @mix.resolver + +define ptr @mix.resolver() { + %f = load i32, ptr @features + %a = and i32 %f, 1 + %b = and i32 %f, 2 + %ca = icmp ne i32 %a, 0 + %cb = icmp ne i32 %b, 0 + %lo = select i1 %ca, ptr @mix.avx2, ptr @mix.default + %hi = select i1 %cb, ptr @mix.avx512, ptr %lo + ret ptr %hi +} + +define i32 @mix.default(i32 %x) #0 { + %s = lshr i32 %x, 16 + %a = xor i32 %x, %s + %m = mul i32 %a, 2146121005 + ret i32 %m +} + +define i32 @mix.avx2(i32 %x) #1 { + %s = lshr i32 %x, 16 + %a = xor i32 %x, %s + %m = mul i32 %a, 2146121005 + ret i32 %m +} + +define i32 @mix.avx512(i32 %x) #2 { + %s = lshr i32 %x, 16 + %a = xor i32 %x, %s + %m = mul i32 %a, 2146121005 + ret i32 %m +} + +; CHECK-LABEL: define void @hash_array( +; CHECK-NOT: call +; CHECK: lshr <8 x i32> +; CHECK: mul <8 x i32> +; CHECK-NOT: call +; CHECK: ret void +define void @hash_array(ptr noalias %x, ptr noalias %y, i64 %n) #3 { +entry: + %empty = icmp eq i64 %n, 0 + br i1 %empty, label %exit, label %loop +loop: + %i = phi i64 [ 0, %entry ], [ %next, %loop ] + %p = getelementptr i32, ptr %x, i64 %i + %v = load i32, ptr %p, align 4 + %h = call i32 @mix(i32 %v) + %q = getelementptr i32, ptr %y, i64 %i + store i32 %h, ptr %q, align 4 + %next = add nuw i64 %i, 1 + %done = icmp eq i64 %next, %n + br i1 %done, label %exit, label %loop +exit: + ret void +} +attributes #0 = { "fmv-features" "target-features"="+sse2" } +attributes #1 = { "fmv-features"="avx2" "target-features"="+avx2" } +attributes #2 = { "fmv-features"="avx512f" "target-features"="+avx512f" } +attributes #3 = { "target-features"="+avx2" } diff --git a/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll b/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll new file mode 100644 index 00000000000000..2ccb68a477c4c7 --- /dev/null +++ b/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll @@ -0,0 +1,416 @@ +; REQUIRES: x86-registered-target +; RUN: opt -passes=globalopt -S %s | FileCheck %s +; RUN: opt -passes=globalopt -inline-threshold=0 -inline-call-penalty=0 -S %s | FileCheck %s --check-prefix=EXPENSIVE +; +; All possible resolver choices must have equivalent, small integer bodies. +; A target-compatible body can then replace an unresolved IFUNC call. + +target triple = "x86_64-unknown-linux-gnu" +@features = external global i32 +@effect = external global i32 + +declare i32 @external(i32) + +@equal = weak_odr ifunc i32 (i32, i32), ptr @equal.resolver + +define ptr @equal.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @equal.high, ptr @equal.low + ret ptr %v +} + +define i32 @equal.low(i32 %x, i32 %y) #0 { + %v = xor i32 %x, 17 + ret i32 %v +} +define i32 @equal.high(i32 %x, i32 %y) #1 { + %v = xor i32 %x, 17 + ret i32 %v +} + +; CHECK-LABEL: define i32 @equal.caller( +; CHECK: call i32 @equal.low( +define i32 @equal.caller(i32 %x, i32 %y) #2 { + %v = call i32 @equal(i32 %x, i32 %y) + ret i32 %v +} + +@constant = weak_odr ifunc i32 (i32, i32), ptr @constant.resolver + +define ptr @constant.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @constant.high, ptr @constant.low + ret ptr %v +} + +define i32 @constant.low(i32 %x, i32 %y) #0 { + %v = xor i32 %x, 17 + ret i32 %v +} +define i32 @constant.high(i32 %x, i32 %y) #1 { + %v = xor i32 %x, 18 + ret i32 %v +} + +; CHECK-LABEL: define i32 @constant.caller( +; CHECK: call i32 @constant( +define i32 @constant.caller(i32 %x, i32 %y) #2 { + %v = call i32 @constant(i32 %x, i32 %y) + ret i32 %v +} + +@flags = weak_odr ifunc i32 (i32, i32), ptr @flags.resolver + +define ptr @flags.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @flags.high, ptr @flags.low + ret ptr %v +} + +define i32 @flags.low(i32 %x, i32 %y) #0 { + %v = add nsw i32 %x, 17 + ret i32 %v +} +define i32 @flags.high(i32 %x, i32 %y) #1 { + %v = add i32 %x, 17 + ret i32 %v +} + +; CHECK-LABEL: define i32 @flags.caller( +; CHECK: call i32 @flags( +define i32 @flags.caller(i32 %x, i32 %y) #2 { + %v = call i32 @flags(i32 %x, i32 %y) + ret i32 %v +} + +@operand = weak_odr ifunc i32 (i32, i32), ptr @operand.resolver + +define ptr @operand.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @operand.high, ptr @operand.low + ret ptr %v +} + +define i32 @operand.low(i32 %x, i32 %y) #0 { + %v = sub i32 %x, %y + ret i32 %v +} +define i32 @operand.high(i32 %x, i32 %y) #1 { + %v = sub i32 %y, %x + ret i32 %v +} + +; CHECK-LABEL: define i32 @operand.caller( +; CHECK: call i32 @operand( +define i32 @operand.caller(i32 %x, i32 %y) #2 { + %v = call i32 @operand(i32 %x, i32 %y) + ret i32 %v +} + +@effects = weak_odr ifunc i32 (i32, i32), ptr @effects.resolver + +define ptr @effects.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @effects.high, ptr @effects.low + ret ptr %v +} + +define i32 @effects.low(i32 %x, i32 %y) #0 { + store i32 %x, ptr @effect + %v = xor i32 %x, 17 + ret i32 %v +} +define i32 @effects.high(i32 %x, i32 %y) #1 { + store i32 %x, ptr @effect + %v = xor i32 %x, 17 + ret i32 %v +} + +; CHECK-LABEL: define i32 @effects.caller( +; CHECK: call i32 @effects( +define i32 @effects.caller(i32 %x, i32 %y) #2 { + %v = call i32 @effects(i32 %x, i32 %y) + ret i32 %v +} + +@calls = weak_odr ifunc i32 (i32, i32), ptr @calls.resolver + +define ptr @calls.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @calls.high, ptr @calls.low + ret ptr %v +} + +define i32 @calls.low(i32 %x, i32 %y) #0 { + %v = call i32 @external(i32 %x) + ret i32 %v +} +define i32 @calls.high(i32 %x, i32 %y) #1 { + %v = call i32 @external(i32 %x) + ret i32 %v +} + +; CHECK-LABEL: define i32 @calls.caller( +; CHECK: call i32 @calls( +define i32 @calls.caller(i32 %x, i32 %y) #2 { + %v = call i32 @calls(i32 %x, i32 %y) + ret i32 %v +} + +@preemptible = weak_odr ifunc i32 (i32, i32), ptr @preemptible.resolver + +define ptr @preemptible.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @preemptible.high, ptr @preemptible.low + ret ptr %v +} + +define weak i32 @preemptible.low(i32 %x, i32 %y) #0 { + %v = xor i32 %x, 17 + ret i32 %v +} +define i32 @preemptible.high(i32 %x, i32 %y) #1 { + %v = xor i32 %x, 17 + ret i32 %v +} + +; CHECK-LABEL: define i32 @preemptible.caller( +; CHECK: call i32 @preemptible( +define i32 @preemptible.caller(i32 %x, i32 %y) #2 { + %v = call i32 @preemptible(i32 %x, i32 %y) + ret i32 %v +} + +@attrs = weak_odr ifunc i32 (i32, i32), ptr @attrs.resolver + +define ptr @attrs.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @attrs.high, ptr @attrs.low + ret ptr %v +} + +define i32 @attrs.low(i32 %x, i32 %y) #0 { + %v = xor i32 %x, 17 + ret i32 %v +} +define zeroext i32 @attrs.high(i32 %x, i32 %y) #1 { + %v = xor i32 %x, 17 + ret i32 %v +} + +; CHECK-LABEL: define i32 @attrs.caller( +; CHECK: call i32 @attrs( +define i32 @attrs.caller(i32 %x, i32 %y) #2 { + %v = call i32 @attrs(i32 %x, i32 %y) + ret i32 %v +} + +@poison = weak_odr ifunc i32 (i32, i32), ptr @poison.resolver + +define ptr @poison.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @poison.high, ptr @poison.low + ret ptr %v +} + +define i32 @poison.low(i32 %x, i32 %y) #0 { + ret i32 poison +} +define i32 @poison.high(i32 %x, i32 %y) #1 { + ret i32 poison +} + +; CHECK-LABEL: define i32 @poison.caller( +; CHECK: call i32 @poison( +define i32 @poison.caller(i32 %x, i32 %y) #2 { + %v = call i32 @poison(i32 %x, i32 %y) + ret i32 %v +} + +@noinline = weak_odr ifunc i32 (i32, i32), ptr @noinline.resolver + +define ptr @noinline.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @noinline.high, ptr @noinline.low + ret ptr %v +} + +define i32 @noinline.low(i32 %x, i32 %y) #3 { + %v = xor i32 %x, 17 + ret i32 %v +} +define i32 @noinline.high(i32 %x, i32 %y) #1 { + %v = xor i32 %x, 17 + ret i32 %v +} + +; CHECK-LABEL: define i32 @noinline.caller( +; CHECK: call i32 @noinline( +define i32 @noinline.caller(i32 %x, i32 %y) #2 { + %v = call i32 @noinline(i32 %x, i32 %y) + ret i32 %v +} + +; A declaration cannot establish equivalence with all runtime choices. +@unknown = weak_odr ifunc i32 (i32, i32), ptr @unknown.resolver +define ptr @unknown.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @unknown.high, ptr @equal.low + ret ptr %v +} +declare i32 @unknown.high(i32, i32) #1 +; CHECK-LABEL: define i32 @unknown.caller( +; CHECK: call i32 @unknown( +define i32 @unknown.caller(i32 %x, i32 %y) #2 { + %v = call i32 @unknown(i32 %x, i32 %y) + ret i32 %v +} + +; Do not bypass callsite restrictions or rewrite an interposable IFUNC. +@preemptible_ifunc = weak ifunc i32 (i32, i32), ptr @equal.resolver +; CHECK-LABEL: define i32 @restricted( +; CHECK: call i32 @equal( +; CHECK: call i32 @preemptible_ifunc( +define i32 @restricted(i32 %x, i32 %y) #2 { + %a = call i32 @equal(i32 %x, i32 %y) #4 + %b = call i32 @preemptible_ifunc(i32 %a, i32 %y) + ret i32 %b +} + +attributes #0 = { "fmv-features" "target-features"="+sse2" } +attributes #1 = { "fmv-features"="avx512f" "target-features"="+avx512f" } +attributes #2 = { "target-features"="+avx2" } +attributes #3 = { noinline "fmv-features" "target-features"="+sse2" } +attributes #4 = { noinline } + +; Exact resolution can select a body incompatible with the caller's codegen +; settings. A runtime predicate and codegen features need not be identical. +@known = weak_odr ifunc i32 (i32, i32), ptr @known.resolver +@normal = weak_odr ifunc i32 (i32, i32), ptr @normal.resolver + +define ptr @known.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @known.high, ptr @known.low + ret ptr %v +} +define i32 @known.low(i32 %x, i32 %y) #0 { + ret i32 %x +} +; CHECK-LABEL: define i32 @known.high( +; CHECK: call i32 @equal.low( +define i32 @known.high(i32 %x, i32 %y) #5 { + %v = call i32 @equal(i32 %x, i32 %y) + ret i32 %v +} +define ptr @normal.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @normal.high, ptr @known.low + ret ptr %v +} +; Keep the exact resolver choice when it is already inline-compatible. +; CHECK-LABEL: define i32 @normal.high( +; CHECK: call i32 @equal.high( +define i32 @normal.high(i32 %x, i32 %y) #1 { + %v = call i32 @equal(i32 %x, i32 %y) + ret i32 %v +} +attributes #5 = { "fmv-features"="avx512f" "target-features"="+avx2" } + +; A compatible equivalent body must also pass the inline cost model. +@costly = weak_odr ifunc i32 (i32), ptr @costly.resolver +define ptr @costly.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @costly.high, ptr @costly.low + ret ptr %v +} +define i32 @costly.low(i32 %x) #0 { + %v0 = mul i32 %x, 101 + %v1 = mul i32 %v0, 103 + %v2 = mul i32 %v1, 105 + %v3 = mul i32 %v2, 107 + %v4 = mul i32 %v3, 109 + %v5 = mul i32 %v4, 111 + %v6 = mul i32 %v5, 113 + %v7 = mul i32 %v6, 115 + %v8 = mul i32 %v7, 117 + %v9 = mul i32 %v8, 119 + %v10 = mul i32 %v9, 121 + %v11 = mul i32 %v10, 123 + %v12 = mul i32 %v11, 125 + %v13 = mul i32 %v12, 127 + %v14 = mul i32 %v13, 129 + %v15 = mul i32 %v14, 131 + %v16 = mul i32 %v15, 133 + %v17 = mul i32 %v16, 135 + %v18 = mul i32 %v17, 137 + %v19 = mul i32 %v18, 139 + ret i32 %v19 +} +define i32 @costly.high(i32 %x) #1 { + %v0 = mul i32 %x, 101 + %v1 = mul i32 %v0, 103 + %v2 = mul i32 %v1, 105 + %v3 = mul i32 %v2, 107 + %v4 = mul i32 %v3, 109 + %v5 = mul i32 %v4, 111 + %v6 = mul i32 %v5, 113 + %v7 = mul i32 %v6, 115 + %v8 = mul i32 %v7, 117 + %v9 = mul i32 %v8, 119 + %v10 = mul i32 %v9, 121 + %v11 = mul i32 %v10, 123 + %v12 = mul i32 %v11, 125 + %v13 = mul i32 %v12, 127 + %v14 = mul i32 %v13, 129 + %v15 = mul i32 %v14, 131 + %v16 = mul i32 %v15, 133 + %v17 = mul i32 %v16, 135 + %v18 = mul i32 %v17, 137 + %v19 = mul i32 %v18, 139 + ret i32 %v19 +} +; CHECK-LABEL: define i32 @costly.caller( +; CHECK: call i32 @costly.low( +; EXPENSIVE-LABEL: define i32 @costly.caller( +; EXPENSIVE: call i32 @costly( +define i32 @costly.caller(i32 %x) #2 { + %v = call i32 @costly(i32 %x) + ret i32 %v +} + +; Floating-point bodies need stronger target-semantic reasoning. +@floating = weak_odr ifunc float (float), ptr @floating.resolver +define ptr @floating.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @floating.high, ptr @floating.low + ret ptr %v +} +define float @floating.low(float %x) #0 { + %v = fadd float %x, 1.0 + ret float %v +} +define float @floating.high(float %x) #1 { + %v = fadd float %x, 1.0 + ret float %v +} +; CHECK-LABEL: define float @floating.caller( +; CHECK: call float @floating( +define float @floating.caller(float %x) #2 { + %v = call float @floating(float %x) + ret float %v +} diff --git a/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll b/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll index 1f2280e4a9c3ef..6a490f8d489517 100644 --- a/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll +++ b/llvm/test/Transforms/GlobalOpt/X86/target-clones-inline.ll @@ -2,12 +2,15 @@ ; 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. +; function pointer and x86-64 ISA-level predicates. Equivalent integer bodies +; also allow callers with fewer clones and a global initializer to inline. +; CPU-model versions without dispatch metadata still 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" +; CHECK: @dynamic_init = {{.*}}global i32 1 +; CHECK: @llvm.global_ctors = appending global [0 x @dynamic_init = global i32 0, align 4 @features = external global i32 @cpu_model = external global i32 @@ -101,7 +104,9 @@ define ptr @caller.resolver() { } ; CHECK-LABEL: define{{.*}} i32 @limited.default.1( -; CHECK: call i32 @callee( +; CHECK-NOT: call +; CHECK: add nsw i32 +; CHECK-NEXT: ret i32 define i32 @limited.default.1(i32 %x) #0 { entry: %call = call i32 @callee(i32 %x) @@ -109,7 +114,9 @@ entry: } ; CHECK-LABEL: define{{.*}} i32 @limited.sse4.2.0( -; CHECK: call i32 @callee( +; CHECK-NOT: call +; CHECK: add nsw i32 +; CHECK-NEXT: ret i32 define i32 @limited.sse4.2.0(i32 %x) #1 { entry: %call = call i32 @callee(i32 %x) @@ -221,8 +228,7 @@ entry: ret void } -; CHECK-LABEL: define{{.*}} void @global_init( -; CHECK: call i32 @callee( +; The equivalent helper bodies let GlobalOpt fold this initializer to 1. define internal void @global_init() { entry: call void @__cxx_global_var_init() >From b3108689e0f3e097fac978138f03285c83d318b1 Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov <[email protected]> Date: Fri, 6 Nov 2026 17:47:57 +0100 Subject: [PATCH 5/6] [GlobalOpt] Cover excluded equivalent-clone callsites and metadata Assisted-by: OpenAI Codex. --- .../GlobalOpt/X86/resolve-equivalent-fmv.ll | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll b/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll index 2ccb68a477c4c7..d81d344f2025ca 100644 --- a/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll +++ b/llvm/test/Transforms/GlobalOpt/X86/resolve-equivalent-fmv.ll @@ -414,3 +414,48 @@ define float @floating.caller(float %x) #2 { %v = call float @floating(float %x) ret float %v } + +; Preserve unsupported callsite contracts even for equivalent versions. +; CHECK-LABEL: define i32 @bundled.caller( +; CHECK: call i32 @equal(i32 %x, i32 %y) [ "deopt"(i32 7) ] +define i32 @bundled.caller(i32 %x, i32 %y) #2 { + %v = call i32 @equal(i32 %x, i32 %y) [ "deopt"(i32 7) ] + ret i32 %v +} +; CHECK-LABEL: define i32 @musttail.caller( +; CHECK: musttail call i32 @equal( +define i32 @musttail.caller(i32 %x, i32 %y) #2 { + %v = musttail call i32 @equal(i32 %x, i32 %y) + ret i32 %v +} +; CHECK-LABEL: define i32 @optnone.caller( +; CHECK: call i32 @equal( +define i32 @optnone.caller(i32 %x, i32 %y) #6 { + %v = call i32 @equal(i32 %x, i32 %y) + ret i32 %v +} +attributes #6 = { noinline optnone "target-features"="+avx2" } + +; Even equal instruction metadata needs an explicit equivalence policy. +@annotated = weak_odr ifunc i32 (i32, i32), ptr @annotated.resolver +define ptr @annotated.resolver() { + %f = load i32, ptr @features + %c = icmp ne i32 %f, 0 + %v = select i1 %c, ptr @annotated.high, ptr @annotated.low + ret ptr %v +} +define i32 @annotated.low(i32 %x, i32 %y) #0 { + %v = xor i32 %x, 17, !annotation !0 + ret i32 %v +} +define i32 @annotated.high(i32 %x, i32 %y) #1 { + %v = xor i32 %x, 17, !annotation !0 + ret i32 %v +} +; CHECK-LABEL: define i32 @annotated.caller( +; CHECK: call i32 @annotated( +define i32 @annotated.caller(i32 %x, i32 %y) #2 { + %v = call i32 @annotated(i32 %x, i32 %y) + ret i32 %v +} +!0 = !{!"retain"} >From 93cd6619d9eb6cadba27b20ec692842de56ed48d Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov <[email protected]> Date: Fri, 6 Nov 2026 18:15:30 +0100 Subject: [PATCH 6/6] [GlobalOpt] Apply clang-format to equivalent-clone resolution --- llvm/lib/Transforms/IPO/GlobalOpt.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index 2d22d653a5d775..8a3111aba34b1c 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -2254,10 +2254,11 @@ static bool haveSameIntegerBody(const Function &L, const Function &R) { // // Presentation in EuroLLVM2025: // https://www.youtube.com/watch?v=k54MFimPz-A&t=867s -static bool OptimizeNonTrivialIFuncs( - Module &M, function_ref<TargetTransformInfo &(Function &)> GetTTI, - function_ref<TargetLibraryInfo &(Function &)> GetTLI, - function_ref<AssumptionCache &(Function &)> GetAC) { +static bool +OptimizeNonTrivialIFuncs(Module &M, + function_ref<TargetTransformInfo &(Function &)> GetTTI, + function_ref<TargetLibraryInfo &(Function &)> GetTLI, + function_ref<AssumptionCache &(Function &)> GetAC) { const IPOOptions &Opts = getIPOOptions(M); bool Changed = false; @@ -2614,8 +2615,9 @@ PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) { }; auto DeleteFnCallback = [&FAM](Function &F) { FAM.clear(F, F.getName()); }; - if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetAC, GetBFI, LookupDomTree, - ChangedCFGCallback, DeleteFnCallback)) + if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetAC, GetBFI, + LookupDomTree, ChangedCFGCallback, + DeleteFnCallback)) return PreservedAnalyses::all(); PreservedAnalyses PA = PreservedAnalyses::none(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
