-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArr.hs
256 lines (212 loc) · 6.54 KB
/
Arr.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
{-# LANGUAGE Arrows #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
module Arr where
import Data.Traversable
import qualified Data.Map.Strict as M
import Control.Arrow
import Data.List (intercalate)
-- | Width of the vector instruction
newtype Width = Width Int
-- | Identifier
type Id = String
-- | Index expression
type Ix = Exp
-- | Length expression
type Length = Exp
-- | Eliminate superfluous skip expressions in the code.
elimSkip :: Code -> Code
elimSkip (Skip :>>: c) = elimSkip c
elimSkip (c :>>: Skip) = elimSkip c
elimSkip (c :>>: c') = elimSkip c :>>: elimSkip c'
elimSkip c = c
data Value =
IntVal Int
| FloatVal Float
| BoolVal Bool
deriving(Eq, Ord)
instance Show Value where
show (IntVal i) = show i
show (FloatVal f) = show f
show (BoolVal b) = show b
data Exp =
Var Id
| MatMul Exp Exp
| Literal Value
| Index Id Ix
| Exp :+: Exp
| Exp :-: Exp
| Exp :*: Exp
| Mod Exp Exp
| Div Exp Exp
| Eq Exp Exp
| Gt Exp Exp
| LEq Exp Exp
| Min Exp Exp
| IfThenElse Exp Exp Exp
deriving(Eq, Ord)
showSexp :: [String] -> String
showSexp as = "(" <> (intercalate " " as) <> ")"
instance Show Exp where
show (Var id) = id
show (MatMul e e') = showSexp $ ["matmul", show e, show e']
show (Literal l) = show l
show (Index id ix) = id <> "[" <> show ix <> "]"
show (e :+: e') = showSexp $ ["+", show e, show e']
show (e :*: e') = showSexp $ ["*", show e, show e']
show (Mod e e') = showSexp $ ["%", show e, show e']
show (Div e e') = showSexp $ ["/", show e, show e']
show (Eq e e') = showSexp $ ["==", show e, show e']
show (LEq e e') = showSexp $ ["<=", show e, show e']
show (Min e e') = showSexp $ ["min", show e, show e']
show (IfThenElse i t e) =
showSexp $ ["if", show i, show t, show e]
instance Num Exp where
(+) = (:+:)
(-) = (:-:)
(*) = (:*:)
fromInteger = Literal . IntVal . fromInteger
abs = error "no abs on Exp"
signum = error "no signum on Exp"
data Code =
Skip
| Code :>>: Code
| For Id Exp Code
| Allocate Id Length
| Write Id Ix Exp
deriving(Eq, Ord)
instance Show Code where
show Skip = "skip"
show (c :>>: c') = show c <> ";\n" <> show c'
show (For id lim body) =
showSexp ["for", showSexp [id, "<=", show lim], show body]
show (Allocate id len) = showSexp $ ["alloc", id, show len]
show (Write id ix exp) =
showSexp $ [id <> "[" <> show ix <> "]", ":=", show exp]
instance Semigroup Code where
(<>) = (:>>:)
instance Monoid Code where
mempty = Skip
newtype CM a = CM {runCM :: Integer -> (Integer, Code, a) }
instance Functor CM where
fmap f cm =
CM $ \i ->
let (i', c, a) = runCM cm i
in (i', c, f a)
instance Applicative CM where
pure = return
cma2b <*> cma = do
a <- cma
a2b <- cma2b
return $ a2b a
instance Monad CM where
return a = CM $ \i -> (i, mempty, a)
cm >>= f = CM $ \i ->
let (i', c', a') = runCM cm i
(i'', c'', a'') = runCM (f a') i'
in (i'', c' <> c'', a'')
-- | Generate a new ID
newID :: String -> CM Id
newID name = CM $ \i -> (i+1, mempty, (name <> "-" <> show i))
-- | Append a section of code
appendCode :: Code -> CM ()
appendCode c = CM $ \i -> (i, c, ())
-- | Run a CM to extract out the code. Useful to generate code
-- | and then transplant to another location while ensure we
-- | do not create overlapping IDs
extractCMCode :: CM () -> CM Code
extractCMCode cm =
CM $ \i ->
let (i', c, _) = runCM cm i
in (i', mempty, c)
-- | Generate code from the CM
genCMCode :: CM () -> Code
genCMCode cm = let (_, c, _) = runCM cm 0 in c
--- for loop
for_ :: Exp -- ^ Limit of the loop. Variable goes from 0 <= v <= limit
-> (Exp -> CM ()) -- ^ Function that receives the loop induction variable and generates the loop body
-> CM ()
for_ lim f = do
id <- newID "%iv"
code <- extractCMCode $ f (Var id)
appendCode $ For id lim code
-- | A chunk of linear memory with an ID and a length attached to it
data CMMem = CMMem Id Length
-- | Generate an index expression into the CMMem
cmIndex :: CMMem -> Ix -> Exp
cmIndex (CMMem name _) ix = Index name ix
-- | Generate a write statement into the CMMem
cmWrite ::CMMem -- ^ Array to be written
-> Ix -- ^ Index to write to
-> Exp -- ^ Value to write
-> CM ()
cmWrite (CMMem name _) ix v =
appendCode $ Write name ix v
-- | Defunctionalized push array
data PushT where
Generate :: Length -> (Ix -> Exp) -> PushT
Use :: CMMem -> PushT
Map :: (Exp -> Exp) -> PushT -> PushT
Append :: Length -> PushT -> PushT -> PushT
-- | Compute the length of a PushT
pushTLength :: PushT -> Length
pushTLength (Generate l _ ) = l
pushTLength (Use (CMMem _ l)) = l
pushTLength (Map _ p) = pushTLength p
pushTLength (Append l p1 p2) = pushTLength p1 + pushTLength p2
-- | code to index into the PushT
index :: PushT -> Ix -> Exp
index (Generate n ixf) ix = ixf ix
index (Use (CMMem id _)) ix = Index id ix
index (Map f p) ix = f (index p ix)
index (Append l p p') ix =
IfThenElse
(Gt ix l)
(index p' (ix - l))
(index p ix)
-- | Generate code from a pushT given an index and an expression for
-- | the value at that index
apply :: PushT -> (Ix -> Exp -> CM ()) -> CM ()
apply (Generate l ix2v) k =
for_ l (\ix -> k ix (ix2v ix))
apply (Use cmem@(CMMem _ n)) k = for_ n $ \ix -> k ix (cmIndex cmem ix)
apply (Map f p) k = apply p (\i a -> k i (f a))
apply (Append l p1 p2) k =
apply p1 k >>
apply p2 (\i a -> k (l + i) a)
-- | Code generate the allocation of an array and return a handle
-- | to the alocated array
allocate :: Length -> CM (CMMem)
allocate l = do
id <- newID "#arr"
appendCode $ Allocate id l
return (CMMem id l)
-- | Materialize an array, and return a handle to the materialized array
toVector :: PushT -> CM (CMMem)
toVector p = do
-- | How do I get the length of the array I need to materialize?
writeloc <- allocate (pushTLength p)
apply p $ \ix val -> (cmWrite writeloc ix val)
return $ writeloc
-- | Materialize an array and ignore the handle
toVector_ :: PushT -> CM ()
toVector_ p = toVector p >> pure ()
pushTZipWith :: (Exp -> Exp -> Exp) -> PushT -> PushT -> PushT
pushTZipWith f a1 a2 =
Generate
(min (pushTLength a1) (pushTLength a2))
(\i -> f (index a1 i) (index a2 i))
saxpy :: Exp -- ^ a
-> PushT -- ^ x
-> PushT -- ^ b
-> PushT
saxpy a x b = pushTZipWith (\x b -> a * x + b) x b
main :: IO ()
main = do
let vec1 = CMMem "src1" 10
let vec2 = CMMem "src2" 10
let code = elimSkip $ genCMCode $ toVector_ $ saxpy 10 (Use vec1) (Generate 100 (\ix -> Div ix 2))
print code