[Sbcl-help] SSE and Vectorisation - the patch they tried to ban! From: John Connors - 2005-05-05 21:35 ```Is there absolutely any chance that this would still work? --- From: Christophe Rhodes Vectorizing, part II 2001-10-25 07:52 So, as threatened, here"s a similarly half-baked file that begins the implementation of vectorizing perfectly good user code (for those latecomers to this, see "SSE instructions and vectorization" on the archives). I post this, much as I did the last, mostly to generate discussion and ask for comments ("That"s an absolutely hideous idea." "You haven"t done this with supported IR1 functions." and so on). The idea behind this twisty bit of logic is that we define a transform for map-into that sees if all its sequence arguments are vectors; if they are, and if it can find an inline expansion for the function it vectorizes this inline function, using the (proof-of-concept and highly inefficient) sb-vector::vector-foo functions. This can be tested by * starting a recent SBCL (0.pre7.73 is what I"m using; you may be able to get it to work with others...) * compiling and loading the attached lisp file * doing: (declaim (inline foo)) (defun foo (x y) (+ x y)) (defun bar (a b) (declare (vector a b)) (declare (optimize (inhibit-warnings 0))) (map-into a #"foo a b)) (disassemble "bar) [ the inhibit-warnings is 0 in case you want to mess around with the declaration to see failure modes of the ir1 transform ] Comments? Cheers, Christophe -- Jesus College, Cambridge, CB5 8BL +44 1223 510 299 http://www-jcsu.jesus.cam.ac.uk/~csr21/ (defun pling-dollar (str schar arg) (first (last +))) (make-dispatch-macro-character #\! t) (set-dispatch-macro-character #\! #\\$ #"pling-dollar) (defpackage :sb-vector (:use :cl)) (in-package :sb-c) (defparameter *vectorize-replacements* (list "(cl:block . cl:block) ; block is fine if there"s no return-from "(cl:+ . sb-vector::vector+) ; "(sb-c:lambda-with-lexenv . sb-c:lambda-with-lexenv) )) (defknown map-into (sequence callable &rest sequence) sequence (call unsafe) :derive-type #"result-type-first-arg) (deftransform map-into ((result fun &rest sequences) * *) "vectorize" (unless (csubtypep (continuation-type result) (specifier-type "vector)) (give-up-ir1-transform "result is not a vector.")) (unless (every #"(lambda (x) (csubtypep (continuation-type x) (specifier-type "vector))) sequences) (give-up-ir1-transform "one or more arguments are not vectors.")) (let ((seq-names (make-gensym-list (length sequences)))) (let ((leaf (ref-leaf (continuation-use fun)))) (unless (typep leaf "defined-fun) (give-up-ir1-transform "can"t vectorize undefined functions")) (let ((expansion (defined-fun-inline-expansion leaf))) (when (null expansion) (give-up-ir1-transform "won"t vectorize uninlined function")) (let ((*vectorize-replacements* *vectorize-replacements*)) (push (cons (leaf-name leaf) (leaf-name leaf)) *vectorize-replacements*) (let ((v (vectorize expansion))) `(lambda (result fun ,@seq-names) (declare (ignore fun)) (sb-vector::vector-copy-data (funcall (lambda ,@v) ,@seq-names) result)))))))) (defun vectorize (expansion) (assert (eq (first expansion) "lambda-with-lexenv)) (assert (and ; (null (cadr expansion)) decls (null (third expansion)) ; macros (null (fourth expansion)) ;symbol-macros )) (let ((*vectorize-replacements* *vectorize-replacements*)) (dolist (var (fifth expansion)) (push (cons var var) *vectorize-replacements*)) (labels ((frob (tree) (cond ((null tree) nil) ((symbolp tree) (or (cdr (assoc tree *vectorize-replacements*)) (give-up-ir1-transform "Can"t vectorize some symbol"))) ((consp tree) (cons (frob (car tree)) (frob (cdr tree)))) (t (give-up-ir1-transform "Aieee!"))))) (cons (fifth expansion) (frob (nthcdr 5 expansion)))))) (defun sb-vector::vector-copy-data (src dest) (dotimes (i (min (length src) (length dest))) (setf (aref dest i) (aref src i))) dest) (defun sb-vector::vector+ (vector &rest vectors) (let ((result (make-array (length vector)))) (dotimes (i (length result)) (setf (aref result i) (apply #"+ (aref vector i) (loop for x in vectors collect (aref x i))))) result)) -- Cyborg Animation Programmer http://yagc.blogspot.com http://badbyteblues.blogspot.com ```