Skip to content

Commit

Permalink
[X86][EVEX512] Restrict attaching EVEX512 for default CPU only, NFCI (#…
Browse files Browse the repository at this point in the history
…65920)

Attaching EVEX512 is used to provide backward compatibility for legacy
LLVM IR files, which didn't set EVEX512 feature explicitly.

AVX512 and AVX10 targets have set or unset EVEX512 properly through
X86.td.

However, it's not feasible to list all AVX512 and AVX10 targets or their
complementary set here to skip/restrict such code.

Instead, we can restrict it for default CPU only. "generic" is used when
"target-cpu" is not specified in IR, while "pentium4" and "x86-64" is
the default CPU if "-march" is not specified in Clang for 32-bit and
64-bit targets respectively.

This patch is no functional change intended, though it might affect
scenarios like "-march=broadwell -mavx512bw", which looks like a misuse
of "-march" and can be solved by changing to "-mtune=broadwell
-mavx512bw".
  • Loading branch information
phoebewang authored Sep 11, 2023
1 parent 1d8a94c commit 8a58407
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions llvm/lib/Target/X86/X86Subtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,23 @@ void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
if (!FS.empty())
FullFS = (Twine(FullFS) + "," + FS).str();

// Attach EVEX512 feature when we have AVX512 features and EVEX512 is not set.
size_t posNoEVEX512 = FS.rfind("-evex512");
// Make sure we won't be cheated by "-avx512fp16".
size_t posNoAVX512F = FS.endswith("-avx512f") ? FS.size() - 8
: FS.rfind("-avx512f,");
size_t posEVEX512 = FS.rfind("+evex512");
size_t posAVX512F = FS.rfind("+avx512"); // Any AVX512XXX will enable AVX512F.

if (posAVX512F != StringRef::npos &&
(posNoAVX512F == StringRef::npos || posNoAVX512F < posAVX512F))
if (posEVEX512 == StringRef::npos && posNoEVEX512 == StringRef::npos)
FullFS += ",+evex512";
// Attach EVEX512 feature when we have AVX512 features with a default CPU.
// "pentium4" is default CPU for 32-bit targets.
// "x86-64" is default CPU for 64-bit targets.
if (CPU == "generic" || CPU == "pentium4" || CPU == "x86-64") {
size_t posNoEVEX512 = FS.rfind("-evex512");
// Make sure we won't be cheated by "-avx512fp16".
size_t posNoAVX512F = FS.endswith("-avx512f") ? FS.size() - 8
: FS.rfind("-avx512f,");
size_t posEVEX512 = FS.rfind("+evex512");
// Any AVX512XXX will enable AVX512F.
size_t posAVX512F = FS.rfind("+avx512");

if (posAVX512F != StringRef::npos &&
(posNoAVX512F == StringRef::npos || posNoAVX512F < posAVX512F))
if (posEVEX512 == StringRef::npos && posNoEVEX512 == StringRef::npos)
FullFS += ",+evex512";
}

// Parse features string and set the CPU.
ParseSubtargetFeatures(CPU, TuneCPU, FullFS);
Expand Down

0 comments on commit 8a58407

Please sign in to comment.