[clang] [Driver][Sparc] Default to -mcpu=v9 for 32-bit Linux/sparc64 (PR #109278)

Rainer Orth via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 19 05:48:26 PDT 2024


https://github.com/rorth created https://github.com/llvm/llvm-project/pull/109278

While working on supporting PR #109101 on Linux/sparc64, I was reminded that `clang -m32` still defaults to generating V8 code, although the 64-bit kernel requires a V9 CPU.

This patch corrects that on V9-only distros (Debian, Gentoo).

Tested on `sparc64-unknown-linux-gnu`, `x86_64-pc-linux-gnu`, `sparcv9-sun-solaris2.11`, and `amd64-pc-solaris2.11`.

A previous version of this patch was submitted as [[Driver][Sparc] Default to -mcpu=v9 for SparcV8 on Linux](https://reviews.llvm.org/D130688), but got stalled for 2+ years.

>From 9123b7eae524418d0a671ac2114ddea2b2cf9695 Mon Sep 17 00:00:00 2001
From: Rainer Orth <ro at gcc.gnu.org>
Date: Thu, 19 Sep 2024 14:46:54 +0200
Subject: [PATCH] [Driver][Sparc] Default to -mcpu=v9 for 32-bit Linux/sparc64

While working on supporting PR #109101 on Linux/sparc64, I was reminded
that `clang -m32` still defaults to generating V8 code, although the 64-bit
kernel requires a V9 CPU.

This patch corrects that on V9-only distros (Debian, Gentoo).

Tested on `sparc64-unknown-linux-gnu`, `x86_64-pc-linux-gnu`,
`sparcv9-sun-solaris2.11`, and `amd64-pc-solaris2.11`.

A previous version of this patch was submitted as [[Driver][Sparc] Default
to -mcpu=v9 for SparcV8 on Linux](https://reviews.llvm.org/D130688), but
got stalled for 2+ years.
---
 clang/lib/Driver/ToolChains/Arch/Sparc.cpp       | 5 ++++-
 clang/test/Preprocessor/predefined-arch-macros.c | 7 +++++--
 clang/test/lit.cfg.py                            | 4 ++++
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/Sparc.cpp b/clang/lib/Driver/ToolChains/Arch/Sparc.cpp
index f7f0a265fef68b..44eddc7603b4bd 100644
--- a/clang/lib/Driver/ToolChains/Arch/Sparc.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/Sparc.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "Sparc.h"
+#include "clang/Driver/Distro.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
@@ -125,7 +126,9 @@ std::string sparc::getSparcTargetCPU(const Driver &D, const ArgList &Args,
     return std::string(CPUName);
   }
 
-  if (Triple.getArch() == llvm::Triple::sparc && Triple.isOSSolaris())
+  Distro Dist(D.getVFS(), Triple);
+  if (Triple.getArch() == llvm::Triple::sparc &&
+      (Triple.isOSSolaris() || Dist.IsDebian() || Dist.IsGentoo()))
     return "v9";
   return "";
 }
diff --git a/clang/test/Preprocessor/predefined-arch-macros.c b/clang/test/Preprocessor/predefined-arch-macros.c
index a149c69ee0cdb2..2e576546b45c7a 100644
--- a/clang/test/Preprocessor/predefined-arch-macros.c
+++ b/clang/test/Preprocessor/predefined-arch-macros.c
@@ -4131,13 +4131,16 @@
 
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target sparc-unknown-linux \
-// RUN:   | FileCheck -match-full-lines %s -check-prefix=CHECK_SPARC
+// RUN:   | FileCheck -match-full-lines %s -check-prefixes=CHECK_SPARC,CHECK_SPARC%if sparc64-distro %{_V9%} %else %{_V8%}
 // CHECK_SPARC: #define __BIG_ENDIAN__ 1
 // CHECK_SPARC: #define __sparc 1
 // CHECK_SPARC: #define __sparc__ 1
 // CHECK_SPARC-NOT: #define __sparcv9 1
 // CHECK_SPARC-NOT: #define __sparcv9__ 1
-// CHECK_SPARC: #define __sparcv8 1
+// CHECK_SPARC_V8: #define __sparcv8 1
+// CHECK_SPARC_V8-NOT: #define __sparc_v9__ 1
+// CHECK_SPARC_V9: #define __sparc_v9__ 1
+// CHECK_SPARC_V9-NOT: #define __sparcv8 1
 // CHECK_SPARC-NOT: #define __sparcv9 1
 // CHECK_SPARC-NOT: #define __sparcv9__ 1
 
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index 92a3361ce672e2..068975fac5a7a4 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -224,6 +224,10 @@ def have_host_clang_repl_cuda():
         "default-cxx-stdlib={}".format(config.clang_default_cxx_stdlib)
     )
 
+# clang -m32 defaults to -mcpu=v9 on Linux/sparc64 distros.
+if re.search(r"debian|gentoo", platform.version(), re.I):
+    config.available_features.add("sparc64-distro")
+
 # As of 2011.08, crash-recovery tests still do not pass on FreeBSD.
 if platform.system() not in ["FreeBSD"]:
     config.available_features.add("crash-recovery")



More information about the cfe-commits mailing list