Skip to content

[Clang][AArch64] Fix checkArmStreamingBuiltin for 'sve-b16b16' #109420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

sdesmalen-arm
Copy link
Collaborator

The implementation made the assumption that any feature starting with "sve" meant that this was an SVE feature. This is not the case for "sve-b16b16", as this is a feature that applies to both SVE and SME.

This meant that:

  __attribute__((target("+sme2,+sve2,+sve-b16b16")))
  svbfloat16_t foo(svbfloat16_t a, svbfloat16_t b, svbfloat16_t c)
                                                      __arm_streaming {
      return svclamp_bf16(a, b, c);
  }

would result in an incorrect diagnostic saying that svclamp_bf16 could only be used in non-streaming functions.

The implementation made the assumption that any feature starting with
"sve" meant that this was an SVE feature. This is not the case for
"sve-b16b16", as this is a feature that applies to both SVE and SME.

This meant that:

  __attribute__((target("+sme2,+sve2,+sve-b16b16")))
  svbfloat16_t foo(svbfloat16_t a, svbfloat16_t b, svbfloat16_t c)
                                                      __arm_streaming {
      return svclamp_bf16(a, b, c);
  }

would result in an incorrect diagnostic saying that `svclamp_bf16`
could only be used in non-streaming functions.
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:ARM backend:AArch64 clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2024

@llvm/pr-subscribers-backend-arm
@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-clang

Author: Sander de Smalen (sdesmalen-arm)

Changes

The implementation made the assumption that any feature starting with "sve" meant that this was an SVE feature. This is not the case for "sve-b16b16", as this is a feature that applies to both SVE and SME.

This meant that:

  __attribute__((target("+sme2,+sve2,+sve-b16b16")))
  svbfloat16_t foo(svbfloat16_t a, svbfloat16_t b, svbfloat16_t c)
                                                      __arm_streaming {
      return svclamp_bf16(a, b, c);
  }

would result in an incorrect diagnostic saying that svclamp_bf16 could only be used in non-streaming functions.


Full diff: https://github.com/llvm/llvm-project/pull/109420.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaARM.cpp (+13-8)
  • (modified) clang/test/Sema/aarch64-streaming-sme-or-nonstreaming-sve-builtins.c (+6)
