llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Nemanja Ivanovic (nemanjai)

<details>
<summary>Changes</summary>

The only target that allows using the 'target' attribute for multiversioning is 
X86. It is currently disallowed on RISC-V and AArch64, but there is no specific 
diagnostic informing the user of that. For AIX, this is not rejected by Sema at 
all which allows the flow to continue and eventually assert when trying to 
produce the actual multiversioned function. This patch adds the diagnostic and 
uses it for the affected targets.

Fixes: https://github.com/llvm/llvm-project/issues/236036

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


3 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+8-7) 
- (added) clang/test/Sema/attr-target-no-mv.c (+24) 


``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 56cc9721781063..a0e4f58aec70be 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13379,6 +13379,8 @@ def err_multiversion_doesnt_support
             "function|lambdas}1">;
 def err_multiversion_not_allowed_on_main : Error<
   "'main' cannot be a multiversioned function">;
+def err_target_attr_not_used_for_multiversion : Error<
+  "'target' attribute cannot be used for multiversioning on current target">;
 def err_multiversion_not_supported : Error<
  "function multiversioning is not supported on the current target">;
 def err_multiversion_types_mixed : Error<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f3d38a555a524a..b855c3181073d3 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -30,6 +30,7 @@
 #include "clang/AST/StmtCXX.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/Builtins.h"
+#include "clang/Basic/DiagnosticSema.h"
 #include "clang/Basic/HLSLRuntime.h"
 #include "clang/Basic/PartialDiagnostic.h"
 #include "clang/Basic/SourceManager.h"
@@ -12365,13 +12366,13 @@ static bool CheckMultiVersionFunction(Sema &S, 
FunctionDecl *NewFD,
     return false;
   }
 
-  // Target attribute on AArch64 is not used for multiversioning
-  if (NewTA && TI.getTriple().isAArch64())
-    return false;
-
-  // Target attribute on RISCV is not used for multiversioning
-  if (NewTA && TI.getTriple().isRISCV())
-    return false;
+  // Target attribute is not used for multiversioning on AArch64, RISC-V or 
AIX.
+  if (NewTA && (TI.getTriple().isAArch64() || TI.getTriple().isRISCV() ||
+                TI.getTriple().isOSAIX())) {
+    S.Diag(NewFD->getLocation(),
+           diag::err_target_attr_not_used_for_multiversion);
+    return true;
+  }
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
       !OldDecl->getDeclContext()->getRedeclContext()->Equals(
diff --git a/clang/test/Sema/attr-target-no-mv.c 
b/clang/test/Sema/attr-target-no-mv.c
new file mode 100644
index 00000000000000..1428b462d062fb
--- /dev/null
+++ b/clang/test/Sema/attr-target-no-mv.c
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple powerpc64-ibm-aix -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple aarch64 -target-feature +fmv -fsyntax-only -verify 
%s
+// RUN: %clang_cc1 -triple riscv32 -fsyntax-only -verify %s
+__attribute__((target("default")))
+int f(void) { return 0; } // expected-error {{'target' attribute cannot be 
used for multiversioning on current target}}
+// expected-note@-1 {{candidate function}}
+
+#ifdef __riscv
+__attribute__((target("cpu=sifive-e20")))
+int f(void) { return 1; } // expected-error {{'target' attribute cannot be 
used for multiversioning on current target}}
+// expected-note@-1 {{candidate function}}
+
+#elif defined __aarch64__
+__attribute__((target("cpu=apple-a18")))
+int f(void) { return 1; } // expected-error {{'target' attribute cannot be 
used for multiversioning on current target}}
+// expected-note@-1 {{candidate function}}
+
+#elif defined _AIX
+__attribute__((target("cpu=power10")))
+int f(void) { return 1; } // expected-error {{'target' attribute cannot be 
used for multiversioning on current target}}
+// expected-note@-1 {{candidate function}}
+#endif
+
+int g(void) { return f(); } // expected-error {{call to 'f' is ambiguous}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/236782
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to