| 1 |
;;; -*- mode: Lisp; indent-tabs-mode: nil; -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2010, Alexander Gavrilov ([email protected]) |
| 4 |
;;; |
| 5 |
;;; This file contains the groundwork for vectorized |
| 6 |
;;; array access intrinsics. |
| 7 |
;;; |
| 8 |
|
| 9 |
(in-package #:SSE) |
| 10 |
|
| 11 |
;; SSE array element size calculation |
| 12 |
|
| 13 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 14 |
(defun sse-elt-shift-from-saetp (info) |
| 15 |
(and info |
| 16 |
(subtypep (saetp-specifier info) 'number) |
| 17 |
(not (saetp-fixnum-p info)) |
| 18 |
(case (saetp-n-bits info) |
| 19 |
(8 0) (16 1) (32 2) (64 3) (128 4))))) |
| 20 |
|
| 21 |
(defglobal %%size-shift-table%% |
| 22 |
(let ((arr (make-array (1+ widetag-mask) :initial-element nil))) |
| 23 |
(loop |
| 24 |
for info across *specialized-array-element-type-properties* |
| 25 |
for shift = (sse-elt-shift-from-saetp info) |
| 26 |
when shift |
| 27 |
do (setf (svref arr (saetp-typecode info)) shift)) |
| 28 |
arr) |
| 29 |
"A table of element size shifts for supported SSE array types.") |
| 30 |
|
| 31 |
(declaim (inline sse-elt-shift-of) |
| 32 |
(ftype (function (t) (integer 0 4)) sse-elt-shift-of)) |
| 33 |
|
| 34 |
(defun sse-elt-shift-of (obj) |
| 35 |
"Returns the SSE element size shift for the given object, |
| 36 |
or fails if it is not a valid SSE vector." |
| 37 |
(declare (optimize (safety 0))) |
| 38 |
(the (integer 0 4) |
| 39 |
(or (svref %%size-shift-table%% |
| 40 |
(if (sb-vm::%other-pointer-p obj) |
| 41 |
(%other-pointer-widetag obj) |
| 42 |
0)) |
| 43 |
(error 'type-error |
| 44 |
:datum obj |
| 45 |
:expected-type 'sse-array)))) |
| 46 |
|
| 47 |
;;; Type and allocation |
| 48 |
|
| 49 |
(deftype sse-array (&optional (elt-type '* et-p) dims) |
| 50 |
"Type of arrays efficiently accessed by SSE aref intrinsics and returned by make-sse-array. |
| 51 |
Should be assumed to be SIMPLE-ARRAY, except that displacing with MAKE-SSE-ARRAY is allowed." |
| 52 |
(if (eq elt-type '*) |
| 53 |
(progn |
| 54 |
(when et-p |
| 55 |
(error "SSE-ARRAY must have a specific element type.")) |
| 56 |
`(simple-array * ,dims)) |
| 57 |
(let* ((upgraded (upgraded-array-element-type elt-type)) |
| 58 |
(shift (sse-elt-shift-from-saetp (find-saetp upgraded)))) |
| 59 |
(when (null shift) |
| 60 |
(error "Invalid SSE-ARRAY element type: ~S" elt-type)) |
| 61 |
(unless (subtypep upgraded elt-type) |
| 62 |
(warn "SSE-ARRAY element type ~S has been upgraded to ~S" elt-type upgraded)) |
| 63 |
`(simple-array ,upgraded ,dims)))) |
| 64 |
|
| 65 |
(defun make-sse-array (dimensions &key (element-type '(unsigned-byte 8)) (initial-element nil ie-p) displaced-to (displaced-index-offset 0)) |
| 66 |
"Allocates an SSE-ARRAY aligned to the 16-byte boundary. Flattens displacement chains for performance reasons." |
| 67 |
(let* ((upgraded (upgraded-array-element-type element-type)) |
| 68 |
(shift (sse-elt-shift-from-saetp (find-saetp upgraded)))) |
| 69 |
(when (null shift) |
| 70 |
(error "Invalid SSE-ARRAY element type: ~S" element-type)) |
| 71 |
(if displaced-to |
| 72 |
;; Fake displacement by allocating a simple-array header |
| 73 |
(let* ((dimensions (if (listp dimensions) dimensions (list dimensions))) |
| 74 |
(rank (length dimensions)) |
| 75 |
(count (reduce #'* dimensions))) |
| 76 |
(unless (subtypep element-type (array-element-type displaced-to)) |
| 77 |
(error "can't displace an array of type ~S into another of type ~S" |
| 78 |
element-type (array-element-type displaced-to))) |
| 79 |
(with-array-data ((data displaced-to) |
| 80 |
(start displaced-index-offset) |
| 81 |
(end)) |
| 82 |
(unless (= start 0) |
| 83 |
(error "SSE-ARRAY does not support displaced index offset.")) |
| 84 |
(unless (<= count end) |
| 85 |
(array-bounding-indices-bad-error data start count)) |
| 86 |
(if (= rank 1) |
| 87 |
(progn |
| 88 |
(when (< count end) |
| 89 |
(warn "SSE-ARRAY displaced size extended to the full length of the vector.")) |
| 90 |
data) |
| 91 |
(let ((new-array (make-array-header simple-array-widetag rank))) |
| 92 |
(set-array-header new-array data count nil 0 dimensions nil t))))) |
| 93 |
;; X86-64 vectors are already aligned to 16 bytes |
| 94 |
(apply #'make-array dimensions :element-type upgraded |
| 95 |
(if ie-p (list :initial-element initial-element)))))) |
| 96 |
|
| 97 |
;;; AREF intrinsic definition helpers |
| 98 |
|
| 99 |
(defconstant +vector-data-fixup+ |
| 100 |
(- (* vector-data-offset n-word-bytes) other-pointer-lowtag) |
| 101 |
"Offset from a tagged vector pointer to its data") |
| 102 |
|
| 103 |
(defmacro array-data-expr (array-var &optional is-vector) |
| 104 |
(ecase is-vector |
| 105 |
(:yes array-var) |
| 106 |
(:no `(%array-data-vector ,array-var)) |
| 107 |
((nil) |
| 108 |
`(if (array-header-p ,array-var) |
| 109 |
(%array-data-vector ,array-var) |
| 110 |
,array-var)))) |
| 111 |
|
| 112 |
;; Depends on the vector-length field being in the same place |
| 113 |
;; as the array fill pointer, which for simple-array is equal |
| 114 |
;; to the total size. |
| 115 |
(defknown %sse-array-size (simple-array fixnum) array-total-size (flushable always-translatable)) |
| 116 |
|
| 117 |
(define-vop (%sse-array-size/0) |
| 118 |
(:translate %sse-array-size) |
| 119 |
(:args (array :scs (descriptor-reg))) |
| 120 |
(:arg-types * (:constant (integer 0 0))) |
| 121 |
(:info gap) |
| 122 |
(:ignore gap) |
| 123 |
(:policy :fast-safe) |
| 124 |
(:results (result :scs (any-reg))) |
| 125 |
(:result-types tagged-num) |
| 126 |
(:generator 3 |
| 127 |
(loadw result array vector-length-slot other-pointer-lowtag))) |
| 128 |
|
| 129 |
(define-vop (%sse-array-size %sse-array-size/0) |
| 130 |
(:arg-types * (:constant (integer 1 16))) |
| 131 |
(:ignore) |
| 132 |
(:temporary (:sc any-reg) tmp) |
| 133 |
(:generator 8 |
| 134 |
(loadw result array vector-length-slot other-pointer-lowtag) |
| 135 |
(inst mov tmp (fixnumize gap)) |
| 136 |
(inst cmp result tmp) |
| 137 |
(inst cmov :ng tmp result) |
| 138 |
(inst sub result tmp))) |
| 139 |
|
| 140 |
(defmacro with-sse-data (((sap-var data-var array) (offset-var index)) &body code) |
| 141 |
;; Compute a SAP and offset for the specified array and index. Check bounds. |
| 142 |
(with-unique-names (data-index data-end elt-shift access-size) |
| 143 |
(once-only ((array array) |
| 144 |
(index index)) |
| 145 |
`(locally |
| 146 |
(declare (optimize (insert-array-bounds-checks 0))) |
| 147 |
(with-array-data ((,data-var ,array) |
| 148 |
(,data-index ,index) |
| 149 |
(,data-end)) |
| 150 |
(let* ((,sap-var (int-sap (get-lisp-obj-address ,data-var))) |
| 151 |
(,elt-shift (sse-elt-shift-of ,data-var)) |
| 152 |
(,access-size (ash 16 (- ,elt-shift))) |
| 153 |
(,offset-var (+ (ash ,data-index ,elt-shift) +vector-data-fixup+))) |
| 154 |
(declare (type system-area-pointer ,sap-var) |
| 155 |
(type fixnum ,offset-var)) |
| 156 |
(unless (<= 0 ,data-index (+ ,data-index ,access-size) ,data-end) |
| 157 |
(array-bounding-indices-bad-error ,array ,index (+ ,index ,access-size))) |
| 158 |
,@code)))))) |
| 159 |
|
| 160 |
(defun sse-array-info-or-give-up (lvar ref-size) |
| 161 |
;; Look up the SSE element size and check if it is definitely a vector |
| 162 |
(let ((type (lvar-type lvar))) |
| 163 |
(unless (and (array-type-p type) |
| 164 |
(not (array-type-complexp type))) |
| 165 |
(give-up-ir1-transform "not a simple array")) |
| 166 |
(let* ((etype (array-type-specialized-element-type type)) |
| 167 |
(shift (sse-elt-shift-from-saetp |
| 168 |
(if (eq etype *wild-type*) nil |
| 169 |
(find-saetp-by-ctype etype))))) |
| 170 |
(unless shift |
| 171 |
(give-up-ir1-transform "not a known SSE-compatible array element type: ~S" |
| 172 |
(type-specifier etype))) |
| 173 |
(values (ash 1 shift) ; step |
| 174 |
(ash (1- ref-size) (- shift)) ; gap |
| 175 |
(and (listp (array-type-dimensions type)) |
| 176 |
(if (null (cdr (array-type-dimensions type))) :yes :no)))))) |
| 177 |
|
| 178 |
(defmacro def-aref-intrinsic (postfix rtype reader writer &key (ref-size 16)) |
| 179 |
(let* ((rm-aref (symbolicate "ROW-MAJOR-AREF-" postfix)) |
| 180 |
(rm-aset (if writer (symbolicate "ROW-MAJOR-ASET-" postfix))) |
| 181 |
(aref (symbolicate "AREF-" postfix)) |
| 182 |
(aset (if writer (symbolicate "%ASET-" postfix))) |
| 183 |
(reader-vop (symbolicate "%" reader)) |
| 184 |
(reader/ix-vop (symbolicate "%" reader "/IX")) |
| 185 |
(writer-vop (if writer (symbolicate "%" writer))) |
| 186 |
(writer/ix-vop (if writer (symbolicate "%" writer "/IX"))) |
| 187 |
(rtype (or rtype '(values))) |
| 188 |
(index-expression |
| 189 |
(if (= ref-size 0) |
| 190 |
``(the signed-word index) |
| 191 |
``(the signed-word (%check-bound array (%sse-array-size array ,gap) index))))) |
| 192 |
`(progn |
| 193 |
;; ROW-MAJOR-AREF |
| 194 |
(export ',rm-aref) |
| 195 |
(defknown ,rm-aref (array index) ,rtype (foldable flushable)) |
| 196 |
(defun ,rm-aref (array index) |
| 197 |
(with-sse-data ((sap data array) |
| 198 |
(offset index)) |
| 199 |
(,reader-vop sap offset 1 0))) |
| 200 |
;; |
| 201 |
(deftransform ,rm-aref ((array index) (simple-array t) * :important t) |
| 202 |
,(format nil "open-code ~A" rm-aref) |
| 203 |
(multiple-value-bind (step gap is-vector) (sse-array-info-or-give-up array ,ref-size) |
| 204 |
(declare (ignorable gap)) |
| 205 |
`(,',reader/ix-vop (array-data-expr array ,is-vector) |
| 206 |
,,index-expression |
| 207 |
,step ,+vector-data-fixup+))) |
| 208 |
;; AREF |
| 209 |
(export ',aref) |
| 210 |
(defknown ,aref (array &rest index) ,rtype (foldable flushable)) |
| 211 |
(defun ,aref (array &rest indices) |
| 212 |
(declare (truly-dynamic-extent indices)) |
| 213 |
(with-sse-data ((sap data array) |
| 214 |
(offset (%array-row-major-index array indices))) |
| 215 |
(,reader-vop sap offset 1 0))) |
| 216 |
;; |
| 217 |
(defoptimizer (,aref derive-type) ((array &rest indices) node) |
| 218 |
(assert-array-rank array (length indices)) |
| 219 |
(values-specifier-type ',rtype)) |
| 220 |
(deftransform ,aref ((array &rest indices) (simple-array &rest t) * :important t) |
| 221 |
,(format nil "open-code ~A" aref) |
| 222 |
(multiple-value-bind (step gap is-vector) (sse-array-info-or-give-up array ,ref-size) |
| 223 |
(declare (ignorable gap)) |
| 224 |
(let ((syms (make-gensym-list (length indices)))) |
| 225 |
`(lambda (array ,@syms) |
| 226 |
(let ((index ,(if (eq is-vector :yes) (first syms) |
| 227 |
`(array-row-major-index array ,@syms)))) |
| 228 |
(,',reader/ix-vop (array-data-expr array ,is-vector) |
| 229 |
,,index-expression |
| 230 |
,step ,+vector-data-fixup+)))))) |
| 231 |
,@(if writer |
| 232 |
`(;; ROW-MAJOR-ASET |
| 233 |
(defknown ,rm-aset (array index sse-pack) ,rtype (unsafe)) |
| 234 |
(defsetf ,rm-aref ,rm-aset) |
| 235 |
(defun ,rm-aset (array index new-value) |
| 236 |
(with-sse-data ((sap data array) |
| 237 |
(offset index)) |
| 238 |
(,writer-vop sap offset 1 0 (the ,rtype new-value)) |
| 239 |
new-value)) |
| 240 |
;; |
| 241 |
(deftransform ,rm-aset ((array index value) (simple-array t t) * :important t) |
| 242 |
,(format nil "open-code ~A" rm-aset) |
| 243 |
(multiple-value-bind (step gap is-vector) (sse-array-info-or-give-up array ,ref-size) |
| 244 |
(declare (ignorable gap)) |
| 245 |
`(progn |
| 246 |
(,',writer/ix-vop (array-data-expr array ,is-vector) |
| 247 |
,,index-expression |
| 248 |
,step ,+vector-data-fixup+ |
| 249 |
(the sse-pack value)) |
| 250 |
value))) |
| 251 |
;; %ASET |
| 252 |
(defknown ,aset (array &rest t) ,rtype (unsafe)) |
| 253 |
(defsetf ,aref ,aset) |
| 254 |
(defun ,aset (array &rest stuff) |
| 255 |
(let ((new-value (car (last stuff)))) |
| 256 |
(with-sse-data ((sap data array) |
| 257 |
(offset (%array-row-major-index array (nbutlast stuff)))) |
| 258 |
(,writer-vop sap offset 1 0 (the ,rtype new-value)) |
| 259 |
new-value))) |
| 260 |
;; |
| 261 |
(defoptimizer (,aset derive-type) ((array &rest stuff) node) |
| 262 |
(assert-array-rank array (1- (length stuff))) |
| 263 |
(assert-lvar-type (car (last stuff)) (specifier-type 'sse-pack) |
| 264 |
(lexenv-policy (node-lexenv node))) |
| 265 |
(specifier-type ',rtype)) |
| 266 |
(deftransform ,aset ((array &rest stuff) (simple-array &rest t) * :important t) |
| 267 |
,(format nil "open-code ~A" aset) |
| 268 |
(multiple-value-bind (step gap is-vector) (sse-array-info-or-give-up array ,ref-size) |
| 269 |
(declare (ignorable gap)) |
| 270 |
(let ((syms (make-gensym-list (length stuff)))) |
| 271 |
`(lambda (array ,@syms) |
| 272 |
(let ((index ,(if (eq is-vector :yes) (first syms) |
| 273 |
`(array-row-major-index array ,@(butlast syms))))) |
| 274 |
(,',writer/ix-vop (array-data-expr array ,is-vector) |
| 275 |
,,index-expression |
| 276 |
,step ,+vector-data-fixup+ |
| 277 |
(the sse-pack ,(car (last syms))))) |
| 278 |
,(car (last syms))))))))))) |
| 279 |
|