[clang] [llvm] Look for compiler-rt from subdir given by --target (PR #88334)

YunQiang Su via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 10 17:38:58 PDT 2024


https://github.com/wzssyqa created https://github.com/llvm/llvm-project/pull/88334

Currently, clang looks for compiler-rt only from the normalized triple subdir. While if we are configured with a non-normalized triple with -DLLVM_DEFAULT_TARGET_TRIPLE, such as triples without vendor section, clang will fail to find compiler_rt.

Let's look for compiler_rt from the subdir with name from --target option, too.

To archive this, we add a new member called Origin to class Triple.

Fixes: #87150.

>From a4d6590cf3cecf90b3914933313749e8aad03210 Mon Sep 17 00:00:00 2001
From: YunQiang Su <syq at gcc.gnu.org>
Date: Mon, 1 Apr 2024 17:17:08 +0800
Subject: [PATCH] Look for compiler-rt from subdir given by --target

Currently, clang looks for compiler-rt only from the normalized
triple subdir. While if we are configured with a non-normalized
triple with -DLLVM_DEFAULT_TARGET_TRIPLE, such as triples without
vendor section, clang will fail to find compiler_rt.

Let's look for compiler_rt from the subdir with name from --target
option.

To archive this, we add a new member called Origin to class Triple.

Fixes: #87150.
---
 clang/lib/Driver/Driver.cpp             | 1 +
 clang/lib/Driver/ToolChain.cpp          | 6 ++++++
 llvm/include/llvm/TargetParser/Triple.h | 6 ++++++
 llvm/lib/TargetParser/Triple.cpp        | 6 +++---
 4 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index e7335a61b10c53..4a0c939039eb31 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -516,6 +516,7 @@ static llvm::Triple computeTargetTriple(const Driver &D,
     TargetTriple = A->getValue();
 
   llvm::Triple Target(llvm::Triple::normalize(TargetTriple));
+  Target.setOrigin(TargetTriple);
 
   // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made
   // -gnu* only, and we can not change this, so we have to detect that case as
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 237092ed07e5dc..57f27a61c4060b 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -755,6 +755,12 @@ std::optional<std::string>
 ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
   auto getPathForTriple =
       [&](const llvm::Triple &Triple) -> std::optional<std::string> {
+    if (!Triple.getOrigin().empty()) {
+      SmallString<128> Po(BaseDir);
+      llvm::sys::path::append(Po, Triple.getOrigin());
+      if (getVFS().exists(Po))
+        return std::string(Po);
+    }
     SmallString<128> P(BaseDir);
     llvm::sys::path::append(P, Triple.str());
     if (getVFS().exists(P))
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index f256e2b205a889..a2fc28ada0ca31 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -298,6 +298,8 @@ class Triple {
 private:
   std::string Data;
 
+  StringRef Origin = StringRef();
+
   /// The parsed arch type.
   ArchType Arch{};
 
@@ -425,6 +427,8 @@ class Triple {
 
   const std::string &getTriple() const { return Data; }
 
+  const StringRef getOrigin() const { return Origin; }
+
   /// Get the architecture (first) component of the triple.
   StringRef getArchName() const;
 
@@ -1058,6 +1062,8 @@ class Triple {
   /// @name Mutators
   /// @{
 
+  void setOrigin(StringRef Orig) { Origin = Orig; };
+
   /// Set the architecture (first) component of the triple to a known type.
   void setArch(ArchType Kind, SubArchType SubArch = NoSubArch);
 
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 624679ff507a7f..ce44903d0f7d70 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -928,9 +928,9 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
 /// This stores the string representation and parses the various pieces into
 /// enum members.
 Triple::Triple(const Twine &Str)
-    : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
-      Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
-      ObjectFormat(UnknownObjectFormat) {
+    : Data(Str.str()), Origin(Str.getSingleStringRef()), Arch(UnknownArch),
+      SubArch(NoSubArch), Vendor(UnknownVendor), OS(UnknownOS),
+      Environment(UnknownEnvironment), ObjectFormat(UnknownObjectFormat) {
   // Do minimal parsing by hand here.
   SmallVector<StringRef, 4> Components;
   StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);



More information about the cfe-commits mailing list