[PATCH] D144853: [Clang][RISCV] Add CMake options to configure default CPU

Wang Pengcheng via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 27 01:33:04 PST 2023


pcwang-thead created this revision.
pcwang-thead added reviewers: asb, jrtc27, kito-cheng, craig.topper, reames, eopXD.
Herald added subscribers: luke, VincentWu, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, arichardson.
Herald added a project: All.
pcwang-thead requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

For GCC, we can configure default target with `with-{tune/arch/abi}`
options. While for Clang, we don't have the similar way to configure.

Here we add two CMake options `CLANG_RISCV32_DEFAULT_CPU` and
`CLANG_RISCV64_DEFAULT_CPU` to configure default CPU for riscv32
and riscv64.

By specifying these two options when compiling Clang/LLVM, we can
achieve the same goal just like GCC's `with-{tune/arch/abi}`:

- Tune/arch info can be inferred from CPU info.
- Default ABI can be computed from arch string.

And we can do native detection via specifying default CPU to `native`.

Test `clang/test/Driver/riscv-march-mcpu-mtune.c` will failed if we
specify different default CPU.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144853

Files:
  clang/CMakeLists.txt
  clang/include/clang/Config/config.h.cmake
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp


Index: clang/lib/Driver/ToolChains/Arch/RISCV.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -10,6 +10,7 @@
 #include "../Clang.h"
 #include "ToolChains/CommonArgs.h"
 #include "clang/Basic/CharInfo.h"
+#include "clang/Config/config.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
@@ -320,5 +321,11 @@
   if (!CPU.empty())
     return CPU;
 
-  return Triple.isRISCV64() ? "generic-rv64" : "generic-rv32";
+  std::string DefaultCPU = Triple.isRISCV64() ? CLANG_RISCV64_DEFAULT_CPU
+                                              : CLANG_RISCV32_DEFAULT_CPU;
+  // Handle default CPU name is 'native'.
+  if (DefaultCPU == "native")
+    DefaultCPU = llvm::sys::getHostCPUName();
+
+  return DefaultCPU;
 }
Index: clang/include/clang/Config/config.h.cmake
===================================================================
--- clang/include/clang/Config/config.h.cmake
+++ clang/include/clang/Config/config.h.cmake
@@ -32,6 +32,10 @@
 /* Default architecture for SystemZ. */
 #define CLANG_SYSTEMZ_DEFAULT_ARCH "${CLANG_SYSTEMZ_DEFAULT_ARCH}"
 
+/* Default CPU for RISCV. */
+#define CLANG_RISCV32_DEFAULT_CPU "${CLANG_RISCV32_DEFAULT_CPU}"
+#define CLANG_RISCV64_DEFAULT_CPU "${CLANG_RISCV64_DEFAULT_CPU}"
+
 /* Multilib basename for libdir. */
 #define CLANG_INSTALL_LIBDIR_BASENAME "${CLANG_INSTALL_LIBDIR_BASENAME}"
 
Index: clang/CMakeLists.txt
===================================================================
--- clang/CMakeLists.txt
+++ clang/CMakeLists.txt
@@ -247,6 +247,9 @@
 
 set(CLANG_SYSTEMZ_DEFAULT_ARCH "z10" CACHE STRING "SystemZ Default Arch")
 
+set(CLANG_RISCV32_DEFAULT_CPU "generic-rv32" CACHE STRING "RISCV32 Default CPU")
+set(CLANG_RISCV64_DEFAULT_CPU "generic-rv64" CACHE STRING "RISCV64 Default CPU")
+
 set(CLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
   "Vendor-specific text for showing with version information.")
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144853.500701.patch
Type: text/x-patch
Size: 2038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230227/4cd6a274/attachment-0001.bin>


More information about the cfe-commits mailing list