[clang] [clang][sema] forbid vector_size attr when specify `-mgeneral-regs-only` on x86 (PR #75350)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 13 08:32:46 PST 2023


https://github.com/knightXun updated https://github.com/llvm/llvm-project/pull/75350

>From b4fcae6b7f80a9888d361ba24ce8fb5ecf1e2df3 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Wed, 13 Dec 2023 23:45:47 +0800
Subject: [PATCH 1/2] [clang][sema] forbid vector_size attr when specify
 `-mgeneral-regs-only` on x86 fix issue:
 https://github.com/llvm/llvm-project/issues/75301

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  4 +++-
 clang/lib/Sema/SemaType.cpp                   | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 94e97a891baedc..8216861e162828 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3146,8 +3146,10 @@ def err_attribute_bad_neon_vector_size : Error<
   "Neon vector size must be 64 or 128 bits">;
 def err_attribute_invalid_sve_type : Error<
   "%0 attribute applied to non-SVE type %1">;
+def err_attribute_x86_feature_gro_vector_size_unsupported : Error<
+  "vector size is not supported when '-mgeneral-regs-only' is specified">;
 def err_attribute_bad_sve_vector_size : Error<
-  "invalid SVE vector size '%0', must match value set by "
+  "invalid vector size '%0', must match value set by "
   "'-msve-vector-bits' ('%1')">;
 def err_attribute_arm_feature_sve_bits_unsupported : Error<
   "%0 is only supported when '-msve-vector-bits=<bits>' is specified with a "
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 83610503ed9b16..0a24c9325dfa68 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -8251,6 +8251,25 @@ static void HandleVectorSizeAttr(QualType &CurType, const ParsedAttr &Attr,
     return;
   }
 
+  // check -mgeneral-regs-only is specified
+  const TargetInfo &targetInfo = S.getASTContext().getTargetInfo();
+  llvm::Triple::ArchType arch = targetInfo.getTriple().getArch();
+  const auto HasFeature = [](const clang::TargetOptions &targetOpts,
+                             const std::string &feature) {
+    return std::find(targetOpts.Features.begin(), targetOpts.Features.end(),
+                     feature) != targetOpts.Features.end();
+  };
+  if (CurType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
+    if (arch == llvm::Triple::x86_64 &&
+        HasFeature(targetInfo.getTargetOpts(), "-x87") &&
+        HasFeature(targetInfo.getTargetOpts(), "-mmx") &&
+        HasFeature(targetInfo.getTargetOpts(), "-sse")) {
+      S.Diag(Attr.getLoc(),
+             diag::err_attribute_x86_feature_gro_vector_size_unsupported);
+      Attr.setInvalid();
+      return;
+    }
+  }
   Expr *SizeExpr = Attr.getArgAsExpr(0);
   QualType T = S.BuildVectorType(CurType, SizeExpr, Attr.getLoc());
   if (!T.isNull())

>From 882c565b52d6dab5344c6a4ccf4c27b75f680a10 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Thu, 14 Dec 2023 00:32:02 +0800
Subject: [PATCH 2/2] fix error message

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 8216861e162828..2c87d8eb2a5ae5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3147,9 +3147,9 @@ def err_attribute_bad_neon_vector_size : Error<
 def err_attribute_invalid_sve_type : Error<
   "%0 attribute applied to non-SVE type %1">;
 def err_attribute_x86_feature_gro_vector_size_unsupported : Error<
-  "vector size is not supported when '-mgeneral-regs-only' is specified">;
+  "vector_size is not supported when '-mgeneral-regs-only' is specified on x86">;
 def err_attribute_bad_sve_vector_size : Error<
-  "invalid vector size '%0', must match value set by "
+  "invalid SVE vector size '%0', must match value set by "
   "'-msve-vector-bits' ('%1')">;
 def err_attribute_arm_feature_sve_bits_unsupported : Error<
   "%0 is only supported when '-msve-vector-bits=<bits>' is specified with a "



More information about the cfe-commits mailing list