[clang] [Clang] Support target attr specifying CPU (PR #68678)

Qiu Chaofan via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 10 02:23:33 PDT 2023


https://github.com/ecnelises created https://github.com/llvm/llvm-project/pull/68678

Currently targets except AArch64 cannot recognize function attribute specifying target CPU. Make it equivalent to `arch` directive.

>From 78f22a8a57f5b67660763b8c7731b9d3cddede72 Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Tue, 10 Oct 2023 17:20:00 +0800
Subject: [PATCH] [Clang] Support target attr specifying CPU

Currently targets except AArch64 cannot recognize function attribute
specifying target CPU. Make it equivalent to arch directive.
---
 clang/lib/Basic/TargetInfo.cpp | 7 ++++---
 clang/test/Sema/attr-target.c  | 4 ++++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index 6cd5d618a4acaa5..474f4173eb5257d 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -560,11 +560,12 @@ ParsedTargetAttr TargetInfo::parseTargetAttr(StringRef Features) const {
     }
 
     // While we're here iterating check for a different target cpu.
-    if (Feature.startswith("arch=")) {
+    if (Feature.startswith("arch=") || Feature.startswith("cpu=")) {
+      auto [Key, CPU] = Feature.split("=");
       if (!Ret.CPU.empty())
-        Ret.Duplicate = "arch=";
+        Ret.Duplicate = StringRef(Key.data(), Key.size() + 1);
       else
-        Ret.CPU = Feature.split("=").second.trim();
+        Ret.CPU = CPU.trim();
     } else if (Feature.startswith("tune=")) {
       if (!Ret.Tune.empty())
         Ret.Duplicate = "tune=";
diff --git a/clang/test/Sema/attr-target.c b/clang/test/Sema/attr-target.c
index 3416a3d0a6ba134..631e40b947ed69b 100644
--- a/clang/test/Sema/attr-target.c
+++ b/clang/test/Sema/attr-target.c
@@ -17,10 +17,14 @@ int __attribute__((target("avx,sse4.2,arch=hiss"))) meow(void) {  return 4; }
 int __attribute__((target("woof"))) bark(void) {  return 4; }
 // no warning, same as saying 'nothing'.
 int __attribute__((target("arch="))) turtle(void) { return 4; }
+// no warning, same as saying 'nothing'.
+int __attribute__((target("cpu="))) equus(void) { return 4; }
 //expected-warning at +1 {{unknown CPU 'hiss' in the 'target' attribute string; 'target' attribute ignored}}
 int __attribute__((target("arch=hiss,arch=woof"))) pine_tree(void) { return 4; }
 //expected-warning at +1 {{duplicate 'arch=' in the 'target' attribute string; 'target' attribute ignored}}
 int __attribute__((target("arch=ivybridge,arch=haswell"))) oak_tree(void) { return 4; }
+//expected-warning at +1 {{duplicate 'cpu=' in the 'target' attribute string; 'target' attribute ignored}}
+int __attribute__((target("arch=ivybridge,cpu=haswell"))) cypress_tree(void) { return 4; }
 //expected-warning at +1 {{unsupported 'branch-protection' in the 'target' attribute string; 'target' attribute ignored}}
 int __attribute__((target("branch-protection=none"))) birch_tree(void) { return 5; }
 //expected-warning at +1 {{unknown tune CPU 'hiss' in the 'target' attribute string; 'target' attribute ignored}}



More information about the cfe-commits mailing list