Skip to content

Commit 9b2e09e

Browse files
nemanjaitstellar
authored andcommitted
[PowerPC] Recognize long CPU name for -mtune in Clang
There are two ways of specifying a CPU on PowerPC: power<N> and pwr<N>. Clang/LLVM traditionally supports the latter and Clang replaces the former with the latter when passing it to the back end for the -mcpu= option. However, when the -mtune= option was introduced, this replacement was not implemented for it. This leaves us in an inconsistent state of accepting both forms for -mcpu= and and only the latter for -mtune=. Furthermore, it leaves us incompatible with GCC which only understands the power<N> version for both options. This patch just adds the same handling for the long names for -mtune= as already exists for -mcpu=. Differential revision: https://reviews.llvm.org/D144967 (cherry picked from commit 59cd692)
1 parent fd37b4b commit 9b2e09e

File tree

4 files changed

+57
-48
lines changed

4 files changed

+57
-48
lines changed

clang/lib/Driver/ToolChains/Arch/PPC.cpp

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,53 +34,60 @@ static std::string getPPCGenericTargetCPU(const llvm::Triple &T) {
3434
return "ppc";
3535
}
3636

37-
/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
38-
std::string ppc::getPPCTargetCPU(const ArgList &Args, const llvm::Triple &T) {
39-
if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
40-
StringRef CPUName = A->getValue();
41-
42-
// Clang/LLVM does not actually support code generation
43-
// for the 405 CPU. However, there are uses of this CPU ID
44-
// in projects that previously used GCC and rely on Clang
45-
// accepting it. Clang has always ignored it and passed the
46-
// generic CPU ID to the back end.
47-
if (CPUName == "generic" || CPUName == "405")
37+
static std::string normalizeCPUName(StringRef CPUName, const llvm::Triple &T) {
38+
// Clang/LLVM does not actually support code generation
39+
// for the 405 CPU. However, there are uses of this CPU ID
40+
// in projects that previously used GCC and rely on Clang
41+
// accepting it. Clang has always ignored it and passed the
42+
// generic CPU ID to the back end.
43+
if (CPUName == "generic" || CPUName == "405")
44+
return getPPCGenericTargetCPU(T);
45+
46+
if (CPUName == "native") {
47+
std::string CPU = std::string(llvm::sys::getHostCPUName());
48+
if (!CPU.empty() && CPU != "generic")
49+
return CPU;
50+
else
4851
return getPPCGenericTargetCPU(T);
52+
}
4953

50-
if (CPUName == "native") {
51-
std::string CPU = std::string(llvm::sys::getHostCPUName());
52-
if (!CPU.empty() && CPU != "generic")
53-
return CPU;
54-
else
55-
return getPPCGenericTargetCPU(T);
56-
}
54+
return llvm::StringSwitch<const char *>(CPUName)
55+
.Case("common", "generic")
56+
.Case("440fp", "440")
57+
.Case("630", "pwr3")
58+
.Case("G3", "g3")
59+
.Case("G4", "g4")
60+
.Case("G4+", "g4+")
61+
.Case("8548", "e500")
62+
.Case("G5", "g5")
63+
.Case("power3", "pwr3")
64+
.Case("power4", "pwr4")
65+
.Case("power5", "pwr5")
66+
.Case("power5x", "pwr5x")
67+
.Case("power6", "pwr6")
68+
.Case("power6x", "pwr6x")
69+
.Case("power7", "pwr7")
70+
.Case("power8", "pwr8")
71+
.Case("power9", "pwr9")
72+
.Case("power10", "pwr10")
73+
.Case("future", "future")
74+
.Case("powerpc", "ppc")
75+
.Case("powerpc64", "ppc64")
76+
.Case("powerpc64le", "ppc64le")
77+
.Default(CPUName.data());
78+
}
5779

58-
return llvm::StringSwitch<const char *>(CPUName)
59-
.Case("common", "generic")
60-
.Case("440fp", "440")
61-
.Case("630", "pwr3")
62-
.Case("G3", "g3")
63-
.Case("G4", "g4")
64-
.Case("G4+", "g4+")
65-
.Case("8548", "e500")
66-
.Case("G5", "g5")
67-
.Case("power3", "pwr3")
68-
.Case("power4", "pwr4")
69-
.Case("power5", "pwr5")
70-
.Case("power5x", "pwr5x")
71-
.Case("power6", "pwr6")
72-
.Case("power6x", "pwr6x")
73-
.Case("power7", "pwr7")
74-
.Case("power8", "pwr8")
75-
.Case("power9", "pwr9")
76-
.Case("power10", "pwr10")
77-
.Case("future", "future")
78-
.Case("powerpc", "ppc")
79-
.Case("powerpc64", "ppc64")
80-
.Case("powerpc64le", "ppc64le")
81-
.Default(CPUName.data());
82-
}
80+
/// Get the (LLVM) name of the PowerPC cpu we are tuning for.
81+
std::string ppc::getPPCTuneCPU(const ArgList &Args, const llvm::Triple &T) {
82+
if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ))
83+
return normalizeCPUName(A->getValue(), T);
84+
return getPPCGenericTargetCPU(T);
85+
}
8386

