-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-extent.impure.lisp
1463 lines (1278 loc) · 51.2 KB
/
dynamic-extent.impure.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; tests that dynamic-extent functionality works.
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.
(when (eq sb-ext:*evaluator-mode* :interpret)
(sb-ext:exit :code 104))
(load "compiler-test-util.lisp")
(use-package :ctu)
(setq sb-c::*check-consistency* t
sb-ext:*stack-allocate-dynamic-extent* t)
(defun crashme (a)
(declare (optimize (speed 3) (safety 0) (space 0)))
(declare (muffle-conditions style-warning)) ; re F1 and F2
(restart-case
(progn (ignore-errors (error "Foo")) (write-char #\.))
(retry () (f1 a))
(use-value (new) (f2 new))))
;; lp#1530390
(with-test (:name :do-not-dxify-restarts)
(let ((a (make-array 100))) (unwind-protect (crashme 'bork) (fill a 0)))
(let ((a (make-array 100))) (unwind-protect (crashme 'bork) (fill a 0)))
(let ((a (make-array 100))) (unwind-protect (crashme 'bork) (fill a 0)))
(let ((a (make-array 100))) (unwind-protect (crashme 'bork) (fill a 0)))
)
(defmacro defun-with-dx (name arglist &body body)
(let ((debug-name (sb-int:symbolicate name "-HIGH-DEBUG"))
(default-name (sb-int:symbolicate name "-DEFAULT")))
`(progn
(defun ,debug-name ,arglist
(declare (optimize debug))
,@body)
(defun ,default-name ,arglist
,@body)
(defun ,name (&rest args)
(apply #',debug-name args)
(apply #',default-name args)))))
(declaim (notinline opaque-identity))
(defun opaque-identity (x)
x)
;;; &REST lists
(defun-with-dx dxlength (&rest rest)
(declare (dynamic-extent rest))
(length rest))
(with-test (:name (:dx-&rest :basics))
(assert (= (dxlength 1 2 3) 3))
(assert (= (dxlength t t t t t t) 6))
(assert (= (dxlength) 0)))
(defun callee (list)
(destructuring-bind (a b c d e f &rest g) list
(+ a b c d e f (length g))))
(defun-with-dx dxcaller (&rest rest)
(declare (dynamic-extent rest))
(callee rest))
(with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
(assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
(defun-with-dx dxcaller-align-1 (x &rest rest)
(declare (dynamic-extent rest))
(+ x (callee rest)))
(with-test (:name (:dx-&rest :pass-down-to-callee :non-tail-call))
(assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
(assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
;;; %NIP-VALUES
(defun-with-dx test-nip-values ()
(flet ((bar (x &rest y)
(declare (dynamic-extent y))
(if (> x 0)
(values x (length y))
(values (car y)))))
(multiple-value-call #'values
(bar 1 2 3 4 5 6)
(bar -1 'a 'b))))
(with-test (:name (:nip-values))
(assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
;;; LET-variable substitution
(defun-with-dx test-let-var-subst1 (x)
(let ((y (list x (1- x))))
(opaque-identity :foo)
(let ((z (the list y)))
(declare (dynamic-extent z))
(length z))))
(with-test (:name (:let-variable-substitution))
(assert (eql (test-let-var-subst1 17) 2)))
(defun-with-dx test-let-var-subst2 (x)
(let ((y (list x (1- x))))
(declare (dynamic-extent y))
(opaque-identity :foo)
(let ((z (the list y)))
(length z))))
(with-test (:name (:let-variable-substitution-2))
(assert (eql (test-let-var-subst2 17) 2)))
;;; DX propagation through LET-return.
(defun-with-dx test-lvar-subst (x)
(let ((y (list x (1- x))))
(declare (dynamic-extent y))
(second (let ((z (the list y)))
(opaque-identity :foo)
z))))
(with-test (:name (:dx-propagation-through-let-return))
(assert (eql (test-lvar-subst 11) 10)))
;;; this code is incorrect, but the compiler should not fail
(defun-with-dx test-let-var-subst-incorrect (x)
(let ((y (list x (1- x))))
(opaque-identity :foo)
(let ((z (the list y)))
(declare (dynamic-extent z))
(opaque-identity :bar)
z)))
;;; alignment
(defvar *x*)
(defun-with-dx test-alignment-dx-list (form)
(multiple-value-prog1 (eval form)
(let ((l (list 1 2 3 4)))
(declare (dynamic-extent l))
(setq *x* (copy-list l)))))
(with-test (:name (:dx-list :alignment))
(dotimes (n 64)
(let* ((res (loop for i below n collect i))
(form `(values ,@res)))
(assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
(assert (equal *x* '(1 2 3 4))))))
;;; closure
(declaim (notinline true))
(defun true (x)
(declare (ignore x))
t)
(defun-with-dx dxclosure (x)
(flet ((f (y)
(+ y x)))
(declare (dynamic-extent #'f))
(true #'f)))
(with-test (:name (:dx-closure))
(assert (eq t (dxclosure 13))))
;;; value-cells
(defun-with-dx dx-value-cell (x)
;; Not implemented everywhere, yet.
#+(or x86 x86-64 mips hppa)
(let ((cell x))
(declare (sb-int:truly-dynamic-extent cell))
(flet ((f ()
(incf cell)))
(declare (dynamic-extent #'f))
(true #'f))))
;;; CONS
(defun-with-dx cons-on-stack (x)
(let ((cons (cons x x)))
(declare (dynamic-extent cons))
(true cons)
nil))
;;; MAKE-ARRAY
(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)
(true v)
nil))
(defun-with-dx make-array-on-stack-1 ()
(let ((v (make-array '(42) :element-type 'single-float)))
(declare (dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-2 (n x)
(declare (integer n))
(let ((v (make-array n :initial-contents x)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-3 (x y z)
(let ((v (make-array 3
:element-type 'fixnum :initial-contents (list x y z)
:element-type t :initial-contents x)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-4 ()
(let ((v (make-array 3 :initial-contents '(1 2 3))))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-5 ()
(let ((v (make-array 3 :initial-element 12 :element-type t)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-6 ()
(let ((v (make-array 3 :initial-element 12 :element-type '(unsigned-byte 8))))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-7 ()
(let ((v (make-array 3 :initial-element 12 :element-type '(signed-byte 8))))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-8 ()
(let ((v (make-array 3 :initial-element 12 :element-type 'word)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-9 ()
(let ((v (make-array 3 :initial-element 12.0 :element-type 'single-float)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-10 ()
(let ((v (make-array 3 :initial-element 12.0d0 :element-type 'double-float)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx make-array-on-stack-11 ()
(let ((v (make-array (the integer (opaque-identity 3)) :initial-element 12.0d0 :element-type 'double-float)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
(true v)
nil))
(defun-with-dx vector-on-stack (x y)
(let ((v (vector 1 x 2 y 3)))
(declare (sb-int:truly-dynamic-extent v))
(true v)
nil))
(defun-with-dx make-3d-fixed-array-on-stack-1 ()
(let ((a (make-array '(4 8 3) :initial-element 12 :element-type t)))
(declare (sb-int:truly-dynamic-extent a))
(true a)
(true a)
nil))
(defun-with-dx make-3d-fixed-array-on-stack-2 (a b c d)
(sb-int:dx-let ((a (make-array '(2 2 1)
:element-type 'bit
:initial-contents `(#((,a) (,b)) (#(,c) (,d))))))
(true a)
(true a)
nil))
(defun-with-dx make-2d-variable-array-on-stack ()
(let* ((n (opaque-identity 5))
(a (make-array `(,n 2) :initial-element 12 :element-type t)))
(declare (sb-int:truly-dynamic-extent a))
(true a)
(true a)
nil))
(defun 2d-array-initializer (n)
(ecase n
(1 '((a)))
(2 '((a b) (c d)))
(3 '((a b c) (c d e) (f g h)))))
(defun-with-dx make-2d-array-function-initializer (n)
(let* ((x (opaque-identity n))
(y (opaque-identity x))
(a (make-array `(,x ,y) :initial-contents (2d-array-initializer x))))
(declare (sb-int:truly-dynamic-extent a))
(true a)
(true a)
nil))
;;; MAKE-LIST
(declaim (inline make-list-container))
(defstruct list-container listy-slot)
(defun make-var-length-dx-list (n thunk)
(sb-int:dx-let ((s (make-list-container :listy-slot (make-list n))))
(funcall thunk s)))
;; :stack-allocatable-lists is necessary but not sufficient
(with-test (:name (:dx-list :make-list) :skipped-on (not :x86-64))
(assert (null (ctu:find-named-callees #'make-var-length-dx-list)))
(assert-no-consing (make-var-length-dx-list
50 (lambda (x) (declare (ignore x))))))
;;; 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)
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)
s
(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)))))
(with-test (:name (:test-fp-struct-1.1))
(test-fp-struct-1.1 123.456 876.243d0))
(with-test (:name (:test-fp-struct-1.2))
(test-fp-struct-1.2 123.456 876.243d0))
(with-test (:name (:test-fp-struct-1.3))
(test-fp-struct-1.3 123.456 876.243d0))
(with-test (:name (:test-fp-struct-1.4))
(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)
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)
s
(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)))))
(with-test (:name (:test-fp-struct-2.1))
(test-fp-struct-2.1 123.456 876.243d0))
(with-test (:name (:test-fp-struct-2.2))
(test-fp-struct-2.2 123.456 876.243d0))
(with-test (:name (:test-fp-struct-2.3))
(test-fp-struct-2.3 123.456 876.243d0))
(with-test (:name (:test-fp-struct-2.4))
(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)
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)
s
(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)))))
(with-test (:name (:test-cfp-struct-1.1))
(test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
(with-test (:name (:test-cfp-struct-1.2))
(test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
(with-test (:name (:test-cfp-struct-1.3))
(test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
(with-test (:name (:test-cfp-struct-1.4))
(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)
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)
s
(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)))))
(with-test (:name (:test-cfp-struct-2.1))
(test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
(with-test (:name (:test-cfp-struct-2.2))
(test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
(with-test (:name (:test-cfp-struct-2.3))
(test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
(with-test (:name (:test-cfp-struct-2.4))
(test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
;; It works to declare a structure constructor INLINE after the DEFSTRUCT
;; was processed, as long as it was in the null lexical environment.
;; In a perfect world, we'd figure out that a variable declared DX which
;; receives the value of a structure constructor defined in the same file
;; and neither expressly INLINE nor NOTINLINE should be locally inlined.
;; But at least this shows that a declaration at the intended use point
;; is sufficient, without also a bracketing INLINE/NOTINLINE at the DEFSTRUCT.
;; Verify the precondition for the assertions that claim that DXifying works
;; even though the MAKE- function was not expressly INLINE when its DEFSTRUCT
;; was compiled.
(dolist (s '(make-foo1 make-foo2 make-foo3))
(assert (null (sb-int:info :function :inlining-data s))))
(defstruct foo1 x)
(defun-with-dx make-foo1-on-stack (x)
(declare (inline make-foo1))
(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)
(defun-with-dx make-foo2-on-stack (x y)
(declare (inline make-foo2))
x
(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 ()
(declare (inline make-foo3))
(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*)))
;; the NESTED instance itself *should* be DX!
(copy-nested (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)))))))
;;; Conditional nested DX
;; These two test cases prompted a substantial redesign of the STACK
;; phase of the compiler to handle their particular permutation of
;; nested DX.
(with-test (:name (:bug-1044465 :reduced))
;; Test case from Stas Boukarev
(checked-compile '(lambda (x)
(let ((a (if x
(list (list x))
(list (list x)))))
(declare (dynamic-extent a))
(prin1 a)
1))))
(with-test (:name (:bug-1044465 :nasty))
;; Test case from Alastair Bridgewater
(checked-compile '(lambda (x y)
(dotimes (i 2)
(block bar
(let ((a (if x
(list (list x))
(list (list x))))
(b (if y x (return-from bar x))))
(declare (dynamic-extent a))
(prin1 a)
b))))))
;;; 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))
;;; mapfoo should make the initial cons dx
(defun mapcar-negate (x) (mapcar #'- x))
(defun mapcan-reverse (x) (mapcan #'reverse x))
;;; handler-case and handler-bind should use DX internally
(defun dx-handler-bind (x)
(let ((y 3))
(macrolet ((fool () `(lambda (c) (print (list c (incf y))))))
(handler-bind ((error
#'(lambda (c)
(break "OOPS: ~S caused ~S" x c)))
(warning (fool))
((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)
-1)
(:no-error (res)
(1- res))))))
(defun list-delete-some-stuff ()
;; opaque-identity hides the fact that we are calling a destructive function
;; on a constant, which is technically illegal. But no deletion occurs,
;; so it's innocuous. Also these aren't tests of DX, but oh well...
(declare (muffle-conditions style-warning))
(delete 'a (opaque-identity '(x y)))
(delete 'a (opaque-identity '(x y)) :from-end t)
(delete-duplicates (opaque-identity '(x y))))
(defvar *a-cons* (cons nil nil))
(with-test (:name (:no-consing :dx-closures) :skipped-on (not :stack-allocatable-closures))
(assert-no-consing (dxclosure 42)))
(with-test (:name (:no-consing :dx-lists)
:skipped-on (not (and :stack-allocatable-lists
:stack-allocatable-fixed-objects)))
(assert-no-consing (dxlength 1 2 3))
(assert-no-consing (dxlength t t t t t t))
(assert-no-consing (dxlength))
(assert-no-consing (dxcaller 1 2 3 4 5 6 7))
(assert-no-consing (test-nip-values))
(assert-no-consing (test-let-var-subst2 17))
(assert-no-consing (test-lvar-subst 11))
(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 (mapcar-negate nil))
(assert-no-consing (mapcan-reverse nil))
(assert-no-consing (list-delete-some-stuff))
(assert-no-consing (multiple-dx-uses)))
(with-test (:name (:no-consing :dx-value-cell)
:skipped-on (not :stack-allocatable-closures))
(assert-no-consing (dx-value-cell 13)))
(with-test (:name (:no-consing :dx-fixed-objects)
:skipped-on (not (and :stack-allocatable-fixed-objects
:stack-allocatable-closures)))
(assert-no-consing (cons-on-stack 42))
(assert-no-consing (make-foo1-on-stack 123))
(assert-no-consing (nested-good 42))
(assert-no-consing (nested-dx-conses))
(assert-no-consing (dx-handler-bind 2))
(assert-no-consing (dx-handler-case 2)))
(with-test (:name (:no-consing :dx-vectors) :skipped-on (not :stack-allocatable-vectors))
(assert-no-consing (force-make-array-on-stack 128))
(assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
(assert-no-consing (make-array-on-stack-3 9 8 7))
(assert-no-consing (make-array-on-stack-4))
(assert-no-consing (make-array-on-stack-5))
(assert-no-consing (vector-on-stack :x :y)))
(with-test (:name (:no-consing :dx-arrays) :skipped-on (not :stack-allocatable-vectors))
(assert-no-consing (make-3d-fixed-array-on-stack-1))
(assert-no-consing (make-2d-variable-array-on-stack))
(assert-no-consing (make-2d-array-function-initializer 1))
(assert-no-consing (make-2d-array-function-initializer 2))
(assert-no-consing (make-2d-array-function-initializer 3)))
(with-test (:name (:no-consing :dx-specialized-arrays)
:skipped-on (not (and :stack-allocatable-vectors
:c-stack-is-control-stack)))
(assert-no-consing (make-3d-fixed-array-on-stack-2 0 0 1 1)))
(with-test (:name (:no-consing :specialized-dx-vectors)
:skipped-on (not (and :stack-allocatable-vectors
:c-stack-is-control-stack)))
(assert-no-consing (make-array-on-stack-1))
(assert-no-consing (make-array-on-stack-6))
(assert-no-consing (make-array-on-stack-7))
(assert-no-consing (make-array-on-stack-8))
(assert-no-consing (make-array-on-stack-9))
(assert-no-consing (make-array-on-stack-10))
(assert-no-consing (make-array-on-stack-11)))
(with-test (:name (:no-consing :dx-raw-instances) :skipped-on (or (not :raw-instance-init-vops)
(not (and :gencgc :c-stack-is-control-stack))))
(let (a b)
(setf a 1.24 b 1.23d0)
(assert-no-consing (make-foo2-on-stack a b)))
(assert-no-consing (make-foo3-on-stack)))
;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
(defvar *table* (make-hash-table))
(defun test-hash-table ()
(setf (gethash 5 *table*) 13)
(gethash 5 *table*))
(with-test (:name (:no-consing :hash-tables))
(assert-no-consing (test-hash-table)))
;;; Both with-pinned-objects and without-gcing should not cons
(defun call-without-gcing (fun)
(sb-sys:without-gcing (funcall fun)))
(defun call-with-pinned-object (fun obj)
(sb-sys:with-pinned-objects (obj)
(funcall fun obj)))
(with-test (:name (:no-consing :without-gcing))
(assert-no-consing (call-without-gcing (lambda ()))))
(with-test (:name (:no-consing :with-pinned-objects))
(assert-no-consing (call-with-pinned-object #'identity 42)))
;;; with-mutex should use DX and not cons
(defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
(defun test-mutex ()
(sb-thread:with-mutex (*mutex*)
(true *mutex*)))
(with-test (:name (:no-consing :mutex) :skipped-on (not :sb-thread))
(assert-no-consing (test-mutex)))
;;; Bugs found by Paul F. Dietz
(with-test (:name (:dx-bug-misc :pfdietz))
(checked-compile-and-assert (:optimize :all)
'(lambda (a b)
(let* ((v5 (cons b b)))
(declare (dynamic-extent v5))
v5
a))
(('x 'y) 'x)))
(with-test (:name :bug-1738095)
;; STACK analysis wasn't marking UVL or DX LVARs that are held live
;; by a DX allocation as being live in blocks along the path from
;; the allocation to the ENTRY node, causing problems when there is
;; a block that is outside the lexical environment of the "earlier"
;; DX allocation (in the case below, such a block would be the first
;; (outermost) use of LIST).
(checked-compile '(lambda ()
(let ((* (list (let ((* (list nil)))
(declare (dynamic-extent *))
(list 1)))))
(declare (dynamic-extent *))))))
(with-test (:name :bug-1739308)
;; STACK analysis wasn't propagating DX LVARs back from ENTRY to
;; allocation through non-local-entry environments (below, the CATCH
;; entry point), causing problems when such is the ONLY live path
;; back to the allocation.
(checked-compile '(lambda (x y)
(let ((s
(multiple-value-prog1
(list x)
(catch 'ct1
(throw 'ct8 30)))))
(declare (dynamic-extent s))
(funcall (the function y) s)))))
;;; bug reported by Svein Ove Aas
(defun svein-2005-ii-07 (x y)
(declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
(let ((args (list* y 1 2 x)))
(declare (dynamic-extent args))
(apply #'aref args)))
(with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
(assert (eql
(svein-2005-ii-07
'(0)
#3A(((1 1 1) (1 1 1) (1 1 1))
((1 1 1) (1 1 1) (4 1 1))
((1 1 1) (1 1 1) (1 1 1))))
4)))
;;; bug reported by Brian Downing: stack-allocated arrays were not
;;; filled with zeroes.
(defun-with-dx bdowning-2005-iv-16 ()
(let ((a (make-array 11 :initial-element 0)))
(declare (dynamic-extent a))
(assert (every (lambda (x) (eql x 0)) a))))
(with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
#+(or hppa mips x86 x86-64)
(assert-no-consing (bdowning-2005-iv-16))
(bdowning-2005-iv-16))
(declaim (inline my-nconc))
(defun my-nconc (&rest lists)
(declare (dynamic-extent lists))
(apply #'nconc lists))
(defun-with-dx my-nconc-caller (a b c)
(let ((l1 (list a b c))
(l2 (list a b c)))
(my-nconc l1 l2)))
(with-test (:name :rest-stops-the-buck)
(let ((list1 (my-nconc-caller 1 2 3))
(list2 (my-nconc-caller 9 8 7)))
(assert (equal list1 '(1 2 3 1 2 3)))
(assert (equal list2 '(9 8 7 9 8 7)))))
(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)))))
(with-test (:name (:dx-bug-misc :bug419))
(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))))))
#+(or hppa mips x86 x86-64)
(assert-no-consing (assert (eql n (funcall fun nil))))
(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))
(with-test (:name (:dx-flet-test ,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)
))))
(with-test (:name (:update-uvl-live-sets))
(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)
(checked-compile '(lambda ()
(declare (muffle-conditions compiler-note))
(flet ((make (x y)
(let ((res (cons x x)))
(setf (cdr res) y)
res)))
(declare (inline make))
(let ((z (make 1 2)))
(declare (dynamic-extent z))
(print z)
t))))
(checked-compile '(lambda ()
(declare (muffle-conditions compiler-note))
(flet ((make (x y)
(let ((res (cons x x)))
(setf (cdr res) y)
(if x res y))))
(declare (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)))