[compiler-rt] [Clang] Support target attr specifying CPU (PR #68678)
Qiu Chaofan via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 23 22:52:20 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/4] [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/4] 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/4] 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,
>From 5d10f10987f720ddaa1d42ebb68f9a16e562b268 Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Tue, 24 Oct 2023 13:47:30 +0800
Subject: [PATCH 4/4] Exclude x86
---
clang/include/clang/Basic/AttrDocs.td | 2 +-
clang/lib/Basic/Targets/X86.cpp | 40 +++++++++++++++++++++++++++
clang/lib/Basic/Targets/X86.h | 1 +
clang/test/Sema/attr-target.c | 4 ---
4 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 7f97f3e0a170263..a98ea0cbbef8acb 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -2421,7 +2421,7 @@ 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"`` and ``cpu="CPU"`` which will change
-the default "CPU" for the function.
+the default processor for the function.
For X86 and PowerPC, the attribute also allows ``tune="CPU"`` to optimize the
generated code for the given CPU without changing the available instructions.
diff --git a/clang/lib/Basic/Targets/X86.cpp b/clang/lib/Basic/Targets/X86.cpp
index eec3cd558435e2a..ddec93ab79f2d78 100644
--- a/clang/lib/Basic/Targets/X86.cpp
+++ b/clang/lib/Basic/Targets/X86.cpp
@@ -1695,6 +1695,46 @@ ArrayRef<TargetInfo::AddlRegName> X86TargetInfo::getGCCAddlRegNames() const {
return llvm::ArrayRef(AddlRegNames);
}
+ParsedTargetAttr X86TargetInfo::parseTargetAttr(StringRef Features) const {
+ ParsedTargetAttr Ret;
+ if (Features == "default")
+ return Ret;
+ SmallVector<StringRef, 1> AttrFeatures;
+ Features.split(AttrFeatures, ",");
+
+ for (auto &Feature : AttrFeatures) {
+ Feature = Feature.trim();
+
+ // TODO: Support the fpmath option. It will require checking
+ // overall feature validity for the function with the rest of the
+ // attributes on the function.
+ if (Feature.startswith("fpmath="))
+ continue;
+
+ if (Feature.startswith("branch-protection=")) {
+ Ret.BranchProtection = Feature.split('=').second.trim();
+ continue;
+ }
+
+ if (Feature.startswith("arch=")) {
+ auto [Key, CPU] = Feature.split("=");
+ if (!Ret.CPU.empty())
+ Ret.Duplicate = StringRef(Key.data(), Key.size() + 1);
+ else
+ Ret.CPU = CPU.trim();
+ } else if (Feature.startswith("tune=")) {
+ if (!Ret.Tune.empty())
+ Ret.Duplicate = "tune=";
+ else
+ Ret.Tune = Feature.split("=").second.trim();
+ } else if (Feature.startswith("no-"))
+ Ret.Features.push_back("-" + Feature.split("-").second.str());
+ else
+ Ret.Features.push_back("+" + Feature.str());
+ }
+ return Ret;
+}
+
ArrayRef<Builtin::Info> X86_32TargetInfo::getTargetBuiltins() const {
return llvm::ArrayRef(BuiltinInfoX86, clang::X86::LastX86CommonBuiltin -
Builtin::FirstTSBuiltin + 1);
diff --git a/clang/lib/Basic/Targets/X86.h b/clang/lib/Basic/Targets/X86.h
index 99a64501d263ce4..a338eb2360c024d 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -423,6 +423,7 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
return getPointerWidthV(AddrSpace);
}
+ ParsedTargetAttr parseTargetAttr(StringRef Features) const override;
};
// X86-32 generic target
diff --git a/clang/test/Sema/attr-target.c b/clang/test/Sema/attr-target.c
index 3939f4d02744e4a..c3cf73f57fe1833 100644
--- a/clang/test/Sema/attr-target.c
+++ b/clang/test/Sema/attr-target.c
@@ -19,14 +19,10 @@ 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 llvm-commits
mailing list