X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Fdynamic-extent.impure.lisp;h=7867bae61a559b3a7097ea23d0757602adb2b59a;hb=007bcd5aac2f3a1e714563bd39f7a2db2d0bf7c2;hp=704a55d183aa1bc2de2b1f7acf2c6d9d2110dce0;hpb=66cff1e1319861c080d563359afea284614b3a7f;p=sbcl.git diff --git a/tests/dynamic-extent.impure.lisp b/tests/dynamic-extent.impure.lisp index 704a55d..7867bae 100644 --- a/tests/dynamic-extent.impure.lisp +++ b/tests/dynamic-extent.impure.lisp @@ -14,13 +14,12 @@ (when (eq sb-ext:*evaluator-mode* :interpret) (sb-ext:quit :unix-status 104)) -(setq sb-c::*check-consistency* t) +(setq sb-c::*check-consistency* t + sb-ext:*stack-allocate-dynamic-extent* t) (defmacro defun-with-dx (name arglist &body body) - `(locally - (declare (optimize sb-c::stack-allocate-dynamic-extent)) - (defun ,name ,arglist - ,@body))) + `(defun ,name ,arglist + ,@body)) (declaim (notinline opaque-identity)) (defun opaque-identity (x) @@ -130,9 +129,9 @@ (defun-with-dx dx-value-cell (x) ;; Not implemented everywhere, yet. - #+(or x86 x86-64 mips) + #+(or x86 x86-64 mips hppa) (let ((cell x)) - (declare (dynamic-extent cell)) + (declare (sb-int:truly-dynamic-extent cell)) (flet ((f () (incf cell))) (declare (dynamic-extent #'f)) @@ -146,7 +145,281 @@ (true cons) nil)) -;;; with-spinlock should use DX and not cons +;;; MAKE-ARRAY + +(defun-with-dx make-array-on-stack () + (let ((v (make-array '(42) :element-type 'single-float))) + (declare (dynamic-extent v)) + (true v) + nil)) + +;;; MAKE-STRUCTURE + +(declaim (inline make-fp-struct-1)) +(defstruct fp-struct-1 + (s 0.0 :type single-float) + (d 0.0d0 :type double-float)) + +(defun-with-dx test-fp-struct-1.1 (s d) + (let ((fp (make-fp-struct-1 :s s))) + (declare (dynamic-extent fp)) + (assert (eql s (fp-struct-1-s fp))) + (assert (eql 0.0d0 (fp-struct-1-d fp))))) + +(defun-with-dx test-fp-struct-1.2 (s d) + (let ((fp (make-fp-struct-1 :d d))) + (declare (dynamic-extent fp)) + (assert (eql 0.0 (fp-struct-1-s fp))) + (assert (eql d (fp-struct-1-d fp))))) + +(defun-with-dx test-fp-struct-1.3 (s d) + (let ((fp (make-fp-struct-1 :d d :s s))) + (declare (dynamic-extent fp)) + (assert (eql s (fp-struct-1-s fp))) + (assert (eql d (fp-struct-1-d fp))))) + +(defun-with-dx test-fp-struct-1.4 (s d) + (let ((fp (make-fp-struct-1 :s s :d d))) + (declare (dynamic-extent fp)) + (assert (eql s (fp-struct-1-s fp))) + (assert (eql d (fp-struct-1-d fp))))) + +(test-fp-struct-1.1 123.456 876.243d0) +(test-fp-struct-1.2 123.456 876.243d0) +(test-fp-struct-1.3 123.456 876.243d0) +(test-fp-struct-1.4 123.456 876.243d0) + +(declaim (inline make-fp-struct-2)) +(defstruct fp-struct-2 + (d 0.0d0 :type double-float) + (s 0.0 :type single-float)) + +(defun-with-dx test-fp-struct-2.1 (s d) + (let ((fp (make-fp-struct-2 :s s))) + (declare (dynamic-extent fp)) + (assert (eql s (fp-struct-2-s fp))) + (assert (eql 0.0d0 (fp-struct-2-d fp))))) + +(defun-with-dx test-fp-struct-2.2 (s d) + (let ((fp (make-fp-struct-2 :d d))) + (declare (dynamic-extent fp)) + (assert (eql 0.0 (fp-struct-2-s fp))) + (assert (eql d (fp-struct-2-d fp))))) + +(defun-with-dx test-fp-struct-2.3 (s d) + (let ((fp (make-fp-struct-2 :d d :s s))) + (declare (dynamic-extent fp)) + (assert (eql s (fp-struct-2-s fp))) + (assert (eql d (fp-struct-2-d fp))))) + +(defun-with-dx test-fp-struct-2.4 (s d) + (let ((fp (make-fp-struct-2 :s s :d d))) + (declare (dynamic-extent fp)) + (assert (eql s (fp-struct-2-s fp))) + (assert (eql d (fp-struct-2-d fp))))) + +(test-fp-struct-2.1 123.456 876.243d0) +(test-fp-struct-2.2 123.456 876.243d0) +(test-fp-struct-2.3 123.456 876.243d0) +(test-fp-struct-2.4 123.456 876.243d0) + +(declaim (inline make-cfp-struct-1)) +(defstruct cfp-struct-1 + (s (complex 0.0) :type (complex single-float)) + (d (complex 0.0d0) :type (complex double-float))) + +(defun-with-dx test-cfp-struct-1.1 (s d) + (let ((cfp (make-cfp-struct-1 :s s))) + (declare (dynamic-extent cfp)) + (assert (eql s (cfp-struct-1-s cfp))) + (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp))))) + +(defun-with-dx test-cfp-struct-1.2 (s d) + (let ((cfp (make-cfp-struct-1 :d d))) + (declare (dynamic-extent cfp)) + (assert (eql (complex 0.0) (cfp-struct-1-s cfp))) + (assert (eql d (cfp-struct-1-d cfp))))) + +(defun-with-dx test-cfp-struct-1.3 (s d) + (let ((cfp (make-cfp-struct-1 :d d :s s))) + (declare (dynamic-extent cfp)) + (assert (eql s (cfp-struct-1-s cfp))) + (assert (eql d (cfp-struct-1-d cfp))))) + +(defun-with-dx test-cfp-struct-1.4 (s d) + (let ((cfp (make-cfp-struct-1 :s s :d d))) + (declare (dynamic-extent cfp)) + (assert (eql s (cfp-struct-1-s cfp))) + (assert (eql d (cfp-struct-1-d cfp))))) + +(test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) +(test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) +(test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) +(test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) + +(declaim (inline make-cfp-struct-2)) +(defstruct cfp-struct-2 + (d (complex 0.0d0) :type (complex double-float)) + (s (complex 0.0) :type (complex single-float))) + +(defun-with-dx test-cfp-struct-2.1 (s d) + (let ((cfp (make-cfp-struct-2 :s s))) + (declare (dynamic-extent cfp)) + (assert (eql s (cfp-struct-2-s cfp))) + (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp))))) + +(defun-with-dx test-cfp-struct-2.2 (s d) + (let ((cfp (make-cfp-struct-2 :d d))) + (declare (dynamic-extent cfp)) + (assert (eql (complex 0.0) (cfp-struct-2-s cfp))) + (assert (eql d (cfp-struct-2-d cfp))))) + +(defun-with-dx test-cfp-struct-2.3 (s d) + (let ((cfp (make-cfp-struct-2 :d d :s s))) + (declare (dynamic-extent cfp)) + (assert (eql s (cfp-struct-2-s cfp))) + (assert (eql d (cfp-struct-2-d cfp))))) + +(defun-with-dx test-cfp-struct-2.4 (s d) + (let ((cfp (make-cfp-struct-2 :s s :d d))) + (declare (dynamic-extent cfp)) + (assert (eql s (cfp-struct-2-s cfp))) + (assert (eql d (cfp-struct-2-d cfp))))) + +(test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) +(test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) +(test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) +(test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)) + +(declaim (inline make-foo1 make-foo2 make-foo3)) +(defstruct foo1 x) + +(defun-with-dx make-foo1-on-stack (x) + (let ((foo (make-foo1 :x x))) + (declare (dynamic-extent foo)) + (assert (eql x (foo1-x foo))))) + +(defstruct foo2 + (x 0.0 :type single-float) + (y 0.0d0 :type double-float) + a + b + c) + +(defmacro assert-eql (expected got) + `(let ((exp ,expected) + (got ,got)) + (unless (eql exp got) + (error "Expected ~S, got ~S!" exp got)))) + +(defun-with-dx make-foo2-on-stack (x y) + (let ((foo (make-foo2 :y y :c 'c))) + (declare (dynamic-extent foo)) + (assert-eql 0.0 (foo2-x foo)) + (assert-eql y (foo2-y foo)) + (assert-eql 'c (foo2-c foo)) + (assert-eql nil (foo2-b foo)))) + +;;; Check that constants work out as argument for all relevant +;;; slot types. +(defstruct foo3 + (a 0 :type t) + (b 1 :type fixnum) + (c 2 :type sb-vm:word) + (d 3.0 :type single-float) + (e 4.0d0 :type double-float)) +(defun-with-dx make-foo3-on-stack () + (let ((foo (make-foo3))) + (declare (dynamic-extent foo)) + (assert (eql 0 (foo3-a foo))) + (assert (eql 1 (foo3-b foo))) + (assert (eql 2 (foo3-c foo))) + (assert (eql 3.0 (foo3-d foo))) + (assert (eql 4.0d0 (foo3-e foo))))) + +;;; Nested DX + +(defun-with-dx nested-dx-lists () + (let ((dx (list (list 1 2) (list 3 4)))) + (declare (dynamic-extent dx)) + (true dx) + nil)) + +(defun-with-dx nested-dx-conses () + (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil)))))) + (declare (dynamic-extent dx)) + (true dx) + nil)) + +(defun-with-dx nested-dx-not-used (x) + (declare (list x)) + (let ((l (setf (car x) (list x x x)))) + (declare (dynamic-extent l)) + (true l) + (true (length l)) + nil)) + +(defun-with-dx nested-evil-dx-used (x) + (declare (list x)) + (let ((l (list x x x))) + (declare (dynamic-extent l)) + (unwind-protect + (progn + (setf (car x) l) + (true l)) + (setf (car x) nil)) + nil)) + +(defparameter *bar* nil) +(declaim (inline make-nested-bad make-nested-good)) +(defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar)))) + (:constructor make-nested-good (&key bar))) + bar) + +(defun-with-dx nested-good (y) + (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y))))))))) + (declare (dynamic-extent x)) + (true x))) + +(defun-with-dx nested-bad (y) + (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y))))))))) + (declare (dynamic-extent x)) + (unless (equalp (caar x) (make-nested-good :bar *bar*)) + (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*))) + (caar x))) + +(with-test (:name :conservative-nested-dx) + ;; NESTED-BAD should not stack-allocate :BAR due to the SETF. + (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*))) + (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42))))))) + +;;; multiple uses for dx lvar + +(defun-with-dx multiple-dx-uses () + (let ((dx (if (true t) + (list 1 2 3) + (list 2 3 4)))) + (declare (dynamic-extent dx)) + (true dx) + nil)) + +;;; handler-case and handler-bind should use DX internally + +(defun dx-handler-bind (x) + (handler-bind ((error + (lambda (c) (break "OOPS: ~S caused ~S" x c))) + ((and serious-condition (not error)) + #'(lambda (c) (break "OOPS2: ~S did ~S" x c)))) + (/ 2 x))) + +(defun dx-handler-case (x) + (assert (zerop (handler-case (/ 2 x) + (error (c) + (break "OOPS: ~S caused ~S" x c)) + (:no-error (res) + (1- res)))))) + +;;; with-spinlock and with-mutex should use DX and not cons (defvar *slock* (sb-thread::make-spinlock :name "slocklock")) @@ -154,6 +427,12 @@ (sb-thread::with-spinlock (*slock*) (true *slock*))) +(defvar *mutex* (sb-thread::make-mutex :name "mutexlock")) + +(defun test-mutex () + (sb-thread:with-mutex (*mutex*) + (true *mutex*))) + ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons (defvar *table* (make-hash-table)) @@ -172,7 +451,19 @@ (funcall thunk)) (assert (< (- (get-bytes-consed) before) times)))) -#+(or x86 x86-64 alpha ppc sparc mips) +(defmacro assert-consing (form &optional times) + `(%assert-consing (lambda () ,form) ,times)) +(defun %assert-consing (thunk &optional times) + (let ((before (get-bytes-consed)) + (times (or times 10000))) + (declare (type (integer 1 *) times)) + (dotimes (i times) + (funcall thunk)) + (assert (not (< (- (get-bytes-consed) before) times))))) + +(defvar *a-cons* (cons nil nil)) + +#+(or x86 x86-64 alpha ppc sparc mips hppa) (progn (assert-no-consing (dxclosure 42)) (assert-no-consing (dxlength 1 2 3)) @@ -185,10 +476,28 @@ (assert-no-consing (test-lvar-subst 11)) (assert-no-consing (dx-value-cell 13)) (assert-no-consing (cons-on-stack 42)) + (assert-no-consing (make-array-on-stack)) + (assert-no-consing (make-foo1-on-stack 123)) + (assert-no-consing (nested-good 42)) + (#+raw-instance-init-vops assert-no-consing + #-raw-instance-init-vops progn + (make-foo2-on-stack 1.24 1.23d0)) + (#+raw-instance-init-vops assert-no-consing + #-raw-instance-init-vops progn + (make-foo3-on-stack)) + (assert-no-consing (nested-dx-conses)) + (assert-no-consing (nested-dx-lists)) + (assert-consing (nested-dx-not-used *a-cons*)) + (assert-no-consing (nested-evil-dx-used *a-cons*)) + (assert-no-consing (multiple-dx-uses)) + (assert-no-consing (dx-handler-bind 2)) + (assert-no-consing (dx-handler-case 2)) ;; Not strictly DX.. (assert-no-consing (test-hash-table)) #+sb-thread - (assert-no-consing (test-spinlock))) + (progn + (assert-no-consing (test-spinlock)) + (assert-no-consing (test-mutex)))) ;;; Bugs found by Paul F. Dietz @@ -229,6 +538,100 @@ (let ((a (make-array 11 :initial-element 0))) (declare (dynamic-extent a)) (assert (every (lambda (x) (eql x 0)) a)))) -(bdowning-2005-iv-16) - +(assert-no-consing (bdowning-2005-iv-16)) + +(defun-with-dx let-converted-vars-dx-allocated-bug (x y z) + (let* ((a (list x y z)) + (b (list x y z)) + (c (list a b))) + (declare (dynamic-extent c)) + (values (first c) (second c)))) + +(with-test (:name :let-converted-vars-dx-allocated-bug) + (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3) + (assert (and (equal i j) + (equal i (list 1 2 3)))))) + +;;; workaround for bug 419 -- real issue remains, but check that the +;;; bandaid holds. +(defun-with-dx bug419 (x) + (multiple-value-call #'list + (eval '(values 1 2 3)) + (let ((x x)) + (declare (dynamic-extent x)) + (flet ((mget (y) + (+ x y)) + (mset (z) + (incf x z))) + (declare (dynamic-extent #'mget #'mset)) + ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset))))) +(assert (equal (bug419 42) '(1 2 3 4 5 6))) + +;;; Multiple DX arguments in a local function call +(defun test-dx-flet-test (fun n f1 f2 f3) + (let ((res (with-output-to-string (s) + (assert (eql n (ignore-errors (funcall fun s))))))) + (multiple-value-bind (x pos) (read-from-string res nil) + (assert (equalp f1 x)) + (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos) + (assert (equalp f2 y)) + (assert (equalp f3 (read-from-string res nil nil :start pos2)))))) + (assert-no-consing (assert (eql n (funcall fun nil))))) +(macrolet ((def (n f1 f2 f3) + (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n))) + `(progn + (defun-with-dx ,name (s) + (flet ((f (x) + (declare (dynamic-extent x)) + (when s + (print x s) + (finish-output s)) + nil)) + (f ,f1) + (f ,f2) + (f ,f3) + ,n)) + (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))) + (def 0 (list :one) (list :two) (list :three)) + (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list)) + (def 2 (list 1) (list 2 3) (list 4 5 6 7))) + +;;; Test that unknown-values coming after a DX value won't mess up the stack analysis +(defun test-update-uvl-live-sets (x y z) + (declare (optimize speed (safety 0))) + (flet ((bar (a b) + (declare (dynamic-extent a)) + (eval `(list (length ',a) ',b)))) + (list (bar x y) + (bar (list x y z) ; dx push + (list + (multiple-value-call 'list + (eval '(values 1 2 3)) ; uv push + (max y z) + ) ; uv pop + 14) + )))) +(assert (equal '((0 4) (3 ((1 2 3 5) 14))) (test-update-uvl-live-sets #() 4 5))) + +(with-test (:name :regression-1.0.23.38) + (compile nil '(lambda () + (flet ((make (x y) + (let ((res (cons x x))) + (setf (cdr res) y) + res))) + (declaim (inline make)) + (let ((z (make 1 2))) + (declare (dynamic-extent z)) + (print z) + t)))) + (compile nil '(lambda () + (flet ((make (x y) + (let ((res (cons x x))) + (setf (cdr res) y) + (if x res y)))) + (declaim (inline make)) + (let ((z (make 1 2))) + (declare (dynamic-extent z)) + (print z) + t)))))