<div class="gmail_quote">On Mon, Jun 11, 2012 at 3:35 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: hfinkel<br>
Date: Mon Jun 11 17:35:19 2012<br>
New Revision: 158334<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=158334&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=158334&view=rev</a><br>
Log:<br>
Add PPC support for translating gcc-style -mcpu options into LLVM -target-cpu options.<br></blockquote><div><br></div><div>Very cool.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This functionality is based on what is done on ARM, and enables selecting PPC CPUs<br>
in a way compatible with gcc's driver. Also, mirroring gcc (and what is done on x86),<br>
-mcpu=native support was added. This uses the host cpu detection from LLVM<br>
(which will also soon be updated by refactoring code currently in backend).<br>
<br>
In order for this to work, the target needs a list of valid CPUs -- we now accept all CPUs accepted by LLVM.<br></blockquote><div><br></div><div>Is there any way to re-use the list from LLVM? Would be nice...</div><div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
A few preprocessor defines for common CPU types have been added.<br></blockquote><div><br></div><div>I wonder, could we get more exhaustive testing here? I worry that these code paths are particularly prone to rot due to lots of developers not working on a power platform.</div>
<div><br></div><div>In particular, I'd at least ask for tests for each of the preprocessor macros you added. I'd ideally like tests for all the CPU variants, but there are a *lot* of them, so likely not worth it...</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Modified:<br>
    cfe/trunk/lib/Basic/Targets.cpp<br>
    cfe/trunk/lib/Driver/Tools.cpp<br>
    cfe/trunk/lib/Driver/Tools.h<br>
    cfe/trunk/test/Driver/clang-translation.c<br>
    cfe/trunk/test/Preprocessor/init.c<br>
<br>
Modified: cfe/trunk/lib/Basic/Targets.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Targets.cpp?rev=158334&r1=158333&r2=158334&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Targets.cpp?rev=158334&r1=158333&r2=158334&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Basic/Targets.cpp (original)<br>
+++ cfe/trunk/lib/Basic/Targets.cpp Mon Jun 11 17:35:19 2012<br>
@@ -575,12 +575,47 @@<br>
   static const Builtin::Info BuiltinInfo[];<br>
   static const char * const GCCRegNames[];<br>
   static const TargetInfo::GCCRegAlias GCCRegAliases[];<br>
+  std::string CPU;<br>
 public:<br>
   PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {<br>
     LongDoubleWidth = LongDoubleAlign = 128;<br>
     LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble;<br>
   }<br>
<br>
+  virtual bool setCPU(const std::string &Name) {<br>
+    bool CPUKnown = llvm::StringSwitch<bool>(Name)<br>
+      .Case("generic", true)<br>
+      .Case("440", true)<br>
+      .Case("450", true)<br>
+      .Case("601", true)<br>
+      .Case("602", true)<br>
+      .Case("603", true)<br>
+      .Case("603e", true)<br>
+      .Case("603ev", true)<br>
+      .Case("604", true)<br>
+      .Case("604e", true)<br>
+      .Case("620", true)<br>
+      .Case("g3", true)<br>
+      .Case("7400", true)<br>
+      .Case("g4", true)<br>
+      .Case("7450", true)<br>
+      .Case("g4+", true)<br>
+      .Case("750", true)<br>
+      .Case("970", true)<br>
+      .Case("g5", true)<br>
+      .Case("a2", true)<br>
+      .Case("pwr6", true)<br>
+      .Case("pwr7", true)<br>
+      .Case("ppc", true)<br>
+      .Case("ppc64", true)<br>
+      .Default(false);<br>
+<br>
+    if (CPUKnown)<br>
+      CPU = Name;<br>
+<br>
+    return CPUKnown;<br>
+  }<br>
+<br>
   virtual void getTargetBuiltins(const Builtin::Info *&Records,<br>
                                  unsigned &NumRecords) const {<br>
     Records = BuiltinInfo;<br>
@@ -744,6 +779,20 @@<br>
     Builder.defineMacro("__VEC__", "10206");<br>
     Builder.defineMacro("__ALTIVEC__");<br>
   }<br>
