1 ;;;; tests that dynamic-extent functionality works.
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (when (eq sb-ext
:*evaluator-mode
* :interpret
)
15 (sb-ext:exit
:code
104))
17 (load "compiler-test-util.lisp")
20 (setq sb-c
::*check-consistency
* t
21 sb-ext
:*stack-allocate-dynamic-extent
* t
)
23 (defmacro defun-with-dx
(name arglist
&body body
)
24 (let ((debug-name (sb-int:symbolicate name
"-HIGH-DEBUG"))
25 (default-name (sb-int:symbolicate name
"-DEFAULT")))
27 (defun ,debug-name
,arglist
28 (declare (optimize debug
))
30 (defun ,default-name
,arglist
32 (defun ,name
(&rest args
)
33 (apply #',debug-name args
)
34 (apply #',default-name args
)))))
36 (declaim (notinline opaque-identity
))
37 (defun opaque-identity (x)
42 (defun-with-dx dxlength
(&rest rest
)
43 (declare (dynamic-extent rest
))
46 (with-test (:name
(:dx-
&rest
:basics
))
47 (assert (= (dxlength 1 2 3) 3))
48 (assert (= (dxlength t t t t t t
) 6))
49 (assert (= (dxlength) 0)))
52 (destructuring-bind (a b c d e f
&rest g
) list
53 (+ a b c d e f
(length g
))))
55 (defun-with-dx dxcaller
(&rest rest
)
56 (declare (dynamic-extent rest
))
59 (with-test (:name
(:dx-
&rest
:pass-down-to-callee
:tail-call
))
60 (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
62 (defun-with-dx dxcaller-align-1
(x &rest rest
)
63 (declare (dynamic-extent rest
))
66 (with-test (:name
(:dx-
&rest
:pass-down-to-callee
:non-tail-call
))
67 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
68 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
72 (defun-with-dx test-nip-values
()
73 (flet ((bar (x &rest y
)
74 (declare (dynamic-extent y
))
78 (multiple-value-call #'values
82 (with-test (:name
(:nip-values
))
83 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a
))))
85 ;;; LET-variable substitution
87 (defun-with-dx test-let-var-subst1
(x)
88 (let ((y (list x
(1- x
))))
89 (opaque-identity :foo
)
90 (let ((z (the list y
)))
91 (declare (dynamic-extent z
))
94 (with-test (:name
(:let-variable-substitution
))
95 (assert (eql (test-let-var-subst1 17) 2)))
97 (defun-with-dx test-let-var-subst2
(x)
98 (let ((y (list x
(1- x
))))
99 (declare (dynamic-extent y
))
100 (opaque-identity :foo
)
101 (let ((z (the list y
)))
104 (with-test (:name
(:let-variable-substitution-2
))
105 (assert (eql (test-let-var-subst2 17) 2)))
108 ;;; DX propagation through LET-return.
110 (defun-with-dx test-lvar-subst
(x)
111 (let ((y (list x
(1- x
))))
112 (declare (dynamic-extent y
))
113 (second (let ((z (the list y
)))
114 (opaque-identity :foo
)
117 (with-test (:name
(:dx-propagation-through-let-return
))
118 (assert (eql (test-lvar-subst 11) 10)))
120 ;;; this code is incorrect, but the compiler should not fail
121 (defun-with-dx test-let-var-subst-incorrect
(x)
122 (let ((y (list x
(1- x
))))
123 (opaque-identity :foo
)
124 (let ((z (the list y
)))
125 (declare (dynamic-extent z
))
126 (opaque-identity :bar
)
132 (defun-with-dx test-alignment-dx-list
(form)
133 (multiple-value-prog1 (eval form
)
134 (let ((l (list 1 2 3 4)))
135 (declare (dynamic-extent l
))
136 (setq *x
* (copy-list l
)))))
138 (with-test (:name
(:dx-list
:alignment
))
140 (let* ((res (loop for i below n collect i
))
141 (form `(values ,@res
)))
142 (assert (equal (multiple-value-list (test-alignment-dx-list form
)) res
))
143 (assert (equal *x
* '(1 2 3 4))))))
147 (declaim (notinline true
))
152 (defun-with-dx dxclosure
(x)
155 (declare (dynamic-extent #'f
))
158 (with-test (:name
(:dx-closure
))
159 (assert (eq t
(dxclosure 13))))
163 (defun-with-dx dx-value-cell
(x)
164 ;; Not implemented everywhere, yet.
165 #+(or x86 x86-64 mips hppa
)
167 (declare (sb-int:truly-dynamic-extent cell
))
170 (declare (dynamic-extent #'f
))
175 (defun-with-dx cons-on-stack
(x)
176 (let ((cons (cons x x
)))
177 (declare (dynamic-extent cons
))
183 (defun force-make-array-on-stack (n)
184 (declare (optimize safety
))
185 (let ((v (make-array (min n
1))))
186 (declare (sb-int:truly-dynamic-extent v
))
191 (defun-with-dx make-array-on-stack-1
()
192 (let ((v (make-array '(42) :element-type
'single-float
)))
193 (declare (dynamic-extent v
))
198 (defun-with-dx make-array-on-stack-2
(n x
)
199 (declare (integer n
))
200 (let ((v (make-array n
:initial-contents x
)))
201 (declare (sb-int:truly-dynamic-extent v
))
206 (defun-with-dx make-array-on-stack-3
(x y z
)
207 (let ((v (make-array 3
208 :element-type
'fixnum
:initial-contents
(list x y z
)
209 :element-type t
:initial-contents x
)))
210 (declare (sb-int:truly-dynamic-extent v
))
215 (defun-with-dx make-array-on-stack-4
()
216 (let ((v (make-array 3 :initial-contents
'(1 2 3))))
217 (declare (sb-int:truly-dynamic-extent v
))
222 (defun-with-dx make-array-on-stack-5
()
223 (let ((v (make-array 3 :initial-element
12 :element-type t
)))
224 (declare (sb-int:truly-dynamic-extent v
))
229 (defun-with-dx make-array-on-stack-6
()
230 (let ((v (make-array 3 :initial-element
12 :element-type
'(unsigned-byte 8))))
231 (declare (sb-int:truly-dynamic-extent v
))
236 (defun-with-dx make-array-on-stack-7
()
237 (let ((v (make-array 3 :initial-element
12 :element-type
'(signed-byte 8))))
238 (declare (sb-int:truly-dynamic-extent v
))
243 (defun-with-dx make-array-on-stack-8
()
244 (let ((v (make-array 3 :initial-element
12 :element-type
'word
)))
245 (declare (sb-int:truly-dynamic-extent v
))
250 (defun-with-dx make-array-on-stack-9
()
251 (let ((v (make-array 3 :initial-element
12.0 :element-type
'single-float
)))
252 (declare (sb-int:truly-dynamic-extent v
))
257 (defun-with-dx make-array-on-stack-10
()
258 (let ((v (make-array 3 :initial-element
12.0d0
:element-type
'double-float
)))
259 (declare (sb-int:truly-dynamic-extent v
))
264 (defun-with-dx make-array-on-stack-11
()
265 (let ((v (make-array (the integer
(opaque-identity 3)) :initial-element
12.0d0
:element-type
'double-float
)))
266 (declare (sb-int:truly-dynamic-extent v
))
271 (defun-with-dx vector-on-stack
(x y
)
272 (let ((v (vector 1 x
2 y
3)))
273 (declare (sb-int:truly-dynamic-extent v
))
279 (declaim (inline make-list-container
))
280 (defstruct list-container listy-slot
)
281 (defun make-var-length-dx-list (n thunk
)
282 (sb-int:dx-let
((s (make-list-container :listy-slot
(make-list n
))))
284 ;; :stack-allocatable-lists is necessary but not sufficient
285 (with-test (:name
(:dx-list
:make-list
) :skipped-on
'(not :x86-64
))
286 (assert (null (ctu:find-named-callees
#'make-var-length-dx-list
)))
287 (assert-no-consing (make-var-length-dx-list
288 50 (lambda (x) (declare (ignore x
))))))
292 (declaim (inline make-fp-struct-1
))
293 (defstruct fp-struct-1
294 (s 0.0 :type single-float
)
295 (d 0.0d0
:type double-float
))
297 (defun-with-dx test-fp-struct-1.1
(s d
)
299 (let ((fp (make-fp-struct-1 :s s
)))
300 (declare (dynamic-extent fp
))
301 (assert (eql s
(fp-struct-1-s fp
)))
302 (assert (eql 0.0d0
(fp-struct-1-d fp
)))))
304 (defun-with-dx test-fp-struct-1.2
(s d
)
306 (let ((fp (make-fp-struct-1 :d d
)))
307 (declare (dynamic-extent fp
))
308 (assert (eql 0.0 (fp-struct-1-s fp
)))
309 (assert (eql d
(fp-struct-1-d fp
)))))
311 (defun-with-dx test-fp-struct-1.3
(s d
)
312 (let ((fp (make-fp-struct-1 :d d
:s s
)))
313 (declare (dynamic-extent fp
))
314 (assert (eql s
(fp-struct-1-s fp
)))
315 (assert (eql d
(fp-struct-1-d fp
)))))
317 (defun-with-dx test-fp-struct-1.4
(s d
)
318 (let ((fp (make-fp-struct-1 :s s
:d d
)))
319 (declare (dynamic-extent fp
))
320 (assert (eql s
(fp-struct-1-s fp
)))
321 (assert (eql d
(fp-struct-1-d fp
)))))
323 (with-test (:name
(:test-fp-struct-1.1
))
324 (test-fp-struct-1.1
123.456 876.243d0
))
325 (with-test (:name
(:test-fp-struct-1.2
))
326 (test-fp-struct-1.2
123.456 876.243d0
))
327 (with-test (:name
(:test-fp-struct-1.3
))
328 (test-fp-struct-1.3
123.456 876.243d0
))
329 (with-test (:name
(:test-fp-struct-1.4
))
330 (test-fp-struct-1.4
123.456 876.243d0
))
332 (declaim (inline make-fp-struct-2
))
333 (defstruct fp-struct-2
334 (d 0.0d0
:type double-float
)
335 (s 0.0 :type single-float
))
337 (defun-with-dx test-fp-struct-2.1
(s d
)
339 (let ((fp (make-fp-struct-2 :s s
)))
340 (declare (dynamic-extent fp
))
341 (assert (eql s
(fp-struct-2-s fp
)))
342 (assert (eql 0.0d0
(fp-struct-2-d fp
)))))
344 (defun-with-dx test-fp-struct-2.2
(s d
)
346 (let ((fp (make-fp-struct-2 :d d
)))
347 (declare (dynamic-extent fp
))
348 (assert (eql 0.0 (fp-struct-2-s fp
)))
349 (assert (eql d
(fp-struct-2-d fp
)))))
351 (defun-with-dx test-fp-struct-2.3
(s d
)
352 (let ((fp (make-fp-struct-2 :d d
:s s
)))
353 (declare (dynamic-extent fp
))
354 (assert (eql s
(fp-struct-2-s fp
)))
355 (assert (eql d
(fp-struct-2-d fp
)))))
357 (defun-with-dx test-fp-struct-2.4
(s d
)
358 (let ((fp (make-fp-struct-2 :s s
:d d
)))
359 (declare (dynamic-extent fp
))
360 (assert (eql s
(fp-struct-2-s fp
)))
361 (assert (eql d
(fp-struct-2-d fp
)))))
363 (with-test (:name
(:test-fp-struct-2.1
))
364 (test-fp-struct-2.1
123.456 876.243d0
))
365 (with-test (:name
(:test-fp-struct-2.2
))
366 (test-fp-struct-2.2
123.456 876.243d0
))
367 (with-test (:name
(:test-fp-struct-2.3
))
368 (test-fp-struct-2.3
123.456 876.243d0
))
369 (with-test (:name
(:test-fp-struct-2.4
))
370 (test-fp-struct-2.4
123.456 876.243d0
))
372 (declaim (inline make-cfp-struct-1
))
373 (defstruct cfp-struct-1
374 (s (complex 0.0) :type
(complex single-float
))
375 (d (complex 0.0d0
) :type
(complex double-float
)))
377 (defun-with-dx test-cfp-struct-1.1
(s d
)
379 (let ((cfp (make-cfp-struct-1 :s s
)))
380 (declare (dynamic-extent cfp
))
381 (assert (eql s
(cfp-struct-1-s cfp
)))
382 (assert (eql (complex 0.0d0
) (cfp-struct-1-d cfp
)))))
384 (defun-with-dx test-cfp-struct-1.2
(s d
)
386 (let ((cfp (make-cfp-struct-1 :d d
)))
387 (declare (dynamic-extent cfp
))
388 (assert (eql (complex 0.0) (cfp-struct-1-s cfp
)))
389 (assert (eql d
(cfp-struct-1-d cfp
)))))
391 (defun-with-dx test-cfp-struct-1.3
(s d
)
392 (let ((cfp (make-cfp-struct-1 :d d
:s s
)))
393 (declare (dynamic-extent cfp
))
394 (assert (eql s
(cfp-struct-1-s cfp
)))
395 (assert (eql d
(cfp-struct-1-d cfp
)))))
397 (defun-with-dx test-cfp-struct-1.4
(s d
)
398 (let ((cfp (make-cfp-struct-1 :s s
:d d
)))
399 (declare (dynamic-extent cfp
))
400 (assert (eql s
(cfp-struct-1-s cfp
)))
401 (assert (eql d
(cfp-struct-1-d cfp
)))))
403 (with-test (:name
(:test-cfp-struct-1.1
))
404 (test-cfp-struct-1.1
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
405 (with-test (:name
(:test-cfp-struct-1.2
))
406 (test-cfp-struct-1.2
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
407 (with-test (:name
(:test-cfp-struct-1.3
))
408 (test-cfp-struct-1.3
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
409 (with-test (:name
(:test-cfp-struct-1.4
))
410 (test-cfp-struct-1.4
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
412 (declaim (inline make-cfp-struct-2
))
413 (defstruct cfp-struct-2
414 (d (complex 0.0d0
) :type
(complex double-float
))
415 (s (complex 0.0) :type
(complex single-float
)))
417 (defun-with-dx test-cfp-struct-2.1
(s d
)
419 (let ((cfp (make-cfp-struct-2 :s s
)))
420 (declare (dynamic-extent cfp
))
421 (assert (eql s
(cfp-struct-2-s cfp
)))
422 (assert (eql (complex 0.0d0
) (cfp-struct-2-d cfp
)))))
424 (defun-with-dx test-cfp-struct-2.2
(s d
)
426 (let ((cfp (make-cfp-struct-2 :d d
)))
427 (declare (dynamic-extent cfp
))
428 (assert (eql (complex 0.0) (cfp-struct-2-s cfp
)))
429 (assert (eql d
(cfp-struct-2-d cfp
)))))
431 (defun-with-dx test-cfp-struct-2.3
(s d
)
432 (let ((cfp (make-cfp-struct-2 :d d
:s s
)))
433 (declare (dynamic-extent cfp
))
434 (assert (eql s
(cfp-struct-2-s cfp
)))
435 (assert (eql d
(cfp-struct-2-d cfp
)))))
437 (defun-with-dx test-cfp-struct-2.4
(s d
)
438 (let ((cfp (make-cfp-struct-2 :s s
:d d
)))
439 (declare (dynamic-extent cfp
))
440 (assert (eql s
(cfp-struct-2-s cfp
)))
441 (assert (eql d
(cfp-struct-2-d cfp
)))))
443 (with-test (:name
(:test-cfp-struct-2.1
))
444 (test-cfp-struct-2.1
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
445 (with-test (:name
(:test-cfp-struct-2.2
))
446 (test-cfp-struct-2.2
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
447 (with-test (:name
(:test-cfp-struct-2.3
))
448 (test-cfp-struct-2.3
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
449 (with-test (:name
(:test-cfp-struct-2.4
))
450 (test-cfp-struct-2.4
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
452 ;; It works to declare a structure constructor INLINE after the DEFSTRUCT
453 ;; was processed, as long as it was in the null lexical environment.
454 ;; In a perfect world, we'd figure out that a variable declared DX which
455 ;; receives the value of a structure constructor defined in the same file
456 ;; and neither expressly INLINE nor NOTINLINE should be locally inlined.
457 ;; But at least this shows that a declaration at the intended use point
458 ;; is sufficient, without also a bracketing INLINE/NOTINLINE at the DEFSTRUCT.
460 ;; Verify the precondition for the assertions that claim that DXifying works
461 ;; even though the MAKE- function was not expressly INLINE when its DEFSTRUCT
463 (dolist (s '(make-foo1 make-foo2 make-foo3
))
464 (assert (null (sb-int:info
:function
:inline-expansion-designator s
))))
468 (defun-with-dx make-foo1-on-stack
(x)
469 (declare (inline make-foo1
))
470 (let ((foo (make-foo1 :x x
)))
471 (declare (dynamic-extent foo
))
472 (assert (eql x
(foo1-x foo
)))))
475 (x 0.0 :type single-float
)
476 (y 0.0d0
:type double-float
)
481 (defun-with-dx make-foo2-on-stack
(x y
)
482 (declare (inline make-foo2
))
484 (let ((foo (make-foo2 :y y
:c
'c
)))
485 (declare (dynamic-extent foo
))
486 (assert (eql 0.0 (foo2-x foo
)))
487 (assert (eql y
(foo2-y foo
)))
488 (assert (eql 'c
(foo2-c foo
)))
489 (assert (eql nil
(foo2-b foo
)))))
491 ;;; Check that constants work out as argument for all relevant
496 (c 2 :type sb-vm
:word
)
497 (d 3.0 :type single-float
)
498 (e 4.0d0
:type double-float
))
500 (defun-with-dx make-foo3-on-stack
()
501 (declare (inline make-foo3
))
502 (let ((foo (make-foo3)))
503 (declare (dynamic-extent foo
))
504 (assert (eql 0 (foo3-a foo
)))
505 (assert (eql 1 (foo3-b foo
)))
506 (assert (eql 2 (foo3-c foo
)))
507 (assert (eql 3.0 (foo3-d foo
)))
508 (assert (eql 4.0d0
(foo3-e foo
)))))
512 (defun-with-dx nested-dx-lists
()
513 (let ((dx (list (list 1 2) (list 3 4))))
514 (declare (dynamic-extent dx
))
518 (defun-with-dx nested-dx-conses
()
519 (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t
) nil
))))))
520 (declare (dynamic-extent dx
))
524 (defun-with-dx nested-dx-not-used
(x)
526 (let ((l (setf (car x
) (list x x x
))))
527 (declare (dynamic-extent l
))
532 (defun-with-dx nested-evil-dx-used
(x)
534 (let ((l (list x x x
)))
535 (declare (dynamic-extent l
))
543 (defparameter *bar
* nil
)
544 (declaim (inline make-nested-bad make-nested-good
))
545 (defstruct (nested (:constructor make-nested-bad
(&key bar
&aux
(bar (setf *bar
* bar
))))
546 (:constructor make-nested-good
(&key bar
)))
549 (defun-with-dx nested-good
(y)
550 (let ((x (list (list (make-nested-good :bar
(list (list (make-nested-good :bar
(list y
)))))))))
551 (declare (dynamic-extent x
))
554 (defun-with-dx nested-bad
(y)
555 (let ((x (list (list (make-nested-bad :bar
(list (list (make-nested-bad :bar
(list y
)))))))))
556 (declare (dynamic-extent x
))
557 (unless (equalp (caar x
) (make-nested-good :bar
*bar
*))
558 (error "got ~S, wanted ~S" (caar x
) (make-nested-good :bar
*bar
*)))
559 ;; the NESTED instance itself *should* be DX!
560 (copy-nested (caar x
))))
562 (with-test (:name
:conservative-nested-dx
)
563 ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
564 (assert (equalp (nested-bad 42) (make-nested-good :bar
*bar
*)))
565 (assert (equalp *bar
* (list (list (make-nested-bad :bar
(list 42)))))))
567 ;;; multiple uses for dx lvar
569 (defun-with-dx multiple-dx-uses
()
570 (let ((dx (if (true t
)
573 (declare (dynamic-extent dx
))
577 ;;; mapfoo should make the initial cons dx
579 (defun mapcar-negate (x) (mapcar #'- x
))
580 (defun mapcan-reverse (x) (mapcan #'reverse x
))
582 ;;; handler-case and handler-bind should use DX internally
584 (defun dx-handler-bind (x)
585 (handler-bind ((error
587 (break "OOPS: ~S caused ~S" x c
)))
588 ((and serious-condition
(not error
))
590 (break "OOPS2: ~S did ~S" x c
))))
593 (defun dx-handler-case (x)
594 (assert (zerop (handler-case (/ 2 x
)
596 (break "OOPS: ~S caused ~S" x c
)
601 (defun list-delete-some-stuff ()
602 ;; opaque-identity hides the fact that we are calling a destructive function
603 ;; on a constant, which is technically illegal. But no deletion occurs,
604 ;; so it's innocuous. Also these aren't tests of DX, but oh well...
605 (declare (muffle-conditions style-warning
))
606 (delete 'a
(opaque-identity '(x y
)))
607 (delete 'a
(opaque-identity '(x y
)) :from-end t
)
608 (delete-duplicates (opaque-identity '(x y
))))
610 (defvar *a-cons
* (cons nil nil
))
612 (with-test (:name
(:no-consing
:dx-closures
) :skipped-on
'(not :stack-allocatable-closures
))
613 (assert-no-consing (dxclosure 42)))
615 (with-test (:name
(:no-consing
:dx-lists
)
616 :skipped-on
'(not (and :stack-allocatable-lists
617 :stack-allocatable-fixed-objects
)))
618 (assert-no-consing (dxlength 1 2 3))
619 (assert-no-consing (dxlength t t t t t t
))
620 (assert-no-consing (dxlength))
621 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
622 (assert-no-consing (test-nip-values))
623 (assert-no-consing (test-let-var-subst2 17))
624 (assert-no-consing (test-lvar-subst 11))
625 (assert-no-consing (nested-dx-lists))
626 (assert-consing (nested-dx-not-used *a-cons
*))
627 (assert-no-consing (nested-evil-dx-used *a-cons
*))
628 (assert-no-consing (mapcar-negate nil
))
629 (assert-no-consing (mapcan-reverse nil
))
630 (assert-no-consing (list-delete-some-stuff))
631 (assert-no-consing (multiple-dx-uses)))
633 (with-test (:name
(:no-consing
:dx-value-cell
)
634 :skipped-on
'(not :stack-allocatable-closures
))
635 (assert-no-consing (dx-value-cell 13)))
637 (with-test (:name
(:no-consing
:dx-fixed-objects
)
638 :skipped-on
'(not (and :stack-allocatable-fixed-objects
639 :stack-allocatable-closures
)))
640 (assert-no-consing (cons-on-stack 42))
641 (assert-no-consing (make-foo1-on-stack 123))
642 (assert-no-consing (nested-good 42))
643 (assert-no-consing (nested-dx-conses))
644 (assert-no-consing (dx-handler-bind 2))
645 (assert-no-consing (dx-handler-case 2)))
647 (with-test (:name
(:no-consing
:dx-vectors
) :skipped-on
'(not :stack-allocatable-vectors
))
648 (assert-no-consing (force-make-array-on-stack 128))
649 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
650 (assert-no-consing (make-array-on-stack-3 9 8 7))
651 (assert-no-consing (make-array-on-stack-4))
652 (assert-no-consing (make-array-on-stack-5))
653 (assert-no-consing (vector-on-stack :x
:y
)))
655 (with-test (:name
(:no-consing
:specialized-dx-vectors
)
656 :skipped-on
`(not (and :stack-allocatable-vectors
657 :c-stack-is-control-stack
)))
658 (assert-no-consing (make-array-on-stack-1))
659 (assert-no-consing (make-array-on-stack-6))
660 (assert-no-consing (make-array-on-stack-7))
661 (assert-no-consing (make-array-on-stack-8))
662 (assert-no-consing (make-array-on-stack-9))
663 (assert-no-consing (make-array-on-stack-10))
664 (assert-no-consing (make-array-on-stack-11)))
666 (with-test (:name
(:no-consing
:dx-raw-instances
) :skipped-on
'(or (not :raw-instance-init-vops
)
667 (not (and :gencgc
:c-stack-is-control-stack
))))
669 (setf a
1.24 b
1.23d0
)
670 (assert-no-consing (make-foo2-on-stack a b
)))
671 (assert-no-consing (make-foo3-on-stack)))
673 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
675 (defvar *table
* (make-hash-table))
677 (defun test-hash-table ()
678 (setf (gethash 5 *table
*) 13)
681 (with-test (:name
(:no-consing
:hash-tables
))
682 (assert-no-consing (test-hash-table)))
684 ;;; Both with-pinned-objects and without-gcing should not cons
686 (defun call-without-gcing (fun)
687 (sb-sys:without-gcing
(funcall fun
)))
689 (defun call-with-pinned-object (fun obj
)
690 (sb-sys:with-pinned-objects
(obj)
693 (with-test (:name
(:no-consing
:without-gcing
))
694 (assert-no-consing (call-without-gcing (lambda ()))))
696 (with-test (:name
(:no-consing
:with-pinned-objects
))
697 (assert-no-consing (call-with-pinned-object #'identity
42)))
699 ;;; with-mutex should use DX and not cons
701 (defvar *mutex
* (sb-thread::make-mutex
:name
"mutexlock"))
704 (sb-thread:with-mutex
(*mutex
*)
707 (with-test (:name
(:no-consing
:mutex
) :skipped-on
'(not :sb-thread
))
708 (assert-no-consing (test-mutex)))
711 ;;; Bugs found by Paul F. Dietz
713 (with-test (:name
(:dx-bug-misc
:pfdietz
))
720 (declare (optimize (speed 2) (space 0) (safety 0)
721 (debug 1) (compilation-speed 3)))
722 (let* ((v5 (cons b b
)))
723 (declare (dynamic-extent v5
))
729 ;;; bug reported by Svein Ove Aas
730 (defun svein-2005-ii-07 (x y
)
731 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
732 (let ((args (list* y
1 2 x
)))
733 (declare (dynamic-extent args
))
734 (apply #'aref args
)))
736 (with-test (:name
(:dx-bugs-misc
:svein-2005-ii-07
))
740 #3A
(((1 1 1) (1 1 1) (1 1 1))
741 ((1 1 1) (1 1 1) (4 1 1))
742 ((1 1 1) (1 1 1) (1 1 1))))
745 ;;; bug reported by Brian Downing: stack-allocated arrays were not
746 ;;; filled with zeroes.
747 (defun-with-dx bdowning-2005-iv-16
()
748 (let ((a (make-array 11 :initial-element
0)))
749 (declare (dynamic-extent a
))
750 (assert (every (lambda (x) (eql x
0)) a
))))
752 (with-test (:name
(:dx-bug-misc
:bdowning-2005-iv-16
))
753 #+(or hppa mips x86 x86-64
)
754 (assert-no-consing (bdowning-2005-iv-16))
755 (bdowning-2005-iv-16))
757 (declaim (inline my-nconc
))
758 (defun my-nconc (&rest lists
)
759 (declare (dynamic-extent lists
))
760 (apply #'nconc lists
))
761 (defun-with-dx my-nconc-caller
(a b c
)
762 (let ((l1 (list a b c
))
765 (with-test (:name
:rest-stops-the-buck
)
766 (let ((list1 (my-nconc-caller 1 2 3))
767 (list2 (my-nconc-caller 9 8 7)))
768 (assert (equal list1
'(1 2 3 1 2 3)))
769 (assert (equal list2
'(9 8 7 9 8 7)))))
771 (defun-with-dx let-converted-vars-dx-allocated-bug
(x y z
)
772 (let* ((a (list x y z
))
775 (declare (dynamic-extent c
))
776 (values (first c
) (second c
))))
777 (with-test (:name
:let-converted-vars-dx-allocated-bug
)
778 (multiple-value-bind (i j
) (let-converted-vars-dx-allocated-bug 1 2 3)
779 (assert (and (equal i j
)
780 (equal i
(list 1 2 3))))))
782 ;;; workaround for bug 419 -- real issue remains, but check that the
784 (defun-with-dx bug419
(x)
785 (multiple-value-call #'list
786 (eval '(values 1 2 3))
788 (declare (dynamic-extent x
))
793 (declare (dynamic-extent #'mget
#'mset
))
794 ((lambda (f g
) (eval `(progn ,f
,g
(values 4 5 6)))) #'mget
#'mset
)))))
796 (with-test (:name
(:dx-bug-misc
:bug419
))
797 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
799 ;;; Multiple DX arguments in a local function call
800 (defun test-dx-flet-test (fun n f1 f2 f3
)
801 (let ((res (with-output-to-string (s)
802 (assert (eql n
(ignore-errors (funcall fun s
)))))))
803 (multiple-value-bind (x pos
) (read-from-string res nil
)
804 (assert (equalp f1 x
))
805 (multiple-value-bind (y pos2
) (read-from-string res nil nil
:start pos
)
806 (assert (equalp f2 y
))
807 (assert (equalp f3
(read-from-string res nil nil
:start pos2
))))))
808 #+(or hppa mips x86 x86-64
)
809 (assert-no-consing (assert (eql n
(funcall fun nil
))))
810 (assert (eql n
(funcall fun nil
))))
812 (macrolet ((def (n f1 f2 f3
)
813 (let ((name (sb-pcl::format-symbol
:cl-user
"DX-FLET-TEST.~A" n
)))
815 (defun-with-dx ,name
(s)
817 (declare (dynamic-extent x
))
826 (with-test (:name
(:dx-flet-test
,n
))
827 (test-dx-flet-test #',name
,n
,f1
,f2
,f3
))))))
828 (def 0 (list :one
) (list :two
) (list :three
))
829 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list
))
830 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
832 ;;; Test that unknown-values coming after a DX value won't mess up the
834 (defun test-update-uvl-live-sets (x y z
)
835 (declare (optimize speed
(safety 0)))
837 (declare (dynamic-extent a
))
838 (eval `(list (length ',a
) ',b
))))
840 (bar (list x y z
) ; dx push
842 (multiple-value-call 'list
843 (eval '(values 1 2 3)) ; uv push
849 (with-test (:name
(:update-uvl-live-sets
))
850 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
851 (test-update-uvl-live-sets #() 4 5))))
853 (with-test (:name
:regression-1.0
.23.38)
854 (compile nil
'(lambda ()
855 (declare (muffle-conditions compiler-note
))
857 (let ((res (cons x x
)))
860 (declare (inline make
))
861 (let ((z (make 1 2)))
862 (declare (dynamic-extent z
))
865 (compile nil
'(lambda ()
866 (declare (muffle-conditions compiler-note
))
868 (let ((res (cons x x
)))
871 (declare (inline make
))
872 (let ((z (make 1 2)))
873 (declare (dynamic-extent z
))
877 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
878 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
879 ;;; location, leading to all manner of badness. ...reproducing this
880 ;;; reliably is hard, but this it at least used to break on x86-64.
881 (defun length-and-words-packed-in-same-tn (m)
882 (declare (optimize speed
(safety 0) (debug 0) (space 0)))
883 (let ((array (make-array (max 1 m
) :element-type
'fixnum
)))
884 (declare (dynamic-extent array
))
885 (array-total-size array
)))
886 (with-test (:name
:length-and-words-packed-in-same-tn
)
887 (assert (= 1 (length-and-words-packed-in-same-tn -
3))))
889 (with-test (:name
:handler-case-bogus-compiler-note
890 :skipped-on
'(not (and :stack-allocatable-fixed-objects
891 :stack-allocatable-closures
)))
893 ((compiler-note (lambda (note)
894 (error "compiler issued note ~S during test" note
))))
895 ;; Taken from SWANK, used to signal a bogus stack allocation
898 `(lambda (files fasl-dir load
)
899 (declare (muffle-conditions style-warning
))
900 (let ((needs-recompile nil
))
902 (let ((dest (binary-pathname src fasl-dir
)))
905 (when (or needs-recompile
906 (not (probe-file dest
))
907 (file-newer-p src dest
))
908 (setq needs-recompile t
)
909 (ensure-directories-exist dest
)
910 (compile-file src
:output-file dest
:print nil
:verbose t
))
912 (load dest
:verbose t
)))
913 (serious-condition (c)
914 (handle-loadtime-error c dest
))))))))))
916 (declaim (inline foovector barvector
))
917 (defun foovector (x y z
)
918 (let ((v (make-array 3)))
923 (defun barvector (x y z
)
924 (make-array 3 :initial-contents
(list x y z
)))
925 (with-test (:name
:dx-compiler-notes
926 :skipped-on
'(not (and :stack-allocatable-vectors
927 :stack-allocatable-closures
)))
928 (flet ((assert-notes (j lambda
)
930 (handler-bind ((compiler-note (lambda (c)
933 (let ((*error-output
* (make-broadcast-stream)))
934 (compile nil lambda
))
936 (error "Wanted ~S notes, got ~S for~% ~S"
938 ;; These ones should complain.
939 (assert-notes 1 `(lambda (x)
940 (let ((v (make-array x
)))
941 (declare (dynamic-extent v
))
943 (assert-notes 2 `(lambda (x)
944 (let ((y (if (plusp x
)
947 (declare (dynamic-extent y
))
950 (assert-notes 1 `(lambda (x)
951 (let ((y (foovector x x x
)))
952 (declare (sb-int:truly-dynamic-extent y
))
955 ;; These ones should not complain.
956 (assert-notes 0 `(lambda (name)
958 ((posix-getenv (function c-string c-string
)
961 (alien-funcall posix-getenv name
)))))
962 (assert-notes 0 `(lambda (x)
963 (let ((y (barvector x x x
)))
964 (declare (dynamic-extent y
))
967 (assert-notes 0 `(lambda (list)
968 (declare (optimize (space 0)))
969 (sort list
(lambda (x y
) ; shut unrelated notes up
970 (< (truly-the fixnum x
)
971 (truly-the fixnum y
))))))
972 (assert-notes 0 `(lambda (other)
974 (ignore-errors (funcall other s c n
)))))))
976 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
977 ;;; in an unfortunate loop.
978 (defun handler-case-eating-stack ()
979 (declare (muffle-conditions warning
)) ; "dead code detected ... IR1-PHASES"
981 (do ((n 0 (logand most-positive-fixnum
(1+ n
))))
983 (multiple-value-bind (value error
) (ignore-errors)
984 (when (and value error
) nil
))
986 (assert (= sp
(sb-c::%primitive sb-c
:current-stack-pointer
)))
987 (setf sp
(sb-c::%primitive sb-c
:current-stack-pointer
))))))
988 (with-test (:name
:handler-case-eating-stack
989 :skipped-on
'(not (and :stack-allocatable-fixed-objects
990 :stack-allocatable-closures
)))
991 (assert-no-consing (handler-case-eating-stack)))
993 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
994 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
995 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
997 `(simple-array t
(3)))
998 (declaim (ftype (function (t t t
) vec
) vec
))
999 (declaim (inline vec
))
1001 (make-array 3 :initial-contents
(list a b c
)))
1002 (defun bad-boy (vec)
1003 (declare (type vec vec
))
1005 (let ((vec (vec (aref vec
0) (aref vec
1) (aref vec
2))))
1006 (declare (dynamic-extent vec
))
1007 (funcall fun vec
))))
1008 (with-test (:name
:recheck-nested-dx-bug
1009 :skipped-on
'(not :stack-allocatable-vectors
))
1010 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
1011 (lambda (vec) (equalp vec
(vec 1.0 2.0 3.3)))))
1012 (flet ((foo (x) (declare (ignore x
))))
1013 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
1014 (assert-no-consing (funcall bad-boy
#'foo
)))))
1016 (with-test (:name
:bug-497321
)
1017 (flet ((test (lambda type
)
1019 (handler-bind ((condition (lambda (c)
1021 (unless (typep c type
)
1022 (error "wanted ~S for~% ~S~%got ~S"
1023 type lambda
(type-of c
))))))
1024 (let ((*error-output
* (make-broadcast-stream)))
1025 (compile nil lambda
)))
1027 (test `(lambda () (declare (dynamic-extent #'bar
)))
1029 (test `(lambda () (declare (dynamic-extent bar
)))
1031 (test `(lambda (bar) (cons bar
(lambda () (declare (dynamic-extent bar
)))))
1032 'sb-ext
:compiler-note
)
1035 (cons #'bar
(lambda () (declare (dynamic-extent #'bar
))))))
1036 'sb-ext
:compiler-note
)))
1038 (with-test (:name
:bug-586105
1039 :skipped-on
'(not (and :stack-allocatable-vectors
1040 :stack-allocatable-lists
)))
1042 (let ((vec1 (make-array 1 :initial-contents
(list (list x
))))
1043 (vec2 (make-array 1 :initial-contents
`((,x
))))
1044 (vec3 (make-array 1 :initial-contents
`#((,x
))))
1045 (vec4 (make-array 1 :initial-contents
`(#(,x
)))))
1046 (declare (dynamic-extent vec1 vec2 vec3 vec4
))
1047 (assert (eql x
(car (aref vec1
0))))
1048 (assert (eql x
(car (aref vec2
0))))
1049 (assert (eql x
(car (aref vec3
0))))
1050 (assert (eql x
(elt (aref vec4
0) 0))))))
1051 (assert-no-consing (test 42))))
1053 (defun bug-681092 ()
1054 (declare (optimize speed
))
1057 (declare (dynamic-extent #'bar
))
1058 (do () ((list) (bar))
1061 (with-test (:name
:bug-681092
)
1062 (assert (= 10 (bug-681092))))
1064 ;;;; Including a loop in the flow graph between a DX-allocation and
1065 ;;;; the start of its environment would, for a while, cause executing
1066 ;;;; any of the code in the loop to discard the value. Found via
1067 ;;;; attempting to DX-allocate an array, which triggered the FILL
1068 ;;;; transform, which inserted such a loop.
1069 (defun bug-1472785 (x)
1070 (let ((y (let ((z (cons nil nil
)))
1074 (when (= x i
) (go b2
))
1080 (declare (dynamic-extent y w
))
1082 (with-test (:name
:bug-1472785
)
1083 (assert (null (bug-1472785 1))))
1085 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
1086 ;;;; but required by sanity.
1088 (declaim (inline rest-stops-dx
))
1089 (defun-with-dx rest-stops-dx
(&rest args
)
1090 (declare (dynamic-extent args
))
1091 (apply #'opaque-identity args
))
1093 (defun-with-dx rest-stops-dx-ok
()
1094 (equal '(:foo
) (rest-stops-dx (list :foo
))))
1096 (with-test (:name
:rest-stops-dynamic-extent
)
1097 (assert (rest-stops-dx-ok)))
1099 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
1101 (with-test (:name
:rest-to-more-conversion
)
1102 (let ((f1 (compile nil
`(lambda (f &rest args
)
1104 (assert-no-consing (assert (eql f1
(funcall f1
#'identity f1
)))))
1105 (let ((f2 (compile nil
`(lambda (f1 f2
&rest args
)
1106 (values (apply f1 args
) (apply f2 args
))))))
1107 (assert-no-consing (multiple-value-bind (a b
)
1108 (funcall f2
(lambda (x y z
) (+ x y z
)) (lambda (x y z
) (- x y z
))
1110 (assert (and (eql 6 a
) (eql -
4 b
))))))
1111 (let ((f3 (compile nil
`(lambda (f &optional x
&rest args
)
1113 (apply f x args
))))))
1114 (assert-no-consing (assert (eql 42 (funcall f3
1115 (lambda (a b c
) (+ a b c
))
1120 (let ((*error-output
* (make-broadcast-stream)))
1121 (compile nil
`(lambda (f &optional x
&rest args
1122 &key y
&allow-other-keys
)
1123 (apply f y x args
))))))
1124 (assert-no-consing (funcall f4
(lambda (y x yk y2 b c
)
1132 (let ((f5 (compile nil
`(lambda (a b c
&rest args
)
1133 (apply #'list
* a b c args
)))))
1134 (assert (equal '(1 2 3 4 5 6 7) (funcall f5
1 2 3 4 5 6 '(7)))))
1135 (let ((f6 (compile nil
`(lambda (x y
)
1136 (declare (optimize speed
))
1137 (concatenate 'string x y
)))))
1138 (assert (equal "foobar" (funcall f6
"foo" "bar"))))
1139 (let ((f7 (compile nil
`(lambda (&rest args
)
1142 (assert (equal '(a b c d e f
) (funcall (funcall f7
'a
'b
'c
'd
'e
'f
) 'list
))))
1143 (let ((f8 (compile nil
`(lambda (&rest args
)
1147 (assert (equal '(a b c d e f
) (funcall (funcall f8
'a
'b
'c
'd
'e
'f
) 'list
))))
1148 (let ((f9 (compile nil
`(lambda (f &rest args
)
1151 (declare (dynamic-extent #'foo
))
1152 (funcall f
#'foo
))))))
1153 (assert (equal '(a b c d e f
)
1154 (funcall f9
(lambda (f) (funcall f
'list
)) 'a
'b
'c
'd
'e
'f
))))
1155 (let ((f10 (compile nil
`(lambda (f &rest args
)
1158 (funcall f
#'foo
))))))
1159 (assert (equal '(a b c d e f
)
1160 (funcall f10
(lambda (f) (funcall f
'list
)) 'a
'b
'c
'd
'e
'f
))))
1161 (let ((f11 (compile nil
`(lambda (x y z
)
1163 (labels ((foo (x &rest rest
)
1164 (apply (lambda (&rest rest2
)
1165 (return-from out
(values-list rest2
)))
1170 (multiple-value-bind (a b c
) (funcall f11
1 2 3)
1173 (assert (eql c
3)))))
1175 (defun opaque-funcall (function &rest arguments
)
1176 (apply function arguments
))
1178 (with-test (:name
:implicit-value-cells
)
1179 (flet ((test-it (type input output
)
1180 (let ((f (compile nil
`(lambda (x)
1181 (declare (type ,type x
))
1184 (declare (dynamic-extent #'inc
))
1185 (list (opaque-funcall #'inc
) x
))))))
1186 (assert (equal (funcall f input
)
1187 (list output output
))))))
1188 (let ((width sb-vm
:n-word-bits
))
1189 (test-it t
(1- most-positive-fixnum
) most-positive-fixnum
)
1190 (test-it `(unsigned-byte ,(1- width
)) (ash 1 (- width
2)) (1+ (ash 1 (- width
2))))
1191 (test-it `(signed-byte ,width
) (ash -
1 (- width
2)) (1+ (ash -
1 (- width
2))))
1192 (test-it `(unsigned-byte ,width
) (ash 1 (1- width
)) (1+ (ash 1 (1- width
))))
1193 (test-it 'single-float
3f0
4f0
)
1194 (test-it 'double-float
3d0
4d0
)
1195 (test-it '(complex single-float
) #c
(3f0 4f0
) #c
(4f0 4f0
))
1196 (test-it '(complex double-float
) #c
(3d0 4d0
) #c
(4d0 4d0
)))))
1198 (with-test (:name
:sap-implicit-value-cells
)
1199 (let ((f (compile nil
`(lambda (x)
1200 (declare (type system-area-pointer x
))
1202 (setf x
(sb-sys:sap
+ x
16))))
1203 (declare (dynamic-extent #'inc
))
1204 (list (opaque-funcall #'inc
) x
)))))
1205 (width sb-vm
:n-machine-word-bits
))
1206 (assert (every (lambda (x)
1207 (sb-sys:sap
= x
(sb-sys:int-sap
(+ 16 (ash 1 (1- width
))))))
1208 (funcall f
(sb-sys:int-sap
(ash 1 (1- width
))))))))
1210 (with-test (:name
:&more-bounds
)
1212 (assert (not (funcall (compile nil
'(lambda (&rest args
) (car args
))))))
1213 (assert (not (funcall (compile nil
'(lambda (&rest args
) (nth 6 args
))))))
1214 (assert (not (funcall (compile nil
'(lambda (&rest args
) (elt args
10))))))
1215 (assert (not (funcall (compile nil
'(lambda (&rest args
) (cadr args
))))))
1216 (assert (not (funcall (compile nil
'(lambda (&rest args
) (third args
)))))))