[clang] [RISCV][NFC] Use RISCVISAInfo instead of string comparison (PR #76387)

Wang Pengcheng via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 26 01:16:15 PST 2023


https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/76387

>From ed8ebdb6f2133f84d1f5a8d2cd580dba4ceed922 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Tue, 26 Dec 2023 15:58:10 +0800
Subject: [PATCH 1/2] [RISCV][NFC] Use RISCVISAInfo instead of string
 comparison

The arch string may not start with rv32/rv64 if we have supported
profiles in `-march`.
---
 clang/lib/Driver/Driver.cpp | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index ff95c899c5f3d4..671a11f9811f5b 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -86,6 +86,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Process.h"
 #include "llvm/Support/Program.h"
+#include "llvm/Support/RISCVISAInfo.h"
 #include "llvm/Support/StringSaver.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
@@ -670,10 +671,15 @@ static llvm::Triple computeTargetTriple(const Driver &D,
     if (Args.hasArg(options::OPT_march_EQ) ||
         Args.hasArg(options::OPT_mcpu_EQ)) {
       StringRef ArchName = tools::riscv::getRISCVArch(Args, Target);
-      if (ArchName.starts_with_insensitive("rv32"))
-        Target.setArch(llvm::Triple::riscv32);
-      else if (ArchName.starts_with_insensitive("rv64"))
-        Target.setArch(llvm::Triple::riscv64);
+      auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
+          ArchName, /*EnableExperimentalExtensions=*/true);
+      if (ISAInfo) {
+        unsigned XLen = (*ISAInfo)->getXLen();
+        if (XLen == 32)
+          Target.setArch(llvm::Triple::riscv32);
+        else if (XLen == 64)
+          Target.setArch(llvm::Triple::riscv64);
+      }
     }
   }
 

>From 24f77edf357eb5f6fef207ab79d8df725a1ec27f Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Tue, 26 Dec 2023 17:16:00 +0800
Subject: [PATCH 2/2] Handle the error case

---
 clang/lib/Driver/Driver.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 671a11f9811f5b..a8b7d0587c2f3f 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -673,7 +673,10 @@ static llvm::Triple computeTargetTriple(const Driver &D,
       StringRef ArchName = tools::riscv::getRISCVArch(Args, Target);
       auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
           ArchName, /*EnableExperimentalExtensions=*/true);
-      if (ISAInfo) {
+      if (!ISAInfo) {
+        // Ignore any error here, we assume it will be handled in another place.
+        consumeError(ISAInfo.takeError());
+      } else {
         unsigned XLen = (*ISAInfo)->getXLen();
         if (XLen == 32)
           Target.setArch(llvm::Triple::riscv32);



More information about the cfe-commits mailing list