X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Fdynamic-extent.impure.lisp;h=99d925acfd794dfd6c7087cceae43ef3955ce573;hb=9837343101c3da7b3a8f94609ec116ec5025436a;hp=da58dbf97bdaf4ada3afae22fe54828e92645ae8;hpb=bef03694b858728bfe9481385631daeda607b5c6;p=sbcl.git diff --git a/tests/dynamic-extent.impure.lisp b/tests/dynamic-extent.impure.lisp index da58dbf..99d925a 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) @@ -129,11 +128,10 @@ ;;; value-cells (defun-with-dx dx-value-cell (x) - (declare (optimize sb-c::stack-allocate-value-cells)) ;; 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)) @@ -155,6 +153,13 @@ (true v) nil)) +(defun force-make-array-on-stack (n) + (declare (optimize safety)) + (let ((v (make-array (min n 1)))) + (declare (sb-int:truly-dynamic-extent v)) + (true v) + nil)) + ;;; MAKE-STRUCTURE (declaim (inline make-fp-struct-1)) @@ -372,6 +377,29 @@ (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 () @@ -385,7 +413,8 @@ ;;; 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))) + (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))) @@ -397,7 +426,7 @@ (:no-error (res) (1- res)))))) -;;; with-spinlock should use DX and not cons +;;; with-spinlock and with-mutex should use DX and not cons (defvar *slock* (sb-thread::make-spinlock :name "slocklock")) @@ -405,6 +434,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)) @@ -435,7 +470,7 @@ (defvar *a-cons* (cons nil nil)) -#+(or x86 x86-64 alpha ppc sparc mips) +#+(or x86 x86-64 alpha ppc sparc mips hppa) (progn (assert-no-consing (dxclosure 42)) (assert-no-consing (dxlength 1 2 3)) @@ -449,7 +484,9 @@ (assert-no-consing (dx-value-cell 13)) (assert-no-consing (cons-on-stack 42)) (assert-no-consing (make-array-on-stack)) + (assert-no-consing (force-make-array-on-stack 128)) (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)) @@ -466,7 +503,9 @@ ;; 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 @@ -509,16 +548,17 @@ (assert (every (lambda (x) (eql x 0)) a)))) (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)))) -(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))))) + +(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. @@ -534,4 +574,125 @@ (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))))) + +;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument +;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same +;;; location, leading to all manner of badness. ...reproducing this +;;; reliably is hard, but this it at least used to break on x86-64. +(defun length-and-words-packed-in-same-tn (m) + (declare (optimize speed (safety 0) (debug 0) (space 0))) + (let ((array (make-array (max 1 m) :element-type 'fixnum))) + (declare (dynamic-extent array)) + (array-total-size array))) +(with-test (:name :length-and-words-packed-in-same-tn) + (assert (= 1 (length-and-words-packed-in-same-tn -3)))) + +(with-test (:name :handler-case-bogus-compiler-note) + (handler-bind ((compiler-note #'error)) + ;; Taken from SWANK, used to signal a bogus stack allocation + ;; failure note. + (compile nil + `(lambda (files fasl-dir load) + (let ((needs-recompile nil)) + (dolist (src files) + (let ((dest (binary-pathname src fasl-dir))) + (handler-case + (progn + (when (or needs-recompile + (not (probe-file dest)) + (file-newer-p src dest)) + (setq needs-recompile t) + (ensure-directories-exist dest) + (compile-file src :output-file dest :print nil :verbose t)) + (when load + (load dest :verbose t))) + (serious-condition (c) + (handle-loadtime-error c dest)))))))))) + +(with-test (:name :dx-compiler-notes) + (let ((n 0)) + (handler-bind ((compiler-note (lambda (c) + (declare (ignore cc)) + (incf n)))) + (compile nil `(lambda (x) + (let ((v (make-array x))) + (declare (dynamic-extent v)) + (length v)))) + (assert (= 1 n)) + (compile nil `(lambda (x) + (let ((y (if (plusp x) + (true x) + (true (- x))))) + (declare (dynamic-extent y)) + (print y) + nil))) + (assert (= 3 n)))))