summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td3
-rw-r--r--clang/lib/CodeGen/CGExpr.cpp9
-rw-r--r--clang/lib/CodeGen/CodeGenFunction.cpp90
-rw-r--r--clang/test/CodeGen/3dnow-builtins.c4
-rw-r--r--clang/test/CodeGen/avx512vl-builtins.c96
-rw-r--r--clang/test/CodeGen/target-features-error-2.c7
-rw-r--r--clang/test/CodeGen/target-features-error.c8
7 files changed, 94 insertions, 123 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index a43923b069a..81a1f4a8498 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -431,6 +431,9 @@ def err_builtin_definition : Error<"definition of builtin function %0">;
def err_arm_invalid_specialreg : Error<"invalid special register for builtin">;
def err_invalid_cpu_supports : Error<"invalid cpu feature string for builtin">;
def err_builtin_needs_feature : Error<"%0 needs target feature %1">;
+def err_function_needs_feature
+ : Error<"function %0 and always_inline callee function %1 are required to "
+ "have matching target features">;
def warn_builtin_unknown : Warning<"use of unknown builtin %0">,
InGroup<ImplicitFunctionDeclare>, DefaultError;
def warn_dyn_class_memaccess : Warning<
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index d606fe79ff5..8b01094168e 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3747,6 +3747,15 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
assert(CalleeType->isFunctionPointerType() &&
"Call must have function pointer type!");
+ if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
+ // If this isn't an always_inline function we can't guarantee that any
+ // function isn't being used correctly so only check if we have the
+ // attribute and a set of target attributes that might be different from
+ // our default.
+ if (TargetDecl->hasAttr<AlwaysInlineAttr>() &&
+ TargetDecl->hasAttr<TargetAttr>())
+ checkTargetFeatures(E, FD);
+
CalleeType = getContext().getCanonicalType(CalleeType);
const auto *FnType =
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 879c1577dbc..8596f97a3b1 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1843,7 +1843,8 @@ template void CGBuilderInserter<PreserveNames>::InsertHelper(
llvm::BasicBlock::iterator InsertPt) const;
#undef PreserveNames
-// Returns true if we have a valid set of target features.
+// Emits an error if we don't have a valid set of target features for the
+// called function.
void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
const FunctionDecl *TargetDecl) {
// Early exit if this is an indirect call.
@@ -1856,31 +1857,70 @@ void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
if (!FD)
return;
+ // 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.
unsigned BuiltinID = TargetDecl->getBuiltinID();
- const char *FeatureList =
- CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
+ if (BuiltinID) {
+ SmallVector<StringRef, 1> ReqFeatures;
+ const char *FeatureList =
+ CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
+ // Return if the builtin doesn't have any required features.
+ if (!FeatureList || StringRef(FeatureList) == "")
+ return;
+ StringRef(FeatureList).split(ReqFeatures, ",");
- if (!FeatureList || StringRef(FeatureList) == "")
- return;
+ // If there aren't any required features listed then go ahead and return.
+ if (ReqFeatures.empty())
+ return;
- llvm::StringMap<bool> FeatureMap;
- CGM.getFunctionFeatureMap(FeatureMap, FD);
-
- // If we have at least one of the features in the feature list return
- // true, otherwise return false.
- SmallVector<StringRef, 1> AttrFeatures;
- StringRef(FeatureList).split(AttrFeatures, ",");
- if (!std::all_of(AttrFeatures.begin(), AttrFeatures.end(),
- [&](StringRef &Feature) {
- SmallVector<StringRef, 1> OrFeatures;
- Feature.split(OrFeatures, "|");
- return std::any_of(OrFeatures.begin(), OrFeatures.end(),
- [&](StringRef &Feature) {
- return FeatureMap[Feature];
- });
- }))
- CGM.getDiags().Report(E->getLocStart(), diag::err_builtin_needs_feature)
- << TargetDecl->getDeclName()
- << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
-}
+ // Now build up the set of caller features and verify that all the required
+ // features are there.
+ llvm::StringMap<bool> CallerFeatureMap;
+ CGM.getFunctionFeatureMap(CallerFeatureMap, FD);
+
+ // If we have at least one of the features in the feature list return
+ // true, otherwise return false.
+ if (!std::all_of(
+ ReqFeatures.begin(), ReqFeatures.end(), [&](StringRef &Feature) {
+ SmallVector<StringRef, 1> OrFeatures;
+ Feature.split(OrFeatures, "|");
+ return std::any_of(OrFeatures.begin(), OrFeatures.end(),
+ [&](StringRef &Feature) {
+ return CallerFeatureMap.lookup(Feature);
+ });
+ }))
+ CGM.getDiags().Report(E->getLocStart(), diag::err_builtin_needs_feature)
+ << TargetDecl->getDeclName()
+ << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
+
+ } else if (TargetDecl->hasAttr<TargetAttr>()) {
+ // Get the required features for the callee.
+ SmallVector<StringRef, 1> ReqFeatures;
+ llvm::StringMap<bool> CalleeFeatureMap;
+ CGM.getFunctionFeatureMap(CalleeFeatureMap, TargetDecl);
+ for (const auto &F : CalleeFeatureMap)
+ ReqFeatures.push_back(F.getKey());
+ // If there aren't any required features listed then go ahead and return.
+ if (ReqFeatures.empty())
+ return;
+ // Now get the features that the caller provides.
+ llvm::StringMap<bool> CallerFeatureMap;
+ CGM.getFunctionFeatureMap(CallerFeatureMap, FD);
+
+ // If we have at least one of the features in the feature list return
+ // true, otherwise return false.
+ if (!std::all_of(
+ ReqFeatures.begin(), ReqFeatures.end(), [&](StringRef &Feature) {
+ SmallVector<StringRef, 1> OrFeatures;
+ Feature.split(OrFeatures, "|");
+ return std::any_of(OrFeatures.begin(), OrFeatures.end(),
+ [&](StringRef &Feature) {
+ return CallerFeatureMap.lookup(Feature);
+ });
+ }))
+ CGM.getDiags().Report(E->getLocStart(), diag::err_function_needs_feature)
+ << FD->getDeclName() << TargetDecl->getDeclName();
+ }
+}
diff --git a/clang/test/CodeGen/3dnow-builtins.c b/clang/test/CodeGen/3dnow-builtins.c
index 367c1328b63..2ed26ea7c15 100644
--- a/clang/test/CodeGen/3dnow-builtins.c
+++ b/clang/test/CodeGen/3dnow-builtins.c
@@ -1,6 +1,6 @@
// REQUIRES: x86-registered-target
-// RUN: %clang_cc1 %s -triple=x86_64-unknown-unknown -target-feature +3dnow -emit-llvm -o - -Werror | FileCheck %s
-// RUN: %clang_cc1 %s -triple=x86_64-unknown-unknown -target-feature +3dnow -S -o - -Werror | FileCheck %s --check-prefix=CHECK-ASM
+// RUN: %clang_cc1 %s -triple=x86_64-unknown-unknown -target-feature +3dnowa -emit-llvm -o - -Werror | FileCheck %s
+// RUN: %clang_cc1 %s -triple=x86_64-unknown-unknown -target-feature +3dnowa -S -o - -Werror | FileCheck %s --check-prefix=CHECK-ASM
// Don't include mm_malloc.h, it's system specific.
#define __MM_MALLOC_H
diff --git a/clang/test/CodeGen/avx512vl-builtins.c b/clang/test/CodeGen/avx512vl-builtins.c
index 9d6237e31e6..445513ccfcc 100644
--- a/clang/test/CodeGen/avx512vl-builtins.c
+++ b/clang/test/CodeGen/avx512vl-builtins.c
@@ -5,102 +5,6 @@
#include <immintrin.h>
-__mmask8 test_mm256_cmpeq_epi32_mask(__m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_cmpeq_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.d.256
- return (__mmask8)_mm256_cmpeq_epi32_mask(__a, __b);
-}
-
-__mmask8 test_mm256_mask_cmpeq_epi32_mask(__mmask8 __u, __m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_mask_cmpeq_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.d.256
- return (__mmask8)_mm256_mask_cmpeq_epi32_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm_cmpeq_epi32_mask(__m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_cmpeq_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.d.128
- return (__mmask8)_mm_cmpeq_epi32_mask(__a, __b);
-}
-
-__mmask8 test_mm_mask_cmpeq_epi32_mask(__mmask8 __u, __m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_mask_cmpeq_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.d.128
- return (__mmask8)_mm_mask_cmpeq_epi32_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm256_cmpeq_epi64_mask(__m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_cmpeq_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.q.256
- return (__mmask8)_mm256_cmpeq_epi64_mask(__a, __b);
-}
-
-__mmask8 test_mm256_mask_cmpeq_epi64_mask(__mmask8 __u, __m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_mask_cmpeq_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.q.256
- return (__mmask8)_mm256_mask_cmpeq_epi64_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm_cmpeq_epi64_mask(__m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_cmpeq_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.q.128
- return (__mmask8)_mm_cmpeq_epi64_mask(__a, __b);
-}
-
-__mmask8 test_mm_mask_cmpeq_epi64_mask(__mmask8 __u, __m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_mask_cmpeq_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpeq.q.128
- return (__mmask8)_mm_mask_cmpeq_epi64_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm256_cmpgt_epi32_mask(__m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_cmpgt_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.d.256
- return (__mmask8)_mm256_cmpgt_epi32_mask(__a, __b);
-}
-
-__mmask8 test_mm256_mask_cmpgt_epi32_mask(__mmask8 __u, __m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_mask_cmpgt_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.d.256
- return (__mmask8)_mm256_mask_cmpgt_epi32_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm_cmpgt_epi32_mask(__m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_cmpgt_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.d.128
- return (__mmask8)_mm_cmpgt_epi32_mask(__a, __b);
-}
-
-__mmask8 test_mm_mask_cmpgt_epi32_mask(__mmask8 __u, __m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_mask_cmpgt_epi32_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.d.128
- return (__mmask8)_mm_mask_cmpgt_epi32_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm256_cmpgt_epi64_mask(__m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_cmpgt_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.q.256
- return (__mmask8)_mm256_cmpgt_epi64_mask(__a, __b);
-}
-
-__mmask8 test_mm256_mask_cmpgt_epi64_mask(__mmask8 __u, __m256i __a, __m256i __b) {
- // CHECK-LABEL: @test_mm256_mask_cmpgt_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.q.256
- return (__mmask8)_mm256_mask_cmpgt_epi64_mask(__u, __a, __b);
-}
-
-__mmask8 test_mm_cmpgt_epi64_mask(__m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_cmpgt_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.q.128
- return (__mmask8)_mm_cmpgt_epi64_mask(__a, __b);
-}
-
-__mmask8 test_mm_mask_cmpgt_epi64_mask(__mmask8 __u, __m128i __a, __m128i __b) {
- // CHECK-LABEL: @test_mm_mask_cmpgt_epi64_mask
- // CHECK: @llvm.x86.avx512.mask.pcmpgt.q.128
- return (__mmask8)_mm_mask_cmpgt_epi64_mask(__u, __a, __b);
-}
-
__mmask8 test_mm_cmpeq_epu32_mask(__m128i __a, __m128i __b) {
// CHECK-LABEL: @test_mm_cmpeq_epu32_mask
// CHECK: @llvm.x86.avx512.mask.ucmp.d.128(<4 x i32> {{.*}}, <4 x i32> {{.*}}, i32 0, i8 -1)
diff --git a/clang/test/CodeGen/target-features-error-2.c b/clang/test/CodeGen/target-features-error-2.c
new file mode 100644
index 00000000000..66b8a46a4df
--- /dev/null
+++ b/clang/test/CodeGen/target-features-error-2.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -verify -o -
+#define __MM_MALLOC_H
+#include <x86intrin.h>
+
+int baz(__m256i a) {
+ return _mm256_extract_epi32(a, 3); // expected-error {{function 'baz' and always_inline callee function '_mm256_extract_epi32' are required to have matching target features}}
+}
diff --git a/clang/test/CodeGen/target-features-error.c b/clang/test/CodeGen/target-features-error.c
new file mode 100644
index 00000000000..c7abbd00433
--- /dev/null
+++ b/clang/test/CodeGen/target-features-error.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -verify -o -
+int __attribute__((target("avx"), always_inline)) foo(int a) {
+ return a + 4;
+}
+int bar() {
+ return foo(4); // expected-error {{function 'bar' and always_inline callee function 'foo' are required to have matching target features}}
+}
+
OpenPOWER on IntegriCloud