[PATCH] D143479: [Clang] Emit error when caller cannot meet target feature requirement from always-inlining callee
Qiu Chaofan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 7 01:51:48 PST 2023
qiucf created this revision.
qiucf added reviewers: echristo, LiuChen3, erichkeane, GBuella.
Herald added subscribers: steven.zhang, kbarton, nemanjai.
Herald added a project: All.
qiucf requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Currently clang emits error when both `always_inline` and `target` attributes are on callee, but caller doesn't have some feature:
// RUN: %clang_cc1 -triple powerpc64le -target-feature -htm
__attribute__((always_inline))
__attribute__((target("htm")))
void foo() {}
void bar() { foo(); }
// error: always_inline function 'foo' requires target feature 'htm', but would be inlined into function 'bar' that is compiled without support for 'htm'
But when the `always_inline` attribute is on caller, clang has no diagnose. If any builtin or inline asm really need the feature, backend will crash.
// RUN: %clang_cc1 -triple powerpc64le -target-feature +htm
__attribute__((always_inline))
void foo() {
// No error, but uncommenting line below triggers ICE
// __builtin_ttest();
}
__attribute__((target("no-htm")))
void bar() { foo(); }
This patch will fix the second case.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D143479
Files:
clang/lib/CodeGen/CGCall.cpp
clang/lib/CodeGen/CodeGenFunction.cpp
clang/test/Sema/ppc-attr-target-inline.c
Index: clang/test/Sema/ppc-attr-target-inline.c
===================================================================
--- /dev/null
+++ clang/test/Sema/ppc-attr-target-inline.c
@@ -0,0 +1,14 @@
+// REQUIRES: powerpc-registered-target
+// RUN: %clang_cc1 -triple powerpc64le -target-feature +htm -fsyntax-only -emit-llvm %s -verify
+
+__attribute__((always_inline))
+int test1(int *x) {
+ *x = __builtin_ttest();
+ return *x;
+}
+
+__attribute__((target("no-htm")))
+int test2(int *x) {
+ *x = test1(x); // expected-error {{always_inline function 'test1' requires target feature 'htm', but would be inlined into function 'test2' that is compiled without support for 'htm'}}
+ return 0;
+}
Index: clang/lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.cpp
+++ clang/lib/CodeGen/CodeGenFunction.cpp
@@ -2611,6 +2611,16 @@
}))
CGM.getDiags().Report(Loc, diag::err_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()))
+ CGM.getDiags().Report(Loc, diag::err_function_needs_feature)
+ << FD->getDeclName() << TargetDecl->getDeclName() << F.getKey();
+ }
}
}
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -4761,7 +4761,8 @@
// 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>())
+ (TargetDecl->hasAttr<TargetAttr>() ||
+ (CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>())))
checkTargetFeatures(Loc, FD);
// Some architectures (such as x86-64) have the ABI changed based on
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143479.495426.patch
Type: text/x-patch
Size: 2248 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230207/e4c96875/attachment.bin>
More information about the cfe-commits
mailing list