cpu.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. ;;; Commentary:
  35. ;;;
  36. ;;; This module provides tools to determine the micro-architecture supported
  37. ;;; by the CPU and to map it to a name known to GCC's '-march'.
  38. ;;;
  39. ;;; Code:
  40. ;; CPU description.
  41. (define-record-type <cpu>
  42. (cpu architecture vendor family model flags)
  43. cpu?
  44. (architecture cpu-architecture) ;string, from 'uname'
  45. (vendor cpu-vendor) ;string
  46. (family cpu-family) ;integer
  47. (model cpu-model) ;integer
  48. (flags cpu-flags)) ;set of strings
  49. (define current-cpu
  50. (mlambda ()
  51. "Return a <cpu> record representing the host CPU."
  52. (define (prefix? prefix)
  53. (lambda (str)
  54. (string-prefix? prefix str)))
  55. (call-with-input-file "/proc/cpuinfo"
  56. (lambda (port)
  57. (let loop ((vendor #f)
  58. (family #f)
  59. (model #f)
  60. (flags (set)))
  61. (match (read-line port)
  62. ((? eof-object?)
  63. (cpu (utsname:machine (uname))
  64. vendor family model flags))
  65. ;; vendor for x86_64 and i686
  66. ((? (prefix? "vendor_id") str)
  67. (match (string-tokenize str)
  68. (("vendor_id" ":" vendor)
  69. (loop vendor family model flags))))
  70. ;; vendor for aarch64 and armhf
  71. ((? (prefix? "CPU implementer") str)
  72. (match (string-tokenize str)
  73. (("CPU" "implementer" ":" vendor)
  74. (loop vendor family model flags))))
  75. ;; family for x86_64 and i686
  76. ((? (prefix? "cpu family") str)
  77. (match (string-tokenize str)
  78. (("cpu" "family" ":" family)
  79. (loop vendor (string->number family) model flags))))
  80. ;; model for x86_64 and i686
  81. ((? (prefix? "model") str)
  82. (match (string-tokenize str)
  83. (("model" ":" model)
  84. (loop vendor family (string->number model) flags))
  85. (_
  86. (loop vendor family model flags))))
  87. ;; model for aarch64 and armhf
  88. ((? (prefix? "CPU part") str)
  89. (match (string-tokenize str)
  90. (("CPU" "part" ":" model)
  91. (loop vendor family (string->number (string-drop model 2) 16) flags))))
  92. ;; flags for x86_64 and i686
  93. ((? (prefix? "flags") str)
  94. (match (string-tokenize str)
  95. (("flags" ":" flags ...)
  96. (loop vendor family model (list->set flags)))))
  97. ;; flags for aarch64 and armhf
  98. ((? (prefix? "Features") str)
  99. (match (string-tokenize str)
  100. (("Features" ":" flags ...)
  101. (loop vendor family model (list->set flags)))))
  102. (_
  103. (loop vendor family model flags))))))))
  104. (define (cpu->gcc-architecture cpu)
  105. "Return the architecture name, suitable for GCC's '-march' flag, that
  106. corresponds to CPU, a record as returned by 'current-cpu'."
  107. (match (cpu-architecture cpu)
  108. ("x86_64"
  109. ;; Transcribed from GCC's 'host_detect_local_cpu' in driver-i386.cc.
  110. (or (and (equal? "GenuineIntel" (cpu-vendor cpu))
  111. (= 6 (cpu-family cpu)) ;the "Pentium Pro" family
  112. (letrec-syntax ((if-flags (syntax-rules (=>)
  113. ((_)
  114. #f)
  115. ((_ (flags ... => name) rest ...)
  116. (if (every (lambda (flag)
  117. (set-contains? (cpu-flags cpu)
  118. flag))
  119. '(flags ...))
  120. name
  121. (if-flags rest ...))))))
  122. (if-flags ("avx" "raoint" => "grandridge")
  123. ("avx" "amx_fp16" => "graniterapids")
  124. ("avx" "avxvnniint8" => "sierraforest")
  125. ("avx" "avx512vp2intersect" => "tigerlake")
  126. ("avx" "tsxldtrk" => "sapphirerapids")
  127. ("avx" "avx512bf16" => "cooperlake")
  128. ("avx" "wbnoinvd" => "icelake-server")
  129. ("avx" "avx512bitalg" => "icelake-client")
  130. ("avx" "avx512vbmi" => "cannonlake")
  131. ("avx" "avx5124vnniw" => "knm")
  132. ("avx" "avx512er" => "knl")
  133. ("avx" "avx512f" => "skylake-avx512")
  134. ("avx" "serialize" => "alderlake")
  135. ("avx" "clflushopt" => "skylake")
  136. ("avx" "adx" => "broadwell")
  137. ("avx" "avx2" => "haswell")
  138. ("avx" => "sandybridge")
  139. ("sse4_2" "gfni" => "tremont")
  140. ("sse4_2" "sgx" => "goldmont-plus")
  141. ("sse4_2" "xsave" => "goldmont")
  142. ("sse4_2" "movbe" => "silvermont")
  143. ("sse4_2" => "nehalem")
  144. ("ssse3" "movbe" => "bonnell")
  145. ("ssse3" => "core2")
  146. ("longmode" => "x86-64")
  147. ("lm" => "x86-64"))))
  148. (and (equal? "AuthenticAMD" (cpu-vendor cpu))
  149. (letrec-syntax ((if-flags (syntax-rules (=>)
  150. ((_)
  151. #f)
  152. ((_ (flags ... => name) rest ...)
  153. (if (every (lambda (flag)
  154. (set-contains? (cpu-flags cpu)
  155. flag))
  156. '(flags ...))
  157. name
  158. (if-flags rest ...))))))
  159. (or (and (= 22 (cpu-family cpu))
  160. (if-flags ("movbe" => "btver2")))
  161. (and (= 6 (cpu-family cpu))
  162. (if-flags ("3dnowp" => "athalon")
  163. ("longmode" "sse3" => "k8-sse3")
  164. ("lm" "sse3" => "k8-sse3")
  165. ("longmode" => "k8")
  166. ("lm" => "k8")))
  167. (if-flags ("avx512f" => "znver4")
  168. ("vaes" => "znver3")
  169. ("clwb" => "znver2")
  170. ("clzero" => "znver1")
  171. ("avx2" => "bdver4")
  172. ("xsaveopt" => "bdver3")
  173. ("bmi" => "bdver2")
  174. ("xop" => "bdver1")
  175. ("sse4a" "has_ssse3" => "btver1")
  176. ("sse4a" => "amdfam10")
  177. ("sse2" "sse3" => "k8-sse3")
  178. ("longmode" "sse3" => "k8-sse3")
  179. ("lm" "sse3" => "k8-sse3")
  180. ("sse2" => "k8")
  181. ("longmode" => "k8")
  182. ("lm" => "k8")
  183. ("mmx" "3dnow" => "k6-3")
  184. ("mmx" => "k6")
  185. (_ => "pentium")))))
  186. ;; Fallback case for non-Intel processors or for Intel processors not
  187. ;; recognized above.
  188. (letrec-syntax ((if-flags (syntax-rules (=>)
  189. ((_)
  190. #f)
  191. ((_ (flags ... => name) rest ...)
  192. (if (every (lambda (flag)
  193. (set-contains? (cpu-flags cpu)
  194. flag))
  195. '(flags ...))
  196. name
  197. (if-flags rest ...))))))
  198. (if (and (= 7 (cpu-family cpu))
  199. (= #x3b (cpu-model cpu)))
  200. "lujiazui"
  201. (if-flags ("avx512" => "knl")
  202. ("adx" => "broadwell")
  203. ("avx2" => "haswell")
  204. ;; TODO: tigerlake, cooperlake, etc.
  205. ("avx" => "sandybridge")
  206. ("sse4_2" "gfni" => "tremont")
  207. ("sse4_2" "sgx" => "goldmont-plus")
  208. ("sse4_2" "xsave" => "goldmont")
  209. ("sse4_2" "movbe" => "silvermont")
  210. ("sse4_2" => "nehalem")
  211. ("ssse3" "movbe" => "bonnell")
  212. ("ssse3" "sse3" "longmode" => "nocona")
  213. ("ssse3" "sse3" "lm" => "nocona")
  214. ("ssse3" "sse3" => "prescott")
  215. ("ssse3" => "core2"))))
  216. ;; TODO: Recognize CENTAUR/CYRIX/NSC?
  217. "x86_64"))
  218. ("aarch64"
  219. ;; Transcribed from GCC's list of aarch64 processors in aarch64-cores.def
  220. ;; What to do with big.LITTLE cores?
  221. (match (cpu-vendor cpu)
  222. ("0x41"
  223. (match (cpu-model cpu)
  224. ((or #xd02 #xd04 #xd03 #xd07 #xd08 #xd09)
  225. "armv8-a")
  226. ((or #xd05 #xd0a #xd0b #xd0e #xd0d #xd41 #xd42 #xd4b #xd06 #xd43 #xd44
  227. #xd4c #xd0c #xd4a)
  228. "armv8.2-a")
  229. (#xd40
  230. "armv8.4-a")
  231. (#xd15
  232. "armv8-r")
  233. ((or #xd46 #xd47 #xd4d #xd48 #xd4e #xd49 #xd4f)
  234. "armv9-a")))
  235. ("0x42"
  236. "armv8.1-a")
  237. ("0x43"
  238. (match (cpu-model cpu)
  239. ((or #x0a0 #x0a1 #x0a2 #x0a3)
  240. "armv8-a")
  241. (#x0af
  242. "armv8.1-a")
  243. ((or #x0b0 #x0b1 #x0b2 #x0b3 #x0b4 #x0b5)
  244. "armv8.2-a")
  245. (#x0b8
  246. "armv8.3-a")))
  247. ("0x46"
  248. "armv8.2-a")
  249. ("0x48"
  250. "armv8.2-a")
  251. ("0x50"
  252. "armv8-a")
  253. ("0x51"
  254. (match (cpu-model cpu)
  255. (#xC00
  256. "armv8-a")
  257. (#x516
  258. "armv8.1-a")
  259. (#xC01
  260. "armv8.4-a")))
  261. ("0x53"
  262. "armv8-a")
  263. ("0x68"
  264. "armv8-a")
  265. ("0xC0"
  266. "armv8.6-a")
  267. ("0xC00"
  268. "armv8-a")
  269. (_
  270. "armv8-a"))
  271. "armv8-a")
  272. (architecture
  273. ;; TODO: More architectures
  274. architecture)))