Skip to content

Commit e53f41c

Browse files
committed
[LoongArch] Support getHostCPUName and getHostCPUFeatures
Reviewed By: xen0n, MaskRay Differential Revision: https://reviews.llvm.org/D142950
1 parent 6bb0ab0 commit e53f41c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

llvm/lib/TargetParser/Host.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,20 @@ StringRef sys::getHostCPUName() {
14481448
return "generic";
14491449
}
14501450
}
1451+
#elif defined(__loongarch__)
1452+
StringRef sys::getHostCPUName() {
1453+
// Use processor id to detect cpu name.
1454+
uint32_t processor_id;
1455+
__asm__("cpucfg %[prid], $zero\n\t" : [prid] "=r"(processor_id));
1456+
switch (processor_id & 0xff00) {
1457+
case 0xc000: // Loongson 64bit, 4-issue
1458+
return "la464";
1459+
// TODO: Others.
1460+
default:
1461+
break;
1462+
}
1463+
return "generic";
1464+
}
14511465
#elif defined(__riscv)
14521466
StringRef sys::getHostCPUName() {
14531467
#if defined(__linux__)
@@ -1842,6 +1856,23 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
18421856

18431857
return true;
18441858
}
1859+
#elif defined(__linux__) && defined(__loongarch__)
1860+
#include <sys/auxv.h>
1861+
bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1862+
unsigned long hwcap = getauxval(AT_HWCAP);
1863+
bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
1864+
uint32_t cpucfg2 = 0x2;
1865+
__asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2));
1866+
1867+
Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
1868+
Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
1869+
1870+
Features["lsx"] = hwcap & (1UL << 4); // HWCAP_LOONGARCH_LSX
1871+
Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
1872+
Features["lvz"] = hwcap & (1UL << 9); // HWCAP_LOONGARCH_LVZ
1873+
1874+
return true;
1875+
}
18451876
#else
18461877
bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
18471878
#endif

0 commit comments

Comments
 (0)