cpu.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022, 2023 Efraim Flashner <efraim@flashner.co.il>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix cpu)
  20. #:use-module (guix sets)
  21. #:use-module (guix memoization)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 rdelim)
  26. #:export (current-cpu
  27. cpu?
  28. cpu-architecture
  29. cpu-vendor
  30. cpu-family
  31. cpu-model
  32. cpu-flags
  33. cpu->gcc-architecture
  34. gcc-architecture->micro-architecture-level))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; This module provides tools to determine the micro-architecture supported
  38. ;;; by the CPU and to map it to a name known to GCC's '-march'.
  39. ;;;
  40. ;;; Code:
  41. ;; CPU description.
  42. (define-record-type <cpu>
  43. (cpu architecture vendor family model flags)
  44. cpu?
  45. (architecture cpu-architecture) ;string, from 'uname'
  46. (vendor cpu-vendor) ;string
  47. (family cpu-family) ;integer
  48. (model cpu-model) ;integer
  49. (flags cpu-flags)) ;set of strings
  50. (define current-cpu
  51. (mlambda ()
  52. "Return a <cpu> record representing the host CPU."
  53. (define (prefix? prefix)
  54. (lambda (str)
  55. (string-prefix? prefix str)))
  56. (call-with-input-file "/proc/cpuinfo"
  57. (lambda (port)
  58. (let loop ((vendor #f)
  59. (family #f)
  60. (model #f)
  61. (flags (set)))
  62. (match (read-line port)
  63. ((? eof-object?)
  64. (cpu (utsname:machine (uname))
  65. vendor family model flags))
  66. ;; vendor for x86_64 and i686
  67. ((? (prefix? "vendor_id") str)
  68. (match (string-tokenize str)
  69. (("vendor_id" ":" vendor)
  70. (loop vendor family model flags))))
  71. ;; vendor for aarch64 and armhf
  72. ((? (prefix? "CPU implementer") str)
  73. (match (string-tokenize str)
  74. (("CPU" "implementer" ":" vendor)
  75. (loop vendor family model flags))))
  76. ;; family for x86_64 and i686
  77. ((? (prefix? "cpu family") str)
  78. (match (string-tokenize str)
  79. (("cpu" "family" ":" family)
  80. (loop vendor (string->number family) model flags))))
  81. ;; model for x86_64 and i686
  82. ((? (prefix? "model") str)
  83. (match (string-tokenize str)
  84. (("model" ":" model)
  85. (loop vendor family (string->number model) flags))
  86. (_
  87. (loop vendor family model flags))))
  88. ;; model for aarch64 and armhf
  89. ((? (prefix? "CPU part") str)
  90. (match (string-tokenize str)
  91. (("CPU" "part" ":" model)
  92. (loop vendor family (string->number (string-drop model 2) 16) flags))))
  93. ;; flags for x86_64 and i686
  94. ((? (prefix? "flags") str)
  95. (match (string-tokenize str)
  96. (("flags" ":" flags ...)
  97. (loop vendor family model (list->set flags)))))
  98. ;; flags for aarch64 and armhf
  99. ((? (prefix? "Features") str)
  100. (match (string-tokenize str)
  101. (("Features" ":" flags ...)
  102. (loop vendor family model (list->set flags)))))
  103. (_
  104. (loop vendor family model flags))))))))
  105. (define (cpu->gcc-architecture cpu)
  106. "Return the architecture name, suitable for GCC's '-march' flag, that
  107. corresponds to CPU, a record as returned by 'current-cpu'."
  108. (match (cpu-architecture cpu)
  109. ("x86_64"
  110. ;; Transcribed from GCC's 'host_detect_local_cpu' in driver-i386.cc.
  111. (letrec-syntax ((if-flags (syntax-rules (=>)
  112. ((_)
  113. #f)
  114. ((_ (flags ... => name) rest ...)
  115. (if (every (lambda (flag)
  116. (set-contains? (cpu-flags cpu)
  117. flag))
  118. '(flags ...))
  119. name
  120. (if-flags rest ...))))))
  121. (or (and (equal? "GenuineIntel" (cpu-vendor cpu))
  122. (= 6 (cpu-family cpu)) ;the "Pentium Pro" family
  123. (if-flags ("avx" "raoint" => "grandridge")
  124. ("avx" "amx_fp16" => "graniterapids")
  125. ("avx" "avxvnniint8" => "sierraforest")
  126. ("avx" "avx512vp2intersect" => "tigerlake")
  127. ("avx" "tsxldtrk" => "sapphirerapids")
  128. ("avx" "avx512bf16" => "cooperlake")
  129. ("avx" "wbnoinvd" => "icelake-server")
  130. ("avx" "avx512bitalg" => "icelake-client")
  131. ("avx" "avx512vbmi" => "cannonlake")
  132. ("avx" "avx5124vnniw" => "knm")
  133. ("avx" "avx512er" => "knl")
  134. ("avx" "avx512f" => "skylake-avx512")
  135. ("avx" "serialize" => "alderlake")
  136. ("avx" "clflushopt" => "skylake")
  137. ("avx" "adx" => "broadwell")
  138. ("avx" "avx2" => "haswell")
  139. ("avx" => "sandybridge")
  140. ("sse4_2" "gfni" => "tremont")
  141. ("sse4_2" "sgx" => "goldmont-plus")
  142. ("sse4_2" "xsave" => "goldmont")
  143. ("sse4_2" "movbe" => "silvermont")
  144. ("sse4_2" => "nehalem")
  145. ("ssse3" "movbe" => "bonnell")
  146. ("ssse3" => "core2")
  147. ("longmode" => "x86-64")
  148. ("lm" => "x86-64")))
  149. (and (equal? "AuthenticAMD" (cpu-vendor cpu))
  150. (or (and (= 22 (cpu-family cpu))
  151. (if-flags ("movbe" => "btver2")))
  152. (and (= 6 (cpu-family cpu))
  153. (if-flags ("3dnowp" => "athalon")
  154. ("longmode" "sse3" => "k8-sse3")
  155. ("lm" "sse3" => "k8-sse3")
  156. ("longmode" => "k8")
  157. ("lm" => "k8")))
  158. (if-flags ("avx512f" => "znver4")
  159. ("vaes" => "znver3")
  160. ("clwb" => "znver2")
  161. ("clzero" => "znver1")
  162. ("avx2" => "bdver4")
  163. ("xsaveopt" => "bdver3")
  164. ("bmi" => "bdver2")
  165. ("xop" => "bdver1")
  166. ("sse4a" "has_ssse3" => "btver1")
  167. ("sse4a" => "amdfam10")
  168. ("sse2" "sse3" => "k8-sse3")
  169. ("longmode" "sse3" => "k8-sse3")
  170. ("lm" "sse3" => "k8-sse3")
  171. ("sse2" => "k8")
  172. ("longmode" => "k8")
  173. ("lm" => "k8")
  174. ("mmx" "3dnow" => "k6-3")
  175. ("mmx" => "k6")
  176. (_ => "pentium"))))
  177. ;; Fallback case for non-Intel processors or for processors not
  178. ;; recognized above.
  179. (if (and (= 7 (cpu-family cpu))
  180. (= #x3b (cpu-model cpu)))
  181. "lujiazui"
  182. (cpu->micro-architecture-level cpu))
  183. ;; TODO: Recognize CENTAUR/CYRIX/NSC?
  184. "x86_64")))
  185. ("aarch64"
  186. ;; Transcribed from GCC's list of aarch64 processors in aarch64-cores.def
  187. ;; What to do with big.LITTLE cores?
  188. (match (cpu-vendor cpu)
  189. ("0x41"
  190. (match (cpu-model cpu)
  191. ((or #xd02 #xd04 #xd03 #xd07 #xd08 #xd09)
  192. "armv8-a")
  193. ((or #xd05 #xd0a #xd0b #xd0e #xd0d #xd41 #xd42 #xd4b #xd06 #xd43 #xd44
  194. #xd4c #xd0c #xd4a)
  195. "armv8.2-a")
  196. (#xd40
  197. "armv8.4-a")
  198. (#xd15
  199. "armv8-r")
  200. ((or #xd46 #xd47 #xd4d #xd48 #xd4e #xd49 #xd4f)
  201. "armv9-a")))
  202. ("0x42"
  203. "armv8.1-a")
  204. ("0x43"
  205. (match (cpu-model cpu)
  206. ((or #x0a0 #x0a1 #x0a2 #x0a3)
  207. "armv8-a")
  208. (#x0af
  209. "armv8.1-a")
  210. ((or #x0b0 #x0b1 #x0b2 #x0b3 #x0b4 #x0b5)
  211. "armv8.2-a")
  212. (#x0b8
  213. "armv8.3-a")))
  214. ("0x46"
  215. "armv8.2-a")
  216. ("0x48"
  217. "armv8.2-a")
  218. ("0x50"
  219. "armv8-a")
  220. ("0x51"
  221. (match (cpu-model cpu)
  222. (#xC00
  223. "armv8-a")
  224. (#x516
  225. "armv8.1-a")
  226. (#xC01
  227. "armv8.4-a")))
  228. ("0x53"
  229. "armv8-a")
  230. ("0x68"
  231. "armv8-a")
  232. ("0xC0"
  233. "armv8.6-a")
  234. ("0xC00"
  235. "armv8-a")
  236. (_
  237. "armv8-a"))
  238. "armv8-a")
  239. (architecture
  240. ;; TODO: More architectures
  241. architecture)))
  242. (define (cpu->micro-architecture-level cpu)
  243. "Return a micro-architecture name, suitable for generalized optimizations that
  244. correspond roughly to CPU, a record as returned by 'current-cpu'."
  245. (match (cpu-architecture cpu)
  246. ("x86_64"
  247. (or (letrec-syntax ((if-flags (syntax-rules (=>)
  248. ((_)
  249. #f)
  250. ((_ (flags ... => name) rest ...)
  251. (if (every (lambda (flag)
  252. (set-contains? (cpu-flags cpu)
  253. flag))
  254. '(flags ...))
  255. name
  256. (if-flags rest ...))))))
  257. (if-flags
  258. ;; https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex
  259. ;; v4: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
  260. ;; v3: AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, OSXSAVE
  261. ;; v2: CMPXCHG16B, LAHF, SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
  262. ("avx512f" "avx512bw" "abx512cd" "abx512dq" "avx512vl"
  263. "avx" "avx2" "bmi1" "bmi2" "f16c" "fma" "movbe"
  264. "popcnt" "sse3" "sse4_1" "sse4_2" "ssse3" => "x86_64-v4")
  265. ("avx" "avx2" "bmi1" "bmi2" "f16c" "fma" "movbe"
  266. "popcnt" "sse3" "sse4_1" "sse4_2" "ssse3" => "x86_64-v3")
  267. ("popcnt" "sse3" "sse4_1" "sse4_2" "ssse3" => "x86_64-v2")
  268. (_ => "x86_64-v1")))
  269. "x86_64-v1"))
  270. (architecture
  271. ;; TODO: More architectures
  272. architecture)))
  273. (define (gcc-architecture->micro-architecture-level gcc-architecture)
  274. "Return a matching psABI micro-architecture, allowing optimizations for x86_64
  275. CPUs for compilers which don't allow for more focused optimizing."
  276. ;; Matching gcc-architectures isn't an easy task, with the rule-of-thumb being
  277. ;; 'Haswell and higher' qualify for x86_64-v3.
  278. ;; https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex
  279. (match gcc-architecture
  280. ((or "grandridge" "graniterapids" "sierraforest" "tigerlake"
  281. "sapphirerapids" "cooperlake" "icelake-server" "icelake-client"
  282. "cannonlake" "knm" "knl" "skylake-avx512" "alderlake" "skylake"
  283. "broadwell" "haswell"
  284. "znver4" "znver3" "znver2" "znver1" "bdver4")
  285. "x86_64-v3")
  286. ((or "sandybridge" "tremont" "goldmont-plus" "goldmont" "silvermont"
  287. "nehalem" "bonnell" "core2"
  288. "btver2" "athalon" "k8-sse3" "k8" "bdver3" "bdver2" "bdver1" "btver1"
  289. "amdfam10"
  290. "lujiazui" "x86-64")
  291. "x86_64-v1")
  292. (_ gcc-architecture)))