Skip to content

Commit

Permalink
Fix crash where VNNI binary was used for iMac Pro
Browse files Browse the repository at this point in the history
iMac Pro (2017) has the W-2150B Xeon (or similar). This Skylake CPU supports AVX-512 BW but not VNNI. However we used to assume that BW implied VNNI which would lead to a crash.

Changed to a more stringent check: IFMA does imply VNNI. But per https://en.wikipedia.org/wiki/AVX-512#CPUs_with_AVX-512 that means we miss the 2019 Mac Pro which uses Cascade Lake (so no IFMA, but yes VNNI). So we also now check the model identifier to see if we are dealing with a 2019 Mac Pro.

Finally get rid of x86-64 binary since all Macs running 10.14 or later support SSE4.1 and POPCNT. See https://support.apple.com/kb/SP777?locale=en_US for system requirements. Mid 2010 Mac Pro is the oldest model year and https://support.apple.com/kb/sp589?locale=en_US says Nehalem/Westmere.
  • Loading branch information
daylen committed Feb 25, 2021
1 parent 4af2cf2 commit ba398a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
16 changes: 6 additions & 10 deletions Stockfish.xcodeproj/project.pbxproj
Expand Up @@ -84,7 +84,6 @@
77F42E73188B8405003A4AC6 /* SFMApplication.m in Sources */ = {isa = PBXBuildFile; fileRef = 77F42E72188B8405003A4AC6 /* SFMApplication.m */; };
BAB10AE724E789A400B270F2 /* stockfish-x86-64-bmi2 in Resources */ = {isa = PBXBuildFile; fileRef = BAB10AE224E789A400B270F2 /* stockfish-x86-64-bmi2 */; };
BAB10AE824E789A400B270F2 /* stockfish-x86-64-sse41-popcnt in Resources */ = {isa = PBXBuildFile; fileRef = BAB10AE324E789A400B270F2 /* stockfish-x86-64-sse41-popcnt */; };
BAB10AE924E789A400B270F2 /* stockfish-x86-64 in Resources */ = {isa = PBXBuildFile; fileRef = BAB10AE424E789A400B270F2 /* stockfish-x86-64 */; };
BB5FC6061F6D510500CDD57A /* SFMNode.m in Sources */ = {isa = PBXBuildFile; fileRef = BB5FC6051F6D510500CDD57A /* SFMNode.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -234,7 +233,6 @@
77F42E72188B8405003A4AC6 /* SFMApplication.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SFMApplication.m; sourceTree = "<group>"; };
BAB10AE224E789A400B270F2 /* stockfish-x86-64-bmi2 */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = "stockfish-x86-64-bmi2"; sourceTree = "<group>"; };
BAB10AE324E789A400B270F2 /* stockfish-x86-64-sse41-popcnt */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = "stockfish-x86-64-sse41-popcnt"; sourceTree = "<group>"; };
BAB10AE424E789A400B270F2 /* stockfish-x86-64 */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = "stockfish-x86-64"; sourceTree = "<group>"; };
BB5FC6041F6D510500CDD57A /* SFMNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SFMNode.h; path = Stockfish/SFMNode.h; sourceTree = "<group>"; };
BB5FC6051F6D510500CDD57A /* SFMNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SFMNode.m; path = Stockfish/SFMNode.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -484,7 +482,6 @@
77F2E51925E11AB300C698A1 /* nn-62ef826d1a6d.nnue */,
77C7D5E72555B7C9003B94D1 /* stockfish-arm64 */,
77C7D5E62555B7C9003B94D1 /* stockfish-x86-64-vnni256 */,
BAB10AE424E789A400B270F2 /* stockfish-x86-64 */,
BAB10AE224E789A400B270F2 /* stockfish-x86-64-bmi2 */,
BAB10AE324E789A400B270F2 /* stockfish-x86-64-sse41-popcnt */,
);
Expand Down Expand Up @@ -628,7 +625,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BAB10AE924E789A400B270F2 /* stockfish-x86-64 in Resources */,
7745E5031A4F99F000E5ED35 /* SFMPreferenceCellView.xib in Resources */,
BAB10AE824E789A400B270F2 /* stockfish-x86-64-sse41-popcnt in Resources */,
773DE97E18822DF8009156C3 /* bishop_w.pdf in Resources */,
Expand Down Expand Up @@ -928,7 +924,7 @@
CODE_SIGN_IDENTITY = "-";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 33;
CURRENT_PROJECT_VERSION = 35;
DEVELOPMENT_TEAM = NR8F7ZFLA3;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -940,7 +936,7 @@
INFOPLIST_FILE = "Stockfish/Stockfish-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 2.3.1;
MARKETING_VERSION = 2.3.2;
PRODUCT_BUNDLE_IDENTIFIER = "com.daylenyang.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand All @@ -959,7 +955,7 @@
CODE_SIGN_IDENTITY = "-";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 33;
CURRENT_PROJECT_VERSION = 35;
DEVELOPMENT_TEAM = NR8F7ZFLA3;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -971,7 +967,7 @@
INFOPLIST_FILE = "Stockfish/Stockfish-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 2.3.1;
MARKETING_VERSION = 2.3.2;
PRODUCT_BUNDLE_IDENTIFIER = "com.daylenyang.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down Expand Up @@ -1090,7 +1086,7 @@
CODE_SIGN_IDENTITY = "-";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 33;
CURRENT_PROJECT_VERSION = 35;
DEVELOPMENT_TEAM = NR8F7ZFLA3;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -1102,7 +1098,7 @@
INFOPLIST_FILE = "Stockfish/Stockfish-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 2.3.1;
MARKETING_VERSION = 2.3.2;
PRODUCT_BUNDLE_IDENTIFIER = "com.daylenyang.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down
37 changes: 21 additions & 16 deletions Stockfish/SFMUCIEngine.m
Expand Up @@ -19,10 +19,9 @@
#include <sys/sysctl.h>

