[clang] [AArch64][SME] Add diagnostics to CheckConstexprFunctionDefinition (PR #121777)

Kerry McLaughlin via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 6 07:32:24 PST 2025


https://github.com/kmclaughlin-arm created https://github.com/llvm/llvm-project/pull/121777

CheckFunctionDeclaration emits diagnostics if any SME attributes are used
by a function definition without the required +sme or +sme2 target features.
This patch adds similar diagnostics to CheckConstexprFunctionDefinition to
ensure this emits the same errors when attributes such as `__arm_new("za")`
are found without +sme/+sme2.

>From 00772b871de43a5e30aca2a65a89675117cafbf1 Mon Sep 17 00:00:00 2001
From: Kerry McLaughlin <kerry.mclaughlin at arm.com>
Date: Tue, 31 Dec 2024 17:22:02 +0000
Subject: [PATCH] [AArch64][SME] Add diagnostics to
 CheckConstexprFunctionDefinition

CheckFunctionDeclaration emits diagnostics if any SME attributes are used
by a function definition without the required +sme or +sme2 target features.
This patch adds similar diagnostics to CheckConstexprFunctionDefinition to
ensure this emits the same errors when attributes such as __arm_new("za")
are found without +sme/+sme2.
---
 clang/lib/Sema/SemaDeclCXX.cpp                | 22 +++++++++++++++++++
 ...-sme-func-attrs-without-target-feature.cpp |  8 ++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c5a72cf812ebc9..3ee26ebabcfdd5 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -1854,6 +1854,28 @@ bool Sema::CheckConstexprFunctionDefinition(const FunctionDecl *NewFD,
     }
   }
 
+  if (Context.getTargetInfo().getTriple().isAArch64()) {
+    const auto *Attr = NewFD->getAttr<ArmNewAttr>();
+    bool LocallyStreaming = NewFD->hasAttr<ArmLocallyStreamingAttr>();
+    llvm::StringMap<bool> FeatureMap;
+    Context.getFunctionFeatureMap(FeatureMap, NewFD);
+    if (!FeatureMap.contains("sme") && LocallyStreaming) {
+      Diag(NewFD->getLocation(),
+           diag::err_sme_definition_using_sm_in_non_sme_target);
+      return false;
+    }
+    if (Attr && Attr->isNewZA() && !FeatureMap.contains("sme")) {
+      Diag(NewFD->getLocation(),
+           diag::err_sme_definition_using_za_in_non_sme_target);
+      return false;
+    }
+    if (Attr && Attr->isNewZT0() && !FeatureMap.contains("sme2")) {
+      Diag(NewFD->getLocation(),
+           diag::err_sme_definition_using_zt0_in_non_sme2_target);
+      return false;
+    }
+  }
+
   // - each of its parameter types shall be a literal type; (removed in C++23)
   if (!getLangOpts().CPlusPlus23 &&
       !CheckConstexprParameterTypes(*this, NewFD, Kind))
diff --git a/clang/test/Sema/aarch64-sme-func-attrs-without-target-feature.cpp b/clang/test/Sema/aarch64-sme-func-attrs-without-target-feature.cpp
index ec6bb6f5035784..a046b5aa1325cc 100644
--- a/clang/test/Sema/aarch64-sme-func-attrs-without-target-feature.cpp
+++ b/clang/test/Sema/aarch64-sme-func-attrs-without-target-feature.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -std=c++23 -fsyntax-only -verify %s
 
 // This test is testing the diagnostics that Clang emits when compiling without '+sme'.
 
@@ -48,3 +48,9 @@ void streaming_compatible_def2(void (*streaming_fn_ptr)(void) __arm_streaming,
 // Also test when call-site is not a function.
 int streaming_decl_ret_int() __arm_streaming;
 int x = streaming_decl_ret_int(); // expected-error {{call to a streaming function requires 'sme'}}
+
+void sme_attrs_method_decls() {
+  [&] __arm_locally_streaming () { return; }();  // expected-error {{function executed in streaming-SVE mode requires 'sme'}}
+  [&] __arm_new("za") () { return; }();  // expected-error {{function using ZA state requires 'sme'}}
+  [&] __arm_new("zt0") () { return; }();  // expected-error {{function using ZT0 state requires 'sme2'}}
+}



More information about the cfe-commits mailing list