New function: SB-EXT:SPIN-LOOP-HINT
authorPaul Khuong <pvk@pvk.ca>
Sun, 14 Aug 2011 22:31:52 +0000 (18:31 -0400)
committerPaul Khuong <pvk@pvk.ca>
Sun, 14 Aug 2011 22:31:52 +0000 (18:31 -0400)
 Some architectures have developed ways to help the processor execute
 spin loops efficiently; expose them, where applicable, via
 SB-EXT:SPIN-LOOP-HINT.

 Assembles to PAUSE on x86oids, and to nothing on other platforms.

12 files changed:
NEWS
package-data-list.lisp-expr
src/code/late-extensions.lisp
src/compiler/alpha/system.lisp
src/compiler/fndb.lisp
src/compiler/hppa/system.lisp
src/compiler/mips/system.lisp
src/compiler/ppc/system.lisp
src/compiler/sparc/system.lisp
src/compiler/x86-64/system.lisp
src/compiler/x86/insts.lisp
src/compiler/x86/system.lisp

diff --git a/NEWS b/NEWS
index 99fd351..66637d2 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,9 @@ changes relative to sbcl-1.0.50:
     (thanks to Anton Kovalenko).
   * enhancement: more, and more correct, SSE instruction definitions on
     x86-64 (thanks to Alexander Gavrilov).
+  * enhancement: SB-EXT:SPIN-LOOP-HINT assembles to an instruction designed
+    to help the processor execute spin loops, when applicable.  Currently
+    implemented for x86 and x86-64.
   * optimization: unsigned integer divisions by a constant are implemented
     using multiplication (affects CEILING, FLOOR, TRUNCATE, MOD, and REM.)
   * optimization: improved type-derivation for LOAD-TIME-VALUE.
index fffc257..c898ccc 100644 (file)
@@ -603,6 +603,9 @@ like *STACK-TOP-HINT* and unsupported stuff like *TRACED-FUN-LIST*."
                "WORD"
                "MOST-POSITIVE-WORD"
 
+               ;; Not an atomic operation, but should be used with them
+               "SPIN-LOOP-HINT"
+
                ;; Time related things
                "CALL-WITH-TIMING"
                "GET-TIME-OF-DAY"
index 5d2d598..890fd43 100644 (file)
@@ -296,6 +296,11 @@ EXPERIMENTAL: Interface subject to change."
            (type sb!vm:signed-word diff))
   (%array-atomic-incf/word array index diff))
 
+(defun spin-loop-hint ()
+  #!+sb-doc
+  "Hints the processor that the current thread is spin-looping."
+  (spin-loop-hint))
+
 (defun call-hooks (kind hooks &key (on-error :error))
   (dolist (hook hooks)
     (handler-case
index c79d117..c051514 100644 (file)
       (inst ldl count offset count-vector)
       (inst addq count 1 count)
       (inst stl count offset count-vector))))
+
+;;;; Dummy definition for a spin-loop hint VOP
+(define-vop (spin-loop-hint)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0))
index 891a39c..edb99d4 100644 (file)
     (unsafe))
 (defknown %compare-and-swap-symbol-value (symbol t t) t
     (unsafe unwind))
+(defknown spin-loop-hint () (values)
+    (always-translatable))
index c99cab8..ad172c2 100644 (file)
       (inst addi 1 count count)
       (inst stw count offset count-vector))))
 
+;;;; Dummy definition for a spin-loop hint VOP
+(define-vop (spin-loop-hint)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0))
index e5841e5..dcf7aba 100644 (file)
       (inst nop)
       (inst addu count 1)
       (inst sw count count-vector offset))))
+
+;;;; Dummy definition for a spin-loop hint VOP
+(define-vop (spin-loop-hint)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0))
index 684bf3c..1482de1 100644 (file)
   (:policy :fast-safe)
   (:translate %data-dependency-barrier)
   (:generator 3))
+
+;;;; Dummy definition for a spin-loop hint VOP
+(define-vop (spin-loop-hint)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0))
index 0e5b993..62d6f4c 100644 (file)
       (inst ld count count-vector offset)
       (inst add count 1)
       (inst st count count-vector offset))))
+
+;;;; Dummy definition for a spin-loop hint VOP
+(define-vop (spin-loop-hint)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0))
index 1edf735..2d710d9 100644 (file)
@@ -394,3 +394,9 @@ number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
   (:policy :fast-safe)
   (:translate %data-dependency-barrier)
   (:generator 3))
+
+(define-vop (pause)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0
+    (inst pause)))
index 6850859..720ad62 100644 (file)
      (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
      (emit-ea segment dst (reg-tn-encoding src)))))
 
+(define-instruction pause (segment)
+  (:printer two-bytes ((op '(#xf3 #x90))))
+  (:emitter
+   (emit-byte segment #xf3)
+   (emit-byte segment #x90)))
 \f
 (defun emit-prefix (segment name)
   (ecase name
index fd14fbf..2128d2b 100644 (file)
@@ -385,3 +385,9 @@ number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
   (:policy :fast-safe)
   (:translate %data-dependency-barrier)
   (:generator 3))
+
+(define-vop (pause)
+  (:translate spin-loop-hint)
+  (:policy :fast-safe)
+  (:generator 0
+    (inst pause)))