From b33063780116332fb0a60d5c4ff62599a05f205d Mon Sep 17 00:00:00 2001
From: Eric Christopher <echristo@gmail.com>
Date: Sat, 14 Nov 2015 02:38:37 +0000
Subject: [PATCH] Add support for the always_inline + target feature diagnostic
 to print out the first missing target feature that's required and reword the
 diagnostic accordingly.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253121 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Basic/DiagnosticSemaKinds.td |  5 +++--
 lib/CodeGen/CodeGenFunction.cpp            | 17 +++++++++++------
 test/CodeGen/target-features-error-2.c     |  2 +-
 test/CodeGen/target-features-error.c       |  2 +-
 4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 76730e6fc7a..c3b501a8b53 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -432,8 +432,9 @@ 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">;
+    : Error<"always_inline function %1 requires target feature '%2', but would "
+            "be inlined into function %0 that is compiled without support for "
+            "'%2'">;
 def warn_builtin_unknown : Warning<"use of unknown builtin %0">,
   InGroup<ImplicitFunctionDeclare>, DefaultError;
 def warn_dyn_class_memaccess : Warning<
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index b5812c8d314..a425c3e229a 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -1852,7 +1852,8 @@ template void CGBuilderInserter<PreserveNames>::InsertHelper(
 #undef PreserveNames
 
 static bool hasRequiredFeatures(const SmallVectorImpl<StringRef> &ReqFeatures,
-                                CodeGenModule &CGM, const FunctionDecl *FD) {
+                                CodeGenModule &CGM, const FunctionDecl *FD,
+                                std::string &FirstMissing) {
   // If there aren't any required features listed then go ahead and return.
   if (ReqFeatures.empty())
     return false;
@@ -1870,7 +1871,11 @@ static bool hasRequiredFeatures(const SmallVectorImpl<StringRef> &ReqFeatures,
         Feature.split(OrFeatures, "|");
         return std::any_of(OrFeatures.begin(), OrFeatures.end(),
                            [&](StringRef Feature) {
-                             return CallerFeatureMap.lookup(Feature);
+                             if (!CallerFeatureMap.lookup(Feature)) {
+                               FirstMissing = Feature.str();
+                               return false;
+                             }
+                             return true;
                            });
       });
 }
@@ -1893,6 +1898,7 @@ void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
   // 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();
+  std::string MissingFeature;
   if (BuiltinID) {
     SmallVector<StringRef, 1> ReqFeatures;
     const char *FeatureList =
@@ -1901,8 +1907,7 @@ void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
     if (!FeatureList || StringRef(FeatureList) == "")
       return;
     StringRef(FeatureList).split(ReqFeatures, ",");
-
-    if (!hasRequiredFeatures(ReqFeatures, CGM, FD))
+    if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature))
       CGM.getDiags().Report(E->getLocStart(), diag::err_builtin_needs_feature)
           << TargetDecl->getDeclName()
           << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
@@ -1914,8 +1919,8 @@ void CodeGenFunction::checkTargetFeatures(const CallExpr *E,
     CGM.getFunctionFeatureMap(CalleeFeatureMap, TargetDecl);
     for (const auto &F : CalleeFeatureMap)
       ReqFeatures.push_back(F.getKey());
-    if (!hasRequiredFeatures(ReqFeatures, CGM, FD))
+    if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature))
       CGM.getDiags().Report(E->getLocStart(), diag::err_function_needs_feature)
-          << FD->getDeclName() << TargetDecl->getDeclName();
+          << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;
   }
 }
diff --git a/test/CodeGen/target-features-error-2.c b/test/CodeGen/target-features-error-2.c
index 66b8a46a4df..c23d152dcfb 100644
--- a/test/CodeGen/target-features-error-2.c
+++ b/test/CodeGen/target-features-error-2.c
@@ -3,5 +3,5 @@
 #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}}
+  return _mm256_extract_epi32(a, 3); // expected-error {{always_inline function '_mm256_extract_epi32' requires target feature 'sse4.2', but would be inlined into function 'baz' that is compiled without support for 'sse4.2'}}
 }
diff --git a/test/CodeGen/target-features-error.c b/test/CodeGen/target-features-error.c
index c7abbd00433..518f6e6189e 100644
--- a/test/CodeGen/target-features-error.c
+++ b/test/CodeGen/target-features-error.c
@@ -3,6 +3,6 @@ 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}}
+  return foo(4); // expected-error {{always_inline function 'foo' requires target feature 'sse4.2', but would be inlined into function 'bar' that is compiled without support for 'sse4.2'}}
 }
 
-- 
GitLab