Skip to content

Commit 09dc998

Browse files
workingjubileepuja2196
authored andcommitted
[TargetParser][AArch64] Believe runtime feature detection (#95694)
In llvm/llvm-project#90365 it was reported that TargetParser arrives at the wrong conclusion regarding what features are enabled when attempting to detect "native" features on the Raspberry Pi 4, because it (correctly) detects it as a Cortex-A72, but LLVM (incorrectly) believes all Cortex-A72s have crypto enabled. Attempt to help ourselves by allowing runtime information derived from the host to contradict whatever we believe is "true" about the architecture.
1 parent 5ed269d commit 09dc998

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

llvm/lib/TargetParser/Host.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,8 @@ const StringMap<bool> sys::getHostCPUFeatures() {
18981898
}
18991899

19001900
#if defined(__aarch64__)
1901-
// Keep track of which crypto features we have seen
1901+
// All of these are "crypto" features, but we must sift out actual features
1902+
// as the former meaning of "crypto" as a single feature is no more.
19021903
enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
19031904
uint32_t crypto = 0;
19041905
#endif
@@ -1941,9 +1942,13 @@ const StringMap<bool> sys::getHostCPUFeatures() {
19411942
}
19421943

19431944
#if defined(__aarch64__)
1944-
// If we have all crypto bits we can add the feature
1945-
if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1946-
Features["crypto"] = true;
1945+
// LLVM has decided some AArch64 CPUs have all the instructions they _may_
1946+
// have, as opposed to all the instructions they _must_ have, so allow runtime
1947+
// information to correct us on that.
1948+
uint32_t Aes = CAP_AES | CAP_PMULL;
1949+
uint32_t Sha2 = CAP_SHA1 | CAP_SHA2;
1950+
Features["aes"] = (crypto & Aes) == Aes;
1951+
Features["sha2"] = (crypto & Sha2) == Sha2;
19471952
#endif
19481953

19491954
return Features;
@@ -1952,12 +1957,17 @@ const StringMap<bool> sys::getHostCPUFeatures() {
19521957
const StringMap<bool> sys::getHostCPUFeatures() {
19531958
StringMap<bool> Features;
19541959

1955-
if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
1956-
Features["neon"] = true;
1957-
if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
1958-
Features["crc"] = true;
1959-
if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
1960-
Features["crypto"] = true;
1960+
// If we're asking the OS at runtime, believe what the OS says
1961+
Features["neon"] =
1962+
IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE);
1963+
Features["crc"] =
1964+
IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE);
1965+
1966+
// Avoid inferring "crypto" means more than the traditional AES + SHA2
1967+
bool TradCrypto =
1968+
IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
1969+
Features["aes"] = TradCrypto;
1970+
Features["sha2"] = TradCrypto;
19611971

19621972
return Features;
19631973
}

0 commit comments

Comments
 (0)