typedef NS_ENUM(NSInteger, SFMCPURating) {
SFMCPURatingX86_64,
SFMCPURatingX86_64_SSE42,
SFMCPURatingX86_64_SSE41_POPCNT,
SFMCPURatingX86_64_BMI2,
SFMCPURatingX86_64_AVX512BW,
SFMCPURatingX86_64_AVX512_VNNI,
SFMCPURatingArm64
};

Expand Down Expand Up @@ -241,17 +240,13 @@ + (NSString *)bestEnginePath {
if (cpuRating == SFMCPURatingArm64)
return [[NSBundle mainBundle] pathForResource:@"stockfish-arm64" ofType:@""];

// In general AVX512BW does not necessarily imply VNNI--Skylake-SP, Skylake-X, and Cannon Lake support AVX512BW but not VNNI. However no Macs use those processor generations.
// VNNI 256 is faster than VNNI 512: https://github.com/official-stockfish/Stockfish/pull/3038#issuecomment-679002949
if (cpuRating == SFMCPURatingX86_64_AVX512BW)
if (cpuRating == SFMCPURatingX86_64_AVX512_VNNI)
return [[NSBundle mainBundle] pathForResource:@"stockfish-x86-64-vnni256" ofType:@""];
if (cpuRating == SFMCPURatingX86_64_BMI2)
return [[NSBundle mainBundle] pathForResource:@"stockfish-x86-64-bmi2" ofType:@""];
// SSE4.2 implies support for POPCNT.
if (cpuRating == SFMCPURatingX86_64_SSE42)
return [[NSBundle mainBundle] pathForResource:@"stockfish-x86-64-sse41-popcnt" ofType:@""];

return [[NSBundle mainBundle] pathForResource:@"stockfish-x86-64" ofType:@""];
return [[NSBundle mainBundle] pathForResource:@"stockfish-x86-64-sse41-popcnt" ofType:@""];
}

- (instancetype)initWithPathToEngine:(NSString *)path applyPreferences:(BOOL)shouldApplyPreferences;
Expand Down Expand Up @@ -306,17 +301,27 @@ + (SFMCPURating)cpuRating
sysctlbyname("hw.optional.arm64", &ret, &size, NULL, 0);
if (ret) return SFMCPURatingArm64;

sysctlbyname("hw.optional.avx512bw", &ret, &size, NULL, 0);
if (ret) return SFMCPURatingX86_64_AVX512BW;
// IFMA implies VNNI.
sysctlbyname("hw.optional.avx512ifma", &ret, &size, NULL, 0);
if (ret) return SFMCPURatingX86_64_AVX512_VNNI;

// 2019 Mac Pro uses Cascade Lake Xeon W which doesn't support IFMA but does support VNNI.
size_t len = 0;
sysctlbyname("hw.model", NULL, &len, NULL, 0);
BOOL isMacProWithVnni = NO;
if (len) {
char *model = malloc(len*sizeof(char));
sysctlbyname("hw.model", model, &len, NULL, 0);
if (strcmp(model, "MacPro7,1") == 0) isMacProWithVnni = YES;
free(model);
}
if (isMacProWithVnni) return SFMCPURatingX86_64_AVX512_VNNI;

sysctlbyname("hw.optional.bmi2", &ret, &size, NULL, 0);
if (ret) return SFMCPURatingX86_64_BMI2;

sysctlbyname("hw.optional.sse4_2", &ret, &size, NULL, 0);
if (ret) return SFMCPURatingX86_64_SSE42;

// Assuming x86-64.
return SFMCPURatingX86_64;
// All Macs running Mojave (10.14) or later support SSE4.1 and POPCNT.
return SFMCPURatingX86_64_SSE41_POPCNT;
}

#pragma mark - Instances
Expand Down
Binary file removed stockfish-x86-64
Binary file not shown.

0 comments on commit ba398a5

Please sign in to comment.