https://github.com/sdesmalen-arm created 
https://github.com/llvm/llvm-project/pull/150592

Previously Clang would give an unwarranted error on the capture of '&a' in the 
function below, even though the parent function and the lambda are both 
`__arm_streaming` functions, when the target is compiled with +sme only.

  void test_both_streaming(int32_t *out) __arm_streaming {
    svint32_t a;
    [&a, &out]() __arm_streaming {
      ^
      error: SVE vector type 'svint32_t' (aka '__SVInt32_t') cannot be used in 
a non-streaming function

      a = svdup_s32(1);
      svst1(svptrue_b32(), out, a);
    }();
  }

That seems to happen because when `checkTypeSupport` is called the 
`FunctionDecl` context of the lambda isn't yet complete and `FD->getType()` 
returns a Null `QualTy`.

This is loosely related to #94976 which removed a `FD->hasBody()` check in the 
same place, because `hasBody()` may incorrectly return `false` when Clang is 
still processing a function.

>From edb2a166583a9de1aa885bc83fa7cd860cbbd9e6 Mon Sep 17 00:00:00 2001
From: Sander de Smalen <sander.desma...@arm.com>
Date: Thu, 24 Jul 2025 13:43:44 +0000
Subject: [PATCH] [Clang][AArch64] Remove unwarranted 'cannot be used in
 non-streaming mode' diagnostic.

Previously Clang would give an unwarranted error on the capture of '&a'
in the function below, even though the parent function and the lambda
are both `__arm_streaming` functions, when the target is compiled with
+sme only.

  void test_both_streaming(int32_t *out) __arm_streaming {
    svint32_t a;
    [&a, &out]() __arm_streaming {
      ^
      error: SVE vector type 'svint32_t' (aka '__SVInt32_t') cannot be used in 
a non-streaming function

      a = svdup_s32(1);
      svst1(svptrue_b32(), out, a);
    }();
  }

That seems to happen because when `checkTypeSupport` is called
the `FunctionDecl` context of the lambda isn't yet complete and
`FD->getType()` returns a Null `QualTy`.
---
 clang/lib/AST/Decl.cpp                        |  8 ++---
 clang/lib/Sema/Sema.cpp                       |  5 ++-
 .../Sema/aarch64-sme-attrs-without-sve.cpp    | 31 +++++++++++++++++++
 3 files changed, 36 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/Sema/aarch64-sme-attrs-without-sve.cpp

diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 83fcd87aec2f8..748d960e63431 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5988,11 +5988,9 @@ bool clang::IsArmStreamingFunction(const FunctionDecl 
*FD,
     if (FD->hasAttr<ArmLocallyStreamingAttr>())
       return true;
 
-  if (const Type *Ty = FD->getType().getTypePtrOrNull())
-    if (const auto *FPT = Ty->getAs<FunctionProtoType>())
-      if (FPT->getAArch64SMEAttributes() &
-          FunctionType::SME_PStateSMEnabledMask)
-        return true;
+  if (const auto *FPT = FD->getType()->getAs<FunctionProtoType>())
+    if (FPT->getAArch64SMEAttributes() & FunctionType::SME_PStateSMEnabledMask)
+      return true;
 
   return false;
 }
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 56608e990fd50..98e155d27ef13 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -2249,16 +2249,15 @@ void Sema::checkTypeSupport(QualType Ty, SourceLocation 
Loc, ValueDecl *D) {
     }
 
     // Don't allow SVE types in functions without a SVE target.
-    if (Ty->isSVESizelessBuiltinType() && FD) {
+    if (Ty->isSVESizelessBuiltinType() && FD && !FD->getType().isNull()) {
       llvm::StringMap<bool> CallerFeatureMap;
       Context.getFunctionFeatureMap(CallerFeatureMap, FD);
       if (!Builtin::evaluateRequiredTargetFeatures("sve", CallerFeatureMap)) {
         if (!Builtin::evaluateRequiredTargetFeatures("sme", CallerFeatureMap))
           Diag(Loc, diag::err_sve_vector_in_non_sve_target) << Ty;
         else if (!IsArmStreamingFunction(FD,
-                                         /*IncludeLocallyStreaming=*/true)) {
+                                         /*IncludeLocallyStreaming=*/true))
           Diag(Loc, diag::err_sve_vector_in_non_streaming_function) << Ty;
-        }
       }
     }
 
diff --git a/clang/test/Sema/aarch64-sme-attrs-without-sve.cpp 
b/clang/test/Sema/aarch64-sme-attrs-without-sve.cpp
new file mode 100644
index 0000000000000..950f26e3a144e
--- /dev/null
+++ b/clang/test/Sema/aarch64-sme-attrs-without-sve.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme 
-fsyntax-only -verify %s
+
+#include <arm_sme.h>
+
+void test_streaming(svint32_t *out, svint32_t *in) __arm_streaming {
+  *out = *in;
+}
+
+void test_non_streaming(svint32_t *out, svint32_t *in) {
+  *out = *in; // expected-error {{SVE vector type 'svint32_t' (aka 
'__SVInt32_t') cannot be used in a non-streaming function}} \
+                 expected-error {{SVE vector type 'svint32_t' (aka 
'__SVInt32_t') cannot be used in a non-streaming function}}
+}
+
+// This previously led to a diagnostic that '&a' could not be used in a 
non-streaming function,
+// even though all functions are streaming.
+void test_both_streaming(int32_t *out) __arm_streaming{
+  svint32_t a;
+  [&a, &out]() __arm_streaming {
+    a = svdup_s32(1);
+    svst1(svptrue_b32(), out, a);
+  }();
+}
+
+void test_lambda_streaming(int32_t *out) {
+  svint32_t a; // expected-error {{SVE vector type 'svint32_t' (aka 
'__SVInt32_t') cannot be used in a non-streaming function}}
+  [&a, &out]() __arm_streaming {
+    a = 1;
+    svst1(svptrue_b32(), out, a);
+  }();
+}
+

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to