[clang] [RISCV] Disallow target attribute use in multiversioning (PR #85899)

Piyou Chen via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 7 22:15:12 PDT 2024


https://github.com/BeMg updated https://github.com/llvm/llvm-project/pull/85899

>From 0bbffb92e7f36957c042cab9a67a483571d9b3f9 Mon Sep 17 00:00:00 2001
From: Piyou Chen <piyou.chen at sifive.com>
Date: Mon, 18 Mar 2024 05:07:14 -0700
Subject: [PATCH 1/3] [RISCV] Disallow target attribute use in multiversioning

---
 clang/lib/Sema/SemaDecl.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5850cd0ab6b9aa..e543aff77e67ea 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11853,6 +11853,10 @@ static bool CheckMultiVersionFunction(Sema &S, FunctionDecl *NewFD,
   if (NewTA && S.getASTContext().getTargetInfo().getTriple().isAArch64())
     return false;
 
+  // Target attribute on RISCV is not used for multiversioning
+  if (NewTA && S.getASTContext().getTargetInfo().getTriple().isRISCV())
+    return false;
+
   if (!OldDecl || !OldDecl->getAsFunction() ||
       OldDecl->getDeclContext()->getRedeclContext() !=
           NewFD->getDeclContext()->getRedeclContext()) {

>From f0057a2c65a030fc5afa30dee4b08c71cf3451bd Mon Sep 17 00:00:00 2001
From: Piyou Chen <piyou.chen at sifive.com>
Date: Mon, 1 Apr 2024 23:21:25 -0700
Subject: [PATCH 2/3] Add testcase

---
 clang/test/Sema/attr-target-riscv.c | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 clang/test/Sema/attr-target-riscv.c

diff --git a/clang/test/Sema/attr-target-riscv.c b/clang/test/Sema/attr-target-riscv.c
new file mode 100644
index 00000000000000..ed4e2915d6c6ef
--- /dev/null
+++ b/clang/test/Sema/attr-target-riscv.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple riscv64-linux-gnu -target-feature +i -fsyntax-only -verify -std=c2x %s
+
+//expected-note at +1 {{previous definition is here}}
+int __attribute__((target("arch=rv64g"))) foo(void) { return 0; }
+//expected-error at +1 {{redefinition of 'foo'}}
+int __attribute__((target("arch=rv64gc"))) foo(void) { return 0; }

>From 6757bf5e0f857acd3cca8a843a481f6658a5e6c0 Mon Sep 17 00:00:00 2001
From: Piyou Chen <piyou.chen at sifive.com>
Date: Sun, 7 Apr 2024 22:14:46 -0700
Subject: [PATCH 3/3] Share Triple for AArch64 and RISCV

---
 clang/lib/Sema/SemaDecl.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e543aff77e67ea..547ff30089a7c6 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11849,12 +11849,14 @@ static bool CheckMultiVersionFunction(Sema &S, FunctionDecl *NewFD,
     return false;
   }
 
+  const llvm::Triple &T = S.getASTContext().getTargetInfo().getTriple();
+
   // Target attribute on AArch64 is not used for multiversioning
-  if (NewTA && S.getASTContext().getTargetInfo().getTriple().isAArch64())
+  if (NewTA && T.isAArch64())
     return false;
 
   // Target attribute on RISCV is not used for multiversioning
-  if (NewTA && S.getASTContext().getTargetInfo().getTriple().isRISCV())
+  if (NewTA && T.isRISCV())
     return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||



More information about the cfe-commits mailing list