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
)
24 (declare (optimize (speed 3) (safety 0) (space 0)))
25 (declare (muffle-conditions style-warning
)) ; re F1 and F2
27 (progn (ignore-errors (error "Foo")) (write-char #\.
))
29 (use-value (new) (f2 new
))))
32 (with-test (:name
:do-not-dxify-restarts
)
33 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
34 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
35 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
36 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
39 (defmacro defun-with-dx
(name arglist
&body body
)
40 (let ((debug-name (sb-int:symbolicate name
"-HIGH-DEBUG"))
41 (default-name (sb-int:symbolicate name
"-DEFAULT")))
43 (defun ,debug-name
,arglist
44 (declare (optimize debug
))
46 (defun ,default-name
,arglist
48 (defun ,name
(&rest args
)
49 (apply #',debug-name args
)
50 (apply #',default-name args
)))))
52 (declaim (notinline opaque-identity
))
53 (defun opaque-identity (x)
58 (defun-with-dx dxlength
(&rest rest
)
59 (declare (dynamic-extent rest
))
62 (with-test (:name
(:dx-
&rest
:basics
))
63 (assert (= (dxlength 1 2 3) 3))
64 (assert (= (dxlength t t t t t t
) 6))
65 (assert (= (dxlength) 0)))
68 (destructuring-bind (a b c d e f
&rest g
) list
69 (+ a b c d e f
(length g
))))
71 (defun-with-dx dxcaller
(&rest rest
)
72 (declare (dynamic-extent rest
))
75 (with-test (:name
(:dx-
&rest
:pass-down-to-callee
:tail-call
))
76 (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
78 (defun-with-dx dxcaller-align-1
(x &rest rest
)
79 (declare (dynamic-extent rest
))
82 (with-test (:name
(:dx-
&rest
:pass-down-to-callee
:non-tail-call
))
83 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
84 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
88 (defun-with-dx test-nip-values
()
89 (flet ((bar (x &rest y
)
90 (declare (dynamic-extent y
))
94 (multiple-value-call #'values
98 (with-test (:name
(:nip-values
))
99 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a
))))
101 ;;; LET-variable substitution
103 (defun-with-dx test-let-var-subst1
(x)
104 (let ((y (list x
(1- x
))))
105 (opaque-identity :foo
)
106 (let ((z (the list y
)))
107 (declare (dynamic-extent z
))
110 (with-test (:name
(:let-variable-substitution
))
111 (assert (eql (test-let-var-subst1 17) 2)))
113 (defun-with-dx test-let-var-subst2
(x)
114 (let ((y (list x
(1- x
))))
115 (declare (dynamic-extent y
))
116 (opaque-identity :foo
)
117 (let ((z (the list y
)))
120 (with-test (:name
(:let-variable-substitution-2
))
121 (assert (eql (test-let-var-subst2 17) 2)))
124 ;;; DX propagation through LET-return.
126 (defun-with-dx test-lvar-subst
(x)
127 (let ((y (list x
(1- x
))))
128 (declare (dynamic-extent y
))
129 (second (let ((z (the list y
)))
130 (opaque-identity :foo
)
133 (with-test (:name
(:dx-propagation-through-let-return
))
134 (assert (eql (test-lvar-subst 11) 10)))
136 ;;; this code is incorrect, but the compiler should not fail
137 (defun-with-dx test-let-var-subst-incorrect
(x)
138 (let ((y (list x
(1- x
))))
139 (opaque-identity :foo
)
140 (let ((z (the list y
)))
141 (declare (dynamic-extent z
))
142 (opaque-identity :bar
)
148 (defun-with-dx test-alignment-dx-list
(form)
149 (multiple-value-prog1 (eval form
)
150 (let ((l (list 1 2 3 4)))
151 (declare (dynamic-extent l
))
152 (setq *x
* (copy-list l
)))))
154 (with-test (:name
(:dx-list
:alignment
))
156 (let* ((res (loop for i below n collect i
))
157 (form `(values ,@res
)))
158 (assert (equal (multiple-value-list (test-alignment-dx-list form
)) res
))
159 (assert (equal *x
* '(1 2 3 4))))))
163 (declaim (notinline true
))
168 (defun-with-dx dxclosure
(x)
171 (declare (dynamic-extent #'f
))
174 (with-test (:name
(:dx-closure
))
175 (assert (eq t
(dxclosure 13))))
179 (defun-with-dx dx-value-cell
(x)
180 ;; Not implemented everywhere, yet.
181 #+(or x86 x86-64 mips hppa
)
183 (declare (sb-int:truly-dynamic-extent cell
))
186 (declare (dynamic-extent #'f
))
191 (defun-with-dx cons-on-stack
(x)
192 (let ((cons (cons x x
)))
193 (declare (dynamic-extent cons
))
199 (defun force-make-array-on-stack (n)
200 (declare (optimize safety
))
201 (let ((v (make-array (min n
1))))
202 (declare (sb-int:truly-dynamic-extent v
))
207 (defun-with-dx make-array-on-stack-1
()
208 (let ((v (make-array '(42) :element-type
'single-float
)))
209 (declare (dynamic-extent v
))
214 (defun-with-dx make-array-on-stack-2
(n x
)
215 (declare (integer n
))
216 (let ((v (make-array n
:initial-contents x
)))
217 (declare (sb-int:truly-dynamic-extent v
))
222 (defun-with-dx make-array-on-stack-3
(x y z
)
223 (let ((v (make-array 3
224 :element-type
'fixnum
:initial-contents
(list x y z
)
225 :element-type t
:initial-contents x
)))
226 (declare (sb-int:truly-dynamic-extent v
))
231 (defun-with-dx make-array-on-stack-4
()
232 (let ((v (make-array 3 :initial-contents
'(1 2 3))))
233 (declare (sb-int:truly-dynamic-extent v
))
238 (defun-with-dx make-array-on-stack-5
()
239 (let ((v (make-array 3 :initial-element
12 :element-type t
)))
240 (declare (sb-int:truly-dynamic-extent v
))
245 (defun-with-dx make-array-on-stack-6
()
246 (let ((v (make-array 3 :initial-element
12 :element-type
'(unsigned-byte 8))))
247 (declare (sb-int:truly-dynamic-extent v
))
252 (defun-with-dx make-array-on-stack-7
()
253 (let ((v (make-array 3 :initial-element
12 :element-type
'(signed-byte 8))))
254 (declare (sb-int:truly-dynamic-extent v
))
259 (defun-with-dx make-array-on-stack-8
()
260 (let ((v (make-array 3 :initial-element
12 :element-type
'word
)))
261 (declare (sb-int:truly-dynamic-extent v
))
266 (defun-with-dx make-array-on-stack-9
()
267 (let ((v (make-array 3 :initial-element
12.0 :element-type
'single-float
)))
268 (declare (sb-int:truly-dynamic-extent v
))
273 (defun-with-dx make-array-on-stack-10
()
274 (let ((v (make-array 3 :initial-element
12.0d0
:element-type
'double-float
)))
275 (declare (sb-int:truly-dynamic-extent v
))
280 (defun-with-dx make-array-on-stack-11
()
281 (let ((v (make-array (the integer
(opaque-identity 3)) :initial-element
12.0d0
:element-type
'double-float
)))
282 (declare (sb-int:truly-dynamic-extent v
))
287 (defun-with-dx vector-on-stack
(x y
)
288 (let ((v (vector 1 x
2 y
3)))
289 (declare (sb-int:truly-dynamic-extent v
))
295 (declaim (inline make-list-container
))
296 (defstruct list-container listy-slot
)
297 (defun make-var-length-dx-list (n thunk
)
298 (sb-int:dx-let
((s (make-list-container :listy-slot
(make-list n
))))
300 ;; :stack-allocatable-lists is necessary but not sufficient
301 (with-test (:name
(:dx-list
:make-list
) :skipped-on
'(not :x86-64
))
302 (assert (null (ctu:find-named-callees
#'make-var-length-dx-list
)))
303 (assert-no-consing (make-var-length-dx-list
304 50 (lambda (x) (declare (ignore x
))))))
308 (declaim (inline make-fp-struct-1
))
309 (defstruct fp-struct-1
310 (s 0.0 :type single-float
)
311 (d 0.0d0
:type double-float
))
313 (defun-with-dx test-fp-struct-1.1
(s d
)
315 (let ((fp (make-fp-struct-1 :s s
)))
316 (declare (dynamic-extent fp
))
317 (assert (eql s
(fp-struct-1-s fp
)))
318 (assert (eql 0.0d0
(fp-struct-1-d fp
)))))
320 (defun-with-dx test-fp-struct-1.2
(s d
)
322 (let ((fp (make-fp-struct-1 :d d
)))
323 (declare (dynamic-extent fp
))
324 (assert (eql 0.0 (fp-struct-1-s fp
)))
325 (assert (eql d
(fp-struct-1-d fp
)))))
327 (defun-with-dx test-fp-struct-1.3
(s d
)
328 (let ((fp (make-fp-struct-1 :d d
:s s
)))
329 (declare (dynamic-extent fp
))
330 (assert (eql s
(fp-struct-1-s fp
)))
331 (assert (eql d
(fp-struct-1-d fp
)))))
333 (defun-with-dx test-fp-struct-1.4
(s d
)
334 (let ((fp (make-fp-struct-1 :s s
:d d
)))
335 (declare (dynamic-extent fp
))
336 (assert (eql s
(fp-struct-1-s fp
)))
337 (assert (eql d
(fp-struct-1-d fp
)))))
339 (with-test (:name
(:test-fp-struct-1.1
))
340 (test-fp-struct-1.1
123.456 876.243d0
))
341 (with-test (:name
(:test-fp-struct-1.2
))
342 (test-fp-struct-1.2
123.456 876.243d0
))
343 (with-test (:name
(:test-fp-struct-1.3
))
344 (test-fp-struct-1.3
123.456 876.243d0
))
345 (with-test (:name
(:test-fp-struct-1.4
))
346 (test-fp-struct-1.4
123.456 876.243d0
))
348 (declaim (inline make-fp-struct-2
))
349 (defstruct fp-struct-2
350 (d 0.0d0
:type double-float
)
351 (s 0.0 :type single-float
))
353 (defun-with-dx test-fp-struct-2.1
(s d
)
355 (let ((fp (make-fp-struct-2 :s s
)))
356 (declare (dynamic-extent fp
))
357 (assert (eql s
(fp-struct-2-s fp
)))
358 (assert (eql 0.0d0
(fp-struct-2-d fp
)))))
360 (defun-with-dx test-fp-struct-2.2
(s d
)
362 (let ((fp (make-fp-struct-2 :d d
)))
363 (declare (dynamic-extent fp
))
364 (assert (eql 0.0 (fp-struct-2-s fp
)))
365 (assert (eql d
(fp-struct-2-d fp
)))))
367 (defun-with-dx test-fp-struct-2.3
(s d
)
368 (let ((fp (make-fp-struct-2 :d d
:s s
)))
369 (declare (dynamic-extent fp
))
370 (assert (eql s
(fp-struct-2-s fp
)))
371 (assert (eql d
(fp-struct-2-d fp
)))))
373 (defun-with-dx test-fp-struct-2.4
(s d
)
374 (let ((fp (make-fp-struct-2 :s s
:d d
)))
375 (declare (dynamic-extent fp
))
376 (assert (eql s
(fp-struct-2-s fp
)))
377 (assert (eql d
(fp-struct-2-d fp
)))))
379 (with-test (:name
(:test-fp-struct-2.1
))
380 (test-fp-struct-2.1
123.456 876.243d0
))
381 (with-test (:name
(:test-fp-struct-2.2
))
382 (test-fp-struct-2.2
123.456 876.243d0
))
383 (with-test (:name
(:test-fp-struct-2.3
))
384 (test-fp-struct-2.3
123.456 876.243d0
))
385 (with-test (:name
(:test-fp-struct-2.4
))
386 (test-fp-struct-2.4
123.456 876.243d0
))
388 (declaim (inline make-cfp-struct-1
))
389 (defstruct cfp-struct-1
390 (s (complex 0.0) :type
(complex single-float
))
391 (d (complex 0.0d0
) :type
(complex double-float
)))
393 (defun-with-dx test-cfp-struct-1.1
(s d
)
395 (let ((cfp (make-cfp-struct-1 :s s
)))
396 (declare (dynamic-extent cfp
))
397 (assert (eql s
(cfp-struct-1-s cfp
)))
398 (assert (eql (complex 0.0d0
) (cfp-struct-1-d cfp
)))))
400 (defun-with-dx test-cfp-struct-1.2
(s d
)
402 (let ((cfp (make-cfp-struct-1 :d d
)))
403 (declare (dynamic-extent cfp
))
404 (assert (eql (complex 0.0) (cfp-struct-1-s cfp
)))
405 (assert (eql d
(cfp-struct-1-d cfp
)))))
407 (defun-with-dx test-cfp-struct-1.3
(s d
)
408 (let ((cfp (make-cfp-struct-1 :d d
:s s
)))
409 (declare (dynamic-extent cfp
))
410 (assert (eql s
(cfp-struct-1-s cfp
)))
411 (assert (eql d
(cfp-struct-1-d cfp
)))))
413 (defun-with-dx test-cfp-struct-1.4
(s d
)
414 (let ((cfp (make-cfp-struct-1 :s s
:d d
)))
415 (declare (dynamic-extent cfp
))
416 (assert (eql s
(cfp-struct-1-s cfp
)))
417 (assert (eql d
(cfp-struct-1-d cfp
)))))
419 (with-test (:name
(:test-cfp-struct-1.1
))
420 (test-cfp-struct-1.1
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
421 (with-test (:name
(:test-cfp-struct-1.2
))
422 (test-cfp-struct-1.2
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
423 (with-test (:name
(:test-cfp-struct-1.3
))
424 (test-cfp-struct-1.3
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
425 (with-test (:name
(:test-cfp-struct-1.4
))
426 (test-cfp-struct-1.4
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
428 (declaim (inline make-cfp-struct-2
))
429 (defstruct cfp-struct-2
430 (d (complex 0.0d0
) :type
(complex double-float
))
431 (s (complex 0.0) :type
(complex single-float
)))
433 (defun-with-dx test-cfp-struct-2.1
(s d
)
435 (let ((cfp (make-cfp-struct-2 :s s
)))
436 (declare (dynamic-extent cfp
))
437 (assert (eql s
(cfp-struct-2-s cfp
)))
438 (assert (eql (complex 0.0d0
) (cfp-struct-2-d cfp
)))))
440 (defun-with-dx test-cfp-struct-2.2
(s d
)
442 (let ((cfp (make-cfp-struct-2 :d d
)))
443 (declare (dynamic-extent cfp
))
444 (assert (eql (complex 0.0) (cfp-struct-2-s cfp
)))
445 (assert (eql d
(cfp-struct-2-d cfp
)))))
447 (defun-with-dx test-cfp-struct-2.3
(s d
)
448 (let ((cfp (make-cfp-struct-2 :d d
:s s
)))
449 (declare (dynamic-extent cfp
))
450 (assert (eql s
(cfp-struct-2-s cfp
)))
451 (assert (eql d
(cfp-struct-2-d cfp
)))))
453 (defun-with-dx test-cfp-struct-2.4
(s d
)
454 (let ((cfp (make-cfp-struct-2 :s s
:d d
)))
455 (declare (dynamic-extent cfp
))
456 (assert (eql s
(cfp-struct-2-s cfp
)))
457 (assert (eql d
(cfp-struct-2-d cfp
)))))
459 (with-test (:name
(:test-cfp-struct-2.1
))
460 (test-cfp-struct-2.1
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
461 (with-test (:name
(:test-cfp-struct-2.2
))
462 (test-cfp-struct-2.2
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
463 (with-test (:name
(:test-cfp-struct-2.3
))
464 (test-cfp-struct-2.3
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
465 (with-test (:name
(:test-cfp-struct-2.4
))
466 (test-cfp-struct-2.4
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
468 ;; It works to declare a structure constructor INLINE after the DEFSTRUCT
469 ;; was processed, as long as it was in the null lexical environment.
470 ;; In a perfect world, we'd figure out that a variable declared DX which
471 ;; receives the value of a structure constructor defined in the same file
472 ;; and neither expressly INLINE nor NOTINLINE should be locally inlined.
473 ;; But at least this shows that a declaration at the intended use point
474 ;; is sufficient, without also a bracketing INLINE/NOTINLINE at the DEFSTRUCT.
476 ;; Verify the precondition for the assertions that claim that DXifying works
477 ;; even though the MAKE- function was not expressly INLINE when its DEFSTRUCT
479 (dolist (s '(make-foo1 make-foo2 make-foo3
))
480 (assert (null (sb-int:info
:function
:inline-expansion-designator s
))))
484 (defun-with-dx make-foo1-on-stack
(x)
485 (declare (inline make-foo1
))
486 (let ((foo (make-foo1 :x x
)))
487 (declare (dynamic-extent foo
))
488 (assert (eql x
(foo1-x foo
)))))
491 (x 0.0 :type single-float
)
492 (y 0.0d0
:type double-float
)
497 (defun-with-dx make-foo2-on-stack
(x y
)
498 (declare (inline make-foo2
))
500 (let ((foo (make-foo2 :y y
:c
'c
)))
501 (declare (dynamic-extent foo
))
502 (assert (eql 0.0 (foo2-x foo
)))
503 (assert (eql y
(foo2-y foo
)))
504 (assert (eql 'c
(foo2-c foo
)))
505 (assert (eql nil
(foo2-b foo
)))))
507 ;;; Check that constants work out as argument for all relevant
512 (c 2 :type sb-vm
:word
)
513 (d 3.0 :type single-float
)
514 (e 4.0d0
:type double-float
))
516 (defun-with-dx make-foo3-on-stack
()
517 (declare (inline make-foo3
))
518 (let ((foo (make-foo3)))
519 (declare (dynamic-extent foo
))
520 (assert (eql 0 (foo3-a foo
)))
521 (assert (eql 1 (foo3-b foo
)))
522 (assert (eql 2 (foo3-c foo
)))
523 (assert (eql 3.0 (foo3-d foo
)))
524 (assert (eql 4.0d0
(foo3-e foo
)))))
528 (defun-with-dx nested-dx-lists
()
529 (let ((dx (list (list 1 2) (list 3 4))))
530 (declare (dynamic-extent dx
))
534 (defun-with-dx nested-dx-conses
()
535 (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t
) nil
))))))
536 (declare (dynamic-extent dx
))
540 (defun-with-dx nested-dx-not-used
(x)
542 (let ((l (setf (car x
) (list x x x
))))
543 (declare (dynamic-extent l
))
548 (defun-with-dx nested-evil-dx-used
(x)
550 (let ((l (list x x x
)))
551 (declare (dynamic-extent l
))
559 (defparameter *bar
* nil
)
560 (declaim (inline make-nested-bad make-nested-good
))
561 (defstruct (nested (:constructor make-nested-bad
(&key bar
&aux
(bar (setf *bar
* bar
))))
562 (:constructor make-nested-good
(&key bar
)))
565 (defun-with-dx nested-good
(y)
566 (let ((x (list (list (make-nested-good :bar
(list (list (make-nested-good :bar
(list y
)))))))))
567 (declare (dynamic-extent x
))
570 (defun-with-dx nested-bad
(y)
571 (let ((x (list (list (make-nested-bad :bar
(list (list (make-nested-bad :bar
(list y
)))))))))
572 (declare (dynamic-extent x
))
573 (unless (equalp (caar x
) (make-nested-good :bar
*bar
*))
574 (error "got ~S, wanted ~S" (caar x
) (make-nested-good :bar
*bar
*)))
575 ;; the NESTED instance itself *should* be DX!
576 (copy-nested (caar x
))))
578 (with-test (:name
:conservative-nested-dx
)
579 ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
580 (assert (equalp (nested-bad 42) (make-nested-good :bar
*bar
*)))
581 (assert (equalp *bar
* (list (list (make-nested-bad :bar
(list 42)))))))
583 ;;; multiple uses for dx lvar
585 (defun-with-dx multiple-dx-uses
()
586 (let ((dx (if (true t
)
589 (declare (dynamic-extent dx
))
593 ;;; mapfoo should make the initial cons dx
595 (defun mapcar-negate (x) (mapcar #'- x
))
596 (defun mapcan-reverse (x) (mapcan #'reverse x
))
598 ;;; handler-case and handler-bind should use DX internally
600 (defun dx-handler-bind (x)
601 (handler-bind ((error
603 (break "OOPS: ~S caused ~S" x c
)))
604 ((and serious-condition
(not error
))
606 (break "OOPS2: ~S did ~S" x c
))))
609 (defun dx-handler-case (x)
610 (assert (zerop (handler-case (/ 2 x
)
612 (break "OOPS: ~S caused ~S" x c
)
617 (defun list-delete-some-stuff ()
618 ;; opaque-identity hides the fact that we are calling a destructive function
619 ;; on a constant, which is technically illegal. But no deletion occurs,
620 ;; so it's innocuous. Also these aren't tests of DX, but oh well...
621 (declare (muffle-conditions style-warning
))
622 (delete 'a
(opaque-identity '(x y
)))
623 (delete 'a
(opaque-identity '(x y
)) :from-end t
)
624 (delete-duplicates (opaque-identity '(x y
))))
626 (defvar *a-cons
* (cons nil nil
))
628 (with-test (:name
(:no-consing
:dx-closures
) :skipped-on
'(not :stack-allocatable-closures
))
629 (assert-no-consing (dxclosure 42)))
631 (with-test (:name
(:no-consing
:dx-lists
)
632 :skipped-on
'(not (and :stack-allocatable-lists
633 :stack-allocatable-fixed-objects
)))
634 (assert-no-consing (dxlength 1 2 3))
635 (assert-no-consing (dxlength t t t t t t
))
636 (assert-no-consing (dxlength))
637 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
638 (assert-no-consing (test-nip-values))
639 (assert-no-consing (test-let-var-subst2 17))
640 (assert-no-consing (test-lvar-subst 11))
641 (assert-no-consing (nested-dx-lists))
642 (assert-consing (nested-dx-not-used *a-cons
*))
643 (assert-no-consing (nested-evil-dx-used *a-cons
*))
644 (assert-no-consing (mapcar-negate nil
))
645 (assert-no-consing (mapcan-reverse nil
))
646 (assert-no-consing (list-delete-some-stuff))
647 (assert-no-consing (multiple-dx-uses)))
649 (with-test (:name
(:no-consing
:dx-value-cell
)
650 :skipped-on
'(not :stack-allocatable-closures
))
651 (assert-no-consing (dx-value-cell 13)))
653 (with-test (:name
(:no-consing
:dx-fixed-objects
)
654 :skipped-on
'(not (and :stack-allocatable-fixed-objects
655 :stack-allocatable-closures
)))
656 (assert-no-consing (cons-on-stack 42))
657 (assert-no-consing (make-foo1-on-stack 123))
658 (assert-no-consing (nested-good 42))
659 (assert-no-consing (nested-dx-conses))
660 (assert-no-consing (dx-handler-bind 2))
661 (assert-no-consing (dx-handler-case 2)))
663 (with-test (:name
(:no-consing
:dx-vectors
) :skipped-on
'(not :stack-allocatable-vectors
))
664 (assert-no-consing (force-make-array-on-stack 128))
665 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
666 (assert-no-consing (make-array-on-stack-3 9 8 7))
667 (assert-no-consing (make-array-on-stack-4))
668 (assert-no-consing (make-array-on-stack-5))
669 (assert-no-consing (vector-on-stack :x
:y
)))
671 (with-test (:name
(:no-consing
:specialized-dx-vectors
)
672 :skipped-on
`(not (and :stack-allocatable-vectors
673 :c-stack-is-control-stack
)))
674 (assert-no-consing (make-array-on-stack-1))
675 (assert-no-consing (make-array-on-stack-6))
676 (assert-no-consing (make-array-on-stack-7))
677 (assert-no-consing (make-array-on-stack-8))
678 (assert-no-consing (make-array-on-stack-9))
679 (assert-no-consing (make-array-on-stack-10))
680 (assert-no-consing (make-array-on-stack-11)))
682 (with-test (:name
(:no-consing
:dx-raw-instances
) :skipped-on
'(or (not :raw-instance-init-vops
)
683 (not (and :gencgc
:c-stack-is-control-stack
))))
685 (setf a
1.24 b
1.23d0
)
686 (assert-no-consing (make-foo2-on-stack a b
)))
687 (assert-no-consing (make-foo3-on-stack)))
689 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
691 (defvar *table
* (make-hash-table))
693 (defun test-hash-table ()
694 (setf (gethash 5 *table
*) 13)
697 (with-test (:name
(:no-consing
:hash-tables
))
698 (assert-no-consing (test-hash-table)))
700 ;;; Both with-pinned-objects and without-gcing should not cons
702 (defun call-without-gcing (fun)
703 (sb-sys:without-gcing
(funcall fun
)))
705 (defun call-with-pinned-object (fun obj
)
706 (sb-sys:with-pinned-objects
(obj)
709 (with-test (:name
(:no-consing
:without-gcing
))
710 (assert-no-consing (call-without-gcing (lambda ()))))
712 (with-test (:name
(:no-consing
:with-pinned-objects
))
713 (assert-no-consing (call-with-pinned-object #'identity
42)))
715 ;;; with-mutex should use DX and not cons
717 (defvar *mutex
* (sb-thread::make-mutex
:name
"mutexlock"))
720 (sb-thread:with-mutex
(*mutex
*)
723 (with-test (:name
(:no-consing
:mutex
) :skipped-on
'(not :sb-thread
))
724 (assert-no-consing (test-mutex)))
727 ;;; Bugs found by Paul F. Dietz
729 (with-test (:name
(:dx-bug-misc
:pfdietz
))
736 (declare (optimize (speed 2) (space 0) (safety 0)
737 (debug 1) (compilation-speed 3)))
738 (let* ((v5 (cons b b
)))
739 (declare (dynamic-extent v5
))
745 ;;; bug reported by Svein Ove Aas
746 (defun svein-2005-ii-07 (x y
)
747 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
748 (let ((args (list* y
1 2 x
)))
749 (declare (dynamic-extent args
))
750 (apply #'aref args
)))
752 (with-test (:name
(:dx-bugs-misc
:svein-2005-ii-07
))
756 #3A
(((1 1 1) (1 1 1) (1 1 1))
757 ((1 1 1) (1 1 1) (4 1 1))
758 ((1 1 1) (1 1 1) (1 1 1))))
761 ;;; bug reported by Brian Downing: stack-allocated arrays were not
762 ;;; filled with zeroes.
763 (defun-with-dx bdowning-2005-iv-16
()
764 (let ((a (make-array 11 :initial-element
0)))
765 (declare (dynamic-extent a
))
766 (assert (every (lambda (x) (eql x
0)) a
))))
768 (with-test (:name
(:dx-bug-misc
:bdowning-2005-iv-16
))
769 #+(or hppa mips x86 x86-64
)
770 (assert-no-consing (bdowning-2005-iv-16))
771 (bdowning-2005-iv-16))
773 (declaim (inline my-nconc
))
774 (defun my-nconc (&rest lists
)
775 (declare (dynamic-extent lists
))
776 (apply #'nconc lists
))
777 (defun-with-dx my-nconc-caller
(a b c
)
778 (let ((l1 (list a b c
))
781 (with-test (:name
:rest-stops-the-buck
)
782 (let ((list1 (my-nconc-caller 1 2 3))
783 (list2 (my-nconc-caller 9 8 7)))
784 (assert (equal list1
'(1 2 3 1 2 3)))
785 (assert (equal list2
'(9 8 7 9 8 7)))))
787 (defun-with-dx let-converted-vars-dx-allocated-bug
(x y z
)
788 (let* ((a (list x y z
))
791 (declare (dynamic-extent c
))
792 (values (first c
) (second c
))))
793 (with-test (:name
:let-converted-vars-dx-allocated-bug
)
794 (multiple-value-bind (i j
) (let-converted-vars-dx-allocated-bug 1 2 3)
795 (assert (and (equal i j
)
796 (equal i
(list 1 2 3))))))
798 ;;; workaround for bug 419 -- real issue remains, but check that the
800 (defun-with-dx bug419
(x)
801 (multiple-value-call #'list
802 (eval '(values 1 2 3))
804 (declare (dynamic-extent x
))
809 (declare (dynamic-extent #'mget
#'mset
))
810 ((lambda (f g
) (eval `(progn ,f
,g
(values 4 5 6)))) #'mget
#'mset
)))))
812 (with-test (:name
(:dx-bug-misc
:bug419
))
813 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
815 ;;; Multiple DX arguments in a local function call
816 (defun test-dx-flet-test (fun n f1 f2 f3
)
817 (let ((res (with-output-to-string (s)
818 (assert (eql n
(ignore-errors (funcall fun s
)))))))
819 (multiple-value-bind (x pos
) (read-from-string res nil
)
820 (assert (equalp f1 x
))
821 (multiple-value-bind (y pos2
) (read-from-string res nil nil
:start pos
)
822 (assert (equalp f2 y
))
823 (assert (equalp f3
(read-from-string res nil nil
:start pos2
))))))
824 #+(or hppa mips x86 x86-64
)
825 (assert-no-consing (assert (eql n
(funcall fun nil
))))
826 (assert (eql n
(funcall fun nil
))))
828 (macrolet ((def (n f1 f2 f3
)
829 (let ((name (sb-pcl::format-symbol
:cl-user
"DX-FLET-TEST.~A" n
)))
831 (defun-with-dx ,name
(s)
833 (declare (dynamic-extent x
))
842 (with-test (:name
(:dx-flet-test
,n
))
843 (test-dx-flet-test #',name
,n
,f1
,f2
,f3
))))))
844 (def 0 (list :one
) (list :two
) (list :three
))
845 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list
))
846 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
848 ;;; Test that unknown-values coming after a DX value won't mess up the
850 (defun test-update-uvl-live-sets (x y z
)
851 (declare (optimize speed
(safety 0)))
853 (declare (dynamic-extent a
))
854 (eval `(list (length ',a
) ',b
))))
856 (bar (list x y z
) ; dx push
858 (multiple-value-call 'list
859 (eval '(values 1 2 3)) ; uv push
865 (with-test (:name
(:update-uvl-live-sets
))
866 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
867 (test-update-uvl-live-sets #() 4 5))))
869 (with-test (:name
:regression-1.0
.23.38)
870 (compile nil
'(lambda ()
871 (declare (muffle-conditions compiler-note
))
873 (let ((res (cons x x
)))
876 (declare (inline make
))
877 (let ((z (make 1 2)))
878 (declare (dynamic-extent z
))
881 (compile nil
'(lambda ()
882 (declare (muffle-conditions compiler-note
))
884 (let ((res (cons x x
)))
887 (declare (inline make
))
888 (let ((z (make 1 2)))
889 (declare (dynamic-extent z
))
893 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
894 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
895 ;;; location, leading to all manner of badness. ...reproducing this
896 ;;; reliably is hard, but this it at least used to break on x86-64.
897 (defun length-and-words-packed-in-same-tn (m)
898 (declare (optimize speed
(safety 0) (debug 0) (space 0)))
899 (let ((array (make-array (max 1 m
) :element-type
'fixnum
)))
900 (declare (dynamic-extent array
))
901 (array-total-size array
)))
902 (with-test (:name
:length-and-words-packed-in-same-tn
)
903 (assert (= 1 (length-and-words-packed-in-same-tn -
3))))
905 (with-test (:name
:handler-case-bogus-compiler-note
906 :skipped-on
'(not (and :stack-allocatable-fixed-objects
907 :stack-allocatable-closures
)))
909 ((compiler-note (lambda (note)
910 (error "compiler issued note ~S during test" note
))))
911 ;; Taken from SWANK, used to signal a bogus stack allocation
914 `(lambda (files fasl-dir load
)
915 (declare (muffle-conditions style-warning
))
916 (let ((needs-recompile nil
))
918 (let ((dest (binary-pathname src fasl-dir
)))
921 (when (or needs-recompile
922 (not (probe-file dest
))
923 (file-newer-p src dest
))
924 (setq needs-recompile t
)
925 (ensure-directories-exist dest
)
926 (compile-file src
:output-file dest
:print nil
:verbose t
))
928 (load dest
:verbose t
)))
929 (serious-condition (c)
930 (handle-loadtime-error c dest
))))))))))
932 (declaim (inline foovector barvector
))
933 (defun foovector (x y z
)
934 (let ((v (make-array 3)))
939 (defun barvector (x y z
)
940 (make-array 3 :initial-contents
(list x y z
)))
941 (with-test (:name
:dx-compiler-notes
942 :skipped-on
'(not (and :stack-allocatable-vectors
943 :stack-allocatable-closures
)))
944 (flet ((assert-notes (j lambda
)
946 (handler-bind ((compiler-note (lambda (c)
949 (let ((*error-output
* (make-broadcast-stream)))
950 (compile nil lambda
))
952 (error "Wanted ~S notes, got ~S for~% ~S"
954 ;; These ones should complain.
955 (assert-notes 1 `(lambda (x)
956 (let ((v (make-array x
)))
957 (declare (dynamic-extent v
))
959 (assert-notes 2 `(lambda (x)
960 (let ((y (if (plusp x
)
963 (declare (dynamic-extent y
))
966 (assert-notes 1 `(lambda (x)
967 (let ((y (foovector x x x
)))
968 (declare (sb-int:truly-dynamic-extent y
))
971 ;; These ones should not complain.
972 (assert-notes 0 `(lambda (name)
974 ((posix-getenv (function c-string c-string
)
977 (alien-funcall posix-getenv name
)))))
978 (assert-notes 0 `(lambda (x)
979 (let ((y (barvector x x x
)))
980 (declare (dynamic-extent y
))
983 (assert-notes 0 `(lambda (list)
984 (declare (optimize (space 0)))
985 (sort list
(lambda (x y
) ; shut unrelated notes up
986 (< (truly-the fixnum x
)
987 (truly-the fixnum y
))))))
988 (assert-notes 0 `(lambda (other)
990 (ignore-errors (funcall other s c n
)))))))
992 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
993 ;;; in an unfortunate loop.
994 (defun handler-case-eating-stack ()
995 (declare (muffle-conditions warning
)) ; "dead code detected ... IR1-PHASES"
997 (do ((n 0 (logand most-positive-fixnum
(1+ n
))))
999 (multiple-value-bind (value error
) (ignore-errors)
1000 (when (and value error
) nil
))
1002 (assert (= sp
(sb-c::%primitive sb-c
:current-stack-pointer
)))
1003 (setf sp
(sb-c::%primitive sb-c
:current-stack-pointer
))))))
1004 (with-test (:name
:handler-case-eating-stack
1005 :skipped-on
'(not (and :stack-allocatable-fixed-objects
1006 :stack-allocatable-closures
)))
1007 (assert-no-consing (handler-case-eating-stack)))
1009 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
1010 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
1011 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
1013 `(simple-array t
(3)))
1014 (declaim (ftype (function (t t t
) vec
) vec
))
1015 (declaim (inline vec
))
1017 (make-array 3 :initial-contents
(list a b c
)))
1018 (defun bad-boy (vec)
1019 (declare (type vec vec
))
1021 (let ((vec (vec (aref vec
0) (aref vec
1) (aref vec
2))))
1022 (declare (dynamic-extent vec
))
1023 (funcall fun vec
))))
1024 (with-test (:name
:recheck-nested-dx-bug
1025 :skipped-on
'(not :stack-allocatable-vectors
))
1026 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
1027 (lambda (vec) (equalp vec
(vec 1.0 2.0 3.3)))))
1028 (flet ((foo (x) (declare (ignore x
))))
1029 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
1030 (assert-no-consing (funcall bad-boy
#'foo
)))))
1032 (with-test (:name
:bug-497321
)
1033 (flet ((test (lambda type
)
1035 (handler-bind ((condition (lambda (c)
1037 (unless (typep c type
)
1038 (error "wanted ~S for~% ~S~%got ~S"
1039 type lambda
(type-of c
))))))
1040 (let ((*error-output
* (make-broadcast-stream)))
1041 (compile nil lambda
)))
1043 (test `(lambda () (declare (dynamic-extent #'bar
)))
1045 (test `(lambda () (declare (dynamic-extent bar
)))
1047 (test `(lambda (bar) (cons bar
(lambda () (declare (dynamic-extent bar
)))))
1048 'sb-ext
:compiler-note
)
1051 (cons #'bar
(lambda () (declare (dynamic-extent #'bar
))))))
1052 'sb-ext
:compiler-note
)))
1054 (with-test (:name
:bug-586105
1055 :skipped-on
'(not (and :stack-allocatable-vectors
1056 :stack-allocatable-lists
)))
1058 (let ((vec1 (make-array 1 :initial-contents
(list (list x
))))
1059 (vec2 (make-array 1 :initial-contents
`((,x
))))
1060 (vec3 (make-array 1 :initial-contents
`#((,x
))))
1061 (vec4 (make-array 1 :initial-contents
`(#(,x
)))))
1062 (declare (dynamic-extent vec1 vec2 vec3 vec4
))
1063 (assert (eql x
(car (aref vec1
0))))
1064 (assert (eql x
(car (aref vec2
0))))
1065 (assert (eql x
(car (aref vec3
0))))
1066 (assert (eql x
(elt (aref vec4
0) 0))))))
1067 (assert-no-consing (test 42))))
1069 (defun bug-681092 ()
1070 (declare (optimize speed
))
1073 (declare (dynamic-extent #'bar
))
1074 (do () ((list) (bar))
1077 (with-test (:name
:bug-681092
)
1078 (assert (= 10 (bug-681092))))
1080 ;;;; Including a loop in the flow graph between a DX-allocation and
1081 ;;;; the start of its environment would, for a while, cause executing
1082 ;;;; any of the code in the loop to discard the value. Found via
1083 ;;;; attempting to DX-allocate an array, which triggered the FILL
1084 ;;;; transform, which inserted such a loop.
1085 (defun bug-1472785 (x)
1086 (let ((y (let ((z (cons nil nil
)))
1090 (when (= x i
) (go b2
))
1096 (declare (dynamic-extent y w
))
1098 (with-test (:name
:bug-1472785
)
1099 (assert (null (bug-1472785 1))))
1101 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
1102 ;;;; but required by sanity.
1104 (declaim (inline rest-stops-dx
))
1105 (defun-with-dx rest-stops-dx
(&rest args
)
1106 (declare (dynamic-extent args
))
1107 (apply #'opaque-identity args
))
1109 (defun-with-dx rest-stops-dx-ok
()
1110 (equal '(:foo
) (rest-stops-dx (list :foo
))))
1112 (with-test (:name
:rest-stops-dynamic-extent
)
1113 (assert (rest-stops-dx-ok)))
1115 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
1117 (with-test (:name
:rest-to-more-conversion
)
1118 (let ((f1 (compile nil
`(lambda (f &rest args
)
1120 (assert-no-consing (assert (eql f1
(funcall f1
#'identity f1
)))))
1121 (let ((f2 (compile nil
`(lambda (f1 f2
&rest args
)
1122 (values (apply f1 args
) (apply f2 args
))))))
1123 (assert-no-consing (multiple-value-bind (a b
)
1124 (funcall f2
(lambda (x y z
) (+ x y z
)) (lambda (x y z
) (- x y z
))
1126 (assert (and (eql 6 a
) (eql -
4 b
))))))
1127 (let ((f3 (compile nil
`(lambda (f &optional x
&rest args
)
1129 (apply f x args
))))))
1130 (assert-no-consing (assert (eql 42 (funcall f3
1131 (lambda (a b c
) (+ a b c
))
1136 (let ((*error-output
* (make-broadcast-stream)))
1137 (compile nil
`(lambda (f &optional x
&rest args
1138 &key y
&allow-other-keys
)
1139 (apply f y x args
))))))
1140 (assert-no-consing (funcall f4
(lambda (y x yk y2 b c
)
1148 (let ((f5 (compile nil
`(lambda (a b c
&rest args
)
1149 (apply #'list
* a b c args
)))))
1150 (assert (equal '(1 2 3 4 5 6 7) (funcall f5
1 2 3 4 5 6 '(7)))))
1151 (let ((f6 (compile nil
`(lambda (x y
)
1152 (declare (optimize speed
))
1153 (concatenate 'string x y
)))))
1154 (assert (equal "foobar" (funcall f6
"foo" "bar"))))
1155 (let ((f7 (compile nil
`(lambda (&rest args
)
1158 (assert (equal '(a b c d e f
) (funcall (funcall f7
'a
'b
'c
'd
'e
'f
) 'list
))))
1159 (let ((f8 (compile nil
`(lambda (&rest args
)
1163 (assert (equal '(a b c d e f
) (funcall (funcall f8
'a
'b
'c
'd
'e
'f
) 'list
))))
1164 (let ((f9 (compile nil
`(lambda (f &rest args
)
1167 (declare (dynamic-extent #'foo
))
1168 (funcall f
#'foo
))))))
1169 (assert (equal '(a b c d e f
)
1170 (funcall f9
(lambda (f) (funcall f
'list
)) 'a
'b
'c
'd
'e
'f
))))
1171 (let ((f10 (compile nil
`(lambda (f &rest args
)
1174 (funcall f
#'foo
))))))
1175 (assert (equal '(a b c d e f
)
1176 (funcall f10
(lambda (f) (funcall f
'list
)) 'a
'b
'c
'd
'e
'f
))))
1177 (let ((f11 (compile nil
`(lambda (x y z
)
1179 (labels ((foo (x &rest rest
)
1180 (apply (lambda (&rest rest2
)
1181 (return-from out
(values-list rest2
)))
1186 (multiple-value-bind (a b c
) (funcall f11
1 2 3)
1189 (assert (eql c
3)))))
1191 (defun opaque-funcall (function &rest arguments
)
1192 (apply function arguments
))
1194 (with-test (:name
:implicit-value-cells
)
1195 (flet ((test-it (type input output
)
1196 (let ((f (compile nil
`(lambda (x)
1197 (declare (type ,type x
))
1200 (declare (dynamic-extent #'inc
))
1201 (list (opaque-funcall #'inc
) x
))))))
1202 (assert (equal (funcall f input
)
1203 (list output output
))))))
1204 (let ((width sb-vm
:n-word-bits
))
1205 (test-it t
(1- most-positive-fixnum
) most-positive-fixnum
)
1206 (test-it `(unsigned-byte ,(1- width
)) (ash 1 (- width
2)) (1+ (ash 1 (- width
2))))
1207 (test-it `(signed-byte ,width
) (ash -
1 (- width
2)) (1+ (ash -
1 (- width
2))))
1208 (test-it `(unsigned-byte ,width
) (ash 1 (1- width
)) (1+ (ash 1 (1- width
))))
1209 (test-it 'single-float
3f0
4f0
)
1210 (test-it 'double-float
3d0
4d0
)
1211 (test-it '(complex single-float
) #c
(3f0 4f0
) #c
(4f0 4f0
))
1212 (test-it '(complex double-float
) #c
(3d0 4d0
) #c
(4d0 4d0
)))))
1214 (with-test (:name
:sap-implicit-value-cells
)
1215 (let ((f (compile nil
`(lambda (x)
1216 (declare (type system-area-pointer x
))
1218 (setf x
(sb-sys:sap
+ x
16))))
1219 (declare (dynamic-extent #'inc
))
1220 (list (opaque-funcall #'inc
) x
)))))
1221 (width sb-vm
:n-machine-word-bits
))
1222 (assert (every (lambda (x)
1223 (sb-sys:sap
= x
(sb-sys:int-sap
(+ 16 (ash 1 (1- width
))))))
1224 (funcall f
(sb-sys:int-sap
(ash 1 (1- width
))))))))
1226 (with-test (:name
:&more-bounds
)
1228 (assert (not (funcall (compile nil
'(lambda (&rest args
) (car args
))))))
1229 (assert (not (funcall (compile nil
'(lambda (&rest args
) (nth 6 args
))))))
1230 (assert (not (funcall (compile nil
'(lambda (&rest args
) (elt args
10))))))
1231 (assert (not (funcall (compile nil
'(lambda (&rest args
) (cadr args
))))))
1232 (assert (not (funcall (compile nil
'(lambda (&rest args
) (third args
)))))))