all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Efraim Flashner <efraim@flashner.co.il>
To: 53895@debbugs.gnu.org
Cc: Efraim Flashner <efraim@flashner.co.il>
Subject: [bug#53895] [PATCH 2/5] gnu: cpu: Add detection for AMD CPUs.
Date: Wed,  9 Feb 2022 12:21:48 +0200	[thread overview]
Message-ID: <ea1c58ab259264fce6e09026fc1afef83e04544e.1644401681.git.efraim@flashner.co.il> (raw)
In-Reply-To: <cover.1644401681.git.efraim@flashner.co.il>

* guix/cpu.scm <cpu>: Add vendor field.
(current-cpu): Also fill in the 'vendor' field.
(cpu->gcc-architecture): Add detection logic for AMD CPUs.
---
 guix/cpu.scm | 60 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 9 deletions(-)

diff --git a/guix/cpu.scm b/guix/cpu.scm
index 5bb3fa9d2f..6d44599822 100644
--- a/guix/cpu.scm
+++ b/guix/cpu.scm
@@ -27,6 +27,7 @@ (define-module (guix cpu)
   #:export (current-cpu
             cpu?
             cpu-architecture
+            cpu-vendor
             cpu-family
             cpu-model
             cpu-flags
@@ -42,9 +43,10 @@ (define-module (guix cpu)
 
 ;; CPU description.
 (define-record-type <cpu>
-  (cpu architecture family model flags)
+  (cpu architecture vendor family model flags)
   cpu?
   (architecture cpu-architecture)                 ;string, from 'uname'
+  (vendor       cpu-vendor)                       ;string
   (family       cpu-family)                       ;integer
   (model        cpu-model)                        ;integer
   (flags        cpu-flags))                       ;set of strings
@@ -58,28 +60,33 @@ (define (prefix? prefix)
 
     (call-with-input-file "/proc/cpuinfo"
       (lambda (port)
-        (let loop ((family #f)
+        (let loop ((vendor #f)
+                   (family #f)
                    (model #f))
           (match (read-line port)
             ((? eof-object?)
              #f)
+            ((? (prefix? "vendor_id") str)
+             (match (string-tokenize str)
+               (("vendor_id" ":" vendor)
+                (loop vendor family model))))
             ((? (prefix? "cpu family") str)
              (match (string-tokenize str)
                (("cpu" "family" ":" family)
-                (loop (string->number family) model))))
+                (loop vendor (string->number family) model))))
             ((? (prefix? "model") str)
              (match (string-tokenize str)
                (("model" ":" model)
-                (loop family (string->number model)))
+                (loop vendor family (string->number model)))
                (_
-                (loop family model))))
+                (loop vendor family model))))
             ((? (prefix? "flags") str)
              (match (string-tokenize str)
                (("flags" ":" flags ...)
                 (cpu (utsname:machine (uname))
-                     family model (list->set flags)))))
+                     vendor family model (list->set flags)))))
             (_
-             (loop family model))))))))
+             (loop vendor family model))))))))
 
 (define (cpu->gcc-architecture cpu)
   "Return the architecture name, suitable for GCC's '-march' flag, that
@@ -87,7 +94,8 @@ (define (cpu->gcc-architecture cpu)
   (match (cpu-architecture cpu)
     ("x86_64"
      ;; Transcribed from GCC's 'host_detect_local_cpu' in driver-i386.c.
-     (or (and (= 6 (cpu-family cpu))              ;the "Pentium Pro" family
+     (or (and (equal? "GenuineIntel" (cpu-vendor cpu))
+              (= 6 (cpu-family cpu))              ;the "Pentium Pro" family
               (letrec-syntax ((if-flags (syntax-rules (=>)
                                           ((_)
                                            #f)
@@ -122,6 +130,40 @@ (define (cpu->gcc-architecture cpu)
                           ("ssse3" => "core2")
                           ("longmode" => "x86-64"))))
 
+         (and (equal? "AuthenticAMD" (cpu-vendor cpu))
+              (letrec-syntax ((if-flags (syntax-rules (=>)
+                                          ((_)
+                                           #f)
+                                          ((_ (flags ... => name) rest ...)
+                                           (if (every (lambda (flag)
+                                                        (set-contains? (cpu-flags cpu)
+                                                                       flag))
+                                                      '(flags ...))
+                                             name
+                                             (if-flags rest ...))))))
+
+                (when (= 22 (cpu-family cpu))
+                  (if-flags ("movbe" => "btver2")))
+                (when (= 6 (cpu-family cpu))
+                  (if-flags ("3dnowp" => "athalon")))
+
+                (if-flags ("vaes" => "znver3")
+                          ("clwb" => "znver2")
+                          ("clzero" => "znver1")
+                          ("avx2" => "bdver4")
+                          ("xsaveopt" => "bdver3")
+                          ("bmi" => "bdver2")
+                          ("xop" => "bdver1")
+                          ("sse4a" "has_ssse3" => "btver1")
+                          ("sse4a" => "amdfam10")
+                          ("sse2" "sse3" => "k8-sse3")
+                          ("longmode" "sse3" => "k8-sse3")
+                          ("sse2" => "k8")
+                          ("longmode" => "k8")
+                          ("mmx" "3dnow" => "k6-3")
+                          ("mmx" => "k6")
+                          (_ => "pentium"))))
+
          ;; Fallback case for non-Intel processors or for Intel processors not
          ;; recognized above.
          (letrec-syntax ((if-flags (syntax-rules (=>)
@@ -147,7 +189,7 @@ (define (cpu->gcc-architecture cpu)
                      ("ssse3" "movbe" => "bonnell")
                      ("ssse3" => "core2")))
 
-         ;; TODO: Recognize AMD models (bdver*, znver*, etc.)?
+         ;; TODO: Recognize CENTAUR/CYRIX/NSC?
 
          "x86_64"))
     (architecture
-- 
2.34.0





  parent reply	other threads:[~2022-02-09 10:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-09 10:16 [bug#53895] [PATCH 0/5] More CPU detection Efraim Flashner
2022-02-09 10:21 ` [bug#53895] [PATCH 1/5] guix: cpu: Rewrite based on feature flags Efraim Flashner
2022-02-09 10:35   ` [bug#53895] [PATCH 0/5] More CPU detection Ludovic Courtès
2022-02-09 10:45     ` Efraim Flashner
2022-02-10 20:42       ` Ludovic Courtès
2022-02-09 10:21 ` Efraim Flashner [this message]
2022-02-09 10:43   ` Ludovic Courtès
2022-02-09 10:54     ` Efraim Flashner
2022-02-09 11:41     ` Efraim Flashner
2022-02-10 20:42       ` Ludovic Courtès
2022-02-13 13:04         ` bug#53895: " Efraim Flashner
2022-02-09 10:21 ` [bug#53895] [PATCH 3/5] gnu: gcc: Add compiler-cpu-architectures for aarch64 Efraim Flashner
2022-02-09 10:21 ` [bug#53895] [PATCH 4/5] gnu: gcc: Add compiler-cpu-architectures for armhf Efraim Flashner
2022-02-09 10:21 ` [bug#53895] [PATCH 5/5] WIP: guix: cpu: Add detection for aarch64 CPUs Efraim Flashner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ea1c58ab259264fce6e09026fc1afef83e04544e.1644401681.git.efraim@flashner.co.il \
    --to=efraim@flashner.co.il \
    --cc=53895@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.