factor/basis/compiler/cfg/intrinsics/simd/backend/backend.factor

149 lines
4.8 KiB
Factor
Raw Normal View History

! (c)2009 Joe Groff bsd license
2009-11-14 21:59:03 -05:00
USING: accessors arrays classes combinators
combinators.short-circuit compiler.cfg.builder.blocks
compiler.cfg.registers compiler.cfg.stacks
compiler.cfg.stacks.local compiler.tree.propagation.info
cpu.architecture effects fry generalizations help.lint.checks
kernel locals macros math namespaces quotations sequences
splitting words ;
IN: compiler.cfg.intrinsics.simd.backend
! Selection of implementation based on available CPU instructions
: can-has? ( quot -- ? )
[ t \ can-has? ] dip '[ @ drop \ can-has? get ] with-variable ; inline
2009-11-15 00:43:22 -05:00
: can-has-rep? ( rep reps -- )
member? \ can-has? [ and ] change ; inline
2009-11-14 21:59:03 -05:00
GENERIC: create-can-has ( word -- word' )
2009-11-14 21:59:03 -05:00
PREDICATE: vector-op-word < word
{
[ name>> { [ { [ "^" head? ] [ "##" head? ] } 1|| ] [ "-vector" swap subseq? ] } 1&& ]
2009-11-14 21:59:03 -05:00
[ vocabulary>> { "compiler.cfg.intrinsics.simd" "compiler.cfg.hats" } member? ]
} 1&& ;
: reps-word ( word -- word' )
name>> "^^" ?head drop "##" ?head drop
"%" "-reps" surround "cpu.architecture" lookup ;
2009-11-14 21:59:03 -05:00
:: can-has-^^-quot ( word def effect -- quot )
effect in>> { "rep" } split1 [ length ] bi@ 1 +
2009-11-15 00:43:22 -05:00
word reps-word 1quotation
effect out>> length f <array> >quotation
2009-11-15 00:43:22 -05:00
'[ [ _ ndrop ] _ ndip @ can-has-rep? @ ] ;
2009-11-14 21:59:03 -05:00
:: can-has-^-quot ( word def effect -- quot )
2009-11-15 00:43:22 -05:00
def create-can-has first ;
: map-concat-like ( seq quot -- seq' )
'[ _ map ] [ concat-as ] bi ; inline
2009-11-14 21:59:03 -05:00
M: object create-can-has 1quotation ;
2009-11-14 21:59:03 -05:00
M: array create-can-has
2009-11-15 00:43:22 -05:00
[ create-can-has ] map-concat-like 1quotation ;
2009-11-14 21:59:03 -05:00
M: callable create-can-has
2009-11-15 00:43:22 -05:00
[ create-can-has ] map-concat-like 1quotation ;
2009-11-14 21:59:03 -05:00
: (can-has-word) ( word -- word' )
name>> "can-has-" prepend "compiler.cfg.intrinsics.simd.backend" lookup ;
2009-11-14 21:59:03 -05:00
: (can-has-quot) ( word -- quot )
[ ] [ def>> ] [ stack-effect ] tri {
{ [ pick name>> "^^" head? ] [ can-has-^^-quot ] }
{ [ pick name>> "##" head? ] [ can-has-^^-quot ] }
{ [ pick name>> "^" head? ] [ can-has-^-quot ] }
} cond ;
M: vector-op-word create-can-has
2009-11-14 21:59:03 -05:00
dup (can-has-word) [ 1quotation ] [ (can-has-quot) ] ?if ;
GENERIC# >can-has-cond 2 ( quot #pick #dup -- quotpair )
2009-11-14 21:59:03 -05:00
M:: callable >can-has-cond ( quot #pick #dup -- quotpair )
2009-11-15 00:43:22 -05:00
#dup quot create-can-has '[ _ ndup @ can-has? ] quot 2array ;
2009-11-14 21:59:03 -05:00
M:: pair >can-has-cond ( pair #pick #dup -- quotpair )
pair first2 :> ( class quot )
#pick class #dup quot create-can-has
2009-11-15 00:43:22 -05:00
'[ _ npick _ instance? [ _ ndup @ can-has? ] dip and ]
quot 2array ;
MACRO: v-vector-op ( trials -- )
[ 1 2 >can-has-cond ] map '[ _ cond ] ;
MACRO: vl-vector-op ( trials -- )
[ 1 3 >can-has-cond ] map '[ _ cond ] ;
MACRO: vv-vector-op ( trials -- )
[ 1 3 >can-has-cond ] map '[ _ cond ] ;
MACRO: vv-cc-vector-op ( trials -- )
[ 2 4 >can-has-cond ] map '[ _ cond ] ;
MACRO: vvvv-vector-op ( trials -- )
[ 1 5 >can-has-cond ] map '[ _ cond ] ;
! Special-case conditional instructions
: can-has-^(compare-vector) ( src1 src2 rep cc -- dst )
[ 2drop ] 2dip %compare-vector-reps member?
\ can-has? [ and ] change
f ;
2009-11-15 00:43:22 -05:00
: can-has-^^test-vector ( src rep vcc -- dst )
[ drop ] 2dip drop %test-vector-reps member?
\ can-has? [ and ] change
f ;
! Intrinsic code emission
MACRO: if-literals-match ( quots -- )
[ length ] [ ] [ length ] tri
! n quots n
'[
! node quot
[
dup node-input-infos
_ tail-slice* [ literal>> ] map
dup _ check-elements
] dip
swap [
! node literals quot
[ _ firstn ] dip call
drop
] [ 2drop emit-primitive ] if
] ;
CONSTANT: [unary] [ ds-drop ds-pop ]
CONSTANT: [unary/param] [ [ -2 inc-d ds-pop ] dip ]
CONSTANT: [binary] [ ds-drop 2inputs ]
CONSTANT: [quaternary]
[
ds-drop
D 3 peek-loc
D 2 peek-loc
D 1 peek-loc
D 0 peek-loc
-4 inc-d
]
2009-11-14 21:59:03 -05:00
:: [emit-vector-op] ( trials params-quot op-quot literal-preds -- quot )
params-quot trials op-quot literal-preds
'[ [ _ dip _ @ ds-push ] _ if-literals-match ] ;
MACRO: emit-v-vector-op ( trials -- )
[unary] [ v-vector-op ] { [ representation? ] } [emit-vector-op] ;
MACRO: emit-vl-vector-op ( trials literal-pred -- )
[ [unary/param] [ vl-vector-op ] { [ representation? ] } ] dip prefix [emit-vector-op] ;
MACRO: emit-vv-vector-op ( trials -- )
[binary] [ vv-vector-op ] { [ representation? ] } [emit-vector-op] ;
MACRO: emit-vvvv-vector-op ( trials -- )
[quaternary] [ vvvv-vector-op ] { [ representation? ] } [emit-vector-op] ;
2009-11-14 21:59:03 -05:00
MACRO:: emit-vv-or-vl-vector-op ( var-trials imm-trials literal-pred -- )
literal-pred imm-trials literal-pred var-trials
'[
dup node-input-infos 2 tail-slice* first literal>> @
[ _ _ emit-vl-vector-op ]
[ _ emit-vv-vector-op ] if
] ;
2009-11-14 21:59:03 -05:00