TargetParser: fix getProcessTriple in universal builds

The bug happens when you build e.g. an x64_64;arm64 JIT with
LLVM_HOST_TRIPLE=x86_64-apple-macos, and then run it on an apple-m1 not under
Rosetta. In that case, sys::getProcessTriple() will return an x86_64 triple,
not an arm64 one.

Differential revision: https://reviews.llvm.org/D138449
This commit is contained in:
Jon Roelofs
2023-07-14 13:23:00 -07:00
parent b9543f7de6
commit dc078e6eaa

View File

@@ -1878,10 +1878,44 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
#endif
#if __APPLE__
/// \returns the \p triple, but with the Host's arch spliced in.
static Triple withHostArch(Triple T) {
#if defined(__arm__)
T.setArch(Triple::arm);
T.setArchName("arm");
#elif defined(__arm64e__)
T.setArch(Triple::aarch64, Triple::AArch64SubArch_arm64e);
T.setArchName("arm64e");
#elif defined(__aarch64__)
T.setArch(Triple::aarch64);
T.setArchName("arm64");
#elif defined(__x86_64h__)
T.setArch(Triple::x86_64);
T.setArchName("x86_64h");
#elif defined(__x86_64__)
T.setArch(Triple::x86_64);
T.setArchName("x86_64");
#elif defined(__powerpc__)
T.setArch(Triple::ppc);
T.setArchName("powerpc");
#else
# error "Unimplemented host arch fixup"
#endif
return T;
}
#endif
std::string sys::getProcessTriple() {
std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
Triple PT(Triple::normalize(TargetTripleString));
#if __APPLE__
/// In Universal builds, LLVM_HOST_TRIPLE will have the wrong arch in one of
/// the slices. This fixes that up.
PT = withHostArch(PT);
#endif
if (sizeof(void *) == 8 && PT.isArch32Bit())
PT = PT.get64BitArchVariant();
if (sizeof(void *) == 4 && PT.isArch64Bit())