(setf (aref nodes 0) 2)
(assert (every #'~= (apply #'concatenate 'list nodes) '(2 3 6 9)))))
-430: nested structure constructors do not stack allocate
-
- (defun nada (x) (declare (ignore x)) nil)
-
- (declaim (inline make-foo))
- (defstruct foo bar)
-
- (defun foo ()
- (let ((x (list (make-foo))))
- (declare (dynamic-extent x))
- (nada x)))
-
- Result of MAKE-FOO not stack allocated in FOO, because the function
- HANDLE-NESTED-DYNAMIC-EXTENT-LVARS sees is not
- %MAKE-STRUCTURE-INSTANCE, but no-yet-eliminated (VARARGS-ENTRY
- MAKE-FOO).
-
431: alien strucure redefinition doesn't work as expected
fixed in 1.0.21.29
compile-time style-warning.
* bug fix: :ALLOCATION :CLASS slots are type-checked properly
in safe code. (reported by Didier Verna)
+ * bug fix: #430; nested structure constructors can stack allocate.
changes in sbcl-1.0.23 relative to 1.0.22:
* enhancement: when disassembling method functions, disassembly
(source-name '.anonymous.)
debug-name
(note-lexical-bindings t)
- post-binding-lexenv)
+ post-binding-lexenv
+ system-lambda)
(declare (list body vars aux-vars aux-vals))
;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
(lambda (make-lambda :vars vars
:bind bind
:%source-name source-name
- :%debug-name debug-name))
+ :%debug-name debug-name
+ :system-lambda-p system-lambda))
(result-ctran (make-ctran))
(result-lvar (make-lvar)))
(fun (collect ((default-bindings)
(default-vals))
(dolist (default defaults)
- (if (constantp default)
+ (if (sb!xc:constantp default)
(default-vals default)
(let ((var (gensym)))
(default-bindings `(,var ,default))
(default-vals var))))
- (ir1-convert-lambda-body `((let (,@(default-bindings))
- (%funcall ,fun
- ,@(reverse vals)
- ,@(default-vals))))
- arg-vars
- ;; FIXME: Would be nice to
- ;; share these names instead
- ;; of consing up several
- ;; identical ones. Oh well.
- :debug-name (debug-name
- '&optional-processor
- name)
- :note-lexical-bindings nil))))
+ (let ((bindings (default-bindings))
+ (call `(%funcall ,fun ,@(reverse vals) ,@(default-vals))))
+ (ir1-convert-lambda-body (if bindings
+ `((let (,@bindings) ,call))
+ `(,call))
+ arg-vars
+ ;; FIXME: Would be nice to
+ ;; share these names instead
+ ;; of consing up several
+ ;; identical ones. Oh well.
+ :debug-name (debug-name
+ '&optional-processor
+ name)
+ :note-lexical-bindings nil
+ :system-lambda t)))))
(mapc (lambda (var arg-var)
(when (cdr (leaf-refs arg-var))
(setf (leaf-ever-used var) t)))
vars supplied-p-p body
aux-vars aux-vals
source-name debug-name
- force post-binding-lexenv)
+ force post-binding-lexenv
+ system-lambda)
(declare (type optional-dispatch res)
(list default-vars default-vals entry-vars entry-vals vars body
aux-vars aux-vals))
(list* t arg-name entry-vals)
(rest vars) t body aux-vars aux-vals
source-name debug-name
- force post-binding-lexenv)
+ force post-binding-lexenv system-lambda)
(ir1-convert-hairy-args
res
(cons arg default-vars)
(cons arg-name entry-vals)
(rest vars) supplied-p-p body aux-vars aux-vals
source-name debug-name
- force post-binding-lexenv))))
+ force post-binding-lexenv system-lambda))))
;; We want to delay converting the entry, but there exist
;; problems: hidden references should not be established to
:type (leaf-type var)
:where-from (leaf-where-from var))))
- (let* ((*allow-instrumenting* nil)
- (n-context (gensym "N-CONTEXT-"))
+ (let* ((n-context (gensym "N-CONTEXT-"))
(context-temp (make-lambda-var :%source-name n-context))
(n-count (gensym "N-COUNT-"))
(count-temp (make-lambda-var :%source-name n-count
,@(arg-vals))))
(arg-vars)
:debug-name (debug-name '&more-processor name)
- :note-lexical-bindings nil)))
+ :note-lexical-bindings nil
+ :system-lambda t)))
(setf (optional-dispatch-more-entry res)
(register-entry-point ep res)))))
;;; incoming value is NIL, so we must union NULL with the declared
;;; type when computing the type for the main entry's argument.
(defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
- rest more-context more-count keys supplied-p-p
- body aux-vars aux-vals
- source-name debug-name post-binding-lexenv)
+ rest more-context more-count keys supplied-p-p
+ body aux-vars aux-vals source-name debug-name
+ post-binding-lexenv system-lambda)
(declare (type optional-dispatch res)
(list default-vars default-vals entry-vars entry-vals keys body
aux-vars aux-vals))
:aux-vars (append (bind-vars) aux-vars)
:aux-vals (append (bind-vals) aux-vals)
:post-binding-lexenv post-binding-lexenv
- :debug-name (debug-name 'varargs-entry name)))
+ :debug-name (debug-name 'varargs-entry name)
+ :system-lambda system-lambda))
(last-entry (convert-optional-entry main-entry default-vars
(main-vals) () name)))
(setf (optional-dispatch-main-entry res)
vars supplied-p-p body aux-vars
aux-vals
source-name debug-name
- force post-binding-lexenv)
+ force post-binding-lexenv
+ system-lambda)
(declare (type optional-dispatch res)
(list default-vars default-vals entry-vars entry-vals vars body
aux-vars aux-vals))
entry-vars entry-vals
nil nil nil vars supplied-p-p body aux-vars
aux-vals source-name debug-name
- post-binding-lexenv)
+ post-binding-lexenv system-lambda)
(let* ((name (or debug-name source-name))
(fun (ir1-convert-lambda-body
body (reverse default-vars)
:aux-vars aux-vars
:aux-vals aux-vals
:post-binding-lexenv post-binding-lexenv
- :debug-name (debug-name 'hairy-arg-processor name))))
+ :debug-name (debug-name 'hairy-arg-processor name)
+ :system-lambda system-lambda)))
(setf (optional-dispatch-main-entry res) fun)
(register-entry-point fun res)
(ir1-convert-hairy-args res nvars nvals nvars nvals
(rest vars) nil body aux-vars aux-vals
source-name debug-name
- nil post-binding-lexenv)))
+ nil post-binding-lexenv system-lambda)))
(t
(let* ((arg (first vars))
(info (lambda-var-arg-info arg))
entry-vars entry-vals vars supplied-p-p body
aux-vars aux-vals
source-name debug-name
- force post-binding-lexenv)))
+ force post-binding-lexenv
+ system-lambda)))
;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
(push (if (lambda-p ep)
(register-entry-point
arg nil nil (rest vars) supplied-p-p body
aux-vars aux-vals
source-name debug-name
- post-binding-lexenv))
+ post-binding-lexenv system-lambda))
(:more-context
(ir1-convert-more res default-vars default-vals
entry-vars entry-vals
nil arg (second vars) (cddr vars) supplied-p-p
body aux-vars aux-vals
source-name debug-name
- post-binding-lexenv))
+ post-binding-lexenv system-lambda))
(:keyword
(ir1-convert-more res default-vars default-vals
entry-vars entry-vals
nil nil nil vars supplied-p-p body aux-vars
aux-vals source-name debug-name
- post-binding-lexenv)))))))
+ post-binding-lexenv system-lambda)))))))
;;; This function deals with the case where we have to make an
;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
(defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
&key post-binding-lexenv
(source-name '.anonymous.)
- debug-name)
+ debug-name system-lambda)
(declare (list body vars aux-vars aux-vals))
(aver (or debug-name (neq '.anonymous. source-name)))
(let ((res (make-optional-dispatch :arglist vars
(aver-live-component *current-component*)
(push res (component-new-functionals *current-component*))
(ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
- source-name debug-name nil post-binding-lexenv)
+ source-name debug-name nil post-binding-lexenv
+ system-lambda)
(setf (optional-dispatch-min-args res) min)
(setf (optional-dispatch-max-args res)
(+ (1- (length (optional-dispatch-entry-points res))) min))
;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
(defun ir1-convert-lambda (form &key (source-name '.anonymous.)
- debug-name maybe-add-debug-catch)
+ debug-name maybe-add-debug-catch
+ system-lambda)
(unless (consp form)
(compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
(type-of form)
(compiler-error
"The lambda expression has a missing or non-list lambda list:~% ~S"
form))
+ (when (and system-lambda maybe-add-debug-catch)
+ (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
(unless (or debug-name (neq '.anonymous. source-name))
(setf debug-name (name-lambdalike form)))
(multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
(forms (if (eq result-type *wild-type*)
forms
`((the ,result-type (progn ,@forms)))))
- (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
- (ir1-convert-hairy-lambda forms vars keyp
- allow-other-keys
- aux-vars aux-vals
- :post-binding-lexenv post-binding-lexenv
- :source-name source-name
- :debug-name debug-name)
- (ir1-convert-lambda-body forms vars
- :aux-vars aux-vars
- :aux-vals aux-vals
- :post-binding-lexenv post-binding-lexenv
- :source-name source-name
- :debug-name debug-name))))
+ (*allow-instrumenting* (and (not system-lambda) *allow-instrumenting*))
+ (res (cond ((or (find-if #'lambda-var-arg-info vars) keyp)
+ (ir1-convert-hairy-lambda forms vars keyp
+ allow-other-keys
+ aux-vars aux-vals
+ :post-binding-lexenv post-binding-lexenv
+ :source-name source-name
+ :debug-name debug-name
+ :system-lambda system-lambda))
+ (t
+ (ir1-convert-lambda-body forms vars
+ :aux-vars aux-vars
+ :aux-vals aux-vals
+ :post-binding-lexenv post-binding-lexenv
+ :source-name source-name
+ :debug-name debug-name
+ :system-lambda system-lambda)))))
(setf (functional-inline-expansion res) form)
(setf (functional-arg-documentation res) (cadr form))
(when (boundp '*lambda-conversions*)
;; like a much more common case.
:handled-conditions (lexenv-handled-conditions *lexenv*)
:policy (lexenv-policy *lexenv*)))
- (*allow-instrumenting* (and (not system-lambda)
- *allow-instrumenting*))
(clambda (ir1-convert-lambda `(lambda ,@body)
:source-name source-name
- :debug-name debug-name)))
+ :debug-name debug-name
+ :system-lambda system-lambda)))
(setf (functional-inline-expanded clambda) t)
clambda)))
;; across components, or an explanation of when they do it. ...in the
;; meanwhile AVER that our assumption holds true.
(aver (or (not component) (eq component (node-component use))))
- (or (and (combination-p use)
- (eq (combination-kind use) :known)
- (awhen (fun-info-stack-allocate-result (combination-fun-info use))
- (funcall it use dx))
- t)
+ (or (dx-combination-p use dx)
(and (cast-p use)
(not (cast-type-check use))
- (lvar-good-for-dx-p (cast-value use) dx component)
- t)))
+ (lvar-good-for-dx-p (cast-value use) dx component))
+ (and (trivial-lambda-var-ref-p use)
+ (let ((uses (lvar-uses (trivial-lambda-var-ref-lvar use))))
+ (or (eq use uses)
+ (lvar-good-for-dx-p (trivial-lambda-var-ref-lvar use) dx component))))))
(defun lvar-good-for-dx-p (lvar dx &optional component)
(let ((uses (lvar-uses lvar)))
uses)
(use-good-for-dx-p uses dx component))))
+(defun known-dx-combination-p (use dx)
+ (and (eq (combination-kind use) :known)
+ (awhen (fun-info-stack-allocate-result (combination-fun-info use))
+ (funcall it use dx))))
+
+(defun dx-combination-p (use dx)
+ (and (combination-p use)
+ (or
+ ;; Known, and can do DX.
+ (known-dx-combination-p use dx)
+ ;; Possibly a not-yet-eliminated lambda which ends up returning the
+ ;; results of an actual known DX combination.
+ (let* ((fun (combination-fun use))
+ (ref (principal-lvar-use fun))
+ (clambda (when (ref-p ref)
+ (ref-leaf ref)))
+ (creturn (when (lambda-p clambda)
+ (lambda-return clambda)))
+ (result-use (when (return-p creturn)
+ (principal-lvar-use (return-result creturn)))))
+ (when result-use
+ (if (known-dx-combination-p result-use dx)
+ (combination-args-flow-cleanly-p use result-use dx)
+ (dx-combination-p result-use dx)))))
+ t))
+
+(defun combination-args-flow-cleanly-p (combination1 combination2 dx)
+ (labels ((recurse (combination)
+ (or (eq combination combination2)
+ (if (known-dx-combination-p combination dx)
+ (let ((dest (lvar-dest (combination-lvar combination))))
+ (and (combination-p dest)
+ (recurse dest)))
+ (let* ((fun1 (combination-fun combination))
+ (ref1 (principal-lvar-use fun1))
+ (clambda1 (when (ref-p ref1) (ref-leaf ref1))))
+ (when (lambda-p clambda1)
+ (dolist (var (lambda-vars clambda1) t)
+ (dolist (var-ref (lambda-var-refs var))
+ (let ((dest (lvar-dest (ref-lvar var-ref))))
+ (unless (and (combination-p dest) (recurse dest))
+ (return-from combination-args-flow-cleanly-p nil)))))))))))
+ (recurse combination1)))
+
+(defun trivial-lambda-var-ref-p (use)
+ (and (ref-p use)
+ (let ((var (ref-leaf use)))
+ ;; lambda-var, no SETS
+ (when (and (lambda-var-p var) (not (lambda-var-sets var)))
+ (let ((home (lambda-var-home var))
+ (refs (lambda-var-refs var)))
+ ;; bound by a system lambda, no other REFS
+ (when (and (lambda-system-lambda-p home)
+ (eq use (car refs)) (not (cdr refs)))
+ ;; the LAMBDA this var is bound by has only a single REF, going
+ ;; to a combination
+ (let* ((lambda-refs (lambda-refs home))
+ (primary (car lambda-refs)))
+ (and (ref-p primary)
+ (not (cdr lambda-refs))
+ (combination-p (lvar-dest (ref-lvar primary)))))))))))
+
+(defun trivial-lambda-var-ref-lvar (use)
+ (let* ((this (ref-leaf use))
+ (home (lambda-var-home this)))
+ (multiple-value-bind (fun vars)
+ (values home (lambda-vars home))
+ (let* ((combination (lvar-dest (ref-lvar (car (lambda-refs fun)))))
+ (args (combination-args combination)))
+ (assert (= (length vars) (length args)))
+ (loop for var in vars
+ for arg in args
+ when (eq var this)
+ return arg)))))
+
(declaim (inline block-to-be-deleted-p))
(defun block-to-be-deleted-p (block)
(or (block-delete-p block)
(handle-nested-dynamic-extent-lvars dx (cast-value use)))
(combination
(loop for arg in (combination-args use)
- when (lvar-good-for-dx-p arg dx)
- append (handle-nested-dynamic-extent-lvars dx arg))))))
+ ;; deleted args show up as NIL here
+ when (and arg (lvar-good-for-dx-p arg dx))
+ append (handle-nested-dynamic-extent-lvars dx arg)))
+ (ref
+ (let* ((other (trivial-lambda-var-ref-lvar use)))
+ (unless (eq other lvar)
+ (handle-nested-dynamic-extent-lvars dx other)))))))
(cons lvar
(if (listp uses)
(loop for use in uses
(with-ir1-environment-from-node (lambda-bind (main-entry fun))
(let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
:debug-name (debug-name
- 'xep (leaf-debug-name fun)))))
+ 'xep (leaf-debug-name fun))
+ :system-lambda t)))
(setf (functional-kind res) :external
(leaf-ever-used res) t
(functional-entry-fun res) fun
(%funcall ,entry ,@args))
:debug-name (debug-name 'hairy-function-entry
(lvar-fun-debug-name
- (basic-combination-fun call)))))))
+ (basic-combination-fun call)))
+ :system-lambda t))))
(convert-call ref call new-fun)
(dolist (ref (leaf-refs entry))
(convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
;;; return the leaf, otherwise return NIL.
(defun lvar-delayed-leaf (lvar)
(declare (type lvar lvar))
- (let ((use (lvar-uses lvar)))
- (and (ref-p use)
- (let ((leaf (ref-leaf use)))
- (etypecase leaf
- (lambda-var (if (null (lambda-var-sets leaf)) leaf nil))
- (constant leaf)
- ((or functional global-var) nil))))))
+ (unless (lvar-dynamic-extent lvar)
+ (let ((use (lvar-uses lvar)))
+ (and (ref-p use)
+ (let ((leaf (ref-leaf use)))
+ (etypecase leaf
+ (lambda-var (if (null (lambda-var-sets leaf)) leaf nil))
+ (constant leaf)
+ ((or functional global-var) nil)))))))
;;; Annotate a normal single-value lvar. If its only use is a ref that
;;; we are allowed to delay the evaluation of, then we mark the lvar
(functional-lexenv locall-fun))))
(fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
:source-name source-name
- :debug-name (debug-name 'tl-xep debug-name-tail))))
+ :debug-name (debug-name 'tl-xep debug-name-tail)
+ :system-lambda t)))
(when name
(assert-global-function-definition-type name locall-fun))
(setf (functional-entry-fun fun) locall-fun
;; list of embedded lambdas
(children nil :type list)
(parent nil :type (or clambda null))
- (allow-instrumenting *allow-instrumenting* :type boolean))
+ (allow-instrumenting *allow-instrumenting* :type boolean)
+ ;; True if this is a system introduced lambda: it may contain user code, but
+ ;; the lambda itself is not, and the bindings introduced by it are considered
+ ;; transparent by the nested DX analysis.
+ (system-lambda-p nil :type boolean))
(defprinter (clambda :conc-name lambda- :identity t)
%source-name
%debug-name
(setf (car x) nil))
nil))
+(defparameter *bar* nil)
+(declaim (inline make-nested-bad make-nested-good))
+(defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
+ (:constructor make-nested-good (&key bar)))
+ bar)
+
+(defun-with-dx nested-good (y)
+ (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
+ (declare (dynamic-extent x))
+ (true x)))
+
+(defun-with-dx nested-bad (y)
+ (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
+ (declare (dynamic-extent x))
+ (unless (equalp (caar x) (make-nested-good :bar *bar*))
+ (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
+ (caar x)))
+
+(with-test (:name :conservative-nested-dx)
+ ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
+ (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
+ (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
+
;;; multiple uses for dx lvar
(defun-with-dx multiple-dx-uses ()
(assert-no-consing (cons-on-stack 42))
(assert-no-consing (make-array-on-stack))
(assert-no-consing (make-foo1-on-stack 123))
+ (assert-no-consing (nested-good 42))
(#+raw-instance-init-vops assert-no-consing
#-raw-instance-init-vops progn
(make-foo2-on-stack 1.24 1.23d0))
(assert (every (lambda (x) (eql x 0)) a))))
(assert-no-consing (bdowning-2005-iv-16))
-
(defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
(let* ((a (list x y z))
(b (list x y z))
(c (list a b)))
(declare (dynamic-extent c))
(values (first c) (second c))))
-(multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
- (assert (and (equal i j)
- (equal i (list 1 2 3)))))
+
+(with-test (:name :let-converted-vars-dx-allocated-bug)
+ (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
+ (assert (and (equal i j)
+ (equal i (list 1 2 3))))))
;;; workaround for bug 419 -- real issue remains, but check that the
;;; bandaid holds.
;;; checkins which aren't released. (And occasionally for internal
;;; versions, especially for internal versions off the main CVS
;;; branch, it gets hairier, e.g. "0.pre7.14.flaky4.13".)
-"1.0.23.37"
+"1.0.23.38"