1 ;;;; compiler optimization policy stuff
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;; a value for an optimization declaration
15 (def!type policy-quality
() '(integer 0 3))
17 (defvar *macro-policy
* nil
)
18 ;;; global policy restrictions as a POLICY object or nil
19 (!defvar
*policy-restrictions
* nil
)
21 (defun restrict-compiler-policy (&optional quality
(min 0))
22 "Assign a minimum value to an optimization quality. QUALITY is the name of
23 the optimization quality to restrict, and MIN (defaulting to zero) is the
24 minimum allowed value.
26 Returns the alist describing the current policy restrictions.
28 If QUALITY is NIL or not given, nothing is done.
30 Otherwise, if MIN is zero or not given, any existing restrictions of QUALITY
31 are removed. If MIN is between one and three inclusive, it becomes the new
32 minimum value for the optimization quality: any future proclamations or
33 declarations of the quality with a value less then MIN behave as if the value
36 This is intended to be used interactively, to facilitate recompiling large
37 bodies of code with eg. a known minimum safety.
39 See also :POLICY option in WITH-COMPILATION-UNIT.
41 EXPERIMENTAL INTERFACE: Subject to change."
42 (declare (type policy-quality min
))
44 (unless (policy-quality-name-p quality
)
45 (error "~S is not a policy quality" quality
))
46 ;; The dynamic policy object is immutable, otherwise a construct like
47 ;; (let ((*policy-restrictions* *policy-restrictions*)) ...)
48 ;; could allow alterations inside the LET to leak out.
49 ;; The structure itself does not declare slots its read-only, because
50 ;; OPTIMIZE declaration processing uses it as a scratchpad.
51 (setf *policy-restrictions
*
52 (acond (*policy-restrictions
* (copy-structure it
))
53 (t (make-policy 0 0))))
54 (alter-policy *policy-restrictions
* (policy-quality-name-p quality
)
56 ;; Return dotted pairs, not elements that look declaration-like.
57 (if *policy-restrictions
*
58 (mapc (lambda (x) (rplacd x
(cadr x
)))
59 (policy-to-decl-spec *policy-restrictions
*))
62 (defstruct (policy-dependent-quality (:copier nil
))
63 (name nil
:type symbol
:read-only t
)
64 (expression nil
:read-only t
)
65 (getter nil
:read-only t
)
66 (values-documentation nil
:read-only t
))
68 ;;; names of recognized optimization policy qualities
69 (declaim (simple-vector **policy-dependent-qualities
**))
70 (defglobal **policy-dependent-qualities
** #())
72 ;; Return POLICY as a list suitable to the OPTIMIZE declaration.
73 ;; If FORCE-ALL then include qualities without an explicit value too.
74 (defun policy-to-decl-spec (policy &optional
(raw t
) force-all
)
75 (loop with presence
= (policy-presence-bits policy
)
76 for index from
(- n-policy-primary-qualities
)
77 below
(length **policy-dependent-qualities
**)
78 when
(or force-all
(logbitp (mod index max-policy-qualities
) presence
))
80 (list (if (minusp index
)
81 (elt **policy-primary-qualities
** (lognot index
))
82 (policy-dependent-quality-name
83 (elt **policy-dependent-qualities
** index
)))
85 ;; Raw values are insensitive to *POLICY-RESTRICTIONS*.
86 (%%policy-quality policy index
)
87 ;; Otherwise take the adjusted quality.
88 (%policy-quality policy index
)))))
90 ;; Return T if P1 and P2 are policies which are specified to be the same.
91 ;; A result of NIL does not imply that definitely P1 /= P2
92 ;; because a multitude of policies can be effectively equal.
93 ;; [Any dependent quality might be specified the same as its computed
94 ;; value in the absence of an explicit value.]
95 (defun policy= (p1 p2
)
97 (= (policy-primary-qualities p1
) (policy-primary-qualities p2
))
98 (= (policy-dependent-qualities p1
) (policy-dependent-qualities p2
))
99 (= (policy-presence-bits p1
) (policy-presence-bits p2
)))
100 (and (null p1
) (null p2
))))
102 ;;; Is X the name of an optimization policy quality?
103 ;;; If it is, return the integer identifier for the quality name.
104 (defun policy-quality-name-p (x)
105 ;; Standard (and non-standard) primary qualities are numbered from -1 down.
106 (or (awhen (position x
**policy-primary-qualities
** :test
#'eq
)
108 ;; Dependent qualities are numbered from 0 up.
109 (position x
**policy-dependent-qualities
**
110 :key
#'policy-dependent-quality-name
)))
112 ;; Destructively modify POLICY such that quality INDEX has VALUE,
113 ;; and the specified PRESENTP bit.
114 (defun alter-policy (policy index value
&optional
(presentp t
))
115 (if (minusp index
) ; a primary quality
116 (setf (ldb (byte 2 (* 2 (lognot index
)))
117 (policy-primary-qualities policy
)) value
)
118 (setf (ldb (byte 2 (* 2 index
))
119 (policy-dependent-qualities policy
)) value
))
120 ;; Some cross-compilation hosts can't execute (SETF (LOGBITP ...)).
121 (setf (ldb (byte 1 (mod index max-policy-qualities
))
122 (policy-presence-bits policy
)) (if presentp
1 0))
125 ;;; Is it deprecated?
126 (declaim (ftype function deprecation-warn
))
127 (defun policy-quality-deprecation-warning (quality)
129 ((stack-allocate-dynamic-extent stack-allocate-vector stack-allocate-value-cells
)
130 (deprecation-warn :late
"SBCL" "1.0.19.7" 'policy quality
'*stack-allocate-dynamic-extent
*
134 (deprecation-warn :early
"SBCL" "1.0.53.74" 'policy quality nil
:runtime-error nil
)
139 ;;; *POLICY* holds the current global compiler policy information, as
140 ;;; a POLICY object mapping from the compiler-assigned index (unique per
141 ;;; quality name) to quality value.
142 ;;; This used to be an alist, but tail-sharing was never really possible
143 ;;; because for deterministic comparison the list was always freshly
144 ;;; consed so that destructive sorting could be done for canonicalization.
145 (declaim (type policy
*policy
*)
146 (type (or policy null
) *policy-restrictions
*))
148 ;; ANSI-specified default of 1 for each quality.
149 (defglobal **baseline-policy
** nil
)
150 ;; Baseline policy altered with (TYPE-CHECK 0)
151 (defglobal **zero-typecheck-policy
** nil
)
152 #-sb-xc-host
(declaim (type policy
**baseline-policy
**))
154 ;;; This is to be called early in cold init to set things up, and may
155 ;;; also be called again later in cold init in order to reset default
156 ;;; optimization policy back to default values after toplevel PROCLAIM
157 ;;; OPTIMIZE forms have messed with it.
158 (defun !policy-cold-init-or-resanify
()
159 (setq **baseline-policy
**
160 (make-policy (loop for i below n-policy-primary-qualities
161 sum
(ash #b01
(* i
2))))
162 **zero-typecheck-policy
**
163 (alter-policy (copy-policy **baseline-policy
**)
164 #-sb-xc
(policy-quality-name-p 'type-check
)
165 ;; Eval in the host since cold-init won't have
166 ;; executed any forms in 'policies.lisp'
167 #+sb-xc
#.
(policy-quality-name-p 'type-check
)
170 ;; CMU CL didn't use 1 as the default for everything,
171 ;; but since ANSI says 1 is the ordinary value, we do.
172 (setf *policy
* (copy-policy **baseline-policy
**)))
174 ;;; Look up a named optimization quality in POLICY. This is only
175 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
176 ;;; it's an error if it's called for a quality which isn't defined.
177 (defun policy-quality (policy quality-name
)
178 (%policy-quality policy
179 (the fixnum
(policy-quality-name-p quality-name
))))
181 (define-compiler-macro policy-quality
(&whole form policy quality-name
)
182 (acond ((and (constantp quality-name
)
183 ;; CONSTANT-FORM-VALUE can not be called here when building
184 ;; the cross-compiler, but EVAL can safely be used
185 ;; since our own source code is known not to be screwy.
186 (policy-quality-name-p (#-sb-xc-host constant-form-value
187 #+sb-xc-host eval quality-name
)))
188 `(%policy-quality
,policy
,it
))
192 (macrolet ((extract-field (floor-expression-primary
193 floor-expression-dependent
)
195 (let ((byte-pos (* (lognot index
) 2)))
196 (max (ldb (byte 2 byte-pos
)
197 (policy-primary-qualities policy
))
198 ,floor-expression-primary
))
199 (let ((byte-pos (* index
2)))
200 (max (if (logbitp index
(policy-presence-bits policy
))
201 (ldb (byte 2 byte-pos
)
202 (policy-dependent-qualities policy
))
204 ,floor-expression-dependent
))))
205 (define-getter (name &body body
)
206 `(defun ,name
(policy index
)
207 (declare (type policy policy
)
209 #.
(- n-policy-primary-qualities
)
210 #.
(- max-policy-qualities
211 n-policy-primary-qualities
1))
215 ;; Return the value for quality INDEX in POLICY, using *POLICY-RESTRICTIONS*
216 ;; Primary qualities are assumed to exist, however policy-restricting functions
217 ;; can create a POLICY that indicates absence of primary qualities.
218 ;; This does not affect RESTRICT-COMPILER-POLICY because a lower bound of 0
219 ;; can be assumed for everything. SET-MACRO-POLICY might care though.
220 (define-getter %policy-quality
221 (let ((floor *policy-restrictions
*))
222 (macrolet ((quality-floor (get-byte)
223 `(if floor
(ldb (byte 2 byte-pos
) (,get-byte floor
)) 0)))
224 (extract-field (quality-floor policy-primary-qualities
)
225 (quality-floor policy-dependent-qualities
)))))
227 ;; Return the unadjusted value for quality INDEX in POLICY.
228 ;; This is used for converting a policy to a list of elements for display
229 ;; and for verifying that after processing declarations, the new policy
230 ;; matches the given declarations, thus implying no ambiguity.
231 (define-getter %%policy-quality
232 (extract-field 0 0))) ; floor is always 0
234 ;;; Forward declaration of %COERCE-TO-POLICY.
235 ;;; Definition is in 'node' so that FUNCTIONAL and NODE types are defined.
236 ;;; Arg is declared of type T because the function explicitly checks it.
237 (declaim (ftype (sfunction (t) policy
) %coerce-to-policy
))
239 ;;; syntactic sugar for querying optimization policy qualities
241 ;;; Evaluate EXPR in terms of the optimization policy associated with
242 ;;; THING. EXPR is a form which accesses optimization qualities by
243 ;;; referring to them by name, e.g. (> SPEED SPACE).
244 (defmacro policy
(thing expr
&optional
(coercion-fn '%coerce-to-policy
))
245 (let* ((n-policy (make-symbol "P"))
246 (binds (loop for name across
**policy-primary-qualities
**
247 for index downfrom -
1
248 collect
`(,name
(%policy-quality
,n-policy
,index
))))
250 (loop for info across
**policy-dependent-qualities
**
251 for name
= (policy-dependent-quality-name info
)
252 collect
`(,name
(let ((,name
(policy-quality ,n-policy
',name
)))
254 ,(policy-dependent-quality-expression info
)
256 `(let ((,n-policy
(,coercion-fn
,thing
)))
257 ;; FIXME: automatically inserted IGNORABLE decls are
258 ;; often suggestive of poor style, as is this one.
259 (declare (ignorable ,n-policy
))
260 (symbol-macrolet (,@binds
,@dependent-binds
)
263 ;;; Dependent qualities
264 (defmacro define-optimization-quality
265 (name expression
&optional values-documentation documentation
)
266 (declare (ignorable documentation
))
267 `(eval-when (:compile-toplevel
:load-toplevel
:execute
)
268 (let ((number (policy-quality-name-p ',name
))
269 (item (make-policy-dependent-quality
271 :expression
',expression
272 ;; DESCRIBE-COMPILER-POLICY uses the getter
273 :getter
(named-lambda ,(string name
) (policy)
274 (policy policy
,expression
))
275 :values-documentation
',values-documentation
)))
277 (setf (svref **policy-dependent-qualities
** number
) item
)
278 ;; This array is reallocated every time a policy is added,
279 ;; but that's fine - it's not a performance issue.
280 (let ((size (1+ (length **policy-dependent-qualities
**))))
281 ;; Don't overrun the packed bit fields.
282 (when (> (+ n-policy-primary-qualities size
) max-policy-qualities
)
283 (error "Maximum number of policy qualities exceeded."))
284 (setf **policy-dependent-qualities
**
285 (replace (make-array size
:initial-element item
)
286 **policy-dependent-qualities
**)))))
288 ,@(when documentation
`((setf (fdocumentation ',name
'optimize
) ,documentation
)))
291 ;;; Return a new POLICY containing the policy information represented
292 ;;; by the optimize declaration SPEC. Any parameters not specified are
293 ;;; defaulted from the POLICY argument.
294 (declaim (ftype (function (list (or policy null
)) (values policy list
))
295 process-optimize-decl
))
296 (defun process-optimize-decl (spec policy
)
297 (let ((result (copy-policy (or policy
**baseline-policy
**)))
298 (specified-qualities))
299 ;; Add new entries from SPEC.
300 (dolist (q-and-v-or-just-q (cdr spec
) (values result specified-qualities
))
301 (multiple-value-bind (quality raw-value
)
302 (if (atom q-and-v-or-just-q
)
303 (values q-and-v-or-just-q
3)
304 (destructuring-bind (quality raw-value
) q-and-v-or-just-q
305 (values quality raw-value
)))
306 (let ((index (policy-quality-name-p quality
)))
308 (or (policy-quality-deprecation-warning quality
)
310 "~@<Ignoring unknown optimization quality ~S in:~_ ~S~:>"
312 ((not (typep raw-value
'policy-quality
))
314 "~@<Ignoring bad optimization value ~S in:~_ ~S~:>"
317 ;; we can't do this yet, because CLOS macros expand
318 ;; into code containing INHIBIT-WARNINGS.
320 (when (eql quality
'inhibit-warnings
)
321 (compiler-style-warn "~S is deprecated: use ~S instead"
322 quality
'muffle-conditions
))
323 (push (cons quality raw-value
) specified-qualities
)
324 (alter-policy result index raw-value
))))))))
326 (defvar *macro-policy
* nil
)
327 ;; Set an alternate policy that is used to compile all code within DEFMACRO,
328 ;; MACROLET, DEFINE-COMPILER-MARO - whether they occur at toplevel or not -
329 ;; as well as execute all toplevel code in eval-when situation :COMPILE-TOPLEVEL,
330 ;; including such code as emitted into a '.cfasl' file.
331 ;; e.g. (SET-MACRO-POLICY '((SPEED 0) (SAFETY 3))) ensures full error checking
332 ;; regardless of prevailing local policy in situations such as
333 ;; (macrolet ((frob (a b) (declare (type (member :up :down) a)) ...)
335 ;; Todo: it would be nice to allow NOTINLINE, which can be broadly achieved by
336 ;; setting (SPEED 0), but nonetheless more targeted settings should be possible.
337 ;; Same for {UN}MUFFLE-CONDITIONS or anything else that can be proclaimed.
339 (defun set-macro-policy (list)
340 ;: Note that *MACRO-POLICY* does not represent absence of any primary quality,
341 ;; and therefore whenever it is injected into a macro, you get all baseline
342 ;; values of 1, augmented by the specified changes.
343 ;; There are two alternative behaviors that might make sense:
344 ;; - use the value of *POLICY* when SET-MACRO-POLICY is called as the baseline
345 ;; augmented by the specifiers in LIST
346 ;; - use the lexical policy at the time of expansion, augmented by LIST
347 ;; But most probably the current behavior is entirely reasonable.
348 (setq *macro-policy
* (process-optimize-decl `(optimize ,@list
)
349 **baseline-policy
**)))
351 ;; Turn the macro policy into an OPTIMIZE declaration for insertion
352 ;; into a macro body for DEFMACRO, MACROLET, or DEFINE-COMPILER-MACRO.
353 ;; Note that despite it being a style-warning to insert a duplicate,
354 ;; we need no precaution against that even though users may write
355 ;; (DEFMACRO FOO (X) (DECLARE (OPTIMIZE (SAFETY 1))) ...)
356 ;; The expansion of macro-defining forms is such that the macro-policy
357 ;; appears in a different lexical scope from the user's declarations.
358 (defun macro-policy-decls ()
360 `((declare (optimize ,@(policy-to-decl-spec *macro-policy
*))))))