[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 22:30:17 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/3] [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/3] 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);

>From 9574308a1032fb4da3d5db61d144b51f832f6000 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Wed, 27 Dec 2023 14:30:00 +0800
Subject: [PATCH 3/3] Use llvm::errorToBool

---
 clang/lib/Driver/Driver.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a8b7d0587c2f3f..ed1169ac45029f 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -77,6 +77,7 @@
 #include "llvm/Option/OptTable.h"
 #include "llvm/Option/Option.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ExitCodes.h"
 #include "llvm/Support/FileSystem.h"
@@ -673,10 +674,7 @@ static llvm::Triple computeTargetTriple(const Driver &D,
       StringRef ArchName = tools::riscv::getRISCVArch(Args, Target);
       auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
           ArchName, /*EnableExperimentalExtensions=*/true);
-      if (!ISAInfo) {
-        // Ignore any error here, we assume it will be handled in another place.
-        consumeError(ISAInfo.takeError());
-      } else {
+      if (!llvm::errorToBool(ISAInfo.takeError())) {
         unsigned XLen = (*ISAInfo)->getXLen();
         if (XLen == 32)
           Target.setArch(llvm::Triple::riscv32);



More information about the cfe-commits mailing list