unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Tony Garnock-Jones <tonyg@leastfixedpoint.com>
To: 70474@debbugs.gnu.org
Subject: bug#70474: Possible bug with `atomic-box-swap!` on OSX/M3 (?!?!)
Date: Fri, 19 Apr 2024 12:54:32 +0200	[thread overview]
Message-ID: <825897f1-19fa-4f6a-b459-8db93791d1c3@leastfixedpoint.com> (raw)

Hello all,

I'm seeing some very strange behaviour from `atomic-box-swap!` (but not 
`atomic-box-compare-and-swap!`) on Guile 3.0.9 from Homebrew on OSX 
Sonoma using an M3 Pro cpu. The issue does not seem to manifest on 
x86_64. Could it be some interaction between Guile and M3 CPUs?

Or am I just doing something very silly that shouldn't work at all and 
just happens to look like it works on x86_64?

Here's the program that fails. It will run for a few hundred million 
rounds and then yield "q null in get". Note that using CAS seems to 
work, but plain old swap doesn't.

;;--

;; Eventually this fails with "q null in get" if `atomic-box-swap!` is
;; used where marked (*) below. It takes usually between hundreds of
;; millions and a few billion increments to fail.
;;
;; It does NOT fail if the line marked (*) is commented out and the line
;; below it mentioning `atomic-box-compare-and-swap!` is uncommented and
;; used instead.
;;
;; The failure happens on OSX Sonoma 14.4.1 on a MacBook Pro running an
;; M3 Pro CPU using Guile version 3.0.9 from Homebrew as of 2024-04-17.
;;
;; It does NOT happen on AMD x86_64 Debian linux with Guile 3.0.9 from
;; Debian packaging.

(use-modules (ice-9 atomic))

(define r (make-atomic-box '(0)))

(let loop ()
   (let ((v (let ((q (atomic-box-ref r)))
              (when (null? q) (error "q null in get"))
              (unless (eq? (atomic-box-compare-and-swap! r q (cdr q)) q)
                (error "CAS failed in get"))
              (car q))))

     (when (zero? (remainder v 10000000)) (write v) (newline))

     (unless (null?
              (atomic-box-swap! r (list (+ v 1))) ;; (*)
              ;; (atomic-box-compare-and-swap! r '() (list (+ v 1)))
              )
       (error "swap failed in put"))

     (loop)))





             reply	other threads:[~2024-04-19 10:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-19 10:54 Tony Garnock-Jones [this message]
2024-04-19 12:14 ` bug#70474: Also manifests on an M1 running 14.1.1 and with newer Guile versions Tony Garnock-Jones
2024-04-19 13:19 ` bug#70474: Possible bug with `atomic-box-swap!` on OSX/M3 (?!?!) Christopher Baines
2024-04-19 20:46 ` bug#70474: [PATCH 1/2] Including the cast makes Apple clang 15.0.0 happy; without it, clang is sad Tony Garnock-Jones
2024-04-19 20:48 ` bug#70474: [PATCH 2/2] Replace aarch64 CAS and atomic-swap generated JIT code with CASAL and SWPAL instructions Tony Garnock-Jones
2024-04-22  7:52 ` bug#70474: Just adding DMB doesn't help Tony Garnock-Jones
2024-04-22  8:18 ` bug#70474: [PATCH] Move the spin loop target to the LDAXR instruction Tony Garnock-Jones
2024-04-22 11:23   ` Tony Garnock-Jones

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

  List information: https://www.gnu.org/software/guile/

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

  git send-email \
    --in-reply-to=825897f1-19fa-4f6a-b459-8db93791d1c3@leastfixedpoint.com \
    --to=tonyg@leastfixedpoint.com \
    --cc=70474@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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).