Skip to content

Commit

Permalink
Fix build for Apple Silicon (pytorch#48)
Browse files Browse the repository at this point in the history
* Fix build for Apple Silicon

MacOS machines based on Apple M1 silicon are identified by cmake as "arm64"

Modify build rules accordingly to recognize "arm64" is valid CPU configuration for cpuinfo

* Add CPUFAMILY_ARM_FIRESTORM_ICESTORM switch case

* Update comment in src/arm/mach/init.c
  • Loading branch information
malfet authored Nov 19, 2020
1 parent 63b2545 commit ed8b86a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ IF(NOT CMAKE_SYSTEM_PROCESSOR)
"cpuinfo will compile, but cpuinfo_initialize() will always fail.")
SET(CPUINFO_SUPPORTED_PLATFORM FALSE)
ENDIF()
ELSEIF(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?|armv[5-8].*|aarch64)$")
ELSEIF(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?|armv[5-8].*|aarch64|arm64)$")
MESSAGE(WARNING
"Target processor architecture \"${CMAKE_SYSTEM_PROCESSOR}\" is not supported in cpuinfo. "
"cpuinfo will compile, but cpuinfo_initialize() will always fail.")
Expand Down Expand Up @@ -146,7 +146,7 @@ IF(CPUINFO_SUPPORTED_PLATFORM)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "^(Windows|CYGWIN|MSYS)$")
LIST(APPEND CPUINFO_SRCS src/x86/windows/init.c)
ENDIF()
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(armv[5-8].*|aarch64)$" OR IOS_ARCH MATCHES "^(armv7.*|arm64.*)$")
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(armv[5-8].*|aarch64|arm64)$" OR IOS_ARCH MATCHES "^(armv7.*|arm64.*)$")
LIST(APPEND CPUINFO_SRCS
src/arm/uarch.c
src/arm/cache.c)
Expand All @@ -163,10 +163,10 @@ IF(CPUINFO_SUPPORTED_PLATFORM)
IF(CMAKE_SYSTEM_NAME STREQUAL "Android" AND ANDROID_ABI STREQUAL "armeabi")
SET_SOURCE_FILES_PROPERTIES(src/arm/linux/aarch32-isa.c PROPERTIES COMPILE_FLAGS -marm)
ENDIF()
ELSEIF(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$")
LIST(APPEND CPUINFO_SRCS src/arm/linux/aarch64-isa.c)
ENDIF()
ELSEIF(IOS)
ELSEIF(IOS OR (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"))
LIST(APPEND CPUINFO_SRCS src/arm/mach/init.c)
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Android")
Expand Down
4 changes: 4 additions & 0 deletions include/cpuinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ enum cpuinfo_uarch {
cpuinfo_uarch_lightning = 0x00700109,
/** Apple A13 processor (little cores). */
cpuinfo_uarch_thunder = 0x0070010A,
/** Apple M1 processor (big cores). */
cpuinfo_uarch_firestorm = 0x0070010B,
/** Apple M1 processor (little cores). */
cpuinfo_uarch_icestorm = 0x0070010C,

/** Cavium ThunderX. */
cpuinfo_uarch_thunderx = 0x00800100,
Expand Down
7 changes: 7 additions & 0 deletions src/arm/mach/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504D2
#endif

#ifndef CPUFAMILY_ARM_FIRESTORM_ICESTORM
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1B588BB3
#endif

struct cpuinfo_arm_isa cpuinfo_isa = {
#if CPUINFO_ARCH_ARM
.thumb = true,
Expand Down Expand Up @@ -101,6 +105,9 @@ static enum cpuinfo_uarch decode_uarch(uint32_t cpu_family, uint32_t cpu_subtype
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
/* Hexa-core: 2x Lightning + 4x Thunder; Octa-core (presumed): 4x Lightning + 4x Thunder */
return core_index + 4 < core_count ? cpuinfo_uarch_lightning : cpuinfo_uarch_thunder;
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
/* Hexa-core: 2x Firestorm + 4x Icestorm; Octa-core: 4x Firestorm + 4x Icestorm */
return core_index + 4 < core_count ? cpuinfo_uarch_firestorm : cpuinfo_uarch_icestorm;
default:
/* Use hw.cpusubtype for detection */
break;
Expand Down
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ bool CPUINFO_ABI cpuinfo_initialize(void) {
pthread_once(&init_guard, &cpuinfo_arm_linux_init);
#elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
pthread_once(&init_guard, &cpuinfo_arm_mach_init);
#elif defined(__MACH__) && defined(__APPLE__)
pthread_once(&init_guard, &cpuinfo_arm_mach_init);
#else
cpuinfo_log_error("operating system is not supported in cpuinfo");
#endif
Expand Down
4 changes: 4 additions & 0 deletions tools/cpu-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ static const char* uarch_to_string(enum cpuinfo_uarch uarch) {
return "Lightning";
case cpuinfo_uarch_thunder:
return "Thunder";
case cpuinfo_uarch_firestorm:
return "Firestorm";
case cpuinfo_uarch_icestorm:
return "Icestorm";
case cpuinfo_uarch_thunderx:
return "ThunderX";
case cpuinfo_uarch_thunderx2:
Expand Down

0 comments on commit ed8b86a

Please sign in to comment.