[llvm] Triple::normalize: Set OS for 3-component triple with none as middle (PR #89638)

YunQiang Su via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 22 10:52:11 PDT 2024


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

If the middle component of a 3-component triple fails to parse as known Arch/Vendor/OS/Env, it will fallback as Vendor. While for some cases, we may wish to recognize it as OS, such as `arm64-none-elf`.

In this patch, we will set OS as `none`, if:
	1) Arch is found;
	2) Env is found;
	3) OS is not found and thus is set as empty;
	4) Vendor is not found while is set as "none",
we will swap Component[2] and Component[3].

Fixes: #89582.

>From fcac15329241e1d2cf88f7021365c2dde41a3ddb Mon Sep 17 00:00:00 2001
From: YunQiang Su <syq at gcc.gnu.org>
Date: Tue, 23 Apr 2024 01:36:17 +0800
Subject: [PATCH] Triple::normalize: Set OS for 3-component triple with none as
 middle

If the middle component of a 3-component triple fails to parse as known
Arch/Vendor/OS/Env, it will fallback as Vendor. While for some cases,
we may wish to recognize it as OS, such as `arm64-none-elf`.

In this patch, we will set OS as `none`, if:
	1) Arch is found;
	2) Env is found;
	3) OS is not found and thus is set as empty;
	4) Vendor is not found while is set as "none",
we will swap Component[2] and Component[3].

Fixes: #89582.
---
 llvm/lib/TargetParser/Triple.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 77fdf31d4865c0..07f3df4145dad4 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1149,6 +1149,13 @@ std::string Triple::normalize(StringRef Str) {
     }
   }
 
+  // For 3-component triples, the middle component is used to set Vendor;
+  // while if it is "none", we'd prefer to set OS.
+  // This is for some baremetal cases, such as "arm-none-elf".
+  if (Found[0] && !Found[1] && !Found[2] && Found[3] &&
+      Components[1].equals("none") && Components[2].empty())
+    std::swap(Components[1], Components[2]);
+
   // Replace empty components with "unknown" value.
   for (StringRef &C : Components)
     if (C.empty())



More information about the llvm-commits mailing list