Skip to content

[clang] Do not allow unorderable features in [[gnu::target{,_clones}]] #98426

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
merged 2 commits into from
Jan 14, 2025

Conversation

DanShaders
Copy link
Contributor

This partially addresses #98244.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-clang

Author: Dan Klishch (DanShaders)

Changes

This partially addresses #98244.


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

5 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+3-1)
  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+2-1)
  • (modified) clang/test/Sema/attr-target-clones.c (+3)
  • (modified) clang/test/Sema/attr-target-mv.c (+14)
  • (modified) clang/test/Sema/attr-target.c (+2)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d1c7b9d5ae507..4d0c2cfaee965 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -10958,7 +10958,9 @@ static bool CheckMultiVersionValue(Sema &S, const FunctionDecl *FD) {
       }
 
       if (!TargetInfo.validateCpuSupports(BareFeat) ||
-          !TargetInfo.isValidFeatureName(BareFeat)) {
+          !TargetInfo.isValidFeatureName(BareFeat) ||
+          (BareFeat != "default" &&
+           TargetInfo.multiVersionSortPriority(BareFeat) == 0)) {
         S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
             << Feature << BareFeat;
         return true;
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index f2cd46d1e7c93..7e857369c1042 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -3138,7 +3138,8 @@ bool Sema::checkTargetClonesAttrString(
       } else if (Cur == "default") {
         DefaultIsDupe = HasDefault;
         HasDefault = true;
-      } else if (!Context.getTargetInfo().isValidFeatureName(Cur))
+      } else if (!Context.getTargetInfo().isValidFeatureName(Cur) ||
+                 Context.getTargetInfo().multiVersionSortPriority(Cur) == 0)
         return Diag(CurLoc, diag::warn_unsupported_target_attribute)
                << Unsupported << None << Cur << TargetClones;
       if (llvm::is_contained(StringsBuffer, Cur) || DefaultIsDupe)
diff --git a/clang/test/Sema/attr-target-clones.c b/clang/test/Sema/attr-target-clones.c
index e287fce7699b7..4597ea54d02bf 100644
--- a/clang/test/Sema/attr-target-clones.c
+++ b/clang/test/Sema/attr-target-clones.c
@@ -122,3 +122,6 @@ void good_overload5(int) __attribute__((target_clones("mmx", "sse4.2", "default"
 void good_isa_level(int) __attribute__((target_clones("default", "arch=x86-64", "arch=x86-64-v2", "arch=x86-64-v3", "arch=x86-64-v4")));
 // expected-warning@+1 {{unsupported CPU 'x86-64-v5' in the 'target_clones' attribute string; 'target_clones' attribute ignored}}
 void bad_isa_level(int) __attribute__((target_clones("default", "arch=x86-64-v5")));
+
+// expected-warning@+1 {{unsupported 'sha' in the 'target_clones' attribute string; 'target_clones' attribute ignored}}
+void bad_feature(void) __attribute__((target_clones("default", "sse4.2", "sha")));
diff --git a/clang/test/Sema/attr-target-mv.c b/clang/test/Sema/attr-target-mv.c
index 8218771275e1b..ddb1d82b02f09 100644
--- a/clang/test/Sema/attr-target-mv.c
+++ b/clang/test/Sema/attr-target-mv.c
@@ -170,3 +170,17 @@ int __attribute__((__overloadable__)) __attribute__((target("arch=sandybridge"))
 
 int __attribute__((__overloadable__)) __attribute__((target("sse4.2"))) good_overload7(void);
 int __attribute__((target("arch=sandybridge"))) good_overload7(int);
+
+// expected-error@+2 {{function multiversioning doesn't support feature 'sha'}}
+// expected-note@+2 {{function multiversioning caused by this declaration}}
+int __attribute__((target("sha"))) no_priority1(void);
+int __attribute__((target("default"))) no_priority1(void);
+
+int __attribute__((target("default"))) no_priority2(void);
+// expected-error@+1 {{function multiversioning doesn't support feature 'sha'}}
+int __attribute__((target("sha"))) no_priority2(void);
+
+int __attribute__((target("default"))) no_priority3(void);
+int __attribute__((target("avx2"))) no_priority3(void);
+// expected-error@+1 {{function multiversioning doesn't support feature 'sha'}}
+int __attribute__((target("sha"))) no_priority3(void);
diff --git a/clang/test/Sema/attr-target.c b/clang/test/Sema/attr-target.c
index 5328f056507a7..65ece3c27d299 100644
--- a/clang/test/Sema/attr-target.c
+++ b/clang/test/Sema/attr-target.c
@@ -33,6 +33,8 @@ void __attribute__((target("x86-64"))) baseline(void) {}
 //expected-warning@+1 {{unsupported 'x86-64-v2' in the 'target' attribute string}}
 void __attribute__((target("x86-64-v2"))) v2(void) {}
 
+int __attribute__((target("sha"))) good_target_but_not_for_fmv() { return 5; }
+
 #elifdef __aarch64__
 
 int __attribute__((target("sve,arch=armv8-a"))) foo(void) { return 4; }

@DanShaders
Copy link
Contributor Author

CC @FreddyLeaf, @phoebewang

@FreddyLeaf
Copy link
Contributor

Thanks for the fix!

@DanShaders DanShaders force-pushed the unorderable-features branch from a8ddcb1 to 6ac4e25 Compare July 11, 2024 16:34
@DanShaders
Copy link
Contributor Author

Rebased on top of main to hopefully pass github CI (no changes otherwise)

@DanShaders DanShaders force-pushed the unorderable-features branch from 6ac4e25 to 7378d62 Compare January 13, 2025 21:59
@DanShaders
Copy link
Contributor Author

@phoebewang Mind taking a look?

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@phoebewang
Copy link
Contributor

@DanShaders let me know if you need me to merge it for you.

@DanShaders
Copy link
Contributor Author

I do, thanks!

@phoebewang phoebewang merged commit 9988309 into llvm:main Jan 14, 2025
6 of 8 checks passed
@DanShaders DanShaders deleted the unorderable-features branch January 14, 2025 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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