Skip to content

Commit 8f7422b

Browse files
chenfucnChen Fu
and
Chen Fu
authored
Limiting platforms where cpuinfo is included (#8716)
* Limiting platforms where cpuinfo is included * Suppress strncpy warning during msvc build Co-authored-by: Chen Fu <[email protected]>
1 parent e695cd3 commit 8f7422b

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

cmake/CMakeLists.txt

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -799,32 +799,50 @@ if(NOT TARGET re2::re2)
799799
set(RE2_INCLUDE_DIR ${REPO_ROOT}/cmake/external/re2)
800800
endif()
801801

802-
803802
# Adding pytorch CPU info library
804-
# TODO do we have to add target_include_directories to each project that uses this?
805-
if(MSVC AND (( CMAKE_SYSTEM_PROCESSOR MATCHES "^(ARM.*|arm.*)$" ) OR (CMAKE_GENERATOR_PLATFORM MATCHES "^(ARM.*|arm.*)$" ) ))
806-
# cpuinfo fail to compile with windows arm.
807-
else()
808-
set(PYTORCH_CPUINFO_DIR external/pytorch_cpuinfo)
809-
set(PYTORCH_CPUINFO_INCLUDE_DIR ${PYTORCH_CPUINFO_DIR}/include)
810-
set(CPUINFO_BUILD_TOOLS OFF CACHE INTERNAL "")
811-
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE INTERNAL "")
812-
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE INTERNAL "")
813-
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE INTERNAL "")
814-
815-
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
816-
set(IOS ON CACHE INTERNAL "")
817-
set(IOS_ARCH "${CMAKE_OSX_ARCHITECTURES}" CACHE INTERNAL "")
803+
# TODO!! need a better way to find out the supported architectures
804+
set(TARGET_ARCH "${CMAKE_SYSTEM_PROCESSOR}")
805+
if (NOT DEFINED TARGET_ARCH OR "${TARGET_ARCH}" STREQUAL "")
806+
set(TARGET_ARCH "${CMAKE_OSX_ARCHITECTURES}")
807+
endif()
808+
809+
set(CPUINFO_SUPPORTED TRUE)
810+
if (NOT DEFINED TARGET_ARCH OR "${TARGET_ARCH}" STREQUAL "")
811+
message(WARNING
812+
"Target processor architecture is not specified. cpuinfo not included")
813+
set(CPUINFO_SUPPORTED FALSE)
814+
elseif(NOT TARGET_ARCH MATCHES "^(i[3-6]86|AMD64|x86(_64)?|armv[5-8].*|aarch64|arm64)$")
815+
message(WARNING
816+
"Target processor architecture \"${CPUINFO_TARGET_PROCESSOR}\" is not supported in cpuinfo. "
817+
"cpuinfo not included.")
818+
set(CPUINFO_SUPPORTED FALSE)
819+
elseif(MSVC AND (( CMAKE_SYSTEM_PROCESSOR MATCHES "^(ARM.*|arm.*)$" ) OR (CMAKE_GENERATOR_PLATFORM MATCHES "^(ARM.*|arm.*)$" ) ))
820+
message(WARNING
821+
"Cpuinfo not included for compilation problems with Windows ARM.")
822+
set(CPUINFO_SUPPORTED FALSE)
818823
endif()
819824

820-
message(STATUS "CMAKE_SYSTEM_PROCESSOR = ${CMAKE_SYSTEM_PROCESSOR}")
821-
message(STATUS "CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}")
822-
message(STATUS "CMAKE_OSX_ARCHITECTURES = ${CMAKE_OSX_ARCHITECTURES}")
823-
message(STATUS "IOS_ARCH = ${IOS_ARCH}")
824-
825-
add_subdirectory(external/pytorch_cpuinfo EXCLUDE_FROM_ALL)
826-
825+
# TODO do we have to add target_include_directories to each project that uses this?
826+
if(CPUINFO_SUPPORTED)
827+
set(PYTORCH_CPUINFO_DIR external/pytorch_cpuinfo)
828+
set(PYTORCH_CPUINFO_INCLUDE_DIR ${PYTORCH_CPUINFO_DIR}/include)
829+
set(CPUINFO_BUILD_TOOLS OFF CACHE INTERNAL "")
830+
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE INTERNAL "")
831+
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE INTERNAL "")
832+
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE INTERNAL "")
833+
834+
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
835+
set(IOS ON CACHE INTERNAL "")
836+
set(IOS_ARCH "${CMAKE_OSX_ARCHITECTURES}" CACHE INTERNAL "")
837+
endif()
838+
839+
string(APPEND CMAKE_CXX_FLAGS " -DCPUINFO_SUPPORTED")
840+
add_subdirectory(external/pytorch_cpuinfo EXCLUDE_FROM_ALL)
841+
if(MSVC)
842+
target_compile_options(cpuinfo PRIVATE "-D_CRT_SECURE_NO_WARNINGS")
843+
endif()
827844
endif()
845+
828846
# bounds checking behavior.
829847
# throw instead of calling terminate if there's a bounds checking violation.
830848
# we make it through via a handler so CUDA does not complain

onnxruntime/core/common/cpuid_info.cc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,8 @@
2121
#include <mutex>
2222
#include "core/common/cpuid_info.h"
2323

24-
#if defined(CPUIDINFO_ARCH_X86) || defined(CPUIDINFO_ARCH_ARM)
25-
26-
#if defined(_MSC_VER) && defined(CPUIDINFO_ARCH_ARM)
27-
// pytorch cpu info does not work for Windows ARM
28-
// 1. msvc report syntax error in file src/arm/api.h
29-
// 2. features reporting micro-arch in Windows is missing
30-
#else
31-
32-
#define CPUINFO_INCLUDED
24+
#if (defined(CPUIDINFO_ARCH_X86) || defined(CPUIDINFO_ARCH_ARM)) && defined(CPUINFO_SUPPORTED)
3325
#include <cpuinfo.h>
34-
35-
#endif
36-
3726
#endif
3827

3928
namespace onnxruntime {
@@ -64,7 +53,7 @@ CPUIDInfo CPUIDInfo::instance_;
6453

6554

6655
CPUIDInfo::CPUIDInfo() {
67-
#ifdef CPUINFO_INCLUDED
56+
#if (defined(CPUIDINFO_ARCH_X86) || defined(CPUIDINFO_ARCH_ARM)) && defined(CPUINFO_SUPPORTED)
6857
if (!cpuinfo_initialize()) {
6958
// Unfortunately we can not capture cpuinfo log!!
7059
ORT_THROW("Failed to initialize CPU info.");

0 commit comments

Comments
 (0)