[llvm] Triple: Fix handling of macos with unexpected target arches (PR #75469)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 04:56:06 PST 2023


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/75469

Some tools with a specified target arch, but no full triple default to the host triple. On macos hosts, this would then force using macho on targets that didn't expect it, resulting in assertions.

We should also probably emit explicit errors if the object format is specified on targets which don't handle it.

>From eef83d7acb3d37027ec05db60f285a5436cac9ae Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Wed, 13 Dec 2023 13:15:19 +0700
Subject: [PATCH] Triple: Fix handling of macos with unexpected target arches

Some tools with a specified target arch, but no full triple default
to the host triple. On macos hosts, this would then force using macho
on targets that didn't expect it, resulting in assertions.

We should also probably emit explicit errors if the object format
is specified on targets which don't handle it.
---
 llvm/lib/TargetParser/Triple.cpp           | 8 +++++---
 llvm/unittests/TargetParser/TripleTest.cpp | 7 +++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index ac04dab0489717..ae83e06530c67c 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -819,8 +819,6 @@ static Triple::SubArchType parseSubArch(StringRef SubArchName) {
 }
 
 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
-  if (T.isOSDarwin())
-    return Triple::MachO;
   switch (T.getArch()) {
   case Triple::UnknownArch:
   case Triple::aarch64:
@@ -833,8 +831,10 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
       return Triple::COFF;
     else if (T.isUEFI())
       return Triple::COFF;
-    return Triple::ELF;
 
+    if (T.isOSDarwin())
+      return Triple::MachO;
+    return Triple::ELF;
   case Triple::aarch64_be:
   case Triple::amdgcn:
   case Triple::amdil64:
@@ -887,6 +887,8 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
   case Triple::ppc:
     if (T.isOSAIX())
       return Triple::XCOFF;
+    if (T.isOSDarwin())
+      return Triple::MachO;
     return Triple::ELF;
 
   case Triple::systemz:
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index 575e2ca381df36..d3bde2986ea2cd 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -2037,6 +2037,13 @@ TEST(TripleTest, FileFormat) {
   T.setObjectFormat(Triple::SPIRV);
   EXPECT_EQ(Triple::SPIRV, T.getObjectFormat());
   EXPECT_EQ("spirv", Triple::getObjectFormatTypeName(T.getObjectFormat()));
+
+  EXPECT_EQ(Triple::ELF, Triple("amdgcn-apple-macosx").getObjectFormat());
+  EXPECT_EQ(Triple::ELF, Triple("r600-apple-macosx").getObjectFormat());
+  EXPECT_EQ(Triple::SPIRV, Triple("spirv-apple-macosx").getObjectFormat());
+  EXPECT_EQ(Triple::SPIRV, Triple("spirv32-apple-macosx").getObjectFormat());
+  EXPECT_EQ(Triple::SPIRV, Triple("spirv64-apple-macosx").getObjectFormat());
+  EXPECT_EQ(Triple::DXContainer, Triple("dxil-apple-macosx").getObjectFormat());
 }
 
 TEST(TripleTest, NormalizeWindows) {



More information about the llvm-commits mailing list