Skip to content

Commit 6294129

Browse files
authored
Triple: Fix handling of macos with unexpected target arches (#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.
1 parent 6c9813a commit 6294129

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

llvm/lib/TargetParser/Triple.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,6 @@ static Triple::SubArchType parseSubArch(StringRef SubArchName) {
819819
}
820820

821821
static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
822-
if (T.isOSDarwin())
823-
return Triple::MachO;
824822
switch (T.getArch()) {
825823
case Triple::UnknownArch:
826824
case Triple::aarch64:
@@ -829,12 +827,13 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
829827
case Triple::thumb:
830828
case Triple::x86:
831829
case Triple::x86_64:
832-
if (T.isOSWindows())
830+
switch (T.getOS()) {
831+
case Triple::Win32:
832+
case Triple::UEFI:
833833
return Triple::COFF;
834-
else if (T.isUEFI())
835-
return Triple::COFF;
836-
return Triple::ELF;
837-
834+
default:
835+
return T.isOSDarwin() ? Triple::MachO : Triple::ELF;
836+
}
838837
case Triple::aarch64_be:
839838
case Triple::amdgcn:
840839
case Triple::amdil64:
@@ -887,6 +886,8 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
887886
case Triple::ppc:
888887
if (T.isOSAIX())
889888
return Triple::XCOFF;
889+
if (T.isOSDarwin())
890+
return Triple::MachO;
890891
return Triple::ELF;
891892

892893
case Triple::systemz:

llvm/unittests/TargetParser/TripleTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,13 @@ TEST(TripleTest, FileFormat) {
20372037
T.setObjectFormat(Triple::SPIRV);
20382038
EXPECT_EQ(Triple::SPIRV, T.getObjectFormat());
20392039
EXPECT_EQ("spirv", Triple::getObjectFormatTypeName(T.getObjectFormat()));
2040+
2041+
EXPECT_EQ(Triple::ELF, Triple("amdgcn-apple-macosx").getObjectFormat());
2042+
EXPECT_EQ(Triple::ELF, Triple("r600-apple-macosx").getObjectFormat());
2043+
EXPECT_EQ(Triple::SPIRV, Triple("spirv-apple-macosx").getObjectFormat());
2044+
EXPECT_EQ(Triple::SPIRV, Triple("spirv32-apple-macosx").getObjectFormat());
2045+
EXPECT_EQ(Triple::SPIRV, Triple("spirv64-apple-macosx").getObjectFormat());
2046+
EXPECT_EQ(Triple::DXContainer, Triple("dxil-apple-macosx").getObjectFormat());
20402047
}
20412048

20422049
TEST(TripleTest, NormalizeWindows) {

0 commit comments

Comments
 (0)