+<br>
+  // CPU identification.<br>
+  if (CPU == "440") {<br>
+     Builder.defineMacro("_ARCH_440");<br>
+  } else if (CPU == "450") {<br>
+    Builder.defineMacro("_ARCH_440");<br>
+    Builder.defineMacro("_ARCH_450");<br>
+  } else if (CPU == "970") {<br>
+    Builder.defineMacro("_ARCH_970");<br>
+  } else if (CPU == "pwr6") {<br>
+    Builder.defineMacro("_ARCH_PWR6");<br>
+  } else if (CPU == "pwr7") {<br>
+    Builder.defineMacro("_ARCH_PWR7");<br>
+  }<br>
 }<br>
<br>
 bool PPCTargetInfo::hasFeature(StringRef Feature) const {<br>
<br>
Modified: cfe/trunk/lib/Driver/Tools.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=158334&r1=158333&r2=158334&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=158334&r1=158333&r2=158334&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Driver/Tools.cpp (original)<br>
+++ cfe/trunk/lib/Driver/Tools.cpp Mon Jun 11 17:35:19 2012<br>
@@ -902,6 +902,72 @@<br>
   }<br>
 }<br>
<br>
+/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.<br>
+static std::string getPPCTargetCPU(const ArgList &Args) {<br>
+  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {<br>
+    StringRef CPUName = A->getValue(Args);<br>
+<br>
+    if (CPUName == "native") {<br>
+      std::string CPU = llvm::sys::getHostCPUName();<br>
+      if (!CPU.empty() && CPU != "generic")<br>
+        return CPU;<br>
+      else<br>
+        return "";<br>
+    }<br>
+<br>
+    return llvm::StringSwitch<const char *>(CPUName)<br>
+      .Case("common", "generic")<br>
+      .Case("440", "440")<br>
+      .Case("440fp", "440")<br>
+      .Case("450", "450")<br>
+      .Case("601", "601")<br>
+      .Case("602", "602")<br>
+      .Case("603", "603")<br>
+      .Case("603e", "603e")<br>
+      .Case("603ev", "603ev")<br>
+      .Case("604", "604")<br>
+      .Case("604e", "604e")<br>
+      .Case("620", "620")<br>
+      .Case("G3", "g3")<br>
+      .Case("7400", "7400")<br>
+      .Case("G4", "g4")<br>
+      .Case("7450", "7450")<br>
+      .Case("G4+", "g4+")<br>
+      .Case("750", "750")<br>
+      .Case("970", "970")<br>
+      .Case("G5", "g5")<br>
+      .Case("a2", "a2")<br>
+      .Case("power6", "pwr6")<br>
+      .Case("power7", "pwr7")<br>
+      .Case("powerpc", "ppc")<br>
+      .Case("powerpc64", "ppc64")<br>
+      .Default("");<br>
+  }<br>
+<br>
+  return "";<br>
+}<br>
+<br>
+void Clang::AddPPCTargetArgs(const ArgList &Args,<br>
+                             ArgStringList &CmdArgs) const {<br>
+  std::string TargetCPUName = getPPCTargetCPU(Args);<br>
+<br>
+  // LLVM may default to generating code for the native CPU,<br>
+  // but, like gcc, we default to a more generic option for<br>
+  // each architecture. (except on Darwin)<br>
+  llvm::Triple Triple = getToolChain().getTriple();<br>
+  if (TargetCPUName.empty() && !Triple.isOSDarwin()) {<br>
+    if (Triple.getArch() == llvm::Triple::ppc64)<br>
+      TargetCPUName = "ppc64";<br>
+    else<br>
+      TargetCPUName = "ppc";<br>
+  }<br>
+<br>
+  if (!TargetCPUName.empty()) {<br>
+    CmdArgs.push_back("-target-cpu");<br>
+    CmdArgs.push_back(Args.MakeArgString(TargetCPUName.c_str()));<br>
+  }<br>
+}<br>
+<br>
 void Clang::AddSparcTargetArgs(const ArgList &Args,<br>
                              ArgStringList &CmdArgs) const {<br>
   const Driver &D = getToolChain().getDriver();<br>
@@ -1778,6 +1844,11 @@<br>
     AddMIPSTargetArgs(Args, CmdArgs);<br>
     break;<br>
<br>
+  case llvm::Triple::ppc:<br>
+  case llvm::Triple::ppc64:<br>
+    AddPPCTargetArgs(Args, CmdArgs);<br>
+    break;<br>
+<br>
   case llvm::Triple::sparc:<br>
     AddSparcTargetArgs(Args, CmdArgs);<br>
     break;<br>
<br>
Modified: cfe/trunk/lib/Driver/Tools.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.h?rev=158334&r1=158333&r2=158334&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.h?rev=158334&r1=158333&r2=158334&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Driver/Tools.h (original)<br>
+++ cfe/trunk/lib/Driver/Tools.h Mon Jun 11 17:35:19 2012<br>
@@ -39,6 +39,7 @@<br>
     void AddARMTargetArgs(const ArgList &Args, ArgStringList &CmdArgs,<br>
                           bool KernelOrKext) const;<br>
     void AddMIPSTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const;<br>
+    void AddPPCTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const;<br>
     void AddSparcTargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const;<br>
     void AddX86TargetArgs(const ArgList &Args, ArgStringList &CmdArgs) const;<br>
     void AddHexagonTargetArgs (const ArgList &Args, ArgStringList &CmdArgs) const;<br>
<br>
Modified: cfe/trunk/test/Driver/clang-translation.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/clang-translation.c?rev=158334&r1=158333&r2=158334&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/clang-translation.c?rev=158334&r1=158333&r2=158334&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/test/Driver/clang-translation.c (original)<br>
+++ cfe/trunk/test/Driver/clang-translation.c Mon Jun 11 17:35:19 2012<br>
@@ -51,3 +51,23 @@<br>
 // ARMV5E: clang<br>
 // ARMV5E: "-cc1"<br>
 // ARMV5E: "-target-cpu" "arm1022e"<br>
+<br>
+// RUN: %clang -target powerpc64-unknown-linux-gnu -### -S %s 2> %t.log \<br>
+// RUN:   -mcpu=G5<br>
+// RUN: FileCheck -check-prefix=PPCG5 %s < %t.log<br>
+// PPCG5: clang<br>
+// PPCG5: "-cc1"<br>
+// PPCG5: "-target-cpu" "g5"<br>
+<br>
+// RUN: %clang -target powerpc64-unknown-linux-gnu -### -S %s 2> %t.log \<br>
+// RUN:   -mcpu=power7<br>
+// RUN: FileCheck -check-prefix=PPCPWR7 %s < %t.log<br>
+// PPCPWR7: clang<br>
+// PPCPWR7: "-cc1"<br>
+// PPCPWR7: "-target-cpu" "pwr7"<br>
+<br>
+// RUN: %clang -target powerpc64-unknown-linux-gnu -### -S %s 2> %t.log<br>
+// RUN: FileCheck -check-prefix=PPC64NS %s < %t.log<br>
+// PPC64NS: clang<br>
+// PPC64NS: "-cc1"<br>
+// PPC64NS: "-target-cpu" "ppc64"<br>
<br>
Modified: cfe/trunk/test/Preprocessor/init.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/init.c?rev=158334&r1=158333&r2=158334&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/init.c?rev=158334&r1=158333&r2=158334&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/test/Preprocessor/init.c (original)<br>
+++ cfe/trunk/test/Preprocessor/init.c Mon Jun 11 17:35:19 2012<br>
@@ -967,10 +967,11 @@<br>
 // MSP430:#define __WINT_WIDTH__ 16<br>
 // MSP430:#define __clang__ 1<br>
 //<br>
-// RUN: %clang_cc1 -E -dM -ffreestanding -triple=powerpc64-none-none -fno-signed-char < /dev/null | FileCheck -check-prefix PPC64 %s<br>
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=powerpc64-none-none -target-cpu pwr7 -fno-signed-char < /dev/null | FileCheck -check-prefix PPC64 %s<br>
 //<br>
 // PPC64:#define _ARCH_PPC 1<br>
 // PPC64:#define _ARCH_PPC64 1<br>
+// PPC64:#define _ARCH_PWR7 1<br>
 // PPC64:#define _BIG_ENDIAN 1<br>
 // PPC64:#define _LP64 1<br>
 // PPC64:#define __BIG_ENDIAN__ 1<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br>