[libc-commits] [libc] [Clang] Support target attr specifying CPU (PR #68678)

Qiu Chaofan via libc-commits libc-commits at lists.llvm.org
Mon Oct 16 02:06:43 PDT 2023


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

>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 1/3] [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}}

>From 69db1b10f119026c857781ee559b4e3e1b0de8af Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Wed, 11 Oct 2023 11:17:36 +0800
Subject: [PATCH 2/3] Rebase for PowerPC tests

---
 clang/test/Sema/attr-target.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/test/Sema/attr-target.c b/clang/test/Sema/attr-target.c
index 72ca9887b21b016..3939f4d02744e4a 100644
--- a/clang/test/Sema/attr-target.c
+++ b/clang/test/Sema/attr-target.c
@@ -80,10 +80,14 @@ int __attribute__((target("fpmath=387"))) walrus(void) { return 4; }
 int __attribute__((target("float128,arch=hiss"))) meow(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=pwr9,arch=pwr10"))) oak_tree(void) { return 4; }
+//expected-warning at +1 {{duplicate 'cpu=' in the 'target' attribute string; 'target' attribute ignored}}
+int __attribute__((target("arch=pwr8,cpu=pwr9"))) 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}}

>From 4e984e123b1a71b80d4d134059371cf1a6ad2a94 Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Mon, 16 Oct 2023 16:53:33 +0800
Subject: [PATCH 3/3] Update attribute docs

---
 clang/include/clang/Basic/AttrDocs.td | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index cbbf69faeb308ad..762c8596862dd6b 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -2420,11 +2420,11 @@ command line.
 
 The current set of options correspond to the existing "subtarget features" for
 the target with or without a "-mno-" in front corresponding to the absence
-of the feature, as well as ``arch="CPU"`` which will change the default "CPU"
-for the function.
+of the feature, as well as ``arch="CPU"`` and ``cpu="CPU"`` which will change
+the default "CPU" for the function.
 
-For X86, the attribute also allows ``tune="CPU"`` to optimize the generated
-code for the given CPU without changing the available instructions.
+For X86 and PowerPC, the attribute also allows ``tune="CPU"`` to optimize the
+generated code for the given CPU without changing the available instructions.
 
 For AArch64, ``arch="Arch"`` will set the architecture, similar to the -march
 command line options. ``cpu="CPU"`` can be used to select a specific cpu,



More information about the libc-commits mailing list