87+
/// Get the (LLVM) name of the PowerPC cpu we are targeting.
88+
std::string ppc::getPPCTargetCPU(const ArgList &Args, const llvm::Triple &T) {
89+
if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
90+
return normalizeCPUName(A->getValue(), T);
8491
return getPPCGenericTargetCPU(T);
8592
}
8693

clang/lib/Driver/ToolChains/Arch/PPC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ FloatABI getPPCFloatABI(const Driver &D, const llvm::opt::ArgList &Args);
3737

3838
std::string getPPCTargetCPU(const llvm::opt::ArgList &Args,
3939
const llvm::Triple &T);
40+
std::string getPPCTuneCPU(const llvm::opt::ArgList &Args,
41+
const llvm::Triple &T);
4042
const char *getPPCAsmModeForCPU(StringRef Name);
4143
ReadGOTPtrMode getPPCReadGOTPtrMode(const Driver &D, const llvm::Triple &Triple,
4244
const llvm::opt::ArgList &Args);

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,17 +1989,15 @@ void Clang::AddMIPSTargetArgs(const ArgList &Args,
19891989

19901990
void Clang::AddPPCTargetArgs(const ArgList &Args,
19911991
ArgStringList &CmdArgs) const {
1992+
const llvm::Triple &T = getToolChain().getTriple();
19921993
if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) {
19931994
CmdArgs.push_back("-tune-cpu");
1994-
if (strcmp(A->getValue(), "native") == 0)
1995-
CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName()));
1996-
else
1997-
CmdArgs.push_back(A->getValue());
1995+
std::string CPU = ppc::getPPCTuneCPU(Args, T);
1996+
CmdArgs.push_back(Args.MakeArgString(CPU));
19981997
}
19991998

20001999
// Select the ABI to use.
20012000
const char *ABIName = nullptr;
2002-
const llvm::Triple &T = getToolChain().getTriple();
20032001
if (T.isOSBinFormatELF()) {
20042002
switch (getToolChain().getArch()) {
20052003
case llvm::Triple::ppc64: {

clang/test/Driver/ppc-cpus.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
// RUN: %clang -### -c --target=powerpc64 %s -mcpu=generic -mtune=pwr9 2>&1 | FileCheck %s --check-prefix=TUNE
3333
// TUNE: "-target-cpu" "ppc64" "-tune-cpu" "pwr9"
34+
// RUN: %clang -### -c --target=powerpc64le %s -mcpu=power9 -mtune=power10 2>&1 | FileCheck %s --check-prefix=TUNE-LONG
35+
// TUNE-LONG: "-target-cpu" "pwr9" "-tune-cpu" "pwr10"
3436

3537
/// Test mcpu options that are equivalent to "generic"
3638
// RUN: %clang -### -c -target powerpc64 %s -mcpu=generic 2>&1 | FileCheck %s --check-prefix=GENERIC

0 commit comments

Comments
 (0)