[clang] [llvm] [LoongArch] add la v1.1 features for sys::getHostCPUFeatures (PR #115832)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 13 18:12:30 PST 2024


https://github.com/tangaac updated https://github.com/llvm/llvm-project/pull/115832

>From 206bfe8e5e504620c1cd089298a2fedf8c4f3d23 Mon Sep 17 00:00:00 2001
From: tangaac <tangyan01 at loongson.cn>
Date: Tue, 12 Nov 2024 16:31:57 +0800
Subject: [PATCH 1/2] add la v1.1 features for sys::getHostCPUFeatures

---
 clang/lib/Driver/ToolChains/Arch/LoongArch.cpp | 10 ++++++++--
 llvm/lib/TargetParser/Host.cpp                 |  9 +++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index e69a5562137ccd..8cd51d47e14d72 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -14,6 +14,7 @@
 #include "clang/Driver/Options.h"
 #include "llvm/TargetParser/Host.h"
 #include "llvm/TargetParser/LoongArchTargetParser.h"
+#include <string>
 
 using namespace clang::driver;
 using namespace clang::driver::tools;
@@ -135,10 +136,15 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
     Features.push_back("+lsx");
 
   std::string ArchName;
-  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
-    ArchName = A->getValue();
+  const Arg *MArch = Args.getLastArg(options::OPT_march_EQ);
+  if (MArch)
+    ArchName = MArch->getValue();
   ArchName = postProcessTargetCPUString(ArchName, Triple);
   llvm::LoongArch::getArchFeatures(ArchName, Features);
+  if ((std::string)(MArch->getValue()) == "native")
+    for (auto &F : llvm::sys::getHostCPUFeatures())
+      Features.push_back(
+          Args.MakeArgString((F.second ? "+" : "-") + F.first()));
 
   // Select floating-point features determined by -mdouble-float,
   // -msingle-float, -msoft-float and -mfpu.
diff --git a/llvm/lib/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp
index 58ba2553633221..0250aac9a7cb86 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -2006,6 +2006,15 @@ const StringMap<bool> sys::getHostCPUFeatures() {
   Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
   Features["lvz"] = hwcap & (1UL << 9);  // HWCAP_LOONGARCH_LVZ
 
+  Features["frecipe"] = cpucfg2 & (1U << 25); // CPUCFG.2.FRECIPE
+  Features["lam-bh"] = cpucfg2 & (1U << 27);  // CPUCFG.2.LAM_BH
+
+  // TODO: Need to complete.
+  // Features["div32"] = cpucfg2 & (1U << 26);       // CPUCFG.2.DIV32
+  // Features["lamcas"] = cpucfg2 & (1U << 28);      // CPUCFG.2.LAMCAS
+  // Features["llacq-screl"] = cpucfg2 & (1U << 29); // CPUCFG.2.LLACQ_SCREL
+  // Features["scq"] = cpucfg2 & (1U << 30);         // CPUCFG.2.SCQ
+  // Features["ld-seq-sa"] = cpucfg3 & (1U << 23); // CPUCFG.3.LD_SEQ_SA
   return Features;
 }
 #elif defined(__linux__) && defined(__riscv)

>From 1f3511382eea81ec3ae8ebe1fefad0fd64faa3fd Mon Sep 17 00:00:00 2001
From: tangaac <tangyan01 at loongson.cn>
Date: Thu, 14 Nov 2024 09:14:47 +0800
Subject: [PATCH 2/2] Update clang/lib/Driver/ToolChains/Arch/LoongArch.cpp

Co-authored-by: Lu Weining <luweining at loongson.cn>
---
 clang/lib/Driver/ToolChains/Arch/LoongArch.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index 8cd51d47e14d72..987db4638fca88 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -14,7 +14,6 @@
 #include "clang/Driver/Options.h"
 #include "llvm/TargetParser/Host.h"
 #include "llvm/TargetParser/LoongArchTargetParser.h"
-#include <string>
 
 using namespace clang::driver;
 using namespace clang::driver::tools;
@@ -141,7 +140,7 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
     ArchName = MArch->getValue();
   ArchName = postProcessTargetCPUString(ArchName, Triple);
   llvm::LoongArch::getArchFeatures(ArchName, Features);
-  if ((std::string)(MArch->getValue()) == "native")
+  if (MArch && StringRef(MArch->getValue()) == "native")
     for (auto &F : llvm::sys::getHostCPUFeatures())
       Features.push_back(
           Args.MakeArgString((F.second ? "+" : "-") + F.first()));



More information about the cfe-commits mailing list