Skip to content

Commit 30191cd

Browse files
authored
make : improve cpuinfo handling on x86 hosts (ggml-org#1238)
* make : simplify and correct x86 ISA extensions detection on the host It got broken in commit c5f9acf for Haiku and Mac OS (Intel), which report CPU features in upper case. Now we're finding the names in case-insensitive manner and as words. SSE3 detection has been corrected for Linux, which uses PNI for that (Prescott New Instructions). * make : use dmesg.boot in FreeBSD/DragonFlyBSD to detect x86 ISA extensions on the host * make : enable x86 ISA extensions on the host both in CFLAGS and CXXFLAGS * make : correct AVX x86 ISA extension detection on macOS (Intel) host It got broken in commit c5f9acf. macOS calls it AVX1.0.
1 parent 6985084 commit 30191cd

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

Makefile

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,57 +65,57 @@ endif
6565
# Architecture specific
6666
# TODO: probably these flags need to be tweaked on some architectures
6767
# feel free to update the Makefile for your architecture and send a pull request or issue
68-
ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
68+
ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
6969
ifeq ($(UNAME_S),Darwin)
7070
CPUINFO_CMD := sysctl machdep.cpu.features
7171
else ifeq ($(UNAME_S),Linux)
7272
CPUINFO_CMD := cat /proc/cpuinfo
7373
else ifneq (,$(filter MINGW32_NT% MINGW64_NT%,$(UNAME_S)))
7474
CPUINFO_CMD := cat /proc/cpuinfo
75+
else ifneq (,$(filter DragonFly FreeBSD,$(UNAME_S)))
76+
CPUINFO_CMD := grep Features /var/run/dmesg.boot
7577
else ifeq ($(UNAME_S),Haiku)
7678
CPUINFO_CMD := sysinfo -cpu
7779
endif
7880

79-
ifdef CPUINFO_CMD
80-
AVX_M := $(shell $(CPUINFO_CMD) | grep -m 1 "avx ")
81-
ifneq (,$(findstring avx,$(AVX_M)))
82-
CFLAGS += -mavx
83-
endif
84-
85-
AVX2_M := $(shell $(CPUINFO_CMD) | grep -m 1 "avx2 ")
86-
ifneq (,$(findstring avx2,$(AVX2_M)))
87-
CFLAGS += -mavx2
81+
ifdef CPUINFO_CMD
82+
AVX_M := $(shell $(CPUINFO_CMD) | grep -iwE 'AVX|AVX1.0')
83+
ifneq (,$(AVX_M))
84+
CFLAGS += -mavx
85+
CXXFLAGS += -mavx
8886
endif
8987

90-
FMA_M := $(shell $(CPUINFO_CMD) | grep -m 1 "fma ")
91-
ifneq (,$(findstring fma,$(FMA_M)))
92-
CFLAGS += -mfma
88+
AVX2_M := $(shell $(CPUINFO_CMD) | grep -iw 'AVX2')
89+
ifneq (,$(AVX2_M))
90+
CFLAGS += -mavx2
91+
CXXFLAGS += -mavx2
9392
endif
9493

95-
F16C_M := $(shell $(CPUINFO_CMD) | grep -m 1 "f16c ")
96-
ifneq (,$(findstring f16c,$(F16C_M)))
97-
CFLAGS += -mf16c
94+
FMA_M := $(shell $(CPUINFO_CMD) | grep -iw 'FMA')
95+
ifneq (,$(FMA_M))
96+
CFLAGS += -mfma
97+
CXXFLAGS += -mfma
98+
endif
9899

99-
AVX1_M := $(shell $(CPUINFO_CMD) | grep -m 1 "avx ")
100-
ifneq (,$(findstring avx,$(AVX1_M)))
101-
CFLAGS += -mavx
102-
endif
100+
F16C_M := $(shell $(CPUINFO_CMD) | grep -iw 'F16C')
101+
ifneq (,$(F16C_M))
102+
CFLAGS += -mf16c
103+
CXXFLAGS += -mf16c
103104
endif
104105

105-
SSE3_M := $(shell $(CPUINFO_CMD) | grep -m 1 "sse3 ")
106-
ifneq (,$(findstring sse3,$(SSE3_M)))
107-
CFLAGS += -msse3
106+
SSE3_M := $(shell $(CPUINFO_CMD) | grep -iwE 'PNI|SSE3')
107+
ifneq (,$(SSE3_M))
108+
CFLAGS += -msse3
109+
CXXFLAGS += -msse3
108110
endif
109111

110-
SSSE3_M := $(shell $(CPUINFO_CMD) | grep -m 1 "ssse3 ")
111-
ifneq (,$(findstring ssse3,$(SSSE3_M)))
112-
CFLAGS += -mssse3
112+
SSSE3_M := $(shell $(CPUINFO_CMD) | grep -iw 'SSSE3')
113+
ifneq (,$(SSSE3_M))
114+
CFLAGS += -mssse3
115+
CXXFLAGS += -mssse3
113116
endif
114117
endif
115118
endif
116-
ifeq ($(UNAME_M),amd64)
117-
CFLAGS += -mavx -mavx2 -mfma -mf16c
118-
endif
119119

120120
ifneq ($(filter ppc64%,$(UNAME_M)),)
121121
POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)

0 commit comments

Comments
 (0)