diff --git a/clang/lib/Sema/SemaARM.cpp b/clang/lib/Sema/SemaARM.cpp
index efde354860de43..fba1453e5d38fc 100644
--- a/clang/lib/Sema/SemaARM.cpp
+++ b/clang/lib/Sema/SemaARM.cpp
@@ -567,15 +567,18 @@ static bool checkArmStreamingBuiltin(Sema &S, CallExpr *TheCall,
   // * When compiling for SVE only, the caller must be in non-streaming mode.
   // * When compiling for both SVE and SME, the caller can be in either mode.
   if (BuiltinType == SemaARM::VerifyRuntimeMode) {
-    auto DisableFeatures = [](llvm::StringMap<bool> &Map, StringRef S) {
-      for (StringRef K : Map.keys())
-        if (K.starts_with(S))
-          Map[K] = false;
-    };
-
     llvm::StringMap<bool> CallerFeatureMapWithoutSVE;
     S.Context.getFunctionFeatureMap(CallerFeatureMapWithoutSVE, FD);
-    DisableFeatures(CallerFeatureMapWithoutSVE, "sve");
+    CallerFeatureMapWithoutSVE["sve"] = false;
+    CallerFeatureMapWithoutSVE["sve2"] = false;
+    CallerFeatureMapWithoutSVE["sve2p1"] = false;
+    // FIXME: This list must be updated with future extensions, because when
+    // an intrinsic is enabled by (sve2p1|sme2p1), disabling just "sve" is
+    // not sufficient, as the feature dependences are not resolved.
+    // At the moment, it should be sufficient to test the 'base' architectural
+    // support for SVE and SME, which must always be provided in the
+    // target guard. e.g. TargetGuard = "sve-b16b16" without "sme" or "sve"
+    // is not sufficient.
 
     // Avoid emitting diagnostics for a function that can never compile.
     if (FnType == SemaARM::ArmStreaming && !CallerFeatureMapWithoutSVE["sme"])
@@ -583,7 +586,9 @@ static bool checkArmStreamingBuiltin(Sema &S, CallExpr *TheCall,
 
     llvm::StringMap<bool> CallerFeatureMapWithoutSME;
     S.Context.getFunctionFeatureMap(CallerFeatureMapWithoutSME, FD);
-    DisableFeatures(CallerFeatureMapWithoutSME, "sme");
+    CallerFeatureMapWithoutSME["sme"] = false;
+    CallerFeatureMapWithoutSME["sme2"] = false;
+    CallerFeatureMapWithoutSME["sme2p1"] = false;
 
     // We know the builtin requires either some combination of SVE flags, or
     // some combination of SME flags, but we need to figure out which part
diff --git a/clang/test/Sema/aarch64-streaming-sme-or-nonstreaming-sve-builtins.c b/clang/test/Sema/aarch64-streaming-sme-or-nonstreaming-sve-builtins.c
index 45776eb13e4fbc..792d79ee3e600d 100644
--- a/clang/test/Sema/aarch64-streaming-sme-or-nonstreaming-sve-builtins.c
+++ b/clang/test/Sema/aarch64-streaming-sme-or-nonstreaming-sve-builtins.c
@@ -38,6 +38,12 @@ svfloat32_t good6(svfloat32_t a, svfloat32_t b, svfloat32_t c) __arm_streaming_c
   return svclamp(a, b, c);
 }
 
+// Test that the +sve-b16b16 is not considered an SVE flag (it applies to both)
+__attribute__((target("+sme2,+sve2,+sve-b16b16")))
+svbfloat16_t good7(svbfloat16_t a, svbfloat16_t b, svbfloat16_t c) __arm_streaming {
+    return svclamp_bf16(a, b, c);
+}
+
 // Without '+sme2', the builtin is only valid in non-streaming mode.
 __attribute__((target("+sve2p1,+sme")))
 svfloat32_t bad1(svfloat32_t a, svfloat32_t b, svfloat32_t c) __arm_streaming {

Comment on lines 575 to 581
// FIXME: This list must be updated with future extensions, because when
// an intrinsic is enabled by (sve2p1|sme2p1), disabling just "sve" is
// not sufficient, as the feature dependences are not resolved.
// At the moment, it should be sufficient to test the 'base' architectural
// support for SVE and SME, which must always be provided in the
// target guard. e.g. TargetGuard = "sve-b16b16" without "sme" or "sve"
// is not sufficient.
Copy link
Collaborator

@paulwalker-arm paulwalker-arm Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to add code (SVEEmitter?) to parse the target guards to ensure there is at least one of the required SVE and SME features referenced. This should link to this code as something to update if the intent really is to add a new "base" option.

// 'checkArmStreamingBuiltin'!
if (!Def->getSVEGuard().empty() &&
!verifyGuard(Def->getSVEGuard(),
{"sve", "sve2", "sve2p1", "sve2-aes", "sve2-sha3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should need to have "f32mm" and "f64mm" here (and the CallerFeatureMapWithoutSVE part). This works at the moment as the only intrinsics that have these in their target-guard also redundantly include sve and are invalid in streaming mode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing those out!

It's worth saying that we could also implement things differently and require all SVE target guards to have "sve/sve2/sve2p1" as a base, such that let SVETargetGuard = "sve2-aes" becomes let SVETargetGuard = "sve2,sve2-aes". That means we need to refactor some of the target guards in the .td files, but it means we don't have to continually add all features that imply sve/sve2/sve2p1 to this list (and the list in SemaARM.cpp). For end-users I think it doesn't matter, the only difference is in the diagnostic which prints the required features for the intrinsic. Perhaps that is the better way forward.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much prefer this idea.

Copy link
Contributor

@SpencerAbson SpencerAbson Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I think we would definitely benefit in the long-run if we do not have to modify this code when features are added or changed.

(For future work) is it completely crazy to suggest resolving dependency chains back to SVE or SME (or both/neither) using ExtensionDependencies, which is constructed based on the feature definitions in AArch64Features.td?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you might not need to change arm_sve.td because you could have SVETargetGuard(x) imply sve,(x)? Likewise for SME.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe so.

@sdesmalen-arm sdesmalen-arm force-pushed the fix-check-arm-streaming-builtin-clang branch from e553f8b to 49c3ed3 Compare October 2, 2024 16:38
@sdesmalen-arm sdesmalen-arm merged commit f22e6d5 into llvm:main Oct 8, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AArch64 backend:ARM clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants