Skip to content

Commit dc078e6

Browse files
committed
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
1 parent b9543f7 commit dc078e6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

llvm/lib/TargetParser/Host.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,10 +1878,44 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
18781878
bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
18791879
#endif
18801880

1881+
#if __APPLE__
1882+
/// \returns the \p triple, but with the Host's arch spliced in.
1883+
static Triple withHostArch(Triple T) {
1884+
#if defined(__arm__)
1885+
T.setArch(Triple::arm);
1886+
T.setArchName("arm");
1887+
#elif defined(__arm64e__)
1888+
T.setArch(Triple::aarch64, Triple::AArch64SubArch_arm64e);
1889+
T.setArchName("arm64e");
1890+
#elif defined(__aarch64__)
1891+
T.setArch(Triple::aarch64);
1892+
T.setArchName("arm64");
1893+
#elif defined(__x86_64h__)
1894+
T.setArch(Triple::x86_64);
1895+
T.setArchName("x86_64h");
1896+
#elif defined(__x86_64__)
1897+
T.setArch(Triple::x86_64);
1898+
T.setArchName("x86_64");
1899+
#elif defined(__powerpc__)
1900+
T.setArch(Triple::ppc);
1901+
T.setArchName("powerpc");
1902+
#else
1903+
# error "Unimplemented host arch fixup"
1904+
#endif
1905+
return T;
1906+
}
1907+
#endif
1908+
18811909
std::string sys::getProcessTriple() {
18821910
std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
18831911
Triple PT(Triple::normalize(TargetTripleString));
18841912

1913+
#if __APPLE__
1914+
/// In Universal builds, LLVM_HOST_TRIPLE will have the wrong arch in one of
1915+
/// the slices. This fixes that up.
1916+
PT = withHostArch(PT);
1917+
#endif
1918+
18851919
if (sizeof(void *) == 8 && PT.isArch32Bit())
18861920
PT = PT.get64BitArchVariant();
18871921
if (sizeof(void *) == 4 && PT.isArch64Bit())

0 commit comments

Comments
 (0)