[clang] [FMV] Check ABI compatibility of multi-version functions wrt the module-level target (PR #232988)
Wolfgang Pieb via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 2 15:53:47 PST 2026
https://github.com/wolfy1961 updated https://github.com/llvm/llvm-project/pull/232988
>From 4a0891348f5037054e42001859f9338f1b4a0668 Mon Sep 17 00:00:00 2001
From: Wolfgang Pieb <wolfgang.pieb at sony.com>
Date: Fri, 16 Oct 2026 13:48:50 -0700
Subject: [PATCH 1/2] [FMV] Check ABI compatibility of multi-version functions
wrt the module-level target.
If a resolver function must be generated, a check is made that the
resolver can invoke all versions of the function based on the function's
parameter types and its return type and the module level target.
Fixes issue #232595
Assisted by Codex
---
.../clang/Basic/DiagnosticFrontendKinds.td | 3 +
clang/lib/CodeGen/CodeGenModule.cpp | 72 +++++++++++++++++--
.../CodeGen/X86/multiversion-resolver-abi.cpp | 47 ++++++++++++
3 files changed, 117 insertions(+), 5 deletions(-)
create mode 100644 clang/test/CodeGen/X86/multiversion-resolver-abi.cpp
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index b51aa3ff0a0cb4..5f27bbfbda60b2 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -333,6 +333,9 @@ def warn_avx_calling_convention
"enabled changes the ABI">,
InGroup<DiagGroup<"psabi">>;
def err_avx_calling_convention : Error<warn_avx_calling_convention.Summary>;
+def err_multiversion_resolver_abi : Error<
+ "multiversioned function %0 has %select{a return value|a parameter}1 of "
+ "type %2 whose target ABI is incompatible with the module ABI">;
def err_target_unsupported_type_for_abi
: Error<"%0 requires %1 type support, but ABI '%2' does not support it">;
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 4e04b702b7ffed..dd0470a03fea9b 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5267,7 +5267,7 @@ void CodeGenModule::emitMultiVersionFunctions() {
// in this TU. For other architectures it is always emitted.
bool ShouldEmitResolver = !getTriple().isAArch64();
SmallVector<CodeGenFunction::FMVResolverOption, 10> Options;
- llvm::DenseMap<llvm::Function *, const FunctionDecl *> DeclMap;
+ llvm::DenseMap<llvm::Function *, GlobalDecl> DeclMap;
getContext().forEachMultiversionedFunctionVersion(
FD, [&](const FunctionDecl *CurFD) {
@@ -5278,13 +5278,13 @@ void CodeGenModule::emitMultiVersionFunctions() {
assert(getTarget().getTriple().isX86() && "Unsupported target");
TA->getX86AddedFeatures(Feats);
llvm::Function *Func = createFunction(CurFD);
- DeclMap.insert({Func, CurFD});
+ DeclMap.insert({Func, GlobalDecl(CurFD)});
Options.emplace_back(Func, Feats, TA->getX86Architecture());
} else if (const auto *TVA = CurFD->getAttr<TargetVersionAttr>()) {
if (TVA->isDefaultVersion() && IsDefined)
ShouldEmitResolver = true;
llvm::Function *Func = createFunction(CurFD);
- DeclMap.insert({Func, CurFD});
+ DeclMap.insert({Func, GlobalDecl(CurFD)});
char Delim = getTarget().getTriple().isAArch64() ? '+' : ',';
TVA->getFeatures(Feats, Delim);
Options.emplace_back(Func, Feats);
@@ -5295,7 +5295,7 @@ void CodeGenModule::emitMultiVersionFunctions() {
if (TC->isDefaultVersion(I) && IsDefined)
ShouldEmitResolver = true;
llvm::Function *Func = createFunction(CurFD, I);
- DeclMap.insert({Func, CurFD});
+ DeclMap.insert({Func, GlobalDecl(CurFD, I)});
Feats.clear();
if (getTarget().getTriple().isX86()) {
TC->getX86Feature(Feats, I);
@@ -5313,6 +5313,68 @@ void CodeGenModule::emitMultiVersionFunctions() {
if (!ShouldEmitResolver)
continue;
+ // The public entry point calls every version using the module ABI. Check
+ // vector values whose x86-64 calling convention changes with AVX or
+ // AVX-512 before creating the resolver. Use GlobalDecl so that each
+ // target_clones version is checked with its own features.
+ bool Reported = false;
+ if (getTriple().getArch() == llvm::Triple::x86_64 &&
+ getLangOpts().getClangABICompat() > LangOptions::ClangABI::Ver23) {
+ unsigned ModuleVectorBits =
+ getTarget().getABI() == "avx512" ? 512
+ : getTarget().getABI() == "avx" || getTarget().getABI() == "avx2"
+ ? 256
+ : 128;
+ for (const auto &Option : Options) {
+ GlobalDecl Version = DeclMap.lookup(Option.Function);
+ const auto *VersionFD = cast<FunctionDecl>(Version.getDecl());
+ const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(Version);
+ const auto *VersionType = VersionFD->getType()->getAs<FunctionType>();
+ bool UsesWin64ABI = getTriple().isOSWindowsOrUEFI() &&
+ VersionType->getCallConv() != CC_X86_64SysV;
+ if (UsesWin64ABI ||
+ FI.getCallingConvention() == llvm::CallingConv::Win64)
+ continue;
+
+ llvm::StringMap<bool> Features;
+ getContext().getFunctionFeatureMap(Features, Version);
+ unsigned VersionVectorBits =
+ Features.lookup("avx512f") ? 512
+ : Features.lookup("avx") || Features.lookup("avx2") ? 256
+ : 128;
+ VersionVectorBits = std::max(VersionVectorBits, ModuleVectorBits);
+ if (VersionVectorBits == ModuleVectorBits)
+ continue;
+
+ auto CheckType = [&](QualType Ty, SourceLocation Loc, bool IsParam) {
+ if (!Ty->isVectorType())
+ return false;
+ uint64_t Bits = getContext().getTypeSize(Ty);
+ if (Bits <= ModuleVectorBits || Bits > VersionVectorBits)
+ return false;
+ Diags.Report(Loc, diag::err_multiversion_resolver_abi)
+ << VersionFD << IsParam << Ty;
+ return true;
+ };
+
+ if (CheckType(VersionFD->getReturnType(), VersionFD->getLocation(),
+ false)) {
+ Reported = true;
+ break;
+ }
+ for (const ParmVarDecl *P : VersionFD->parameters()) {
+ if (CheckType(P->getType(), P->getLocation(), true)) {
+ Reported = true;
+ break;
+ }
+ }
+ if (Reported)
+ break;
+ }
+ }
+ if (Reported)
+ continue;
+
llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant)) {
ResolverConstant = IFunc->getResolver();
@@ -5351,7 +5413,7 @@ void CodeGenModule::emitMultiVersionFunctions() {
llvm::APInt LHS = llvm::AArch64::getCpuSupportsMask(RO.Features);
return LHS.isSubsetOf(RHS);
})) {
- Diags.Report(DeclMap[I->Function]->getLocation(),
+ Diags.Report(DeclMap[I->Function].getDecl()->getLocation(),
diag::warn_unreachable_version)
<< I->Function->getName();
assert(I->Function->user_empty() && "unexpected users");
diff --git a/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp b/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp
new file mode 100644
index 00000000000000..838330f20a44e4
--- /dev/null
+++ b/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-psabi -emit-llvm -o /dev/null -verify=base %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx -Wno-psabi -emit-llvm -o /dev/null -verify=avx %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx2 -Wno-psabi -emit-llvm -o /dev/null -verify=avx2 %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx512f -Wno-psabi -emit-llvm -o /dev/null -verify=avx512 %s
+
+typedef float v8f __attribute__((vector_size(32)));
+typedef float v16f __attribute__((vector_size(64)));
+
+__attribute__((target("default"))) int target_arg(v8f x) { return (int)x[0]; }
+__attribute__((target("avx"))) int target_arg(v8f x) { return (int)x[0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+int use_target_arg(v8f x) { return target_arg(x); }
+
+__attribute__((target("default"))) int target_avx2_arg(v8f x) { return (int)x[0]; }
+__attribute__((target("avx2"))) int target_avx2_arg(v8f x) { return (int)x[0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+int use_target_avx2_arg(v8f x) { return target_avx2_arg(x); }
+
+__attribute__((target_clones("default", "avx")))
+int clone_arg(v8f x) { return (int)x[0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+
+__attribute__((target("default"))) int target_wide(v16f x) { return (int)x[0]; }
+__attribute__((target("avx512f"))) int target_wide(v16f x) { return (int)x[0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+// avx-error at -2 {{target ABI is incompatible with the module ABI}}
+// avx2-error at -3 {{target ABI is incompatible with the module ABI}}
+int use_target_wide(v16f x) { return target_wide(x); }
+
+__attribute__((target("default"))) v8f target_return(v8f x) { return x; }
+__attribute__((target("avx"))) v8f target_return(v8f x) { return x; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+v8f use_target_return(v8f x) { return target_return(x); }
+
+__attribute__((target("default"))) v8f target_avx2_return(v8f x) { return x; }
+__attribute__((target("avx2"))) v8f target_avx2_return(v8f x) { return x; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+v8f use_target_avx2_return(v8f x) { return target_avx2_return(x); }
+
+// A 512-bit vector is passed in memory by both versions below.
+__attribute__((target("default"))) int memory_arg(v16f x) { return (int)x[0]; }
+__attribute__((target("avx"))) int memory_arg(v16f x) { return (int)x[0]; }
+
+__attribute__((target_clones("default", "avx")))
+int scalar_arg(int x) { return x; }
+
+// avx512-no-diagnostics
>From 42d9f73a8b800aae560360da735f502d4df40c88 Mon Sep 17 00:00:00 2001
From: Wolfgang Pieb <wolfgang.pieb at sony.com>
Date: Mon, 2 Nov 2026 15:35:12 -0800
Subject: [PATCH 2/2] Make sure structs that wrap vector types are included in
the ABI compatibility check.
---
clang/lib/CodeGen/CodeGenModule.cpp | 7 ++-
.../CodeGen/X86/multiversion-resolver-abi.cpp | 44 +++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index dd0470a03fea9b..cea6ac7ef1f1ca 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -12,6 +12,7 @@
#include "CodeGenModule.h"
#include "ABIInfo.h"
+#include "ABIInfoImpl.h"
#include "CGBlocks.h"
#include "CGCUDARuntime.h"
#include "CGCXXABI.h"
@@ -5347,7 +5348,11 @@ void CodeGenModule::emitMultiVersionFunctions() {
continue;
auto CheckType = [&](QualType Ty, SourceLocation Loc, bool IsParam) {
- if (!Ty->isVectorType())
+ const Type *InnerTy = isSingleElementStruct(Ty, getContext());
+ if (!Ty->isVectorType() && (!InnerTy || !InnerTy->isVectorType()))
+ return false;
+ // C++ records passed indirectly have the same ABI at every AVX level.
+ if (InnerTy && getRecordArgABI(Ty, getCXXABI()))
return false;
uint64_t Bits = getContext().getTypeSize(Ty);
if (Bits <= ModuleVectorBits || Bits > VersionVectorBits)
diff --git a/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp b/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp
index 838330f20a44e4..bae7ffe843251b 100644
--- a/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp
+++ b/clang/test/CodeGen/X86/multiversion-resolver-abi.cpp
@@ -6,11 +6,35 @@
typedef float v8f __attribute__((vector_size(32)));
typedef float v16f __attribute__((vector_size(64)));
+struct Wrapped256 { v8f value; };
+struct Nested256 { Wrapped256 value; };
+struct Array256 { v8f value[1]; };
+struct Wrapped512 { v16f value; };
+struct TwoVectors { v8f first, second; };
+struct NonTrivial256 {
+ v8f value;
+ ~NonTrivial256();
+};
+
__attribute__((target("default"))) int target_arg(v8f x) { return (int)x[0]; }
__attribute__((target("avx"))) int target_arg(v8f x) { return (int)x[0]; }
// base-error at -1 {{target ABI is incompatible with the module ABI}}
int use_target_arg(v8f x) { return target_arg(x); }
+__attribute__((target("default"))) int wrapped_arg(Wrapped256 x) { return (int)x.value[0]; }
+__attribute__((target("avx"))) int wrapped_arg(Wrapped256 x) { return (int)x.value[0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+int use_wrapped_arg(Wrapped256 x) { return wrapped_arg(x); }
+
+__attribute__((target("default"))) int nested_arg(Nested256 x) { return (int)x.value.value[0]; }
+__attribute__((target("avx"))) int nested_arg(Nested256 x) { return (int)x.value.value[0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+int use_nested_arg(Nested256 x) { return nested_arg(x); }
+
+__attribute__((target_clones("default", "avx")))
+int array_arg(Array256 x) { return (int)x.value[0][0]; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+
__attribute__((target("default"))) int target_avx2_arg(v8f x) { return (int)x[0]; }
__attribute__((target("avx2"))) int target_avx2_arg(v8f x) { return (int)x[0]; }
// base-error at -1 {{target ABI is incompatible with the module ABI}}
@@ -32,6 +56,18 @@ __attribute__((target("avx"))) v8f target_return(v8f x) { return x; }
// base-error at -1 {{target ABI is incompatible with the module ABI}}
v8f use_target_return(v8f x) { return target_return(x); }
+__attribute__((target("default"))) Wrapped256 wrapped_return(Wrapped256 x) { return x; }
+__attribute__((target("avx"))) Wrapped256 wrapped_return(Wrapped256 x) { return x; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+Wrapped256 use_wrapped_return(Wrapped256 x) { return wrapped_return(x); }
+
+__attribute__((target("default"))) Wrapped512 wide_return(Wrapped512 x) { return x; }
+__attribute__((target("avx512f"))) Wrapped512 wide_return(Wrapped512 x) { return x; }
+// base-error at -1 {{target ABI is incompatible with the module ABI}}
+// avx-error at -2 {{target ABI is incompatible with the module ABI}}
+// avx2-error at -3 {{target ABI is incompatible with the module ABI}}
+Wrapped512 use_wide_return(Wrapped512 x) { return wide_return(x); }
+
__attribute__((target("default"))) v8f target_avx2_return(v8f x) { return x; }
__attribute__((target("avx2"))) v8f target_avx2_return(v8f x) { return x; }
// base-error at -1 {{target ABI is incompatible with the module ABI}}
@@ -41,6 +77,14 @@ v8f use_target_avx2_return(v8f x) { return target_avx2_return(x); }
__attribute__((target("default"))) int memory_arg(v16f x) { return (int)x[0]; }
__attribute__((target("avx"))) int memory_arg(v16f x) { return (int)x[0]; }
+// A record with two vectors is passed in memory by every version.
+__attribute__((target("default"))) int two_vectors(TwoVectors x) { return (int)x.first[0]; }
+__attribute__((target("avx512f"))) int two_vectors(TwoVectors x) { return (int)x.first[0]; }
+
+// A nontrivial C++ record is passed indirectly at every AVX level.
+__attribute__((target("default"))) int nontrivial_arg(NonTrivial256 x) { return (int)x.value[0]; }
+__attribute__((target("avx"))) int nontrivial_arg(NonTrivial256 x) { return (int)x.value[0]; }
+
__attribute__((target_clones("default", "avx")))
int scalar_arg(int x) { return x; }
More information about the cfe-commits
mailing list