cpu.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 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.c.
  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" "avx512vp2intersect" "tsxldtrk" => "sapphirerapids")
  123. ("avx" "avx512vp2intersect" => "tigerlake")
  124. ("avx" "avx512bf16" => "cooperlake")
  125. ("avx" "wbnoinvd" => "icelake-server")
  126. ("avx" "avx512bitalg" => "icelake-client")
  127. ("avx" "avx512vbmi" => "cannonlake")
  128. ("avx" "avx5124vnniw" => "knm")
  129. ("avx" "avx512er" => "knl")
  130. ("avx" "avx512f" => "skylake-avx512")
  131. ("avx" "serialize" => "alderlake")
  132. ("avx" "clflushopt" => "skylake")
  133. ("avx" "adx" => "broadwell")
  134. ("avx" "avx2" => "haswell")
  135. ("avx" => "sandybridge")
  136. ("sse4_2" "gfni" => "tremont")
  137. ("sse4_2" "sgx" => "goldmont-plus")
  138. ("sse4_2" "xsave" => "goldmont")
  139. ("sse4_2" "movbe" => "silvermont")
  140. ("sse4_2" => "nehalem")
  141. ("ssse3" "movbe" => "bonnell")
  142. ("ssse3" => "core2")
  143. ("longmode" => "x86-64"))))
  144. (and (equal? "AuthenticAMD" (cpu-vendor cpu))
  145. (letrec-syntax ((if-flags (syntax-rules (=>)
  146. ((_)
  147. #f)
  148. ((_ (flags ... => name) rest ...)
  149. (if (every (lambda (flag)
  150. (set-contains? (cpu-flags cpu)
  151. flag))
  152. '(flags ...))
  153. name
  154. (if-flags rest ...))))))
  155. (or (and (= 22 (cpu-family cpu))
  156. (if-flags ("movbe" => "btver2")))
  157. (and (= 6 (cpu-family cpu))
  158. (if-flags ("3dnowp" => "athalon")))
  159. (if-flags ("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. ("sse2" => "k8")
  171. ("longmode" => "k8")
  172. ("mmx" "3dnow" => "k6-3")
  173. ("mmx" => "k6")
  174. (_ => "pentium")))))
  175. ;; Fallback case for non-Intel processors or for Intel processors not
  176. ;; recognized above.
  177. (letrec-syntax ((if-flags (syntax-rules (=>)
  178. ((_)
  179. #f)
  180. ((_ (flags ... => name) rest ...)
  181. (if (every (lambda (flag)
  182. (set-contains? (cpu-flags cpu)
  183. flag))
  184. '(flags ...))
  185. name
  186. (if-flags rest ...))))))
  187. (if-flags ("avx512" => "knl")
  188. ("adx" => "broadwell")
  189. ("avx2" => "haswell")
  190. ;; TODO: tigerlake, cooperlake, etc.
  191. ("avx" => "sandybridge")
  192. ("sse4_2" "gfni" => "tremont")
  193. ("sse4_2" "sgx" => "goldmont-plus")
  194. ("sse4_2" "xsave" => "goldmont")
  195. ("sse4_2" "movbe" => "silvermont")
  196. ("sse4_2" => "nehalem")
  197. ("ssse3" "movbe" => "bonnell")
  198. ("ssse3" => "core2")))
  199. ;; TODO: Recognize CENTAUR/CYRIX/NSC?
  200. "x86_64"))
  201. ("aarch64"
  202. ;; Transcribed from GCC's list of aarch64 processors in aarch64-cores.def
  203. ;; What to do with big.LITTLE cores?
  204. (match (cpu-vendor cpu)
  205. ("0x41"
  206. (match (cpu-model cpu)
  207. ((or #xd02 #xd04 #xd03 #xd07 #xd08 #xd09)
  208. "armv8-a")
  209. ((or #xd05 #xd0a #xd0b #xd0e #xd0d #xd41 #xd42 #xd4b #xd46 #xd43 #xd44 #xd41 #xd0c #xd4a)
  210. "armv8.2-a")
  211. (#xd40
  212. "armv8.4-a")
  213. (#xd15
  214. "armv8-r")
  215. ((or #xd46 #xd47 #xd48 #xd49 #xd4f)
  216. "armv9-a")))
  217. ("0x42"
  218. "armv8.1-a")
  219. ("0x43"
  220. (match (cpu-model cpu)
  221. ((or #x0a0 #x0a1 #x0a2 #x0a3)
  222. "armv8-a")
  223. (#x0af
  224. "armv8.1-a")
  225. ((or #x0b0 #x0b1 #x0b2 #x0b3 #x0b4 #x0b5)
  226. "armv8.2-a")
  227. (#x0b8
  228. "armv8.3-a")))
  229. ("0x46"
  230. "armv8.2-a")
  231. ("0x48"
  232. "armv8.2-a")
  233. ("0x50"
  234. "armv8-a")
  235. ("0x51"
  236. (match (cpu-model cpu)
  237. (#xC00
  238. "armv8-a")
  239. (#x516
  240. "armv8.1-a")
  241. (#xC01
  242. "armv8.4-a")))
  243. ("0x53"
  244. "armv8-a")
  245. ("0x68"
  246. "armv8-a")
  247. ("0xC0"
  248. "armv8.6-a")
  249. (_
  250. "armv8-a"))
  251. "armv8-a")
  252. (architecture
  253. ;; TODO: More architectures
  254. architecture)))