[clang] [Clang]Throw frontend error for target feature mismatch when using `flatten` attribute (PR #150044)
Abhishek Kaushik via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 10 06:07:22 PDT 2025
https://github.com/abhishek-kaushik22 updated https://github.com/llvm/llvm-project/pull/150044
>From 480f5521ff5f0767ea295ef61a054f35961be3b0 Mon Sep 17 00:00:00 2001
From: Abhishek Kaushik <abhishek.kaushik at intel.com>
Date: Tue, 22 Jul 2025 21:03:17 +0530
Subject: [PATCH 1/2] [Clang]Throw frontend error for target feature mismatch
when using flatten attribute
Fixes #149866
---
clang/lib/CodeGen/CGCall.cpp | 8 +++++---
clang/test/CodeGen/target-features-error-3.c | 12 ++++++++++++
2 files changed, 17 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGen/target-features-error-3.c
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 0bceecec6e555..589716460b2de 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -5232,9 +5232,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
// since otherwise we could be making a conditional call after a check for
// the proper cpu features (and it won't cause code generation issues due to
// function based code generation).
- if (TargetDecl->hasAttr<AlwaysInlineAttr>() &&
- (TargetDecl->hasAttr<TargetAttr>() ||
- (CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>())))
+ if ((TargetDecl->hasAttr<AlwaysInlineAttr>() &&
+ (TargetDecl->hasAttr<TargetAttr>() ||
+ (CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>()))) ||
+ (CurFuncDecl && CurFuncDecl->hasAttr<FlattenAttr>() &&
+ TargetDecl->hasAttr<TargetAttr>()))
checkTargetFeatures(Loc, FD);
}
diff --git a/clang/test/CodeGen/target-features-error-3.c b/clang/test/CodeGen/target-features-error-3.c
new file mode 100644
index 0000000000000..8900edfa257c7
--- /dev/null
+++ b/clang/test/CodeGen/target-features-error-3.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -verify -o -
+
+typedef double __v2df __attribute__((__vector_size__(16)));
+
+__v2df __attribute__((target("sse4.1"))) foo() {
+ __v2df v = {0.0, 0.0};
+ return __builtin_ia32_roundpd(v, 2);
+}
+
+__v2df __attribute__((flatten)) bar() {
+ return foo(); // expected-error {{always_inline function 'foo' requires target feature 'sse4.1', but would be inlined into function 'bar' that is compiled without support for 'sse4.1'}}
+}
>From 85f5b283540c9706aa4b45061b92319710e97bdf Mon Sep 17 00:00:00 2001
From: Abhishek Kaushik <abhishek.kaushik at intel.com>
Date: Sun, 10 Aug 2025 18:37:09 +0530
Subject: [PATCH 2/2] Add new diagnostic error for flatten
---
.../clang/Basic/DiagnosticFrontendKinds.td | 4 ++
clang/lib/CodeGen/CGCall.cpp | 3 +-
clang/lib/CodeGen/CodeGenFunction.cpp | 45 +++++++++++++------
clang/test/CodeGen/target-features-error-3.c | 2 +-
clang/test/CodeGen/target-features-error-4.c | 12 +++++
clang/test/CodeGen/target-features-error-5.c | 12 +++++
6 files changed, 62 insertions(+), 16 deletions(-)
create mode 100644 clang/test/CodeGen/target-features-error-4.c
create mode 100644 clang/test/CodeGen/target-features-error-5.c
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 8a8db27490f06..d8f0a6e4f421d 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -287,6 +287,10 @@ def err_function_needs_feature : Error<
"always_inline function %1 requires target feature '%2', but would "
"be inlined into function %0 that is compiled without support for '%2'">;
+def err_flatten_function_needs_feature
+ : Error<"flatten function %0 calls %1 which requires target feature '%2', "
+ "but the caller is compiled without support for '%2'">;
+
let CategoryName = "Codegen ABI Check" in {
def err_function_always_inline_attribute_mismatch : Error<
"always_inline function %1 and its caller %0 have mismatching %2 attributes">;
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 68054d19b08fa..a40e1f02e0417 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -5247,7 +5247,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
(TargetDecl->hasAttr<TargetAttr>() ||
(CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>()))) ||
(CurFuncDecl && CurFuncDecl->hasAttr<FlattenAttr>() &&
- TargetDecl->hasAttr<TargetAttr>()))
+ (CurFuncDecl->hasAttr<TargetAttr>() ||
+ TargetDecl->hasAttr<TargetAttr>())))
checkTargetFeatures(Loc, FD);
}
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index d077ee50856b7..2abe5cc4f9547 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -2827,6 +2827,9 @@ void CodeGenFunction::checkTargetFeatures(SourceLocation Loc,
if (!FD)
return;
+ bool IsAlwaysInline = TargetDecl->hasAttr<AlwaysInlineAttr>();
+ bool IsFlatten = FD && FD->hasAttr<FlattenAttr>();
+
// Grab the required features for the call. For a builtin this is listed in
// the td file with the default cpu, for an always_inline function this is any
// listed cpu and any listed features.
@@ -2869,25 +2872,39 @@ void CodeGenFunction::checkTargetFeatures(SourceLocation Loc,
if (F.getValue())
ReqFeatures.push_back(F.getKey());
}
- if (!llvm::all_of(ReqFeatures, [&](StringRef Feature) {
- if (!CallerFeatureMap.lookup(Feature)) {
- MissingFeature = Feature.str();
- return false;
- }
- return true;
- }) && !IsHipStdPar)
- CGM.getDiags().Report(Loc, diag::err_function_needs_feature)
- << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;
+ if (!llvm::all_of(ReqFeatures,
+ [&](StringRef Feature) {
+ if (!CallerFeatureMap.lookup(Feature)) {
+ MissingFeature = Feature.str();
+ return false;
+ }
+ return true;
+ }) &&
+ !IsHipStdPar) {
+ if (IsAlwaysInline)
+ CGM.getDiags().Report(Loc, diag::err_function_needs_feature)
+ << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;
+ else if (IsFlatten)
+ CGM.getDiags().Report(Loc, diag::err_flatten_function_needs_feature)
+ << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;
+ }
+
} else if (!FD->isMultiVersion() && FD->hasAttr<TargetAttr>()) {
llvm::StringMap<bool> CalleeFeatureMap;
CGM.getContext().getFunctionFeatureMap(CalleeFeatureMap, TargetDecl);
for (const auto &F : CalleeFeatureMap) {
- if (F.getValue() && (!CallerFeatureMap.lookup(F.getKey()) ||
- !CallerFeatureMap.find(F.getKey())->getValue()) &&
- !IsHipStdPar)
- CGM.getDiags().Report(Loc, diag::err_function_needs_feature)
- << FD->getDeclName() << TargetDecl->getDeclName() << F.getKey();
+ if (F.getValue() &&
+ (!CallerFeatureMap.lookup(F.getKey()) ||
+ !CallerFeatureMap.find(F.getKey())->getValue()) &&
+ !IsHipStdPar) {
+ if (IsAlwaysInline)
+ CGM.getDiags().Report(Loc, diag::err_function_needs_feature)
+ << FD->getDeclName() << TargetDecl->getDeclName() << F.getKey();
+ else if (IsFlatten)
+ CGM.getDiags().Report(Loc, diag::err_flatten_function_needs_feature)
+ << FD->getDeclName() << TargetDecl->getDeclName() << F.getKey();
+ }
}
}
}
diff --git a/clang/test/CodeGen/target-features-error-3.c b/clang/test/CodeGen/target-features-error-3.c
index 8900edfa257c7..8ecc7369b4723 100644
--- a/clang/test/CodeGen/target-features-error-3.c
+++ b/clang/test/CodeGen/target-features-error-3.c
@@ -8,5 +8,5 @@ __v2df __attribute__((target("sse4.1"))) foo() {
}
__v2df __attribute__((flatten)) bar() {
- return foo(); // expected-error {{always_inline function 'foo' requires target feature 'sse4.1', but would be inlined into function 'bar' that is compiled without support for 'sse4.1'}}
+ return foo(); // expected-error {{flatten function 'bar' calls 'foo' which requires target feature 'sse4.1', but the caller is compiled without support for 'sse4.1'}}
}
diff --git a/clang/test/CodeGen/target-features-error-4.c b/clang/test/CodeGen/target-features-error-4.c
new file mode 100644
index 0000000000000..a6c2cbf37f92b
--- /dev/null
+++ b/clang/test/CodeGen/target-features-error-4.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -verify -o -
+
+typedef double __v2df __attribute__((__vector_size__(16)));
+
+__v2df __attribute__((target("sse4.1"))) foo() {
+ __v2df v = {0.0, 0.0};
+ return __builtin_ia32_roundpd(v, 2);
+}
+
+__v2df __attribute__((target("no-sse4.1"), flatten)) bar() {
+ return foo(); // expected-error {{flatten function 'bar' calls 'foo' which requires target feature 'sse4.1', but the caller is compiled without support for 'sse4.1'}}
+}
diff --git a/clang/test/CodeGen/target-features-error-5.c b/clang/test/CodeGen/target-features-error-5.c
new file mode 100644
index 0000000000000..625a2ba2b7d67
--- /dev/null
+++ b/clang/test/CodeGen/target-features-error-5.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -target-feature +sse4.1 -S -verify -o -
+
+typedef double __v2df __attribute__((__vector_size__(16)));
+
+__v2df foo() {
+ __v2df v = {0.0, 0.0};
+ return __builtin_ia32_roundpd(v, 2);
+}
+
+__v2df __attribute__((target("no-sse4.1"), flatten)) bar() {
+ return foo(); // expected-error {{flatten function 'bar' calls 'foo' which requires target feature 'sse4.1', but the caller is compiled without support for 'sse4.1'}}
+}
More information about the cfe-commits
mailing list