17 | module Compiler.Passes
19 | import Control.Monad.State
20 | import Data.SortedMap
32 | Rebinds = (Nat, SortedMap (Nat, Nat) Nat)
34 | parameters (fCurrent : Nat)
36 | rebindOp : Op -> State Rebinds Op
39 | rebind : Value -> State Rebinds Value
40 | rebind $
V i (Concrete op) = V i <$> Concrete <$> rebindOp op
41 | rebind $
V i (BoundSet k) =
43 | then pure (V i $
BoundSet k)
45 | (next, rebinds) <- get
46 | let op = BoundSet fCurrent
47 | case lookup (i, k) rebinds of
48 | Just i => pure $
V i op
50 | put (S next, insert (i, k) next rebinds)
54 | rebindFunc : Fn arity -> State Rebinds (Fn arity)
55 | rebindFunc (MkFn tag pt rt results env) = do
56 | results <- traverse rebind results
57 | ops <- traverse (\(t, x) => (t,) <$> rebindOp x) env.ops
58 | pure $
MkFn tag pt rt results ({ops := ops} env)
60 | rebindOp (NamedFunc f) = pure $
NamedFunc f
61 | rebindOp (CallByName tag resTy xs) = CallByName tag resTy <$> traverse rebind xs
63 | rebindOp (Grad shape f x) = Grad shape <$> rebindFunc f <*> rebind x
66 | rebindOp (Map f xs resultType ks) = pure $
Map f !(traverse rebind xs) resultType ks
67 | rebindOp (Reduce op inits axes xs) =
68 | pure $
Reduce op !(traverse rebind inits) axes !(traverse rebind xs)
69 | rebindOp (Sort comp axis isStable x) = pure $
Sort comp axis isStable !(rebind x)
70 | rebindOp (While condition body init) = While condition body <$> traverse rebind init
71 | rebindOp (If resultType p t f) = pure $
If resultType !(rebind p) t f
72 | rebindOp (BitCastConvert dtype ks x) = BitCastConvert dtype ks <$> rebind x
73 | rebindOp (Convert dtype ks x) = Convert dtype ks <$> rebind x
74 | rebindOp (Reshape dtype to x) = Reshape dtype to <$> rebind x
75 | rebindOp (Slice starts stops strides x) = Slice starts stops strides <$> rebind x
76 | rebindOp (DynamicSlice starts sizes x) =
77 | pure $
DynamicSlice !(traverse rebind starts) sizes !(rebind x)
78 | rebindOp (Concat axis xs) = Concat axis <$> traverse rebind xs
79 | rebindOp (Transpose ordering x) = Transpose ordering <$> rebind x
80 | rebindOp (Broadcast dtype from to x) = Broadcast dtype from to <$> rebind x
81 | rebindOp (Reverse axes x) = Reverse axes <$> rebind x
82 | rebindOp (BinaryElementwise op x y) = BinaryElementwise op <$> rebind x <*> rebind y
83 | rebindOp (UnaryElementwise op x) = UnaryElementwise op <$> rebind x
84 | rebindOp (Select p t f) = Select <$> rebind p <*> rebind t <*> rebind f
85 | rebindOp (DotGeneral lb lc rb rc resultType x y) =
86 | DotGeneral lb lc rb rc resultType <$> rebind x <*> rebind y
87 | rebindOp (Cholesky x) = Cholesky <$> rebind x
88 | rebindOp (TriangularSolve x y isLower) = pure $
TriangularSolve !(rebind x) !(rebind y) isLower
89 | rebindOp (Rng state resultType) = pure $
Rng !(rebind state) resultType
93 | removeCaptures : {n : _ } -> Fn n -> (
m ** (Vect m Value, Fn (n + m)))
94 | removeCaptures (MkFn tag paramTypes resultTypes results (MkEnv count ops)) =
95 | let ((_, rebinds), (results, ops)) = runState (n, empty) $
do
96 | results <- traverse (rebind tag) results
97 | ops <- traverse (\(tg, x) => (tg,) <$> rebindOp tag x) ops
100 | argify : (Nat, Nat) -> Value
101 | argify (i, x) = V i $
BoundSet x
103 | rebinds := the (Vect _ _) $
fst <$>
104 | fromList (sortBy (compare `on` snd) (SortedMap.toList rebinds))
105 | paramTypes := paramTypes ++ (uncurry TypeRef <$> rebinds)
107 | in (
_ ** (argify <$> rebinds, MkFn tag paramTypes resultTypes results (MkEnv count ops)))