[clang] 103bbdd - [ARM] Move Triple::getARMCPUForArch into ARMTargetParser
Tomas Matheson via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 9 03:54:27 PST 2022
Author: Tomas Matheson
Date: 2022-11-09T11:52:35Z
New Revision: 103bbddde66f4157b52c2b6d7532c1dd0dfcaf94
URL: https://github.com/llvm/llvm-project/commit/103bbddde66f4157b52c2b6d7532c1dd0dfcaf94
DIFF: https://github.com/llvm/llvm-project/commit/103bbddde66f4157b52c2b6d7532c1dd0dfcaf94.diff
LOG: [ARM] Move Triple::getARMCPUForArch into ARMTargetParser
This is very backend specific so either belongs in Toolchains/ARM or in
ARMTargetParser. Since it is used in lldb, ARMTargetParser made more sense.
This is part of an effort to move information about ARM/AArch64 architecture
versions, extensions and CPUs into their respective TargetParsers.
Differential Revision: https://reviews.llvm.org/D137564
Added:
Modified:
clang/lib/Driver/ToolChains/Arch/ARM.cpp
lldb/source/Utility/ArchSpec.cpp
llvm/include/llvm/ADT/Triple.h
llvm/include/llvm/Support/ARMTargetParser.h
llvm/lib/Support/ARMTargetParser.cpp
llvm/lib/Support/Triple.cpp
llvm/unittests/ADT/TripleTest.cpp
llvm/unittests/Support/TargetParserTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Arch/ARM.cpp b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
index 67dec7cadada5..bbf466ba847de 100644
--- a/clang/lib/Driver/ToolChains/Arch/ARM.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
@@ -938,7 +938,7 @@ StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
// We need to return an empty string here on invalid MArch values as the
// various places that call this function can't cope with a null result.
- return Triple.getARMCPUForArch(MArch);
+ return llvm::ARM::getARMCPUForArch(Triple, MArch);
}
/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
@@ -971,7 +971,8 @@ llvm::ARM::ArchKind arm::getLLVMArchKindForARM(StringRef CPU, StringRef Arch,
if (ArchKind == llvm::ARM::ArchKind::INVALID)
// In case of generic Arch, i.e. "arm",
// extract arch from default cpu of the Triple
- ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch));
+ ArchKind =
+ llvm::ARM::parseCPUArch(llvm::ARM::getARMCPUForArch(Triple, ARMArch));
} else {
// FIXME: horrible hack to get around the fact that Cortex-A7 is only an
// armv7k triple if it's actually been specified via "-arch armv7k".
diff --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp
index 126bedc209232..e1d7ee3ee276d 100644
--- a/lldb/source/Utility/ArchSpec.cpp
+++ b/lldb/source/Utility/ArchSpec.cpp
@@ -16,6 +16,7 @@
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/BinaryFormat/MachO.h"
+#include "llvm/Support/ARMTargetParser.h"
#include "llvm/Support/Compiler.h"
using namespace lldb;
@@ -638,7 +639,7 @@ std::string ArchSpec::GetClangTargetCPU() const {
}
if (GetTriple().isARM())
- cpu = GetTriple().getARMCPUForArch("").str();
+ cpu = llvm::ARM::getARMCPUForArch(GetTriple(), "").str();
return cpu;
}
diff --git a/llvm/include/llvm/ADT/Triple.h b/llvm/include/llvm/ADT/Triple.h
index dc96398dba999..5aba2730c5a8b 100644
--- a/llvm/include/llvm/ADT/Triple.h
+++ b/llvm/include/llvm/ADT/Triple.h
@@ -1024,12 +1024,6 @@ class Triple {
/// architecture if no such variant can be found.
llvm::Triple getLittleEndianArchVariant() const;
- /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
- ///
- /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
- /// string then the triple's arch name is used.
- StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
-
/// Tests whether the target triple is little endian.
///
/// \returns true if the triple is little endian, false otherwise.
diff --git a/llvm/include/llvm/Support/ARMTargetParser.h b/llvm/include/llvm/Support/ARMTargetParser.h
index 5fb4090395c07..32cc31db5936a 100644
--- a/llvm/include/llvm/Support/ARMTargetParser.h
+++ b/llvm/include/llvm/Support/ARMTargetParser.h
@@ -296,6 +296,12 @@ unsigned parseArchVersion(StringRef Arch);
void fillValidCPUArchList(SmallVectorImpl<StringRef> &Values);
StringRef computeDefaultTargetABI(const Triple &TT, StringRef CPU);
+/// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
+///
+/// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
+/// string then the triple's arch name is used.
+StringRef getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch = {});
+
} // namespace ARM
} // namespace llvm
diff --git a/llvm/lib/Support/ARMTargetParser.cpp b/llvm/lib/Support/ARMTargetParser.cpp
index a68244a5e38af..43cbb409b0f04 100644
--- a/llvm/lib/Support/ARMTargetParser.cpp
+++ b/llvm/lib/Support/ARMTargetParser.cpp
@@ -643,3 +643,72 @@ StringRef ARM::computeDefaultTargetABI(const Triple &TT, StringRef CPU) {
return "aapcs";
}
}
+
+StringRef ARM::getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch) {
+ if (MArch.empty())
+ MArch = Triple.getArchName();
+ MArch = llvm::ARM::getCanonicalArchName(MArch);
+
+ // Some defaults are forced.
+ switch (Triple.getOS()) {
+ case llvm::Triple::FreeBSD:
+ case llvm::Triple::NetBSD:
+ case llvm::Triple::OpenBSD:
+ if (!MArch.empty() && MArch == "v6")
+ return "arm1176jzf-s";
+ if (!MArch.empty() && MArch == "v7")
+ return "cortex-a8";
+ break;
+ case llvm::Triple::Win32:
+ // FIXME: this is invalid for WindowsCE
+ if (llvm::ARM::parseArchVersion(MArch) <= 7)
+ return "cortex-a9";
+ break;
+ case llvm::Triple::IOS:
+ case llvm::Triple::MacOSX:
+ case llvm::Triple::TvOS:
+ case llvm::Triple::WatchOS:
+ case llvm::Triple::DriverKit:
+ if (MArch == "v7k")
+ return "cortex-a7";
+ break;
+ default:
+ break;
+ }
+
+ if (MArch.empty())
+ return StringRef();
+
+ StringRef CPU = llvm::ARM::getDefaultCPU(MArch);
+ if (!CPU.empty() && !CPU.equals("invalid"))
+ return CPU;
+
+ // If no specific architecture version is requested, return the minimum CPU
+ // required by the OS and environment.
+ switch (Triple.getOS()) {
+ case llvm::Triple::NetBSD:
+ switch (Triple.getEnvironment()) {
+ case llvm::Triple::EABI:
+ case llvm::Triple::EABIHF:
+ case llvm::Triple::GNUEABI:
+ case llvm::Triple::GNUEABIHF:
+ return "arm926ej-s";
+ default:
+ return "strongarm";
+ }
+ case llvm::Triple::NaCl:
+ case llvm::Triple::OpenBSD:
+ return "cortex-a8";
+ default:
+ switch (Triple.getEnvironment()) {
+ case llvm::Triple::EABIHF:
+ case llvm::Triple::GNUEABIHF:
+ case llvm::Triple::MuslEABIHF:
+ return "arm1176jzf-s";
+ default:
+ return "arm7tdmi";
+ }
+ }
+
+ llvm_unreachable("invalid arch name");
+}
diff --git a/llvm/lib/Support/Triple.cpp b/llvm/lib/Support/Triple.cpp
index 84d45cb4074d9..d24bca84b79f2 100644
--- a/llvm/lib/Support/Triple.cpp
+++ b/llvm/lib/Support/Triple.cpp
@@ -1857,75 +1857,6 @@ VersionTuple Triple::getMinimumSupportedOSVersion() const {
return VersionTuple();
}
-StringRef Triple::getARMCPUForArch(StringRef MArch) const {
- if (MArch.empty())
- MArch = getArchName();
- MArch = ARM::getCanonicalArchName(MArch);
-
- // Some defaults are forced.
- switch (getOS()) {
- case llvm::Triple::FreeBSD:
- case llvm::Triple::NetBSD:
- case llvm::Triple::OpenBSD:
- if (!MArch.empty() && MArch == "v6")
- return "arm1176jzf-s";
- if (!MArch.empty() && MArch == "v7")
- return "cortex-a8";
- break;
- case llvm::Triple::Win32:
- // FIXME: this is invalid for WindowsCE
- if (ARM::parseArchVersion(MArch) <= 7)
- return "cortex-a9";
- break;
- case llvm::Triple::IOS:
- case llvm::Triple::MacOSX:
- case llvm::Triple::TvOS:
- case llvm::Triple::WatchOS:
- case llvm::Triple::DriverKit:
- if (MArch == "v7k")
- return "cortex-a7";
- break;
- default:
- break;
- }
-
- if (MArch.empty())
- return StringRef();
-
- StringRef CPU = ARM::getDefaultCPU(MArch);
- if (!CPU.empty() && !CPU.equals("invalid"))
- return CPU;
-
- // If no specific architecture version is requested, return the minimum CPU
- // required by the OS and environment.
- switch (getOS()) {
- case llvm::Triple::NetBSD:
- switch (getEnvironment()) {
- case llvm::Triple::EABI:
- case llvm::Triple::EABIHF:
- case llvm::Triple::GNUEABI:
- case llvm::Triple::GNUEABIHF:
- return "arm926ej-s";
- default:
- return "strongarm";
- }
- case llvm::Triple::NaCl:
- case llvm::Triple::OpenBSD:
- return "cortex-a8";
- default:
- switch (getEnvironment()) {
- case llvm::Triple::EABIHF:
- case llvm::Triple::GNUEABIHF:
- case llvm::Triple::MuslEABIHF:
- return "arm1176jzf-s";
- default:
- return "arm7tdmi";
- }
- }
-
- llvm_unreachable("invalid arch name");
-}
-
VersionTuple Triple::getCanonicalVersionForOS(OSType OSKind,
const VersionTuple &Version) {
switch (OSKind) {
diff --git a/llvm/unittests/ADT/TripleTest.cpp b/llvm/unittests/ADT/TripleTest.cpp
index 984bb15255d48..2a11648b85c91 100644
--- a/llvm/unittests/ADT/TripleTest.cpp
+++ b/llvm/unittests/ADT/TripleTest.cpp
@@ -1855,70 +1855,6 @@ TEST(TripleTest, NormalizeAndroid) {
Triple::normalize("aarch64-linux-android21"));
}
-TEST(TripleTest, getARMCPUForArch) {
- // Platform specific defaults.
- {
- llvm::Triple Triple("arm--nacl");
- EXPECT_EQ("cortex-a8", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("arm--openbsd");
- EXPECT_EQ("cortex-a8", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armv6-unknown-freebsd");
- EXPECT_EQ("arm1176jzf-s", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("thumbv6-unknown-freebsd");
- EXPECT_EQ("arm1176jzf-s", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armebv6-unknown-freebsd");
- EXPECT_EQ("arm1176jzf-s", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("arm--win32");
- EXPECT_EQ("cortex-a9", Triple.getARMCPUForArch());
- EXPECT_EQ("generic", Triple.getARMCPUForArch("armv8-a"));
- }
- // Some alternative architectures
- {
- llvm::Triple Triple("armv7k-apple-ios9");
- EXPECT_EQ("cortex-a7", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armv7k-apple-watchos3");
- EXPECT_EQ("cortex-a7", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armv7k-apple-tvos9");
- EXPECT_EQ("cortex-a7", Triple.getARMCPUForArch());
- }
- // armeb is permitted, but armebeb is not
- {
- llvm::Triple Triple("armeb-none-eabi");
- EXPECT_EQ("arm7tdmi", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armebeb-none-eabi");
- EXPECT_EQ("", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armebv6eb-none-eabi");
- EXPECT_EQ("", Triple.getARMCPUForArch());
- }
- // xscaleeb is permitted, but armebxscale is not
- {
- llvm::Triple Triple("xscaleeb-none-eabi");
- EXPECT_EQ("xscale", Triple.getARMCPUForArch());
- }
- {
- llvm::Triple Triple("armebxscale-none-eabi");
- EXPECT_EQ("", Triple.getARMCPUForArch());
- }
-}
-
TEST(TripleTest, NormalizeARM) {
EXPECT_EQ("armv6-unknown-netbsd-eabi",
Triple::normalize("armv6-netbsd-eabi"));
diff --git a/llvm/unittests/Support/TargetParserTest.cpp b/llvm/unittests/Support/TargetParserTest.cpp
index 1e0dc3381fffb..d94005e4a551f 100644
--- a/llvm/unittests/Support/TargetParserTest.cpp
+++ b/llvm/unittests/Support/TargetParserTest.cpp
@@ -6,12 +6,13 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/Support/TargetParser.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/Triple.h"
#include "llvm/Support/AArch64TargetParser.h"
#include "llvm/Support/ARMBuildAttributes.h"
#include "llvm/Support/FormatVariadic.h"
-#include "llvm/Support/TargetParser.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
@@ -871,6 +872,70 @@ TEST(TargetParserTest, ARMparseArchVersion) {
EXPECT_EQ(5u, ARM::parseArchVersion(ARMArch[i]));
}
+TEST(TargetParserTest, getARMCPUForArch) {
+ // Platform specific defaults.
+ {
+ llvm::Triple Triple("arm--nacl");
+ EXPECT_EQ("cortex-a8", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("arm--openbsd");
+ EXPECT_EQ("cortex-a8", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armv6-unknown-freebsd");
+ EXPECT_EQ("arm1176jzf-s", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("thumbv6-unknown-freebsd");
+ EXPECT_EQ("arm1176jzf-s", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armebv6-unknown-freebsd");
+ EXPECT_EQ("arm1176jzf-s", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("arm--win32");
+ EXPECT_EQ("cortex-a9", ARM::getARMCPUForArch(Triple));
+ EXPECT_EQ("generic", ARM::getARMCPUForArch(Triple, "armv8-a"));
+ }
+ // Some alternative architectures
+ {
+ llvm::Triple Triple("armv7k-apple-ios9");
+ EXPECT_EQ("cortex-a7", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armv7k-apple-watchos3");
+ EXPECT_EQ("cortex-a7", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armv7k-apple-tvos9");
+ EXPECT_EQ("cortex-a7", ARM::getARMCPUForArch(Triple));
+ }
+ // armeb is permitted, but armebeb is not
+ {
+ llvm::Triple Triple("armeb-none-eabi");
+ EXPECT_EQ("arm7tdmi", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armebeb-none-eabi");
+ EXPECT_EQ("", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armebv6eb-none-eabi");
+ EXPECT_EQ("", ARM::getARMCPUForArch(Triple));
+ }
+ // xscaleeb is permitted, but armebxscale is not
+ {
+ llvm::Triple Triple("xscaleeb-none-eabi");
+ EXPECT_EQ("xscale", ARM::getARMCPUForArch(Triple));
+ }
+ {
+ llvm::Triple Triple("armebxscale-none-eabi");
+ EXPECT_EQ("", ARM::getARMCPUForArch(Triple));
+ }
+}
+
class AArch64CPUTestFixture
: public ::testing::TestWithParam<ARMCPUTestParams> {};
More information about the cfe-commits
mailing list