Skip to content

Commit

Permalink
[llvm][Triple] Add Environment members and parsing for glibc/musl p…
Browse files Browse the repository at this point in the history
…arity. (llvm#107664)

This adds support for:

* `muslabin32` (MIPS N32)
* `muslabi64` (MIPS N64)
* `muslf32` (LoongArch ILP32F/LP64F)
* `muslsf` (LoongArch ILP32S/LP64S)

As we start adding glibc/musl cross-compilation support for these
targets in Zig, it would make our life easier if LLVM recognized these
triples. I'm hoping this'll be uncontroversial since the same has
already been done for `musleabi`, `musleabihf`, and `muslx32`.

I intentionally left out a musl equivalent of `gnuf64` (LoongArch
ILP32D/LP64D); my understanding is that Loongson ultimately settled on
simply `gnu` for this much more common case, so there doesn't *seem* to
be a particularly compelling reason to add a `muslf64` that's basically
deprecated on arrival.

Note: I don't have commit access.
  • Loading branch information
alexrp authored Sep 20, 2024
1 parent b3533a1 commit 72a2180
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 9 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/Mips.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {

if (Triple.isMIPS32())
setABI("o32");
else if (Triple.getEnvironment() == llvm::Triple::GNUABIN32)
else if (Triple.isABIN32())
setABI("n32");
else
setABI("n64");
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,17 @@ static llvm::Triple computeTargetTriple(const Driver &D,
if (Target.getEnvironment() == llvm::Triple::GNU ||
Target.getEnvironment() == llvm::Triple::GNUABI64)
Target.setEnvironment(llvm::Triple::GNUABIN32);
else if (Target.getEnvironment() == llvm::Triple::Musl ||
Target.getEnvironment() == llvm::Triple::MuslABI64)
Target.setEnvironment(llvm::Triple::MuslABIN32);
} else if (ABIName == "64") {
Target = Target.get64BitArchVariant();
if (Target.getEnvironment() == llvm::Triple::GNU ||
Target.getEnvironment() == llvm::Triple::GNUABIN32)
Target.setEnvironment(llvm::Triple::GNUABI64);
else if (Target.getEnvironment() == llvm::Triple::Musl ||
Target.getEnvironment() == llvm::Triple::MuslABIN32)
Target.setEnvironment(llvm::Triple::MuslABI64);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ StringRef loongarch::getLoongArchABI(const Driver &D, const ArgList &Args,
// present, falling back to {ILP32,LP64}D otherwise.
switch (Triple.getEnvironment()) {
case llvm::Triple::GNUSF:
case llvm::Triple::MuslSF:
return IsLA32 ? "ilp32s" : "lp64s";
case llvm::Triple::GNUF32:
case llvm::Triple::MuslF32:
return IsLA32 ? "ilp32f" : "lp64f";
case llvm::Triple::GNUF64:
// This was originally permitted (and indeed the canonical way) to
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Arch/Mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void mips::getMipsCPUAndABI(const ArgList &Args, const llvm::Triple &Triple,
}
}

if (ABIName.empty() && (Triple.getEnvironment() == llvm::Triple::GNUABIN32))
if (ABIName.empty() && Triple.isABIN32())
ABIName = "n32";

if (ABIName.empty() &&
Expand Down
6 changes: 2 additions & 4 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,11 @@ static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
case llvm::Triple::mipsel:
return "elf32ltsmip";
case llvm::Triple::mips64:
if (tools::mips::hasMipsAbiArg(Args, "n32") ||
T.getEnvironment() == llvm::Triple::GNUABIN32)
if (tools::mips::hasMipsAbiArg(Args, "n32") || T.isABIN32())
return "elf32btsmipn32";
return "elf64btsmip";
case llvm::Triple::mips64el:
if (tools::mips::hasMipsAbiArg(Args, "n32") ||
T.getEnvironment() == llvm::Triple::GNUABIN32)
if (tools::mips::hasMipsAbiArg(Args, "n32") || T.isABIN32())
return "elf32ltsmipn32";
return "elf64ltsmip";
case llvm::Triple::systemz:
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ std::string Linux::getMultiarchTriple(const Driver &D,
default:
return TargetTriple.str();
case llvm::Triple::GNUSF:
case llvm::Triple::MuslSF:
FPFlavor = "sf";
break;
case llvm::Triple::GNUF32:
case llvm::Triple::MuslF32:
FPFlavor = "f32";
break;
case llvm::Triple::GNU:
case llvm::Triple::GNUF64:
case llvm::Triple::Musl:
// This was going to be "f64" in an earlier Toolchain Conventions
// revision, but starting from Feb 2023 the F64 ABI variants are
// unmarked in their canonical forms.
Expand Down
14 changes: 14 additions & 0 deletions llvm/include/llvm/TargetParser/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,12 @@ class Triple {
EABIHF,
Android,
Musl,
MuslABIN32,
MuslABI64,
MuslEABI,
MuslEABIHF,
MuslF32,
MuslSF,
MuslX32,

MSVC,
Expand Down Expand Up @@ -783,8 +787,12 @@ class Triple {
/// Tests whether the environment is musl-libc
bool isMusl() const {
return getEnvironment() == Triple::Musl ||
getEnvironment() == Triple::MuslABIN32 ||
getEnvironment() == Triple::MuslABI64 ||
getEnvironment() == Triple::MuslEABI ||
getEnvironment() == Triple::MuslEABIHF ||
getEnvironment() == Triple::MuslF32 ||
getEnvironment() == Triple::MuslSF ||
getEnvironment() == Triple::MuslX32 ||
getEnvironment() == Triple::OpenHOS || isOSLiteOS();
}
Expand Down Expand Up @@ -1033,6 +1041,12 @@ class Triple {
getSubArch() == Triple::AArch64SubArch_arm64e;
}

// Tests whether the target is N32.
bool isABIN32() const {
EnvironmentType Env = getEnvironment();
return Env == Triple::GNUABIN32 || Env == Triple::MuslABIN32;
}

/// Tests whether the target is X32.
bool isX32() const {
EnvironmentType Env = getEnvironment();
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchBaseInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ static ABI getTripleABI(const Triple &TT) {
ABI TripleABI;
switch (TT.getEnvironment()) {
case llvm::Triple::EnvironmentType::GNUSF:
case llvm::Triple::EnvironmentType::MuslSF:
TripleABI = Is64Bit ? ABI_LP64S : ABI_ILP32S;
break;
case llvm::Triple::EnvironmentType::GNUF32:
case llvm::Triple::EnvironmentType::MuslF32:
TripleABI = Is64Bit ? ABI_LP64F : ABI_ILP32F;
break;
// Let the fallback case behave like {ILP32,LP64}D.
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ MipsABIInfo MipsABIInfo::computeTargetABI(const Triple &TT, StringRef CPU,
return MipsABIInfo::N32();
if (Options.getABIName().starts_with("n64"))
return MipsABIInfo::N64();
if (TT.getEnvironment() == llvm::Triple::GNUABIN32)
if (TT.isABIN32())
return MipsABIInfo::N32();
assert(Options.getABIName().empty() && "Unknown ABI option for MIPS");

Expand Down Expand Up @@ -125,4 +125,3 @@ unsigned MipsABIInfo::GetEhDataReg(unsigned I) const {

return IsN64() ? EhDataReg64[I] : EhDataReg[I];
}

12 changes: 12 additions & 0 deletions llvm/lib/TargetParser/Triple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,16 @@ StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
case MSVC: return "msvc";
case MacABI: return "macabi";
case Musl: return "musl";
case MuslABIN32:
return "muslabin32";
case MuslABI64:
return "muslabi64";
case MuslEABI: return "musleabi";
case MuslEABIHF: return "musleabihf";
case MuslF32:
return "muslf32";
case MuslSF:
return "muslsf";
case MuslX32: return "muslx32";
case Simulator: return "simulator";
case Pixel: return "pixel";
Expand Down Expand Up @@ -694,8 +702,12 @@ static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
.StartsWith("code16", Triple::CODE16)
.StartsWith("gnu", Triple::GNU)
.StartsWith("android", Triple::Android)
.StartsWith("muslabin32", Triple::MuslABIN32)
.StartsWith("muslabi64", Triple::MuslABI64)
.StartsWith("musleabihf", Triple::MuslEABIHF)
.StartsWith("musleabi", Triple::MuslEABI)
.StartsWith("muslf32", Triple::MuslF32)
.StartsWith("muslsf", Triple::MuslSF)
.StartsWith("muslx32", Triple::MuslX32)
.StartsWith("musl", Triple::Musl)
.StartsWith("msvc", Triple::MSVC)
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ static ShadowMapping getShadowMapping(const Triple &TargetTriple, int LongSize,
TargetTriple.getArch() == Triple::ppc64le;
bool IsSystemZ = TargetTriple.getArch() == Triple::systemz;
bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64;
bool IsMIPSN32ABI = TargetTriple.getEnvironment() == Triple::GNUABIN32;
bool IsMIPSN32ABI = TargetTriple.isABIN32();
bool IsMIPS32 = TargetTriple.isMIPS32();
bool IsMIPS64 = TargetTriple.isMIPS64();
bool IsArmOrThumb = TargetTriple.isARM() || TargetTriple.isThumb();
Expand Down
108 changes: 108 additions & 0 deletions llvm/unittests/TargetParser/TripleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,18 @@ TEST(TripleTest, ParsedIDs) {
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::Musl, T.getEnvironment());

T = Triple("loongarch32-unknown-linux-muslf32");
EXPECT_EQ(Triple::loongarch32, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslF32, T.getEnvironment());

T = Triple("loongarch32-unknown-linux-muslsf");
EXPECT_EQ(Triple::loongarch32, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslSF, T.getEnvironment());

T = Triple("loongarch64-unknown-linux");
EXPECT_EQ(Triple::loongarch64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
Expand Down Expand Up @@ -691,6 +703,18 @@ TEST(TripleTest, ParsedIDs) {
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::Musl, T.getEnvironment());

T = Triple("loongarch64-unknown-linux-muslf32");
EXPECT_EQ(Triple::loongarch64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslF32, T.getEnvironment());

T = Triple("loongarch64-unknown-linux-muslsf");
EXPECT_EQ(Triple::loongarch64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslSF, T.getEnvironment());

T = Triple("riscv32-unknown-unknown");
EXPECT_EQ(Triple::riscv32, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
Expand Down Expand Up @@ -926,6 +950,90 @@ TEST(TripleTest, ParsedIDs) {
EXPECT_EQ(Triple::GNU, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("mips64el-unknown-linux-muslabi64");
EXPECT_EQ(Triple::mips64el, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABI64, T.getEnvironment());
EXPECT_EQ(Triple::NoSubArch, T.getSubArch());

T = Triple("mips64-unknown-linux-muslabi64");
EXPECT_EQ(Triple::mips64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABI64, T.getEnvironment());
EXPECT_EQ(Triple::NoSubArch, T.getSubArch());

T = Triple("mipsisa64r6el-unknown-linux-muslabi64");
EXPECT_EQ(Triple::mips64el, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABI64, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("mipsisa64r6-unknown-linux-muslabi64");
EXPECT_EQ(Triple::mips64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABI64, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("mips64el-unknown-linux-muslabin32");
EXPECT_EQ(Triple::mips64el, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABIN32, T.getEnvironment());
EXPECT_EQ(Triple::NoSubArch, T.getSubArch());

T = Triple("mips64-unknown-linux-muslabin32");
EXPECT_EQ(Triple::mips64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABIN32, T.getEnvironment());
EXPECT_EQ(Triple::NoSubArch, T.getSubArch());

T = Triple("mipsisa64r6el-unknown-linux-muslabin32");
EXPECT_EQ(Triple::mips64el, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABIN32, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("mipsisa64r6-unknown-linux-muslabin32");
EXPECT_EQ(Triple::mips64, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::MuslABIN32, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("mipsel-unknown-linux-musl");
EXPECT_EQ(Triple::mipsel, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::Musl, T.getEnvironment());
EXPECT_EQ(Triple::NoSubArch, T.getSubArch());

T = Triple("mips-unknown-linux-musl");
EXPECT_EQ(Triple::mips, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::Musl, T.getEnvironment());
EXPECT_EQ(Triple::NoSubArch, T.getSubArch());

T = Triple("mipsisa32r6el-unknown-linux-musl");
EXPECT_EQ(Triple::mipsel, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::Musl, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("mipsisa32r6-unknown-linux-musl");
EXPECT_EQ(Triple::mips, T.getArch());
EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
EXPECT_EQ(Triple::Linux, T.getOS());
EXPECT_EQ(Triple::Musl, T.getEnvironment());
EXPECT_EQ(Triple::MipsSubArch_r6, T.getSubArch());

T = Triple("arm-oe-linux-gnueabi");
EXPECT_EQ(Triple::arm, T.getArch());
EXPECT_EQ(Triple::OpenEmbedded, T.getVendor());
Expand Down

0 comments on commit 72a2180

Please sign in to comment.