Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-mtune=native CPU autodetection for AMD Zen 3 CPU #6648

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,63 @@ static void cpuid(int info[4], int infoType, int extra) {
#endif
#endif

#if defined(__x86_64__) || defined(__i386__) || defined(_MSC_VER)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily as part of this PR, but... this file is getting a lot of processor-specific ifdef stuff in it, which is getting to be a code smell at this point IMHO (I'm a big believer in trying to keep "core" code mostly-core and segregating processor/platform/etc specific code into separate units where possible). We should think about whether and/or how to reorganize this (after these changes) to keep this from exploding into a mess of messiness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.


enum class VendorSignatures {
Unknown,
GenuineIntel,
AuthenticAMD,
};

VendorSignatures get_vendor_signature() {
int info[4];
cpuid(info, 0, 0);

if (info[0] < 1) {
return VendorSignatures::Unknown;
}

// "Genu ineI ntel"
if (info[1] == 0x756e6547 && info[3] == 0x49656e69 && info[2] == 0x6c65746e) {
return VendorSignatures::GenuineIntel;
}

// "Auth enti cAMD"
if (info[1] == 0x68747541 && info[3] == 0x69746e65 && info[2] == 0x444d4163) {
return VendorSignatures::AuthenticAMD;
}

return VendorSignatures::Unknown;
}

void detect_family_and_model(int info0, unsigned &family, unsigned &model) {
family = (info0 >> 8) & 0xF; // Bits 8..11
model = (info0 >> 4) & 0xF; // Bits 4..7
if (family == 0x6 || family == 0xF) {
if (family == 0xF) {
// Examine extended family ID if family ID is 0xF.
family += (info0 >> 20) & 0xFf; // Bits 20..27
}
// Examine extended model ID if family ID is 0x6 or 0xF.
model += ((info0 >> 16) & 0xF) << 4; // Bits 16..19
}
}

Target::Processor get_amd_processor(unsigned family, unsigned model) {
switch (family) {
case 0x19: // AMD Family 19h
if (model <= 0x0f || model == 0x21) {
return Target::Processor::ZnVer3; // 00h-0Fh, 21h: Zen3
}
default:
break; // Unknown AMD CPU.
}

return Target::Processor::ProcessorGeneric;
}

#endif // defined(__x86_64__) || defined(__i386__) || defined(_MSC_VER)

Target calculate_host_target() {
Target::OS os = Target::OSUnknown;
#ifdef __linux__
Expand Down Expand Up @@ -111,8 +168,18 @@ Target calculate_host_target() {
#else
Target::Arch arch = Target::X86;

VendorSignatures vendor_signature = get_vendor_signature();

int info[4];
cpuid(info, 1, 0);

unsigned family = 0, model = 0;
detect_family_and_model(info[0], family, model);

if (vendor_signature == VendorSignatures::AuthenticAMD) {
processor = get_amd_processor(family, model);
}

bool have_sse41 = (info[2] & (1 << 19)) != 0;
bool have_sse2 = (info[3] & (1 << 26)) != 0;
bool have_avx = (info[2] & (1 << 28)) != 0;
Expand Down Expand Up @@ -165,19 +232,23 @@ Target calculate_host_target() {
}
if ((info2[1] & avx512) == avx512) {
initial_features.push_back(Target::AVX512);
// TODO: port to family/model -based detection.
if ((info2[1] & avx512_knl) == avx512_knl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These avx512_foo features don't mean "tune for processor foo, which happens to have avx512", they mean "enable the avx512 instructions that exist on this processor". It was our crude attempt at making sense of the many overlapping avx512 feature sets.

So I think it's a bit of a misnomer to call the new feature AVX2_znver3. I'd just make it Znver3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, that is an interesting context, thanks!

But still, avx512_foo does result in -mcpu=foo, so i would say my comment isn't entirely wrong.

So I think it's a bit of a misnomer to call the new feature AVX2_znver3. I'd just make it Znver3

Yay :)

initial_features.push_back(Target::AVX512_KNL);
}
// TODO: port to family/model -based detection.
if ((info2[1] & avx512_skylake) == avx512_skylake) {
initial_features.push_back(Target::AVX512_Skylake);
}
// TODO: port to family/model -based detection.
if ((info2[1] & avx512_cannonlake) == avx512_cannonlake) {
initial_features.push_back(Target::AVX512_Cannonlake);

const uint32_t avx512vnni = 1U << 11; // vnni result in ecx
const uint32_t avx512bf16 = 1U << 5; // bf16 result in eax, with cpuid(eax=7, ecx=1)
int info3[4];
cpuid(info3, 7, 1);
// TODO: port to family/model -based detection.
if ((info2[2] & avx512vnni) == avx512vnni &&
(info3[0] & avx512bf16) == avx512bf16) {
initial_features.push_back(Target::AVX512_SapphireRapids);
Expand Down